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
00030
00031
00032
00033
00034
00035
00036 #ifndef _SUPPORT_ASSOCIATIVEVECTOR_H
00037 #define _SUPPORT_ASSOCIATIVEVECTOR_H
00038
00039 #include <hiker/prv/support/Vector.h>
00040 #include <hiker/prv/support/OrderedVector.h>
00041
00042 namespace alp {
00043
00044
00045
00046
00047 template<class KEY, class VALUE>
00048 class KeyedVector
00049 {
00050 public:
00051 KeyedVector(const VALUE& undefined = VALUE());
00052 KeyedVector(const KeyedVector<KEY,VALUE>& o);
00053 KeyedVector( const OrderedVector<KEY>& keys,
00054 const Vector<VALUE>& values,
00055 const VALUE& undefined = VALUE());
00056 virtual ~KeyedVector();
00057
00058 KeyedVector<KEY,VALUE>& operator=(const KeyedVector<KEY,VALUE>& o);
00059
00060
00061
00062 void SetCapacity(size_t total_space);
00063 void SetExtraCapacity(size_t extra_space);
00064 size_t Capacity() const;
00065
00066 size_t CountItems() const;
00067
00068
00069
00070 const VALUE& ValueFor(const KEY& key, bool* found = NULL) const;
00071 VALUE& EditValueFor(const KEY& key, bool* found = NULL);
00072
00073
00074
00075 const KEY& KeyAt(size_t i) const;
00076 const VALUE& ValueAt(size_t i) const;
00077 VALUE& EditValueAt(size_t i);
00078
00079 const OrderedVector<KEY>& KeyVector() const;
00080 const Vector<VALUE>& ValueVector() const;
00081 Vector<VALUE>& ValueVector();
00082
00083
00084
00085 ssize_t IndexOf(const KEY& key) const;
00086 bool GetIndexOf(const KEY& key, size_t* index) const;
00087
00088 ssize_t AddItem(const KEY& key, const VALUE& value);
00089
00090 void RemoveItemsAt(size_t index, size_t count = 1);
00091 ssize_t RemoveItemFor(const KEY& key);
00092
00093 void MakeEmpty();
00094
00095 void Swap(KeyedVector& o);
00096
00097 private:
00098 OrderedVector<KEY> m_keys;
00099 Vector<VALUE> m_values;
00100 VALUE m_undefined;
00101 size_t _reserved_data1;
00102 size_t _reserved_data2;
00103 };
00104
00105
00106 template<class KEY, class VALUE>
00107 void Swap(KeyedVector<KEY, VALUE>& v1, KeyedVector<KEY, VALUE>& v2);
00108
00109
00110
00111
00112 template<class KEY, class VALUE> inline
00113 KeyedVector<KEY,VALUE>::KeyedVector(const VALUE& undefined)
00114 : m_undefined(undefined),
00115 _reserved_data1(0), _reserved_data2(0)
00116 {
00117 }
00118
00119 template<class KEY, class VALUE> inline
00120 KeyedVector<KEY,VALUE>::KeyedVector(const KeyedVector<KEY,VALUE>& o)
00121 : m_keys(o.m_keys), m_values(o.m_values), m_undefined(o.m_undefined),
00122 _reserved_data1(0), _reserved_data2(0)
00123 {
00124 }
00125
00126 template<class KEY, class VALUE> inline
00127 KeyedVector<KEY,VALUE>::KeyedVector(const OrderedVector<KEY>& keys,
00128 const Vector<VALUE>& values, const VALUE& undefined)
00129 : m_keys(keys), m_values(values), m_undefined(undefined),
00130 _reserved_data1(0), _reserved_data2(0)
00131 {
00132 }
00133
00134 template<class KEY, class VALUE> inline
00135 KeyedVector<KEY,VALUE>::~KeyedVector()
00136 {
00137 }
00138
00139 template<class KEY, class VALUE> inline
00140 KeyedVector<KEY,VALUE>& KeyedVector<KEY,VALUE>::operator=(const KeyedVector<KEY,VALUE>& o)
00141 {
00142 m_keys = o.m_keys; m_values = o.m_values; m_undefined = o.m_undefined;
00143 return *this;
00144 }
00145
00146 template<class KEY, class VALUE> inline
00147 void KeyedVector<KEY,VALUE>::SetCapacity(size_t total_space)
00148 {
00149 m_keys.SetCapacity(total_space); m_values.SetCapacity(total_space);
00150 }
00151
00152 template<class KEY, class VALUE> inline
00153 void KeyedVector<KEY,VALUE>::SetExtraCapacity(size_t extra_space)
00154 {
00155 m_keys.SetExtraCapacity(extra_space); m_values.SetExtraCapacity(extra_space);
00156 }
00157
00158 template<class KEY, class VALUE> inline
00159 size_t KeyedVector<KEY,VALUE>::Capacity() const
00160 {
00161 return m_keys.Capacity();
00162 }
00163
00164 template<class KEY, class VALUE> inline
00165 size_t KeyedVector<KEY,VALUE>::CountItems() const
00166 {
00167 return m_keys.CountItems();
00168 }
00169
00170 template<class KEY, class VALUE> inline
00171 const VALUE& KeyedVector<KEY,VALUE>::ValueFor(const KEY& key, bool* found) const
00172 {
00173 size_t index = 0;
00174 bool has = m_keys.GetIndexOf(key, &index);
00175
00176 if( NULL != found ) *found = has;
00177
00178 if( true == has )
00179 {
00180 return m_values.ItemAt(index);
00181 }
00182 else
00183 {
00184 return m_undefined;
00185 }
00186 }
00187
00188 template<class KEY, class VALUE> inline
00189 VALUE& KeyedVector<KEY,VALUE>::EditValueFor(const KEY& key, bool* found)
00190 {
00191 size_t index = 0;
00192 bool has = m_keys.GetIndexOf(key, &index);
00193
00194 if( NULL != found ) *found = has;
00195
00196 if( true == *found )
00197 {
00198 return m_values.EditItemAt(index);
00199 }
00200 else
00201 {
00202 return m_undefined;
00203 }
00204 }
00205
00206 template<class KEY, class VALUE> inline
00207 const KEY& KeyedVector<KEY,VALUE>::KeyAt(size_t i) const
00208 {
00209 return m_keys.ItemAt(i);
00210 }
00211
00212 template<class KEY, class VALUE> inline
00213 const VALUE& KeyedVector<KEY,VALUE>::ValueAt(size_t i) const
00214 {
00215 return m_values.ItemAt(i);
00216 }
00217
00218 template<class KEY, class VALUE> inline
00219 VALUE& KeyedVector<KEY,VALUE>::EditValueAt(size_t i)
00220 {
00221 return m_values.EditItemAt(i);
00222 }
00223
00224 template<class KEY, class VALUE> inline
00225 const OrderedVector<KEY>& KeyedVector<KEY,VALUE>::KeyVector() const
00226 {
00227 return m_keys;
00228 }
00229
00230 template<class KEY, class VALUE> inline
00231 const Vector<VALUE>& KeyedVector<KEY,VALUE>::ValueVector() const
00232 {
00233 return m_values;
00234 }
00235
00236 template<class KEY, class VALUE> inline
00237 Vector<VALUE>& KeyedVector<KEY,VALUE>::ValueVector()
00238 {
00239 return m_values;
00240 }
00241
00242 template<class KEY, class VALUE> inline
00243 ssize_t KeyedVector<KEY,VALUE>::IndexOf(const KEY& key) const
00244 {
00245 return m_keys.IndexOf(key);
00246 }
00247
00248 template<class KEY, class VALUE> inline
00249 bool KeyedVector<KEY,VALUE>::GetIndexOf(const KEY& key, size_t* index) const
00250 {
00251 return m_keys.GetIndexOf(key, index);
00252 }
00253
00254 template<class KEY, class VALUE> inline
00255 ssize_t KeyedVector<KEY,VALUE>::AddItem(const KEY& key, const VALUE& value)
00256 {
00257 bool added;
00258 const ssize_t pos = m_keys.AddItem(key, &added);
00259 if (added) {
00260 const ssize_t vpos = m_values.AddItemAt(value, pos);
00261 if (vpos < 0) m_keys.RemoveItemsAt(pos);
00262 return vpos;
00263 } else {
00264 m_values.ReplaceItemAt(value, pos);
00265 return pos;
00266 }
00267 }
00268
00269 template<class KEY, class VALUE> inline
00270 void KeyedVector<KEY,VALUE>::RemoveItemsAt(size_t index, size_t count)
00271 {
00272 m_keys.RemoveItemsAt(index, count); m_values.RemoveItemsAt(index, count);
00273 }
00274
00275 template<class KEY, class VALUE> inline
00276 ssize_t KeyedVector<KEY,VALUE>::RemoveItemFor(const KEY& key)
00277 {
00278 const ssize_t pos = m_keys.RemoveItemFor(key);
00279 if (pos >= 0) m_values.RemoveItemsAt(pos);
00280 return pos;
00281 }
00282
00283 template<class KEY, class VALUE> inline
00284 void KeyedVector<KEY,VALUE>::MakeEmpty()
00285 {
00286 m_keys.MakeEmpty(); m_values.MakeEmpty();
00287 }
00288
00289 template<class KEY, class VALUE> inline
00290 void KeyedVector<KEY, VALUE>::Swap(KeyedVector<KEY, VALUE>& o)
00291 {
00292 Swap(m_keys, o.m_keys);
00293 Swap(m_values, o.m_values);
00294 Swap(m_undefined, o.m_undefined);
00295 }
00296
00297 template<class KEY, class VALUE> inline
00298 void Swap(KeyedVector<KEY, VALUE>& v1, KeyedVector<KEY, VALUE>& v2)
00299 {
00300 v1.Swap(v2);
00301 }
00302
00303 }
00304
00305 #endif