-
Notifications
You must be signed in to change notification settings - Fork 17
/
OrderedMap.h
66 lines (61 loc) · 1.7 KB
/
OrderedMap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef _ORDEREDMAP_H_
#define _ORDEREDMAP_H_
template <class KEY, class TYPE>
class OrderedMap{
public:
bool find(const KEY& key) {
if (this->keyTypeMap.find(key) == this->keyTypeMap.end()){
return false;
}
return true;
}
TYPE& operator[] (const KEY& key) {
if (!this->find(key)){
this->keyVec.push_back(key);
}
return this->keyTypeMap[key];
}
void front(KEY* k, TYPE* v) {
*k = this->keyVec.front();
*v = this->keyTypeMap[(*k)];
}
bool at(unsigned int idx, KEY* k, TYPE* v) {
if (idx >= this->size()) return false;
*k = this->keyVec[idx];
*v = this->keyTypeMap[(*k)];
return true;
}
bool at(unsigned int idx, KEY* k, TYPE* v) const{
if (idx >= this->size()) return false;
*k = this->keyVec[idx];
typename std::map < KEY, TYPE >::const_iterator iter;
iter = this->keyTypeMap.find(*k);
if (iter != this->keyTypeMap.end())
*v = iter->second;
else
return false;
return true;
};
bool value(const KEY& key, TYPE* val) const{
typename std::map < KEY, TYPE >::const_iterator iter;
iter = this->keyTypeMap.find(key);
if (iter != this->keyTypeMap.end()){
*val = iter->second;
return true;
} else {
return false;
}
}
size_t size() const { return this->keyVec.size();} ;
void clear() {
keyVec.clear();
keyTypeMap.clear();
};
const std::vector<KEY>& getKey() const{
return this->keyVec;
};
private:
std::vector < KEY > keyVec;
std::map < KEY, TYPE > keyTypeMap;
};
#endif /* _ORDEREDMAP_H_ */