ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/LocalIndexManager.hpp
(Generate patch)

Comparing trunk/OOPSE-4/src/utils/LocalIndexManager.hpp (file contents):
Revision 1930 by gezelter, Wed Jan 12 22:41:40 2005 UTC vs.
Revision 2204 by gezelter, Fri Apr 15 22:04:00 2005 UTC

# Line 1 | Line 1
1 < /*
1 > /*
2   * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved.
3   *
4   * The University of Notre Dame grants you ("Licensee") a
# Line 56 | Line 56 | namespace oopse {
56  
57   namespace oopse {
58  
59 < /**
60 < * @class IndexListContainer
61 < * @brief
62 < * @todo documentation
63 < */
64 < class IndexListContainer{
65 <    public:
66 <        static const int MAX_INTEGER = 2147483647;
67 <        typedef std::list<std::pair<int, int> >::iterator  IndexListContainerIterator;
59 >  /**
60 >   * @class IndexListContainer
61 >   * @brief
62 >   * @todo documentation
63 >   */
64 >  class IndexListContainer{
65 >  public:
66 >    static const int MAX_INTEGER = 2147483647;
67 >    typedef std::list<std::pair<int, int> >::iterator  IndexListContainerIterator;
68  
69          
70 <        IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER)
71 <                                                 :minIndex_(minIndex), maxIndex_(maxIndex) {
70 >    IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER)
71 >      :minIndex_(minIndex), maxIndex_(maxIndex) {
72              
73 <            indexContainer_.push_back(std::make_pair(minIndex, maxIndex));
74 <        }
73 >        indexContainer_.push_back(std::make_pair(minIndex, maxIndex));
74 >      }
75          
76 <        int pop() {
76 >    int pop() {
77              
78 <            if (indexContainer_.empty()) {
79 <                std::cerr << "" << std::endl;
80 <            }
78 >      if (indexContainer_.empty()) {
79 >        std::cerr << "" << std::endl;
80 >      }
81  
82 <            IndexListContainerIterator i = indexContainer_.begin();
83 <            int result;
82 >      IndexListContainerIterator i = indexContainer_.begin();
83 >      int result;
84              
85 <            result = indexContainer_.front().first;
85 >      result = indexContainer_.front().first;
86              
87 <            if (indexContainer_.front().first  == indexContainer_.front().second) {
88 <                indexContainer_.pop_front();
89 <            } else if (indexContainer_.front().first < indexContainer_.front().second) {
90 <                ++indexContainer_.front().first;
91 <            } else {
92 <                std::cerr << "" << std::endl;
93 <            }
87 >      if (indexContainer_.front().first  == indexContainer_.front().second) {
88 >        indexContainer_.pop_front();
89 >      } else if (indexContainer_.front().first < indexContainer_.front().second) {
90 >        ++indexContainer_.front().first;
91 >      } else {
92 >        std::cerr << "" << std::endl;
93 >      }
94  
95 <            return result;
96 <        }
95 >      return result;
96 >    }
97  
98  
99 <        /**
100 <         *
101 <         */
102 <        void insert(int index) {
103 <            IndexListContainerIterator insertPos = internalInsert(index, index);            
104 <            merge(insertPos);
105 <        }
99 >    /**
100 >     *
101 >     */
102 >    void insert(int index) {
103 >      IndexListContainerIterator insertPos = internalInsert(index, index);            
104 >      merge(insertPos);
105 >    }
106  
107 <        /**
108 <         * Reclaims an index range
109 <         * @param beginIndex
110 <         * @param endIndex
111 <         */
112 <        void insert(int beginIndex, int endIndex) {
113 <             IndexListContainerIterator insertPos = internalInsert(beginIndex, endIndex);
114 <             merge(insertPos);            
115 <        }
107 >    /**
108 >     * Reclaims an index range
109 >     * @param beginIndex
110 >     * @param endIndex
111 >     */
112 >    void insert(int beginIndex, int endIndex) {
113 >      IndexListContainerIterator insertPos = internalInsert(beginIndex, endIndex);
114 >      merge(insertPos);            
115 >    }
116  
117 <        /**
118 <         * Reclaims an index array.
119 <         * @param indices
120 <         */
121 <        void insert(std::vector<int>& indices){
117 >    /**
118 >     * Reclaims an index array.
119 >     * @param indices
120 >     */
121 >    void insert(std::vector<int>& indices){
122  
123 <            if (indices.empty()) {
124 <                return;
125 <            }
123 >      if (indices.empty()) {
124 >        return;
125 >      }
126              
127 <            std::sort(indices.begin(), indices.end());
128 <            std::unique(indices.begin(), indices.end());
127 >      std::sort(indices.begin(), indices.end());
128 >      std::unique(indices.begin(), indices.end());
129  
130 <            std::vector<int>::iterator i;
131 <            IndexListContainerIterator insertPos;
132 <            int beginIndex;
130 >      std::vector<int>::iterator i;
131 >      IndexListContainerIterator insertPos;
132 >      int beginIndex;
133  
134 <            beginIndex = indices[0];
134 >      beginIndex = indices[0];
135          
136 <            for ( i = indices.begin() + 1 ; i != indices.end(); ++i) {
137 <                if (*i != *(i -1) + 1) {
138 <                    insert(beginIndex, *(i-1));
139 <                    beginIndex = *i;
140 <                }
141 <            }
136 >      for ( i = indices.begin() + 1 ; i != indices.end(); ++i) {
137 >        if (*i != *(i -1) + 1) {
138 >          insert(beginIndex, *(i-1));
139 >          beginIndex = *i;
140 >        }
141 >      }
142              
143              
144 <        }
144 >    }
145          
146 <        std::vector<int> getIndicesBefore(int index) {
147 <            std::vector<int> result;
148 <            IndexListContainerIterator i;
146 >    std::vector<int> getIndicesBefore(int index) {
147 >      std::vector<int> result;
148 >      IndexListContainerIterator i;
149  
150 <            for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) {
151 <                if ((*i).first > index) {
152 <                    //we locate the node whose minimum index is greater that index
153 <                    indexContainer_.erase(indexContainer_.begin(), i);
154 <                    break;
155 <                } else if ((*i).second < index) {
156 <                    //The biggest index current node hold is less than the index we want
157 <                    for (int j = (*i).first; j <= (*i).second; ++j) {
158 <                        result.push_back(j);
159 <                    }
160 <                    continue;
161 <                } else if ((*i).first == (*i).second) {
162 <                    //the index happen to equal to a node which only contains one index
163 <                    result.push_back((*i).first);
164 <                    indexContainer_.erase(indexContainer_.begin(), i);
165 <                    break;
166 <                } else {
150 >      for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) {
151 >        if ((*i).first > index) {
152 >          //we locate the node whose minimum index is greater that index
153 >          indexContainer_.erase(indexContainer_.begin(), i);
154 >          break;
155 >        } else if ((*i).second < index) {
156 >          //The biggest index current node hold is less than the index we want
157 >          for (int j = (*i).first; j <= (*i).second; ++j) {
158 >            result.push_back(j);
159 >          }
160 >          continue;
161 >        } else if ((*i).first == (*i).second) {
162 >          //the index happen to equal to a node which only contains one index
163 >          result.push_back((*i).first);
164 >          indexContainer_.erase(indexContainer_.begin(), i);
165 >          break;
166 >        } else {
167  
168 <                    for (int j = (*i).first; j < index; ++j) {
169 <                        result.push_back(j);
170 <                    }
168 >          for (int j = (*i).first; j < index; ++j) {
169 >            result.push_back(j);
170 >          }
171                          
172 <                    (*i).first =  index;                  
173 <                    indexContainer_.erase(indexContainer_.begin(), i);
174 <                    break;
175 <                }
172 >          (*i).first =  index;                  
173 >          indexContainer_.erase(indexContainer_.begin(), i);
174 >          break;
175 >        }
176                  
177 <            }
177 >      }
178  
179 <            return result;
179 >      return result;
180  
181 <        }
181 >    }
182          
183 <        int getMaxIndex() {
184 <            return maxIndex_;
185 <        }
183 >    int getMaxIndex() {
184 >      return maxIndex_;
185 >    }
186                  
187 <    private:
187 >  private:
188          
189 <        IndexListContainerIterator internalInsert(int beginIndex, int endIndex) {
189 >    IndexListContainerIterator internalInsert(int beginIndex, int endIndex) {
190  
191 <            if (beginIndex > endIndex) {
192 <                std::swap(beginIndex, endIndex);
193 <                std::cerr << "" << std::endl;
194 <            }
195 <
196 <            if (endIndex > maxIndex_) {
197 <                std::cerr << "" << std::endl;
198 <            }
191 >      if (beginIndex > endIndex) {
192 >        std::swap(beginIndex, endIndex);
193 >        std::cerr << "" << std::endl;
194 >      }
195  
196 +      if (endIndex > maxIndex_) {
197 +        std::cerr << "" << std::endl;
198 +      }
199 +
200                        
201 <            IndexListContainerIterator j;
201 >      IndexListContainerIterator j;
202  
203 <            IndexListContainerIterator i = indexContainer_.begin();  
204 <            for (; i != indexContainer_.end(); ++i) {
205 <                if ((*i).first > endIndex) {
206 <                    indexContainer_.insert(i, std::make_pair(beginIndex, endIndex));
207 <                    return --i;
208 <                } else if ((*i).second < beginIndex) {
209 <                    continue;
210 <                } else {
211 <                    std::cerr << "" << std::endl;
212 <                }
213 <            }
203 >      IndexListContainerIterator i = indexContainer_.begin();  
204 >      for (; i != indexContainer_.end(); ++i) {
205 >        if ((*i).first > endIndex) {
206 >          indexContainer_.insert(i, std::make_pair(beginIndex, endIndex));
207 >          return --i;
208 >        } else if ((*i).second < beginIndex) {
209 >          continue;
210 >        } else {
211 >          std::cerr << "" << std::endl;
212 >        }
213 >      }
214  
215 <            indexContainer_.push_back(std::make_pair(beginIndex, endIndex));
216 <            return --indexContainer_.end();
215 >      indexContainer_.push_back(std::make_pair(beginIndex, endIndex));
216 >      return --indexContainer_.end();
217  
218 <        }
218 >    }
219  
220 <        void merge(IndexListContainerIterator i) {
221 <            IndexListContainerIterator j;
220 >    void merge(IndexListContainerIterator i) {
221 >      IndexListContainerIterator j;
222  
223 <            //check whether current node can be merged with its previous node            
224 <            if ( i != indexContainer_.begin()) {
225 <                j = i;
226 <                --j;
227 <                if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) {
228 <                    (*i).first = (*j).first;
229 <                    indexContainer_.erase(j);
230 <                }
231 <            }
223 >      //check whether current node can be merged with its previous node            
224 >      if ( i != indexContainer_.begin()) {
225 >        j = i;
226 >        --j;
227 >        if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) {
228 >          (*i).first = (*j).first;
229 >          indexContainer_.erase(j);
230 >        }
231 >      }
232  
233 <            //check whether current node can be merged with its next node            
234 <            if ( i != indexContainer_.end()) {
235 <                j = i;
236 <                ++j;
233 >      //check whether current node can be merged with its next node            
234 >      if ( i != indexContainer_.end()) {
235 >        j = i;
236 >        ++j;
237  
238 <                if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) {
239 <                    (*i).second = (*j).second;
240 <                    indexContainer_.erase(j);
241 <                }
242 <            }
238 >        if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) {
239 >          (*i).second = (*j).second;
240 >          indexContainer_.erase(j);
241 >        }
242 >      }
243  
244 <        }
245 <        int minIndex_;
246 <        int maxIndex_;        
247 <        std::list<std::pair<int, int> > indexContainer_;
248 < };
244 >    }
245 >    int minIndex_;
246 >    int maxIndex_;        
247 >    std::list<std::pair<int, int> > indexContainer_;
248 >  };
249  
250  
251 < /**
252 < * @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp"
253 < * @brief
254 < */
255 < class LocalIndexManager {
256 <    public:
251 >  /**
252 >   * @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp"
253 >   * @brief
254 >   */
255 >  class LocalIndexManager {
256 >  public:
257          
258 <        int getNextAtomIndex() {
259 <            return atomIndexContainer_.pop();
260 <        }
258 >    int getNextAtomIndex() {
259 >      return atomIndexContainer_.pop();
260 >    }
261  
262 <        std::vector<int> getAtomIndicesBefore(int index) {
263 <            return atomIndexContainer_.getIndicesBefore(index);
264 <        }
262 >    std::vector<int> getAtomIndicesBefore(int index) {
263 >      return atomIndexContainer_.getIndicesBefore(index);
264 >    }
265          
266 <        void releaseAtomIndex(int index) {
267 <            atomIndexContainer_.insert(index);
268 <        }
266 >    void releaseAtomIndex(int index) {
267 >      atomIndexContainer_.insert(index);
268 >    }
269  
270 <        void releaseAtomIndex(int beginIndex, int endIndex) {
271 <            atomIndexContainer_.insert(beginIndex, endIndex);
272 <        }
270 >    void releaseAtomIndex(int beginIndex, int endIndex) {
271 >      atomIndexContainer_.insert(beginIndex, endIndex);
272 >    }
273  
274 <        void releaseAtomIndex(std::vector<int> indices) {
275 <            atomIndexContainer_.insert(indices);
276 <        }
274 >    void releaseAtomIndex(std::vector<int> indices) {
275 >      atomIndexContainer_.insert(indices);
276 >    }
277  
278 <       int getNextRigidBodyIndex() {
279 <            return rigidBodyIndexContainer_.pop();
280 <        }
278 >    int getNextRigidBodyIndex() {
279 >      return rigidBodyIndexContainer_.pop();
280 >    }
281  
282 <        std::vector<int> getRigidBodyIndicesBefore(int index) {
283 <            return rigidBodyIndexContainer_.getIndicesBefore(index);
284 <        }
282 >    std::vector<int> getRigidBodyIndicesBefore(int index) {
283 >      return rigidBodyIndexContainer_.getIndicesBefore(index);
284 >    }
285          
286 <        void releaseRigidBodyIndex(int index) {
287 <            rigidBodyIndexContainer_.insert(index);
288 <        }
286 >    void releaseRigidBodyIndex(int index) {
287 >      rigidBodyIndexContainer_.insert(index);
288 >    }
289  
290 <        void releaseRigidBodyIndex(int beginIndex, int endIndex) {
291 <            rigidBodyIndexContainer_.insert(beginIndex, endIndex);
292 <        }
290 >    void releaseRigidBodyIndex(int beginIndex, int endIndex) {
291 >      rigidBodyIndexContainer_.insert(beginIndex, endIndex);
292 >    }
293  
294 <        void releaseRigidBodyIndex(std::vector<int> indices) {
295 <            rigidBodyIndexContainer_.insert(indices);
296 <        }
294 >    void releaseRigidBodyIndex(std::vector<int> indices) {
295 >      rigidBodyIndexContainer_.insert(indices);
296 >    }
297  
298 <    private:
298 >  private:
299  
300 <        IndexListContainer atomIndexContainer_;
301 <        IndexListContainer rigidBodyIndexContainer_;
302 < };
300 >    IndexListContainer atomIndexContainer_;
301 >    IndexListContainer rigidBodyIndexContainer_;
302 >  };
303  
304   } //end namespace oopse
305   #endif //UTILS_LOCALINDEXMANAGER_HPP

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines