00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef MOOT_ASSOC_VECTOR_H
00030 #define MOOT_ASSOC_VECTOR_H
00031
00032 #include <vector>
00033 #include <algorithm>
00034
00035 namespace moot {
00036
00037
00038
00040
00041 struct AssocVectorNode
00042 : public std::pair<KeyT,ValT>
00043 {
00044
00045
00046 typedef KeyT key_type;
00047 typedef ValT value_type;
00048 typedef std::pair<KeyT,ValT> PairT;
00049 typedef AssocVectorNode<KeyT,ValT> ThisT;
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00061
00062
00063 inline AssocVectorNode(void) {};
00064
00066 inline AssocVectorNode(const KeyT &key)
00067 : PairT(key,ValT())
00068 {};
00069
00071 inline AssocVectorNode(const KeyT &key, const ValT &val)
00072 : PairT(key,val)
00073 {};
00074
00076 inline AssocVectorNode(const ThisT &x)
00077 : PairT(x.first,x.second)
00078 {};
00079
00081 inline ~AssocVectorNode(void) {};
00083
00084
00086
00087
00088 inline const key_type &key (void) const { return this->first; };
00089
00090 inline value_type &value (void) { return this->second; };
00091 inline const value_type &value (void) const { return this->second; };
00093
00094
00096
00097
00098 {
00099 return (this->first < x.first
00100 ? true
00101 : (this->first > x.first
00102 ? false
00103 : this->second < x.second));
00104 };
00106 };
00107
00108
00109
00110
00111
00123 template<typename KeyT, typename ValT>
00124 class AssocVector : public std::vector<AssocVectorNode<KeyT,ValT> >
00125 {
00126 public:
00127
00128
00129 typedef KeyT assoc_key_type;
00130 typedef ValT assoc_value_type;
00131 typedef AssocVectorNode<KeyT,ValT> assoc_node_type;
00132 typedef std::vector<assoc_node_type> assoc_vector_type;
00133
00134 typedef typename assoc_vector_type::iterator iterator;
00135 typedef typename assoc_vector_type::const_iterator const_iterator;
00136
00137 typedef typename assoc_vector_type::reverse_iterator reverse_iterator;
00138 typedef typename assoc_vector_type::const_reverse_iterator const_reverse_iterator;
00139
00140 public:
00141
00142
00143
00145 struct value_sort_func {
00146 inline bool operator()(const assoc_node_type &x, const assoc_node_type &y) const
00147 {
00148 return (x.second < y.second
00149 ? true
00150 : (x.second > y.second
00151 ? false
00152 : x.first < y.first));
00153 };
00154 };
00155
00156 public:
00157
00158
00159
00160
00161 public:
00162
00163
00164
00165
00167
00168
00169 inline AssocVector(void) {};
00170
00172 inline AssocVector(const size_t mysize)
00173 {
00174 this->reserve(mysize);
00175 };
00176
00178 inline ~AssocVector(void) {};
00180
00181
00183
00184
00185 * or end() if no such node exists */
00186 inline iterator find(const KeyT &key)
00187 {
00188 iterator i;
00189 for (i = this->begin(); i != this->end() && i->first != key; i++) ;
00190 return i;
00191 };
00192
00195 inline const_iterator find(const KeyT &key) const
00196 {
00197 const_iterator i;
00198 for (i = this->begin(); i != this->end() && i->first != key; i++) ;
00199 return i;
00200 };
00201
00204 inline iterator get(const KeyT &key)
00205 {
00206 iterator i = find(key);
00207 return (i == this->end() ? assoc_vector_type::insert(i,key) : i);
00208 };
00209
00215 inline iterator insert(const KeyT &key, const ValT &val)
00216 {
00217 iterator i = get(key);
00218 i->second = val;
00219 return i;
00220 };
00222
00223
00225
00226
00230 inline assoc_node_type &get_node(const KeyT &key)
00231 { return *(get(key)); };
00232
00234 inline assoc_value_type &get_value(const KeyT &key)
00235 { return get_node(key).second; };
00236
00238 inline assoc_node_type &nth(const size_t n)
00239 { return assoc_vector_type::operator[](n); };
00241
00242
00244
00245
00246 inline assoc_value_type &operator[](const KeyT &key)
00247 { return get_node(key).second; };
00248
00250 inline const assoc_value_type &operator[](const KeyT &key) const
00251 { return find(key)->second; };
00253
00254
00256
00257
00258 inline void sort_bykey(void)
00259 { std::sort(this->begin(),this->end()); };
00260
00262 inline void rsort_bykey(void)
00263 { std::sort(this->rbegin(),this->rend()); };
00264
00266 inline void sort_byvalue(void)
00267 { std::sort(this->begin(),this->end(),value_sort_func()); };
00268
00270 inline void rsort_byvalue(void)
00271 { std::sort(this->rbegin(),this->rend(),value_sort_func()); };
00273
00274 };
00275
00276 };
00277
00278 #endif // MOOT_ASSOC_VECTOR_H