ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/utils/LocalIndexManager.hpp
Revision: 1883
Committed: Mon Dec 13 22:30:27 2004 UTC (19 years, 9 months ago) by tim
File size: 9084 byte(s)
Log Message:
MPI version is built

File Contents

# User Rev Content
1 tim 1802 /*
2     * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project
3     *
4     * Contact: oopse@oopse.org
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public License
8     * as published by the Free Software Foundation; either version 2.1
9     * of the License, or (at your option) any later version.
10     * All we ask is that proper credit is given for our work, which includes
11     * - but is not limited to - adding the above copyright notice to the beginning
12     * of your source code files, and to any copyright notice that you may distribute
13     * with programs based on this work.
14     *
15     * This program is distributed in the hope that it will be useful,
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     * GNU Lesser General Public License for more details.
19     *
20     * You should have received a copy of the GNU Lesser General Public License
21     * along with this program; if not, write to the Free Software
22     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23     *
24     */
25    
26     /**
27     * @file LocalIndexManager.hpp
28     * @author tlin
29     * @date 11/02/2004
30     * @version 1.0
31     */
32    
33     #ifndef UTILS_LOCALINDEXMANAGER_HPP
34     #define UTILS_LOCALINDEXMANAGER_HPP
35     #include <algorithm>
36     #include <iostream>
37     #include <cassert>
38     #include <list>
39     #include <utility>
40    
41     namespace oopse {
42    
43     /**
44     * @class IndexListContainer
45     * @brief
46     * @todo documentation
47     */
48     class IndexListContainer{
49     public:
50 tim 1803 static const int MAX_INTEGER = 2147483647;
51     typedef std::list<std::pair<int, int> >::iterator IndexListContainerIterator;
52 tim 1802
53    
54 tim 1803 IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER)
55 tim 1802 :minIndex_(minIndex), maxIndex_(maxIndex) {
56    
57     indexContainer_.push_back(std::make_pair(minIndex, maxIndex));
58     }
59    
60 tim 1803 int pop() {
61 tim 1802
62     if (indexContainer_.empty()) {
63     std::cerr << "" << std::endl;
64     }
65    
66     IndexListContainerIterator i = indexContainer_.begin();
67 tim 1803 int result;
68 tim 1802
69     result = indexContainer_.front().first;
70    
71     if (indexContainer_.front().first == indexContainer_.front().second) {
72     indexContainer_.pop_front();
73     } else if (indexContainer_.front().first < indexContainer_.front().second) {
74 tim 1841 ++indexContainer_.front().first;
75 tim 1802 } else {
76     std::cerr << "" << std::endl;
77     }
78    
79     return result;
80     }
81    
82    
83     /**
84     *
85     */
86 tim 1803 void insert(int index) {
87     IndexListContainerIterator insertPos = internalInsert(index, index);
88 tim 1802 merge(insertPos);
89     }
90    
91     /**
92     * Reclaims an index range
93     * @param beginIndex
94     * @param endIndex
95     */
96 tim 1803 void insert(int beginIndex, int endIndex) {
97     IndexListContainerIterator insertPos = internalInsert(beginIndex, endIndex);
98 tim 1802 merge(insertPos);
99     }
100    
101     /**
102     * Reclaims an index array.
103     * @param indices
104     */
105 tim 1803 void insert(std::vector<int>& indices){
106 tim 1802
107     if (indices.empty()) {
108     return;
109     }
110    
111     std::sort(indices.begin(), indices.end());
112 tim 1803 std::unique(indices.begin(), indices.end());
113 tim 1802
114 tim 1803 std::vector<int>::iterator i;
115 tim 1802 IndexListContainerIterator insertPos;
116 tim 1803 int beginIndex;
117 tim 1802
118     beginIndex = indices[0];
119    
120     for ( i = indices.begin() + 1 ; i != indices.end(); ++i) {
121     if (*i != *(i -1) + 1) {
122 tim 1803 insert(beginIndex, *(i-1));
123 tim 1802 beginIndex = *i;
124     }
125     }
126    
127    
128     }
129    
130 tim 1803 std::vector<int> getIndicesBefore(int index) {
131     std::vector<int> result;
132 tim 1802 IndexListContainerIterator i;
133    
134     for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) {
135     if ((*i).first > index) {
136 tim 1803 //we locate the node whose minimum index is greater that index
137     indexContainer_.erase(indexContainer_.begin(), i);
138 tim 1802 break;
139     } else if ((*i).second < index) {
140 tim 1803 //The biggest index current node hold is less than the index we want
141     for (int j = (*i).first; j <= (*i).second; ++j) {
142 tim 1802 result.push_back(j);
143     }
144     continue;
145     } else if ((*i).first == (*i).second) {
146 tim 1803 //the index happen to equal to a node which only contains one index
147 tim 1802 result.push_back((*i).first);
148     indexContainer_.erase(indexContainer_.begin(), i);
149     break;
150     } else {
151    
152 tim 1803 for (int j = (*i).first; j < index; ++j) {
153 tim 1802 result.push_back(j);
154     }
155    
156     (*i).first = index;
157     indexContainer_.erase(indexContainer_.begin(), i);
158     break;
159     }
160    
161     }
162    
163     return result;
164    
165     }
166    
167 tim 1803 int getMaxIndex() {
168 tim 1802 return maxIndex_;
169     }
170    
171     private:
172    
173 tim 1803 IndexListContainerIterator internalInsert(int beginIndex, int endIndex) {
174    
175 tim 1802 if (beginIndex > endIndex) {
176     std::swap(beginIndex, endIndex);
177     std::cerr << "" << std::endl;
178     }
179    
180     if (endIndex > maxIndex_) {
181     std::cerr << "" << std::endl;
182     }
183    
184 tim 1803
185 tim 1802 IndexListContainerIterator j;
186    
187 tim 1803 IndexListContainerIterator i = indexContainer_.begin();
188     for (; i != indexContainer_.end(); ++i) {
189     if ((*i).first > endIndex) {
190     indexContainer_.insert(i, std::make_pair(beginIndex, endIndex));
191     return --i;
192     } else if ((*i).second < beginIndex) {
193 tim 1802 continue;
194     } else {
195     std::cerr << "" << std::endl;
196     }
197     }
198    
199 tim 1803 indexContainer_.push_back(std::make_pair(beginIndex, endIndex));
200     return --indexContainer_.end();
201 tim 1802
202     }
203    
204     void merge(IndexListContainerIterator i) {
205     IndexListContainerIterator j;
206 tim 1803
207     //check whether current node can be merged with its previous node
208 tim 1802 if ( i != indexContainer_.begin()) {
209     j = i;
210     --j;
211     if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) {
212     (*i).first = (*j).first;
213     indexContainer_.erase(j);
214     }
215     }
216    
217 tim 1803 //check whether current node can be merged with its next node
218 tim 1802 if ( i != indexContainer_.end()) {
219     j = i;
220     ++j;
221    
222     if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) {
223     (*i).second = (*j).second;
224     indexContainer_.erase(j);
225     }
226     }
227    
228     }
229 tim 1803 int minIndex_;
230     int maxIndex_;
231     std::list<std::pair<int, int> > indexContainer_;
232 tim 1802 };
233    
234    
235     /**
236     * @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp"
237     * @brief
238     */
239     class LocalIndexManager {
240     public:
241    
242 tim 1803 int getNextAtomIndex() {
243 tim 1802 return atomIndexContainer_.pop();
244     }
245    
246 tim 1803 std::vector<int> getAtomIndicesBefore(int index) {
247 tim 1802 return atomIndexContainer_.getIndicesBefore(index);
248     }
249    
250 tim 1803 void releaseAtomIndex(int index) {
251 tim 1802 atomIndexContainer_.insert(index);
252     }
253    
254 tim 1803 void releaseAtomIndex(int beginIndex, int endIndex) {
255 tim 1802 atomIndexContainer_.insert(beginIndex, endIndex);
256     }
257    
258 tim 1803 void releaseAtomIndex(std::vector<int> indices) {
259 tim 1802 atomIndexContainer_.insert(indices);
260     }
261    
262 tim 1803 int getNextRigidBodyIndex() {
263 tim 1802 return rigidBodyIndexContainer_.pop();
264     }
265    
266 tim 1803 std::vector<int> getRigidBodyIndicesBefore(int index) {
267 tim 1802 return rigidBodyIndexContainer_.getIndicesBefore(index);
268     }
269    
270 tim 1803 void releaseRigidBodyIndex(int index) {
271 tim 1802 rigidBodyIndexContainer_.insert(index);
272     }
273    
274 tim 1803 void releaseRigidBodyIndex(int beginIndex, int endIndex) {
275 tim 1802 rigidBodyIndexContainer_.insert(beginIndex, endIndex);
276     }
277    
278 tim 1803 void releaseRigidBodyIndex(std::vector<int> indices) {
279 tim 1802 rigidBodyIndexContainer_.insert(indices);
280     }
281    
282 tim 1803 private:
283 tim 1802
284     IndexListContainer atomIndexContainer_;
285     IndexListContainer rigidBodyIndexContainer_;
286     };
287    
288     } //end namespace oopse
289     #endif //UTILS_LOCALINDEXMANAGER_HPP