KeyedVector.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_ASSOCIATIVEVECTOR_H
00014 #define _SUPPORT_ASSOCIATIVEVECTOR_H
00015 
00021 #include <support/Vector.h>
00022 #include <support/SortedVector.h>
00023 
00024 #if _SUPPORTS_NAMESPACE
00025 namespace palmos {
00026 namespace support {
00027 #endif
00028 
00033 /*--------------------------------------------------------*/
00034 /*----- SAbstractKeyedVector class -----------------------*/
00035 
00037 class SAbstractKeyedVector
00038 {
00039 public:
00040     inline                  SAbstractKeyedVector();
00041     inline virtual          ~SAbstractKeyedVector();
00042 
00043             const void*     AbstractValueAt(size_t index) const;
00044             const void*     AbstractKeyAt(size_t index) const;
00045             const void*     KeyedFor(const void* key, bool* found) const;
00046             const void*     FloorKeyedFor(const void* key, bool* found) const;
00047             const void*     CeilKeyedFor(const void* key, bool* found) const;
00048             void*           EditKeyedFor(const void* key, bool* found);
00049             ssize_t         AddKeyed(const void* key, const void* value);
00050             void            RemoveItemsAt(size_t index, size_t count);
00051             ssize_t         RemoveKeyed(const void* key);
00052             size_t          CountItems() const;
00053 
00054 private:
00055             int32_t         _reserved_data;     // align to 8 bytes
00056 };
00057 
00058 /*--------------------------------------------------------*/
00059 /*----- SKeyedVector class -------------------------------*/
00060 
00062 
00079 template<class KEY, class VALUE>
00080 class SKeyedVector : private SAbstractKeyedVector
00081 {
00082 public:
00083             typedef KEY key_type;
00084             typedef VALUE   value_type;
00085 
00086 public:
00087                             SKeyedVector();
00088                             SKeyedVector(const VALUE& undef);
00089                             SKeyedVector(   const SSortedVector<KEY>& keys,
00090                                             const SVector<VALUE>& values,
00091                                             const VALUE& undef = VALUE());
00092     virtual                 ~SKeyedVector();
00093     
00095             void            SetTo(const SKeyedVector<KEY,VALUE>& o);
00096     
00097     /* Size stats */
00098     
00100             void            SetCapacity(size_t total_space);
00102             void            SetExtraCapacity(size_t extra_space);
00104             size_t          Capacity() const;
00105             
00107             size_t          CountItems() const;
00108         
00109     /* Value by Key */
00110 
00112             const VALUE&    ValueFor(const KEY& key, bool* found = NULL) const;
00114     inline  const VALUE&    operator[](const KEY& key) const { return ValueFor(key); }
00115 
00117             const VALUE&    FloorValueFor(const KEY& key, bool* found = NULL) const;
00119             const VALUE&    CeilValueFor(const KEY& key, bool* found = NULL) const;
00120 
00122             VALUE&          EditValueFor(const KEY& key, bool* found = NULL);
00123             
00124     /* Value/Key by index */
00125 
00127             const KEY&      KeyAt(size_t i) const;
00129             const VALUE&    ValueAt(size_t i) const;
00131             VALUE&          EditValueAt(size_t i);
00132             
00134             const SSortedVector<KEY>&   KeyVector() const;
00136             const SVector<VALUE>&       ValueVector() const;
00138             SVector<VALUE>&             ValueVector();
00139             
00140     /* List manipulation */
00141 
00143             ssize_t         IndexOf(const KEY& key) const;
00145             ssize_t         FloorIndexOf(const KEY& key) const;
00147             ssize_t         CeilIndexOf(const KEY& key) const;
00149             bool            GetIndexOf(const KEY& key, size_t* index) const;
00150             
00152             ssize_t         AddItem(const KEY& key, const VALUE& value);
00153             
00155             void            RemoveItemsAt(size_t index, size_t count = 1);
00157             ssize_t         RemoveItemFor(const KEY& key);
00158             
00160             void            MakeEmpty();
00161             
00163             void            Swap(SKeyedVector<KEY,VALUE>& o);
00164             
00165 private:
00166                             SKeyedVector(const SKeyedVector<KEY,VALUE>& o);
00167             SKeyedVector<KEY,VALUE>&    operator=(const SKeyedVector<KEY,VALUE>& o);
00168 
00169             SSortedVector<KEY>  m_keys;
00170             SVector<VALUE>      m_values;
00171             VALUE               m_undefined;
00172 };
00173 
00174 // Type optimizations.
00175 template<class KEY, class VALUE>
00176 void BSwap(SKeyedVector<KEY, VALUE>& v1, SKeyedVector<KEY, VALUE>& v2);
00177 
00180 /*-------------------------------------------------------------*/
00181 /*---- No user serviceable parts after this -------------------*/
00182 
00183 SAbstractKeyedVector::SAbstractKeyedVector()
00184     : _reserved_data(0)
00185 {
00186 }
00187 
00188 SAbstractKeyedVector::~SAbstractKeyedVector()
00189 {
00190 }
00191 
00192 /*-------------------------------------------------------------*/
00193 
00194 template<class KEY, class VALUE> inline
00195 SKeyedVector<KEY,VALUE>::SKeyedVector()
00196     :   m_undefined(VALUE())
00197 {
00198 }
00199 
00200 template<class KEY, class VALUE> inline
00201 SKeyedVector<KEY,VALUE>::SKeyedVector(const VALUE& undef)
00202     :   m_undefined(undef)
00203 {
00204 }
00205 
00206 template<class KEY, class VALUE> inline
00207 SKeyedVector<KEY,VALUE>::SKeyedVector(const SSortedVector<KEY>& keys,
00208             const SVector<VALUE>& values, const VALUE& undef)
00209     :   m_keys(keys), m_values(values), m_undefined(undef)
00210 {
00211 }
00212 
00213 template<class KEY, class VALUE> inline
00214 SKeyedVector<KEY,VALUE>::~SKeyedVector()
00215 {
00216 }
00217 
00222 template<class KEY, class VALUE> inline
00223 void SKeyedVector<KEY,VALUE>::SetTo(const SKeyedVector<KEY,VALUE>& o)
00224 {
00225     m_keys = o.m_keys; m_values = o.m_values; m_undefined = o.m_undefined;
00226 }
00227 
00234 template<class KEY, class VALUE> inline
00235 void SKeyedVector<KEY,VALUE>::SetCapacity(size_t total_space)
00236 {
00237     m_keys.SetCapacity(total_space); m_values.SetCapacity(total_space);
00238 }
00239 
00242 template<class KEY, class VALUE> inline
00243 void SKeyedVector<KEY,VALUE>::SetExtraCapacity(size_t extra_space)
00244 {
00245     m_keys.SetExtraCapacity(extra_space); m_values.SetExtraCapacity(extra_space);
00246 }
00247 
00251 template<class KEY, class VALUE> inline
00252 size_t SKeyedVector<KEY,VALUE>::Capacity() const
00253 {
00254     return m_keys.Capacity();
00255 }
00256 
00257 template<class KEY, class VALUE> inline
00258 size_t SKeyedVector<KEY,VALUE>::CountItems() const
00259 {
00260     return m_keys.CountItems();
00261 }
00262 
00266 template<class KEY, class VALUE> inline
00267 const VALUE& SKeyedVector<KEY,VALUE>::ValueFor(const KEY& key, bool* found) const
00268 {
00269     return *(const VALUE*)KeyedFor(&key, found);
00270 }
00271 
00288 template<class KEY, class VALUE> inline
00289 const VALUE& SKeyedVector<KEY,VALUE>::FloorValueFor(const KEY& key, bool* found) const
00290 {
00291     return *(const VALUE*)FloorKeyedFor(&key, found);
00292 }
00293 
00294 template<class KEY, class VALUE> inline
00295 const VALUE& SKeyedVector<KEY,VALUE>::CeilValueFor(const KEY& key, bool* found) const
00296 {
00297     return *(const VALUE*)CeilKeyedFor(&key, found);
00298 }
00299 
00302 template<class KEY, class VALUE> inline
00303 VALUE& SKeyedVector<KEY,VALUE>::EditValueFor(const KEY& key, bool* found)
00304 {
00305     return *(VALUE*)EditKeyedFor(&key, found);
00306 }
00307 
00308 template<class KEY, class VALUE> inline
00309 const KEY& SKeyedVector<KEY,VALUE>::KeyAt(size_t i) const
00310 {
00311     return m_keys.ItemAt(i);
00312 }
00313 
00314 template<class KEY, class VALUE> inline
00315 const VALUE& SKeyedVector<KEY,VALUE>::ValueAt(size_t i) const
00316 {
00317     return m_values.ItemAt(i);
00318 }
00319 
00320 template<class KEY, class VALUE> inline
00321 VALUE& SKeyedVector<KEY,VALUE>::EditValueAt(size_t i)
00322 {
00323     return m_values.EditItemAt(i);
00324 }
00325 
00326 template<class KEY, class VALUE> inline
00327 const SSortedVector<KEY>& SKeyedVector<KEY,VALUE>::KeyVector() const
00328 {
00329     return m_keys;
00330 }
00331 
00332 template<class KEY, class VALUE> inline
00333 const SVector<VALUE>& SKeyedVector<KEY,VALUE>::ValueVector() const
00334 {
00335     return m_values;
00336 }
00337 
00338 template<class KEY, class VALUE> inline
00339 SVector<VALUE>& SKeyedVector<KEY,VALUE>::ValueVector()
00340 {
00341     return m_values;
00342 }
00343 
00344 template<class KEY, class VALUE> inline
00345 ssize_t SKeyedVector<KEY,VALUE>::IndexOf(const KEY& key) const
00346 {
00347     return m_keys.IndexOf(key);
00348 }
00349 
00350 template<class KEY, class VALUE> inline
00351 ssize_t SKeyedVector<KEY,VALUE>::FloorIndexOf(const KEY& key) const
00352 {
00353     return m_keys.FloorIndexOf(key);
00354 }
00355 
00356 template<class KEY, class VALUE> inline
00357 ssize_t SKeyedVector<KEY,VALUE>::CeilIndexOf(const KEY& key) const
00358 {
00359     return m_keys.CeilIndexOf(key);
00360 }
00361 
00362 template<class KEY, class VALUE> inline
00363 bool SKeyedVector<KEY,VALUE>::GetIndexOf(const KEY& key, size_t* index) const
00364 {
00365     return m_keys.GetIndexOf(key, index);
00366 }
00367 
00371 template<class KEY, class VALUE> inline
00372 ssize_t SKeyedVector<KEY,VALUE>::AddItem(const KEY& key, const VALUE& value)
00373 {
00374     return AddKeyed(&key, &value);
00375 }
00376 
00377 template<class KEY, class VALUE> inline
00378 void SKeyedVector<KEY,VALUE>::RemoveItemsAt(size_t index, size_t count)
00379 {
00380     SAbstractKeyedVector::RemoveItemsAt(index, count);
00381 }
00382 
00385 template<class KEY, class VALUE> inline
00386 ssize_t SKeyedVector<KEY,VALUE>::RemoveItemFor(const KEY& key)
00387 {
00388     return RemoveKeyed(&key);
00389 }
00390 
00391 template<class KEY, class VALUE> inline
00392 void SKeyedVector<KEY,VALUE>::MakeEmpty()
00393 {
00394     m_keys.MakeEmpty(); m_values.MakeEmpty();
00395 }
00396 
00397 template<class KEY, class VALUE> inline
00398 void SKeyedVector<KEY, VALUE>::Swap(SKeyedVector<KEY, VALUE>& o)
00399 {
00400     BSwap(m_keys, o.m_keys);
00401     BSwap(m_values, o.m_values);
00402     BSwap(m_undefined, o.m_undefined);
00403 }
00404 
00405 template<class KEY, class VALUE> inline
00406 void BSwap(SKeyedVector<KEY, VALUE>& v1, SKeyedVector<KEY, VALUE>& v2)
00407 {
00408     v1.Swap(v2);
00409 }
00410 
00411 #if _SUPPORTS_NAMESPACE
00412 } } // namespace palmos::support
00413 #endif
00414 
00415 #endif