1 /* -*- c++ -*- */
2 #ifndef ARRAYCELLSTRUCTURE_H
3 #define ARRAYCELLSTRUCTURE_H
4 
5 #include "stringutilities.h"
6 #include "CubicCellLocation.h"
7 #include "Vector3D.h"
8 #include "Array.h"
9 //#define DEBUG_ARRAYCELLLISTSTRUCTURE
10 
11 namespace ProtoMol {
12 
13   //_________________________________________________________________ ArrayCellListStructure
14   /**
15    * Implements a cell list with an array, providing STL alike iterators and access
16    */
17   class ArrayCellListStructure {
18     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19     // My typedef's
20     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21   private:
22     typedef CubicCellLocation T1;
23     typedef int T2;
24     typedef std::pair<T1,T2> T;
25     typedef Array<T,3> TContainer;
26 
27   public:
28     // STL-like types
29     typedef TContainer::value_type        value_type;
30     typedef TContainer::reference         reference;
31     typedef TContainer::const_reference   const_reference;
32     typedef TContainer::pointer	      pointer;
33     typedef TContainer::const_pointer     const_pointer;
34     typedef TContainer::iterator	      iterator;
35     typedef TContainer::const_iterator    const_iterator;
36     typedef TContainer::difference_type   difference_type;
37     typedef TContainer::size_type         size_type;
38 
39     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40     // Constructors, destructors, assignment
41     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42   public:
43     ArrayCellListStructure();
44 
45     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
46     // Return STL-like iterators
47     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48   public:
begin()49     iterator       begin()       { return myBegin;}
end()50     iterator       end()         { return myEnd; }
begin()51     const_iterator begin() const { return myBeginConst;}
end()52     const_iterator end()   const { return myEndConst; }
size()53     size_type      size()  const { return mySize;}
54 
find(const T1 & t1)55     iterator find(const T1& t1) {
56       if(!checkBoundaries(t1))
57 	return myEnd;
58       iterator itr = &myArray[t1.x][t1.y][t1.z];
59       if(itr->second < 0)
60 	return myEnd;
61       return itr;
62     }
63 
find(const T1 & t1)64     const_iterator find(const T1& t1) const {
65       if(!checkBoundaries(t1))
66 	return myEndConst;
67       const_iterator itr = &myArray[t1.x][t1.y][t1.z];
68       if(itr->second < 0)
69 	return myEndConst;
70       return itr;
71     }
72 
73     T2& operator[](const T1& t1){
74       return myArray[t1.x][t1.y][t1.z].second;
75     }
76 
checkBoundaries(const T1 & t1)77     bool checkBoundaries(const T1& t1) const{
78       return !(t1.x <  0    || t1.y < 0     || t1.z < 0 ||
79 	       t1.x >= myNX || t1.y >= myNY || t1.z >= myNZ);
80     }
81 
82     /// case for periodic boundary conditions
findPeriodic(const T1 & t1)83     const_iterator findPeriodic(const T1& t1) const{
84       const_iterator itr = &myArray[(t1.x+myMaxNX)%myNX][(t1.y+myMaxNY)%myNY][(t1.z+myMaxNZ)%myNZ];
85       if (itr->second < 0)
86 	return myEndConst;
87       return itr;
88     }
89 
90     /// case for periodic boundary conditions
findPeriodic(const T1 & t1)91     iterator findPeriodic(const T1& t1) {
92       iterator itr = &myArray[(t1.x+myMaxNX)%myNX][(t1.y+myMaxNY)%myNY][(t1.z+myMaxNZ)%myNZ];
93       if (itr->second < 0)
94 	return myEnd;
95       return itr;
96     }
97 
basisCell(const T1 & t1)98     T1 basisCell(const T1& t1) const{
99       return T1((t1.x+myMaxNX1)%myNX+myNX1,
100 		(t1.y+myMaxNY1)%myNY+myNY1,
101 		(t1.z+myMaxNZ1)%myNZ+myNZ1);
102     }
103 
periodicCell(const T1 & t1)104     T1 periodicCell(const T1& t1) const{
105       return T1((t1.x+myMaxNX)%myNX,
106 		(t1.y+myMaxNY)%myNY,
107 		(t1.z+myMaxNZ)%myNZ);
108     }
109 
110     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
111     // New methods of class ArrayCellListStructure
112     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
113   public:
114     /// initialize with the size of the system and the cell size (3D)
115     void initialize(const Vector3D& max, Vector3D cellSize);
116     void updateCache();
uncache()117     void uncache(){valid = false;}
118 
getDimX()119     int getDimX() const{ return myNX;}
getDimY()120     int getDimY() const{ return myNY;}
getDimZ()121     int getDimZ() const{ return myNZ;}
122     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123     // My data members
124     //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125   public:
126     bool        valid;
127   private:
128     TContainer  myArray;
129     Vector3D    myCellSize;
130     int         myNX,myNY,myNZ;
131     int         myMaxNX,myMaxNY,myMaxNZ;
132     int         myNX1,myNY1,myNZ1;
133     int         myMaxNX1,myMaxNY1,myMaxNZ1;
134     iterator          myBegin,myEnd;
135     const_iterator    myBeginConst,myEndConst;
136     size_type   mySize;
137   };
138 }
139 #endif /* ARRAYCELLSTRUCTURE_H */
140