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