1 |
/* |
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 |
|
40 |
#include "utils/Utility.hpp" |
41 |
|
42 |
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 |
TypeContainer() : index_(0) {} |
59 |
|
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 |
assert(elem); |
71 |
return data_.insert(MapType::value_type(keys, std::make_pair(index_++,elem))).second; |
72 |
} |
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 |
return (i->second).second; |
83 |
} |
84 |
|
85 |
KeyType reversedKeys = keys; |
86 |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
87 |
|
88 |
i = data_.find(keys); |
89 |
if (i != data_.end()) { |
90 |
return (i->second).second; |
91 |
} 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 |
while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) { |
109 |
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 |
if (reversedKeys == keys) { |
121 |
|
122 |
|
123 |
//empty the iterator container |
124 |
iterCont.clear(); |
125 |
|
126 |
while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) { |
127 |
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 |
//do it manually |
135 |
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 |
return j == foundTypes.end() ? NULL : j->second; |
147 |
} |
148 |
|
149 |
unsigned int size() { |
150 |
return data_.size(); |
151 |
} |
152 |
|
153 |
ElemPtr beginType(MapTypeIterator& i) { |
154 |
i = data_.begin(); |
155 |
return i != data_.end() ? (i->second).second : NULL; |
156 |
} |
157 |
|
158 |
ElemPtr nextType(MapTypeIterator& i) { |
159 |
++i; |
160 |
return i != data_.end() ? (i->second).second : NULL; |
161 |
} |
162 |
|
163 |
private: |
164 |
int index_; |
165 |
MapType data_; |
166 |
|
167 |
}; |
168 |
|
169 |
|
170 |
}//end namespace oopse |
171 |
|
172 |
#endif //UTILS_TYPECONTAINER_HPP |