ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/utils/TypeContainer.hpp
Revision: 1930
Committed: Wed Jan 12 22:41:40 2005 UTC (19 years, 5 months ago) by gezelter
File size: 6751 byte(s)
Log Message:
merging new_design branch into OOPSE-2.0

File Contents

# Content
1 /*
2 * 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 TypeContainer.hpp
44 * @author tlin
45 * @date 11/04/2004
46 * @time 13:51am
47 * @version 1.0
48 */
49
50 #ifndef UTILS_TYPECONTAINER_HPP
51 #define UTILS_TYPECONTAINER_HPP
52 #include <algorithm>
53 #include <map>
54 #include <vector>
55
56 #include "utils/Utility.hpp"
57
58 namespace oopse {
59
60 /**
61 * @class TypeContainer TypeContainer.hpp "utils/TypeContainer.hpp"
62 */
63 template<class ElemType, int SIZE>
64 class TypeContainer {
65 public:
66
67 typedef ElemType* ElemPtr;
68 typedef std::vector<std::string> KeyType;
69 typedef typename KeyType::iterator KeyTypeIterator;
70 typedef std::pair<int, ElemPtr> ValueType;
71 typedef std::map<KeyType, ValueType> MapType;
72 typedef typename std::map<KeyType, ValueType>::iterator MapTypeIterator;
73
74 TypeContainer() : index_(0) {}
75
76 ~TypeContainer() {
77 MapTypeIterator i;
78 for (i = data_.begin(); i != data_.end(); ++i) {
79 delete (i->second).second;
80 }
81 data_.clear();
82 }
83
84 bool add(KeyType& keys, ElemPtr elem) {
85 assert(keys.size() == SIZE);
86 assert(elem);
87 return data_.insert(MapType::value_type(keys, std::make_pair(index_++,elem))).second;
88 }
89
90 /** Exact Match */
91 ElemPtr find(KeyType& keys) {
92 assert(keys.size() == SIZE);
93 MapTypeIterator i;
94 ValueType foundType;
95
96 i = data_.find(keys);
97 if (i != data_.end()) {
98 return (i->second).second;
99 }
100
101 KeyType reversedKeys = keys;
102 std::reverse(reversedKeys.begin(), reversedKeys.end());
103
104 i = data_.find(reversedKeys);
105 if (i != data_.end()) {
106 return (i->second).second;
107 } else {
108 return NULL;
109 }
110
111 }
112
113 /**
114 * @todo
115 */
116 ElemPtr find(KeyType& keys, const std::string& wildCard) {
117 assert(keys.size() == SIZE);
118
119 std::vector<KeyTypeIterator> iterCont;
120 KeyType replacedKey;
121 MapTypeIterator i;
122 std::vector<ValueType> foundTypes;
123
124 while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) {
125 i = data_.find(replacedKey);
126 if (i != data_.end()) {
127 foundTypes.push_back(i->second);
128 }
129 }
130
131 //reverse the order of keys
132 KeyType reversedKeys = keys;
133 std::reverse(reversedKeys.begin(), reversedKeys.end());
134
135 //if the reversedKeys is the same as keys, just skip it
136 if (reversedKeys != keys) {
137
138
139 //empty the iterator container
140 iterCont.clear();
141
142 while (replaceWithWildCard(iterCont, reversedKeys, replacedKey, wildCard)) {
143 i = data_.find(replacedKey);
144 if (i != data_.end()) {
145 foundTypes.push_back(i->second);
146 }
147 }
148
149 //replaceWithWildCard can not generate this particular sequence, we have to
150 //do it manually
151 KeyType allWildCards(SIZE, wildCard);
152 i = data_.find(replacedKey);
153 if (i != data_.end()) {
154 foundTypes.push_back(i->second);
155 }
156
157 }
158
159 typename std::vector<ValueType>::iterator j;
160 j = std::min_element(foundTypes.begin(), foundTypes.end());
161
162 return j == foundTypes.end() ? NULL : j->second;
163 }
164
165 unsigned int size() {
166 return data_.size();
167 }
168
169 ElemPtr beginType(MapTypeIterator& i) {
170 i = data_.begin();
171 return i != data_.end() ? (i->second).second : NULL;
172 }
173
174 ElemPtr nextType(MapTypeIterator& i) {
175 ++i;
176 return i != data_.end() ? (i->second).second : NULL;
177 }
178
179 private:
180 int index_;
181 MapType data_;
182
183 };
184
185
186 }//end namespace oopse
187
188 #endif //UTILS_TYPECONTAINER_HPP