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 |
return data_.insert(MapType::value_type(keys, make_pair(index_++,elem))).second; |
71 |
} |
72 |
|
73 |
/** Exact Match */ |
74 |
ElemPtr find(KeyType& keys) { |
75 |
assert(keys.size() == SIZE); |
76 |
MapTypeIterator i; |
77 |
ValueType foundType; |
78 |
|
79 |
i = data_.find(keys); |
80 |
if (i != data_.end()) { |
81 |
return (i->second).second; |
82 |
} |
83 |
|
84 |
KeyType reversedKeys = keys; |
85 |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
86 |
|
87 |
i = data_.find(keys); |
88 |
if (i != data_.end()) { |
89 |
return (i->second).second; |
90 |
} else { |
91 |
return NULL; |
92 |
} |
93 |
|
94 |
} |
95 |
|
96 |
/** |
97 |
* @todo |
98 |
*/ |
99 |
ElemPtr find(KeyType& keys, const std::string& wildCard) { |
100 |
assert(keys.size() == SIZE); |
101 |
|
102 |
std::vector<KeyTypeIterator> iterCont; |
103 |
KeyType replacedKey; |
104 |
MapTypeIterator i; |
105 |
std::vector<ValueType> foundTypes; |
106 |
|
107 |
while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) { |
108 |
i = data_.find(replacedKey); |
109 |
if (i != data_.end()) { |
110 |
foundTypes.push_back(i->second); |
111 |
} |
112 |
} |
113 |
|
114 |
//reverse the order of keys |
115 |
KeyType reversedKeys = keys; |
116 |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
117 |
|
118 |
//if the reversedKeys is the same as keys, just skip it |
119 |
if (reversedKeys == keys) { |
120 |
|
121 |
|
122 |
//empty the iterator container |
123 |
iterCont.clear(); |
124 |
|
125 |
while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) { |
126 |
i = data_.find(replacedKey); |
127 |
if (i != data_.end()) { |
128 |
foundTypes.push_back(i->second); |
129 |
} |
130 |
} |
131 |
|
132 |
//replaceWithWildCard can not generate this particular sequence, we have to |
133 |
//do it manually |
134 |
KeyType allWildCards(SIZE, wildCard); |
135 |
i = data_.find(replacedKey); |
136 |
if (i != data_.end()) { |
137 |
foundTypes.push_back(i->second); |
138 |
} |
139 |
|
140 |
} |
141 |
|
142 |
std::vector<ValueType>::iterator j; |
143 |
j = std::min_element(foundTypes.begin(), foundTypes.end()); |
144 |
|
145 |
return j == foundTypes.end() ? NULL : j->second; |
146 |
} |
147 |
|
148 |
unsigned int size() { |
149 |
return data_.size(); |
150 |
} |
151 |
|
152 |
ElemPtr beginType(MapTypeIterator& i) { |
153 |
i = data_.begin(); |
154 |
return i != data_.end() ? (i->second).second : NULL; |
155 |
} |
156 |
|
157 |
ElemPtr nextType(MapTypeIterator& i) { |
158 |
++i; |
159 |
return i != data_.end() ? (i->second).second : NULL; |
160 |
} |
161 |
|
162 |
private: |
163 |
int index_; |
164 |
MapType data_; |
165 |
|
166 |
}; |
167 |
|
168 |
|
169 |
}//end namespace oopse |
170 |
|
171 |
#endif //UTILS_TYPECONTAINER_HPP |