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, 7 months ago) by tim
File size: 9084 byte(s)
Log Message:
MPI version is built

File Contents

# Content
1 /*
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 static const int MAX_INTEGER = 2147483647;
51 typedef std::list<std::pair<int, int> >::iterator IndexListContainerIterator;
52
53
54 IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER)
55 :minIndex_(minIndex), maxIndex_(maxIndex) {
56
57 indexContainer_.push_back(std::make_pair(minIndex, maxIndex));
58 }
59
60 int pop() {
61
62 if (indexContainer_.empty()) {
63 std::cerr << "" << std::endl;
64 }
65
66 IndexListContainerIterator i = indexContainer_.begin();
67 int result;
68
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 ++indexContainer_.front().first;
75 } else {
76 std::cerr << "" << std::endl;
77 }
78
79 return result;
80 }
81
82
83 /**
84 *
85 */
86 void insert(int index) {
87 IndexListContainerIterator insertPos = internalInsert(index, index);
88 merge(insertPos);
89 }
90
91 /**
92 * Reclaims an index range
93 * @param beginIndex
94 * @param endIndex
95 */
96 void insert(int beginIndex, int endIndex) {
97 IndexListContainerIterator insertPos = internalInsert(beginIndex, endIndex);
98 merge(insertPos);
99 }
100
101 /**
102 * Reclaims an index array.
103 * @param indices
104 */
105 void insert(std::vector<int>& indices){
106
107 if (indices.empty()) {
108 return;
109 }
110
111 std::sort(indices.begin(), indices.end());
112 std::unique(indices.begin(), indices.end());
113
114 std::vector<int>::iterator i;
115 IndexListContainerIterator insertPos;
116 int beginIndex;
117
118 beginIndex = indices[0];
119
120 for ( i = indices.begin() + 1 ; i != indices.end(); ++i) {
121 if (*i != *(i -1) + 1) {
122 insert(beginIndex, *(i-1));
123 beginIndex = *i;
124 }
125 }
126
127
128 }
129
130 std::vector<int> getIndicesBefore(int index) {
131 std::vector<int> result;
132 IndexListContainerIterator i;
133
134 for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) {
135 if ((*i).first > index) {
136 //we locate the node whose minimum index is greater that index
137 indexContainer_.erase(indexContainer_.begin(), i);
138 break;
139 } else if ((*i).second < index) {
140 //The biggest index current node hold is less than the index we want
141 for (int j = (*i).first; j <= (*i).second; ++j) {
142 result.push_back(j);
143 }
144 continue;
145 } else if ((*i).first == (*i).second) {
146 //the index happen to equal to a node which only contains one index
147 result.push_back((*i).first);
148 indexContainer_.erase(indexContainer_.begin(), i);
149 break;
150 } else {
151
152 for (int j = (*i).first; j < index; ++j) {
153 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 int getMaxIndex() {
168 return maxIndex_;
169 }
170
171 private:
172
173 IndexListContainerIterator internalInsert(int beginIndex, int endIndex) {
174
175 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
185 IndexListContainerIterator j;
186
187 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 continue;
194 } else {
195 std::cerr << "" << std::endl;
196 }
197 }
198
199 indexContainer_.push_back(std::make_pair(beginIndex, endIndex));
200 return --indexContainer_.end();
201
202 }
203
204 void merge(IndexListContainerIterator i) {
205 IndexListContainerIterator j;
206
207 //check whether current node can be merged with its previous node
208 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 //check whether current node can be merged with its next node
218 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 int minIndex_;
230 int maxIndex_;
231 std::list<std::pair<int, int> > indexContainer_;
232 };
233
234
235 /**
236 * @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp"
237 * @brief
238 */
239 class LocalIndexManager {
240 public:
241
242 int getNextAtomIndex() {
243 return atomIndexContainer_.pop();
244 }
245
246 std::vector<int> getAtomIndicesBefore(int index) {
247 return atomIndexContainer_.getIndicesBefore(index);
248 }
249
250 void releaseAtomIndex(int index) {
251 atomIndexContainer_.insert(index);
252 }
253
254 void releaseAtomIndex(int beginIndex, int endIndex) {
255 atomIndexContainer_.insert(beginIndex, endIndex);
256 }
257
258 void releaseAtomIndex(std::vector<int> indices) {
259 atomIndexContainer_.insert(indices);
260 }
261
262 int getNextRigidBodyIndex() {
263 return rigidBodyIndexContainer_.pop();
264 }
265
266 std::vector<int> getRigidBodyIndicesBefore(int index) {
267 return rigidBodyIndexContainer_.getIndicesBefore(index);
268 }
269
270 void releaseRigidBodyIndex(int index) {
271 rigidBodyIndexContainer_.insert(index);
272 }
273
274 void releaseRigidBodyIndex(int beginIndex, int endIndex) {
275 rigidBodyIndexContainer_.insert(beginIndex, endIndex);
276 }
277
278 void releaseRigidBodyIndex(std::vector<int> indices) {
279 rigidBodyIndexContainer_.insert(indices);
280 }
281
282 private:
283
284 IndexListContainer atomIndexContainer_;
285 IndexListContainer rigidBodyIndexContainer_;
286 };
287
288 } //end namespace oopse
289 #endif //UTILS_LOCALINDEXMANAGER_HPP