IMP logo

ParticleTuple.h

Go to the documentation of this file.
00001 /**
00002  *  \file ParticleTuple.h
00003  *  \brief Classes to handle individual model particles.
00004  *
00005  *  Copyright 2007-2011 IMP Inventors. All rights reserved.
00006  *
00007  */
00008 
00009 #ifndef IMP_PARTICLE_TUPLE_H
00010 #define IMP_PARTICLE_TUPLE_H
00011 
00012 #include "Particle.h"
00013 #include <boost/array.hpp>
00014 
00015 IMP_BEGIN_NAMESPACE
00016 
00017 
00018 //! A class to store a tuple of particles.
00019 /** \note These do not ref counting currently. SWIG prevents
00020     use of internal::OwnerPointer<Particle> as the storage type without some
00021     gymnastics.
00022 
00023     Only the constructor with the correct number of arguments for the
00024     dimensionality can be used.
00025 
00026     \note ParticleTuple objects are ordered.
00027 */
00028 template <unsigned int D>
00029 class ParticleTuple
00030 #if !defined(SWIG) && !defined(IMP_DOXYGEN)
00031 : public boost::array<Particle*, D>
00032 #endif
00033 {
00034   typedef boost::array<Particle*, D> P;
00035   int compare(const ParticleTuple<D> &o) const {
00036     for (unsigned int i=0;i<D; ++i) {
00037       if (P::operator[](i) < o[i]) return -1;
00038       else if (P::operator[](i) > o[i]) return 1;
00039     }
00040     return 0;
00041   }
00042 public:
00043   static unsigned int get_dimension() {return D;};
00044   ParticleTuple(){
00045     for (unsigned int i=0; i< D; ++i) {P::operator[](i)=NULL;}
00046   }
00047   ParticleTuple(Particle* x, Particle* y) {
00048     IMP_USAGE_CHECK(D==2, "Need " << D << " to construct a "
00049               << D << "-tuple.");
00050     P::operator[](0) = x;
00051     P::operator[](1) = y;
00052   }
00053   ParticleTuple(Particle* x, Particle* y, Particle* z) {
00054     IMP_USAGE_CHECK(D==3, "Need " << D << " to construct a "
00055               << D << "-tuple.");
00056     P::operator[](0) = x;
00057     P::operator[](1) = y;
00058     P::operator[](2) = z;
00059   }
00060   ParticleTuple(Particle* x0, Particle* x1, Particle* x2, Particle* x3) {
00061     IMP_USAGE_CHECK(D==4, "Need " << D << " to construct a "
00062               << D << "-tuple.");
00063     P::operator[](0) = x0;
00064     P::operator[](1) = x1;
00065     P::operator[](2) = x2;
00066     P::operator[](3) = x3;
00067   }
00068   Particle *get(unsigned int i) const {
00069     return P::operator[](i);
00070   }
00071   IMP_HASHABLE_INLINE(ParticleTuple<D>, std::size_t seed = 0;
00072                for (unsigned int i=0; i< D; ++i) {
00073                  boost::hash_combine(seed,
00074                                      P::operator[](i));
00075                }
00076                return seed;);
00077   IMP_COMPARISONS(ParticleTuple<D>);
00078   std::string get_name() const {
00079     std::string ret="\"";
00080     for (unsigned int i=0; i< D; ++i) {
00081       if (i>0) {
00082         ret+= "\" and \"";
00083       }
00084       ret+=P::operator[](i)->get_name();
00085     }
00086     ret+="\"";
00087     return ret;
00088   }
00089   IMP_SHOWABLE_INLINE(ParticleTuple, {
00090       out << get_name();
00091     });
00092 #if defined(IMP_DOXYGEN) || defined(SWIG)
00093   Particle * operator[](unsigned int i) const;
00094   Particle *& operator[](unsigned int i);
00095 #endif
00096 };
00097 
00098 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
00099 template<unsigned int D>
00100 std::ostream &operator<<(std::ostream &out,
00101                          const ParticleTuple<D> &d) {
00102   d.show(out);
00103   return out;
00104 }
00105 #endif
00106 
00107 #if !defined(IMP_DOXYGEN)
00108 
00109 template <unsigned int D>
00110 struct RefCountParticleTuple {
00111   template <class O>
00112   static void ref(O o) {
00113     for (unsigned int i=0; i< D; ++i) {
00114       internal::ref(o[i]);
00115     }
00116   }
00117   template <class O>
00118   static void unref(O o) {
00119     for (unsigned int i=0; i< D; ++i) {
00120       internal::unref(o[i]);
00121     }
00122   }
00123 };
00124 
00125 typedef ParticleTuple<2> ParticlePair;
00126 typedef std::vector<ParticleTuple<2> > ParticlePairsTemp;
00127 typedef VectorOfRefCounted<ParticleTuple<2>,
00128                            RefCountParticleTuple<2> > ParticlePairs;
00129 typedef ParticleTuple<3> ParticleTriplet;
00130 typedef std::vector<ParticleTuple<3> > ParticleTripletsTemp;
00131 typedef VectorOfRefCounted<ParticleTuple<3>,
00132                            RefCountParticleTuple<3> > ParticleTriplets;
00133 typedef ParticleTuple<4> ParticleQuad;
00134 typedef std::vector<ParticleTuple<4> > ParticleQuadsTemp;
00135 typedef VectorOfRefCounted<ParticleTuple<4>,
00136                            RefCountParticleTuple<4> > ParticleQuads;
00137 #endif
00138 
00139 IMP_END_NAMESPACE
00140 
00141 #endif  /* IMP_PARTICLE_TUPLE_H */

Generated on Thu Mar 24 2011 02:01:39 for IMP by doxygen 1.7.3