ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/utils/TypeContainer.hpp
Revision: 3460
Committed: Mon Oct 13 21:35:22 2008 UTC (15 years, 8 months ago) by cli2
File size: 6730 byte(s)
Log Message:
Fixes for Inversions for use with Amber

File Contents

# User Rev Content
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 gezelter 2204 /**
43     * @file TypeContainer.hpp
44     * @author tlin
45     * @date 11/04/2004
46     * @time 13:51am
47     * @version 1.0
48     */
49 gezelter 1930
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 gezelter 2204 /**
61     * @class TypeContainer TypeContainer.hpp "utils/TypeContainer.hpp"
62     */
63     template<class ElemType, int SIZE>
64     class TypeContainer {
65     public:
66 gezelter 1930
67 gezelter 2204 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 typename std::map<KeyType, ValueType> MapType;
72     typedef typename std::map<KeyType, ValueType>::iterator MapTypeIterator;
73     typedef typename MapType::value_type value_type;
74 cli2 3460 typedef typename std::vector<int> MutableValues;
75 gezelter 1930
76 gezelter 2204 TypeContainer() : index_(0) {}
77 gezelter 1930
78 gezelter 2204 ~TypeContainer() {
79     MapTypeIterator i;
80     for (i = data_.begin(); i != data_.end(); ++i) {
81     delete (i->second).second;
82     }
83     data_.clear();
84     }
85 gezelter 1930
86 gezelter 2204 bool add(KeyType& keys, ElemPtr elem) {
87     assert(keys.size() == SIZE);
88     assert(elem);
89     return data_.insert(value_type(keys, std::make_pair(index_++,elem))).second;
90     }
91 gezelter 1930
92 gezelter 3437 bool replace(KeyType& keys, ElemPtr elem) {
93     assert(keys.size() == SIZE);
94     assert(elem);
95    
96     MapTypeIterator i;
97     i = data_.find(keys);
98     if (i!=data_.end()) {
99     data_[keys] = std::make_pair((i->second).first, elem);
100     return true;
101     } else {
102     return data_.insert(value_type(keys, std::make_pair(index_++,elem))).second;
103     }
104     }
105    
106 gezelter 2204 /** Exact Match */
107     ElemPtr find(KeyType& keys) {
108     assert(keys.size() == SIZE);
109     MapTypeIterator i;
110     ValueType foundType;
111 gezelter 1930
112 gezelter 2204 i = data_.find(keys);
113     if (i != data_.end()) {
114     return (i->second).second;
115     }
116 gezelter 1930
117 gezelter 2204 KeyType reversedKeys = keys;
118     std::reverse(reversedKeys.begin(), reversedKeys.end());
119 gezelter 1930
120 gezelter 2204 i = data_.find(reversedKeys);
121     if (i != data_.end()) {
122     return (i->second).second;
123     } else {
124     return NULL;
125     }
126 gezelter 1930
127 gezelter 2204 }
128 gezelter 1930
129 cli2 3460 ElemPtr permutedFindSkippingFirstElement(KeyType& keys) {
130     assert(keys.size() == SIZE);
131     MapTypeIterator i;
132     ValueType foundType;
133    
134     KeyType permutedKeys = keys;
135    
136     // skip the first element:
137     KeyTypeIterator start;
138     start = permutedKeys.begin();
139     start++;
140    
141     std::sort(start, permutedKeys.end());
142    
143     do {
144     i = data_.find(permutedKeys);
145     if (i != data_.end()) {
146     return (i->second).second;
147     }
148     } while ( std::next_permutation(start, permutedKeys.end()) );
149    
150     return NULL;
151     }
152    
153    
154 gezelter 2204 /**
155     * @todo
156     */
157     ElemPtr find(KeyType& keys, const std::string& wildCard) {
158     assert(keys.size() == SIZE);
159 gezelter 1930
160 gezelter 2204 std::vector<KeyTypeIterator> iterCont;
161     KeyType replacedKey;
162     MapTypeIterator i;
163     std::vector<ValueType> foundTypes;
164 gezelter 1930
165 gezelter 2204 while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) {
166     i = data_.find(replacedKey);
167     if (i != data_.end()) {
168     foundTypes.push_back(i->second);
169     }
170     }
171 gezelter 1930
172 gezelter 2204 //reverse the order of keys
173     KeyType reversedKeys = keys;
174     std::reverse(reversedKeys.begin(), reversedKeys.end());
175 gezelter 1930
176 gezelter 2204 //if the reversedKeys is the same as keys, just skip it
177     if (reversedKeys != keys) {
178 gezelter 1930
179    
180 gezelter 2204 //empty the iterator container
181     iterCont.clear();
182 gezelter 1930
183 gezelter 2204 while (replaceWithWildCard(iterCont, reversedKeys, replacedKey, wildCard)) {
184     i = data_.find(replacedKey);
185     if (i != data_.end()) {
186     foundTypes.push_back(i->second);
187     }
188     }
189 gezelter 1930
190 gezelter 2204 //replaceWithWildCard can not generate this particular sequence, we have to
191     //do it manually
192     KeyType allWildCards(SIZE, wildCard);
193     i = data_.find(replacedKey);
194     if (i != data_.end()) {
195     foundTypes.push_back(i->second);
196     }
197 gezelter 1930
198 gezelter 2204 }
199 gezelter 1930
200 gezelter 2204 typename std::vector<ValueType>::iterator j;
201     j = std::min_element(foundTypes.begin(), foundTypes.end());
202 gezelter 1930
203 gezelter 2204 return j == foundTypes.end() ? NULL : j->second;
204     }
205 gezelter 1930
206 gezelter 2204 unsigned int size() {
207     return data_.size();
208     }
209 gezelter 1930
210 gezelter 2204 ElemPtr beginType(MapTypeIterator& i) {
211     i = data_.begin();
212     return i != data_.end() ? (i->second).second : NULL;
213     }
214 gezelter 1930
215 gezelter 2204 ElemPtr nextType(MapTypeIterator& i) {
216     ++i;
217     return i != data_.end() ? (i->second).second : NULL;
218     }
219 gezelter 1930
220 gezelter 2204 private:
221     int index_;
222     MapType data_;
223 gezelter 1930
224 gezelter 2204 };
225 gezelter 1930
226    
227     }//end namespace oopse
228    
229     #endif //UTILS_TYPECONTAINER_HPP