1 |
< |
/* |
1 |
> |
/* |
2 |
|
* Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. |
3 |
|
* |
4 |
|
* The University of Notre Dame grants you ("Licensee") a |
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 |
< |
*/ |
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 |
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: |
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 typename std::map<KeyType, ValueType> MapType; |
72 |
< |
typedef typename std::map<KeyType, ValueType>::iterator MapTypeIterator; |
73 |
< |
typedef typename MapType::value_type value_type; |
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 typename std::map<KeyType, ValueType> MapType; |
72 |
> |
typedef typename std::map<KeyType, ValueType>::iterator MapTypeIterator; |
73 |
> |
typedef typename MapType::value_type value_type; |
74 |
|
|
75 |
< |
TypeContainer() : index_(0) {} |
75 |
> |
TypeContainer() : index_(0) {} |
76 |
|
|
77 |
< |
~TypeContainer() { |
78 |
< |
MapTypeIterator i; |
79 |
< |
for (i = data_.begin(); i != data_.end(); ++i) { |
80 |
< |
delete (i->second).second; |
81 |
< |
} |
82 |
< |
data_.clear(); |
83 |
< |
} |
77 |
> |
~TypeContainer() { |
78 |
> |
MapTypeIterator i; |
79 |
> |
for (i = data_.begin(); i != data_.end(); ++i) { |
80 |
> |
delete (i->second).second; |
81 |
> |
} |
82 |
> |
data_.clear(); |
83 |
> |
} |
84 |
|
|
85 |
< |
bool add(KeyType& keys, ElemPtr elem) { |
86 |
< |
assert(keys.size() == SIZE); |
87 |
< |
assert(elem); |
88 |
< |
return data_.insert(value_type(keys, std::make_pair(index_++,elem))).second; |
89 |
< |
} |
85 |
> |
bool add(KeyType& keys, ElemPtr elem) { |
86 |
> |
assert(keys.size() == SIZE); |
87 |
> |
assert(elem); |
88 |
> |
return data_.insert(value_type(keys, std::make_pair(index_++,elem))).second; |
89 |
> |
} |
90 |
|
|
91 |
< |
/** Exact Match */ |
92 |
< |
ElemPtr find(KeyType& keys) { |
93 |
< |
assert(keys.size() == SIZE); |
94 |
< |
MapTypeIterator i; |
95 |
< |
ValueType foundType; |
91 |
> |
/** Exact Match */ |
92 |
> |
ElemPtr find(KeyType& keys) { |
93 |
> |
assert(keys.size() == SIZE); |
94 |
> |
MapTypeIterator i; |
95 |
> |
ValueType foundType; |
96 |
|
|
97 |
< |
i = data_.find(keys); |
98 |
< |
if (i != data_.end()) { |
99 |
< |
return (i->second).second; |
100 |
< |
} |
97 |
> |
i = data_.find(keys); |
98 |
> |
if (i != data_.end()) { |
99 |
> |
return (i->second).second; |
100 |
> |
} |
101 |
|
|
102 |
< |
KeyType reversedKeys = keys; |
103 |
< |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
102 |
> |
KeyType reversedKeys = keys; |
103 |
> |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
104 |
|
|
105 |
< |
i = data_.find(reversedKeys); |
106 |
< |
if (i != data_.end()) { |
107 |
< |
return (i->second).second; |
108 |
< |
} else { |
109 |
< |
return NULL; |
110 |
< |
} |
105 |
> |
i = data_.find(reversedKeys); |
106 |
> |
if (i != data_.end()) { |
107 |
> |
return (i->second).second; |
108 |
> |
} else { |
109 |
> |
return NULL; |
110 |
> |
} |
111 |
|
|
112 |
< |
} |
112 |
> |
} |
113 |
|
|
114 |
< |
/** |
115 |
< |
* @todo |
116 |
< |
*/ |
117 |
< |
ElemPtr find(KeyType& keys, const std::string& wildCard) { |
118 |
< |
assert(keys.size() == SIZE); |
114 |
> |
/** |
115 |
> |
* @todo |
116 |
> |
*/ |
117 |
> |
ElemPtr find(KeyType& keys, const std::string& wildCard) { |
118 |
> |
assert(keys.size() == SIZE); |
119 |
|
|
120 |
< |
std::vector<KeyTypeIterator> iterCont; |
121 |
< |
KeyType replacedKey; |
122 |
< |
MapTypeIterator i; |
123 |
< |
std::vector<ValueType> foundTypes; |
120 |
> |
std::vector<KeyTypeIterator> iterCont; |
121 |
> |
KeyType replacedKey; |
122 |
> |
MapTypeIterator i; |
123 |
> |
std::vector<ValueType> foundTypes; |
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 |
< |
} |
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 |
< |
//reverse the order of keys |
133 |
< |
KeyType reversedKeys = keys; |
134 |
< |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
132 |
> |
//reverse the order of keys |
133 |
> |
KeyType reversedKeys = keys; |
134 |
> |
std::reverse(reversedKeys.begin(), reversedKeys.end()); |
135 |
|
|
136 |
< |
//if the reversedKeys is the same as keys, just skip it |
137 |
< |
if (reversedKeys != keys) { |
136 |
> |
//if the reversedKeys is the same as keys, just skip it |
137 |
> |
if (reversedKeys != keys) { |
138 |
|
|
139 |
|
|
140 |
< |
//empty the iterator container |
141 |
< |
iterCont.clear(); |
140 |
> |
//empty the iterator container |
141 |
> |
iterCont.clear(); |
142 |
|
|
143 |
< |
while (replaceWithWildCard(iterCont, reversedKeys, replacedKey, wildCard)) { |
144 |
< |
i = data_.find(replacedKey); |
145 |
< |
if (i != data_.end()) { |
146 |
< |
foundTypes.push_back(i->second); |
147 |
< |
} |
148 |
< |
} |
143 |
> |
while (replaceWithWildCard(iterCont, reversedKeys, replacedKey, wildCard)) { |
144 |
> |
i = data_.find(replacedKey); |
145 |
> |
if (i != data_.end()) { |
146 |
> |
foundTypes.push_back(i->second); |
147 |
> |
} |
148 |
> |
} |
149 |
|
|
150 |
< |
//replaceWithWildCard can not generate this particular sequence, we have to |
151 |
< |
//do it manually |
152 |
< |
KeyType allWildCards(SIZE, wildCard); |
153 |
< |
i = data_.find(replacedKey); |
154 |
< |
if (i != data_.end()) { |
155 |
< |
foundTypes.push_back(i->second); |
156 |
< |
} |
150 |
> |
//replaceWithWildCard can not generate this particular sequence, we have to |
151 |
> |
//do it manually |
152 |
> |
KeyType allWildCards(SIZE, wildCard); |
153 |
> |
i = data_.find(replacedKey); |
154 |
> |
if (i != data_.end()) { |
155 |
> |
foundTypes.push_back(i->second); |
156 |
> |
} |
157 |
|
|
158 |
< |
} |
158 |
> |
} |
159 |
|
|
160 |
< |
typename std::vector<ValueType>::iterator j; |
161 |
< |
j = std::min_element(foundTypes.begin(), foundTypes.end()); |
160 |
> |
typename std::vector<ValueType>::iterator j; |
161 |
> |
j = std::min_element(foundTypes.begin(), foundTypes.end()); |
162 |
|
|
163 |
< |
return j == foundTypes.end() ? NULL : j->second; |
164 |
< |
} |
163 |
> |
return j == foundTypes.end() ? NULL : j->second; |
164 |
> |
} |
165 |
|
|
166 |
< |
unsigned int size() { |
167 |
< |
return data_.size(); |
168 |
< |
} |
166 |
> |
unsigned int size() { |
167 |
> |
return data_.size(); |
168 |
> |
} |
169 |
|
|
170 |
< |
ElemPtr beginType(MapTypeIterator& i) { |
171 |
< |
i = data_.begin(); |
172 |
< |
return i != data_.end() ? (i->second).second : NULL; |
173 |
< |
} |
170 |
> |
ElemPtr beginType(MapTypeIterator& i) { |
171 |
> |
i = data_.begin(); |
172 |
> |
return i != data_.end() ? (i->second).second : NULL; |
173 |
> |
} |
174 |
|
|
175 |
< |
ElemPtr nextType(MapTypeIterator& i) { |
176 |
< |
++i; |
177 |
< |
return i != data_.end() ? (i->second).second : NULL; |
178 |
< |
} |
175 |
> |
ElemPtr nextType(MapTypeIterator& i) { |
176 |
> |
++i; |
177 |
> |
return i != data_.end() ? (i->second).second : NULL; |
178 |
> |
} |
179 |
|
|
180 |
< |
private: |
181 |
< |
int index_; |
182 |
< |
MapType data_; |
180 |
> |
private: |
181 |
> |
int index_; |
182 |
> |
MapType data_; |
183 |
|
|
184 |
< |
}; |
184 |
> |
}; |
185 |
|
|
186 |
|
|
187 |
|
}//end namespace oopse |