ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/utils/LocalndexManager.hpp
Revision: 1719
Committed: Fri Nov 5 23:38:27 2004 UTC (19 years, 8 months ago) by tim
File size: 9757 byte(s)
Log Message:
Fix Exclude class etc.

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 <cassert>
37 #include <list>
38 #include <utility>
39
40 namespace oopse {
41
42 /**
43 * @class IndexListContainer
44 * @brief
45 * @todo documentation
46 */
47 class IndexListContainer{
48 public:
49 static const unsigned int MAX_INTEGER = 2147483647;
50 typedef IndexListContainerIterator IndexListContainerIterator;
51
52
53 IndexListContainer(unsigned int minIndex = 0, unsigned int maxIndex = MAX_INTEGER)
54 :minIndex_(minIndex) maxIndex_(maxIndex) {
55
56 indexContainer_.push_back(make_pair(minIndex, maxIndex));
57 }
58
59 unsigned int pop() {
60
61 if (indexContainer_.empty()) {
62 std::cerr << "" << std::endl;
63 }
64
65 IndexListContainerIterator i = indexContainer_.begin();
66 unsigned int result;
67
68 result = indexContainer_.front().first;
69
70 if (indexContainer_.front().first == indexContainer_.front().second) {
71 indexContainer_.pop_front();
72 } else if (indexContainer_.front().first < indexContainer_.front().second) {
73 --indexContainer_.front().first;
74 } else {
75 std::cerr << "" << std::endl;
76 }
77
78 return result;
79 }
80
81
82 /**
83 *
84 */
85 void insert(unsigned int index) {
86 IndexListContainerIterator insertPos = internalInsert(index, index, indexContainer_.begin());
87 merge(insertPos);
88 }
89
90 /**
91 * Reclaims an index range
92 * @param beginIndex
93 * @param endIndex
94 */
95 void insert(unsigned int beginIndex, unsigned int endIndex) {
96 IndexListContainerIterator insertPos = internalInsert(index, index, indexContainer_.begin());
97 merge(insertPos);
98 }
99
100 /**
101 * Reclaims an index array.
102 * @param indices
103 */
104 void insert(std::vector<unsigned int>& indices){
105
106 if (indices.empty()) {
107 return;
108 }
109
110 std::sort(indices.begin(), indices.end());
111 std::unique(indices);
112
113 std::vector<unsigned int>::iterator i;
114 IndexListContainerIterator insertPos;
115 unsigned int beginIndex;
116 unsigned int endIndex;
117
118 beginIndex = indices[0];
119
120 for ( i = indices.begin() + 1 ; i != indices.end(); ++i) {
121 if (*i != *(i -1) + 1) {
122 insertPos = insert(beginIndex, *(i-1));
123 merge(insertPos);
124 beginIndex = *i;
125 }
126 }
127
128 insertPos = insert(beginIndex, *(i-1));
129 merge(insertPos);
130
131
132 }
133
134 std::vector<unsigned int> getIndicesBefore(unsigned int index) {
135 std::vector<unsigned int> result;
136 IndexListContainerIterator i;
137
138 for(i = indexContainer_.begin(); i != indexContainer_.end(); ++i) {
139 if ((*i).first > index) {
140 indexContainer_.erase(indexContainer_, i);
141 break;
142 } else if ((*i).second < index) {
143
144 for (unsigned int j = (*i).first; j <= (*i).second; ++j) {
145 result.push_back(j);
146 }
147 continue;
148 } else if ((*i).first == (*i).second) {
149 result.push_back((*i).first);
150 indexContainer_.erase(indexContainer_.begin(), i);
151 break;
152 } else {
153
154 for (unsigned int j = (*i).first; j < index; ++j) {
155 result.push_back(j);
156 }
157
158 (*i).first = index;
159 indexContainer_.erase(indexContainer_.begin(), i);
160 break;
161 }
162
163 }
164
165 return result;
166
167 }
168
169 unsigned int getMaxIndex() {
170 return maxIndex_;
171 }
172
173 private:
174
175 IndexListContainerIterator internalInsert(unsigned int beginIndex, unsigned int endIndex,
176 IndexListContainerIterator i) {
177 if (beginIndex > endIndex) {
178 std::swap(beginIndex, endIndex);
179 std::cerr << "" << std::endl;
180 }
181
182 if (endIndex > maxIndex_) {
183 std::cerr << "" << std::endl;
184 }
185
186 IndexListContainerIterator j;
187
188 if (i == indexContainer_.end()) {
189 j = i;
190 --j;
191 if (j != indexContainer_.begin() && (*j).second >= beginIndex) {
192 std::cerr << "" << std::endl;
193 }
194
195 indexContainer_.insert(i, make_pair(beginIndex, endIndex));
196 return --i;
197 }
198
199
200 for (j = i; j != indexContainer_.end(); ++j) {
201 if ((*j).first > endIndex) {
202 indexContainer_.insert(j, make_pair(beginIndex, endIndex));
203 return --j;
204 } else if ((*j).second < beginIndex) {
205 continue;
206 } else {
207 std::cerr << "" << std::endl;
208 }
209 }
210
211
212
213 }
214
215 void merge(IndexListContainerIterator i) {
216 IndexListContainerIterator j;
217
218 if ( i != indexContainer_.begin()) {
219 j = i;
220 --j;
221 if (j != indexContainer_.begin() && (*j).second + 1 == (*i).first) {
222 (*i).first = (*j).first;
223 indexContainer_.erase(j);
224 }
225 }
226
227 if ( i != indexContainer_.end()) {
228 j = i;
229 ++j;
230
231 if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) {
232 (*i).second = (*j).second;
233 indexContainer_.erase(j);
234 }
235 }
236
237 }
238 unsgined int minIndex_;
239 unsigned int maxIndex_;
240 std::list<std::pair<unsigned int, unsigned int> > indexContainer_;
241 };
242
243
244 /**
245 * @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp"
246 * @brief
247 */
248 class LocalIndexManager {
249 public:
250 ~LocalIndexManager() {
251 delete instance_;
252 }
253
254 unsigned int getNextAtomIndex() {
255 return atomIndexContainer_.pop();
256 }
257
258 std::vector<unsigned int> getAtomIndicesBefore(unsigned int index) {
259 return atomIndexContainer_.getIndicesBefore(index);
260 }
261
262 void releaseAtomIndex(unsigned int index) {
263 atomIndexContainer_.insert(index);
264 }
265
266 void releaseAtomIndex(unsigned int beginIndex, unsigned int endIndex) {
267 atomIndexContainer_.insert(beginIndex, endIndex);
268 }
269
270 void releaseAtomIndex(std::vector<unsigned int> indices) {
271 atomIndexContainer_.insert(indices);
272 }
273
274 unsigned int getNextRigidBodyIndex() {
275 return rigidBodyIndexContainer_.pop();
276 }
277
278 std::vector<unsigned int> getRigidBodyIndicesBefore(unsigned int index) {
279 return rigidBodyIndexContainer_.getIndicesBefore(index);
280 }
281
282 void releaseRigidBodyIndex(unsigned int index) {
283 rigidBodyIndexContainer_.insert(index);
284 }
285
286 void releaseRigidBodyIndex(unsigned int beginIndex, unsigned int endIndex) {
287 rigidBodyIndexContainer_.insert(beginIndex, endIndex);
288 }
289
290 void releaseRigidBodyIndex(std::vector<unsigned int> indices) {
291 rigidBodyIndexContainer_.insert(indices);
292 }
293
294 static LocalIndexManager* getInstance() {
295 if (instance_ == NULL) {
296 instance_ = new LocalIndexManager();
297 } else {
298 return instance_;
299 }
300 }
301
302 private:
303 LocalIndexManager();
304
305 static LocalIndexManager* instance_;
306 IndexListContainer atomIndexContainer_;
307 IndexListContainer rigidBodyIndexContainer_;
308 };
309
310 } //end namespace oopse
311 #endif //UTILS_LOCALINDEXMANAGER_HPP