forked from rperlste/UniversalCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexedMap.h
192 lines (163 loc) · 5.2 KB
/
IndexedMap.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifndef _IndexedMap
#define _IndexedMap
#include <map>
#include "ContainerExceptions.h"
template< typename Key, typename Value >
class IndexedMap{
public:
typedef typename std::map<Key,Value>::key_type key_type;
typedef typename std::map<Key,Value>::value_type value_type;
typedef typename std::map<Key,Value>::size_type size_type;
typedef typename std::map<Key,Value>::iterator iterator;
typedef typename std::map<Key,Value>::const_iterator const_iterator;
virtual ~IndexedMap(){}
inline bool insert( const Key& key, const Value& value ) { return baseMap.insert( value_type(key,value)).second; }
bool insertOverwrite( const Key&, const Value& );
inline bool remove( const Key& key ) { return baseMap.erase( key ) > 0 ? true : false; }
inline size_type size() const { return baseMap.size(); }
inline size_type max_size() const { return baseMap.max_size(); }
inline bool empty() { return baseMap.empty(); }
inline iterator begin() { return baseMap.begin(); }
inline const_iterator begin() const { return baseMap.begin(); }
inline iterator end() { return baseMap.end(); }
inline const_iterator end() const { return baseMap.end(); }
inline iterator find( const Key& key ) { return baseMap.find( key ); }
inline const_iterator find( const Key& key ) const { return baseMap.find( key ); }
bool containsKey( const Key& );
bool containsValue( const Value& );
bool containsPair( const Key&, const Value& );
bool containsPair( const value_type& );
value_type getPairByIndex( const int& );
Key getKeyByValue( const Value& );
Key getKeyByIndex( const int& );
Value getValueByKey( const Key& );
Value getValueByIndex( const int& );
int getIndexByValue( const Value& );
int getIndexByKey( const Key& );
inline Value operator [] ( const Key& key ) { return baseMap[ key ]; }
protected:
std::map< Key, Value > baseMap;
};
template< typename Key, typename Value >
bool IndexedMap<Key,Value>::insertOverwrite( const Key& key, const Value& value ){
iterator keyIter = baseMap.find(key);
if( keyIter != baseMap.end() && !(baseMap.key_comp()( key, keyIter->first ) )){
if( keyIter->second == value ) {
return false;
} else {
keyIter->second = value;
return true;
}
}
else{
baseMap.insert( keyIter, value_type( key, value ) );
return true;
}
}
template< typename Key, typename Value >
bool IndexedMap<Key,Value>::containsKey( const Key& key ){
return baseMap.find( key ) == baseMap.end() ? false : true ;
}
template< typename Key, typename Value >
bool IndexedMap<Key,Value>::containsValue( const Value& value ){
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( it->second == value )
return true;
}
return false;
}
template< typename Key, typename Value >
bool IndexedMap<Key,Value>::containsPair( const value_type& value_type ){
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( *it == value_type )
return true;
}
return false;
}
template< typename Key, typename Value >
bool IndexedMap<Key,Value>::containsPair( const Key& key, const Value& value ){
return containsPair( value_type( key, value ) );
}
template< typename Key, typename Value >
typename IndexedMap<Key,Value>::value_type IndexedMap<Key,Value>::getPairByIndex( const int& index ){
int count = 0;
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( count == index )
return (*it);
count ++;
}
throw IndexOutOfBounds();
}
template< typename Key, typename Value >
Key IndexedMap<Key,Value>::getKeyByValue( const Value& value ){
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( it->second == value )
return it->first;
}
throw ValueNotFound();
}
template< typename Key, typename Value >
Key IndexedMap<Key,Value>::getKeyByIndex( const int& index ){
int count = 0;
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( count == index )
return it->first;
count ++;
}
throw IndexOutOfBounds();
}
template< typename Key, typename Value >
Value IndexedMap<Key,Value>::getValueByKey( const Key& key ){
iterator it = baseMap.find( key );
if( it != baseMap.end() )
return ( it->second );
else
throw KeyNotFound();
}
template< typename Key, typename Value >
Value IndexedMap<Key,Value>::getValueByIndex( const int& index ){
int count = 0;
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( count == index )
return it->second;
count ++;
}
throw IndexOutOfBounds();
}
template< typename Key, typename Value >
int IndexedMap<Key,Value>::getIndexByKey( const Key& key ){
int count = 0;
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( it->first == key )
return count;
count ++;
}
throw KeyNotFound();
}
template< typename Key, typename Value >
int IndexedMap<Key,Value>::getIndexByValue( const Value& value ){
int count = 0;
for( iterator it = baseMap.begin();
it != baseMap.end();
it ++ ){
if( it->second == value )
return count;
count ++;
}
throw ValueNotFound();
}
#endif