| 1 | gezelter | 2204 | /* | 
| 2 | gezelter | 1930 | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  |  | * | 
| 4 |  |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  |  | * non-exclusive, royalty free, license to use, modify and | 
| 6 |  |  | * redistribute this software in source and binary code form, provided | 
| 7 |  |  | * that the following conditions are met: | 
| 8 |  |  | * | 
| 9 |  |  | * 1. Acknowledgement of the program authors must be made in any | 
| 10 |  |  | *    publication of scientific results based in part on use of the | 
| 11 |  |  | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 |  |  | *    the article in which the program was described (Matthew | 
| 13 |  |  | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 |  |  | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 |  |  | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 |  |  | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 |  |  | * | 
| 18 |  |  | * 2. Redistributions of source code must retain the above copyright | 
| 19 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 20 |  |  | * | 
| 21 |  |  | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 23 |  |  | *    documentation and/or other materials provided with the | 
| 24 |  |  | *    distribution. | 
| 25 |  |  | * | 
| 26 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 27 |  |  | * kind. All express or implied conditions, representations and | 
| 28 |  |  | * warranties, including any implied warranty of merchantability, | 
| 29 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 32 |  |  | * using, modifying or distributing the software or its | 
| 33 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 34 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 36 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 37 |  |  | * arising out of the use of or inability to use software, even if the | 
| 38 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 39 |  |  | * such damages. | 
| 40 |  |  | */ | 
| 41 |  |  |  | 
| 42 |  |  | /** | 
| 43 |  |  | * @file LocalIndexManager.hpp | 
| 44 |  |  | * @author    tlin | 
| 45 |  |  | * @date  11/02/2004 | 
| 46 |  |  | * @version 1.0 | 
| 47 |  |  | */ | 
| 48 |  |  |  | 
| 49 |  |  | #ifndef UTILS_LOCALINDEXMANAGER_HPP | 
| 50 |  |  | #define UTILS_LOCALINDEXMANAGER_HPP | 
| 51 |  |  | #include <algorithm> | 
| 52 |  |  | #include <iostream> | 
| 53 |  |  | #include <cassert> | 
| 54 |  |  | #include <list> | 
| 55 |  |  | #include <utility> | 
| 56 |  |  |  | 
| 57 |  |  | namespace oopse { | 
| 58 |  |  |  | 
| 59 | gezelter | 2204 | /** | 
| 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 | gezelter | 1930 |  | 
| 69 |  |  |  | 
| 70 | gezelter | 2204 | IndexListContainer(int minIndex = 0, int maxIndex = MAX_INTEGER) | 
| 71 |  |  | :minIndex_(minIndex), maxIndex_(maxIndex) { | 
| 72 | gezelter | 1930 |  | 
| 73 | gezelter | 2204 | indexContainer_.push_back(std::make_pair(minIndex, maxIndex)); | 
| 74 |  |  | } | 
| 75 | gezelter | 1930 |  | 
| 76 | gezelter | 2204 | int pop() { | 
| 77 | gezelter | 1930 |  | 
| 78 | gezelter | 2204 | if (indexContainer_.empty()) { | 
| 79 |  |  | std::cerr << "" << std::endl; | 
| 80 |  |  | } | 
| 81 | gezelter | 1930 |  | 
| 82 | gezelter | 2204 | IndexListContainerIterator i = indexContainer_.begin(); | 
| 83 |  |  | int result; | 
| 84 | gezelter | 1930 |  | 
| 85 | gezelter | 2204 | result = indexContainer_.front().first; | 
| 86 | gezelter | 1930 |  | 
| 87 | gezelter | 2204 | 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 | gezelter | 1930 |  | 
| 95 | gezelter | 2204 | return result; | 
| 96 |  |  | } | 
| 97 | gezelter | 1930 |  | 
| 98 |  |  |  | 
| 99 | gezelter | 2204 | /** | 
| 100 |  |  | * | 
| 101 |  |  | */ | 
| 102 |  |  | void insert(int index) { | 
| 103 |  |  | IndexListContainerIterator insertPos = internalInsert(index, index); | 
| 104 |  |  | merge(insertPos); | 
| 105 |  |  | } | 
| 106 | gezelter | 1930 |  | 
| 107 | gezelter | 2204 | /** | 
| 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 | gezelter | 1930 |  | 
| 117 | gezelter | 2204 | /** | 
| 118 |  |  | * Reclaims an index array. | 
| 119 |  |  | * @param indices | 
| 120 |  |  | */ | 
| 121 |  |  | void insert(std::vector<int>& indices){ | 
| 122 | gezelter | 1930 |  | 
| 123 | gezelter | 2204 | if (indices.empty()) { | 
| 124 |  |  | return; | 
| 125 |  |  | } | 
| 126 | gezelter | 1930 |  | 
| 127 | gezelter | 2204 | std::sort(indices.begin(), indices.end()); | 
| 128 |  |  | std::unique(indices.begin(), indices.end()); | 
| 129 | gezelter | 1930 |  | 
| 130 | gezelter | 2204 | std::vector<int>::iterator i; | 
| 131 |  |  | IndexListContainerIterator insertPos; | 
| 132 |  |  | int beginIndex; | 
| 133 | gezelter | 1930 |  | 
| 134 | gezelter | 2204 | beginIndex = indices[0]; | 
| 135 | gezelter | 1930 |  | 
| 136 | gezelter | 2204 | 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 | gezelter | 1930 |  | 
| 143 |  |  |  | 
| 144 | gezelter | 2204 | } | 
| 145 | gezelter | 1930 |  | 
| 146 | gezelter | 2204 | std::vector<int> getIndicesBefore(int index) { | 
| 147 |  |  | std::vector<int> result; | 
| 148 |  |  | IndexListContainerIterator i; | 
| 149 | gezelter | 1930 |  | 
| 150 | gezelter | 2204 | 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 | gezelter | 1930 |  | 
| 168 | gezelter | 2204 | for (int j = (*i).first; j < index; ++j) { | 
| 169 |  |  | result.push_back(j); | 
| 170 |  |  | } | 
| 171 | gezelter | 1930 |  | 
| 172 | gezelter | 2204 | (*i).first =  index; | 
| 173 |  |  | indexContainer_.erase(indexContainer_.begin(), i); | 
| 174 |  |  | break; | 
| 175 |  |  | } | 
| 176 | gezelter | 1930 |  | 
| 177 | gezelter | 2204 | } | 
| 178 | gezelter | 1930 |  | 
| 179 | gezelter | 2204 | return result; | 
| 180 | gezelter | 1930 |  | 
| 181 | gezelter | 2204 | } | 
| 182 | gezelter | 1930 |  | 
| 183 | gezelter | 2204 | int getMaxIndex() { | 
| 184 |  |  | return maxIndex_; | 
| 185 |  |  | } | 
| 186 | gezelter | 1930 |  | 
| 187 | gezelter | 2204 | private: | 
| 188 | gezelter | 1930 |  | 
| 189 | gezelter | 2204 | IndexListContainerIterator internalInsert(int beginIndex, int endIndex) { | 
| 190 | gezelter | 1930 |  | 
| 191 | gezelter | 2204 | if (beginIndex > endIndex) { | 
| 192 |  |  | std::swap(beginIndex, endIndex); | 
| 193 |  |  | std::cerr << "" << std::endl; | 
| 194 |  |  | } | 
| 195 | gezelter | 1930 |  | 
| 196 | gezelter | 2204 | if (endIndex > maxIndex_) { | 
| 197 |  |  | std::cerr << "" << std::endl; | 
| 198 |  |  | } | 
| 199 | gezelter | 1930 |  | 
| 200 |  |  |  | 
| 201 | gezelter | 2204 | IndexListContainerIterator j; | 
| 202 | gezelter | 1930 |  | 
| 203 | gezelter | 2204 | 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 | gezelter | 1930 |  | 
| 215 | gezelter | 2204 | indexContainer_.push_back(std::make_pair(beginIndex, endIndex)); | 
| 216 |  |  | return --indexContainer_.end(); | 
| 217 | gezelter | 1930 |  | 
| 218 | gezelter | 2204 | } | 
| 219 | gezelter | 1930 |  | 
| 220 | gezelter | 2204 | void merge(IndexListContainerIterator i) { | 
| 221 |  |  | IndexListContainerIterator j; | 
| 222 | gezelter | 1930 |  | 
| 223 | gezelter | 2204 | //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 | gezelter | 1930 |  | 
| 233 | gezelter | 2204 | //check whether current node can be merged with its next node | 
| 234 |  |  | if ( i != indexContainer_.end()) { | 
| 235 |  |  | j = i; | 
| 236 |  |  | ++j; | 
| 237 | gezelter | 1930 |  | 
| 238 | gezelter | 2204 | if (j != indexContainer_.end() && (*i).second + 1 == (*j).first) { | 
| 239 |  |  | (*i).second = (*j).second; | 
| 240 |  |  | indexContainer_.erase(j); | 
| 241 |  |  | } | 
| 242 |  |  | } | 
| 243 | gezelter | 1930 |  | 
| 244 | gezelter | 2204 | } | 
| 245 |  |  | int minIndex_; | 
| 246 |  |  | int maxIndex_; | 
| 247 |  |  | std::list<std::pair<int, int> > indexContainer_; | 
| 248 |  |  | }; | 
| 249 | gezelter | 1930 |  | 
| 250 |  |  |  | 
| 251 | gezelter | 2204 | /** | 
| 252 |  |  | * @class LocalIndexManager LocalIndexManager.hpp "utils/LocalIndexManager.hpp" | 
| 253 |  |  | * @brief | 
| 254 |  |  | */ | 
| 255 |  |  | class LocalIndexManager { | 
| 256 |  |  | public: | 
| 257 | gezelter | 1930 |  | 
| 258 | gezelter | 2204 | int getNextAtomIndex() { | 
| 259 |  |  | return atomIndexContainer_.pop(); | 
| 260 |  |  | } | 
| 261 | gezelter | 1930 |  | 
| 262 | gezelter | 2204 | std::vector<int> getAtomIndicesBefore(int index) { | 
| 263 |  |  | return atomIndexContainer_.getIndicesBefore(index); | 
| 264 |  |  | } | 
| 265 | gezelter | 1930 |  | 
| 266 | gezelter | 2204 | void releaseAtomIndex(int index) { | 
| 267 |  |  | atomIndexContainer_.insert(index); | 
| 268 |  |  | } | 
| 269 | gezelter | 1930 |  | 
| 270 | gezelter | 2204 | void releaseAtomIndex(int beginIndex, int endIndex) { | 
| 271 |  |  | atomIndexContainer_.insert(beginIndex, endIndex); | 
| 272 |  |  | } | 
| 273 | gezelter | 1930 |  | 
| 274 | gezelter | 2204 | void releaseAtomIndex(std::vector<int> indices) { | 
| 275 |  |  | atomIndexContainer_.insert(indices); | 
| 276 |  |  | } | 
| 277 | gezelter | 1930 |  | 
| 278 | gezelter | 2204 | int getNextRigidBodyIndex() { | 
| 279 |  |  | return rigidBodyIndexContainer_.pop(); | 
| 280 |  |  | } | 
| 281 | gezelter | 1930 |  | 
| 282 | gezelter | 2204 | std::vector<int> getRigidBodyIndicesBefore(int index) { | 
| 283 |  |  | return rigidBodyIndexContainer_.getIndicesBefore(index); | 
| 284 |  |  | } | 
| 285 | gezelter | 1930 |  | 
| 286 | gezelter | 2204 | void releaseRigidBodyIndex(int index) { | 
| 287 |  |  | rigidBodyIndexContainer_.insert(index); | 
| 288 |  |  | } | 
| 289 | gezelter | 1930 |  | 
| 290 | gezelter | 2204 | void releaseRigidBodyIndex(int beginIndex, int endIndex) { | 
| 291 |  |  | rigidBodyIndexContainer_.insert(beginIndex, endIndex); | 
| 292 |  |  | } | 
| 293 | gezelter | 1930 |  | 
| 294 | gezelter | 2204 | void releaseRigidBodyIndex(std::vector<int> indices) { | 
| 295 |  |  | rigidBodyIndexContainer_.insert(indices); | 
| 296 |  |  | } | 
| 297 | gezelter | 1930 |  | 
| 298 | gezelter | 2204 | private: | 
| 299 | gezelter | 1930 |  | 
| 300 | gezelter | 2204 | IndexListContainer atomIndexContainer_; | 
| 301 |  |  | IndexListContainer rigidBodyIndexContainer_; | 
| 302 |  |  | }; | 
| 303 | gezelter | 1930 |  | 
| 304 |  |  | } //end namespace oopse | 
| 305 |  |  | #endif //UTILS_LOCALINDEXMANAGER_HPP |