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