Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Globals

KeyedVector.h

Go to the documentation of this file.
00001 /********************************************************************
00002 
00003 Copyright 2006, ACCESS Systems Americas, Inc. All rights reserved.
00004 
00005 The contents of this file are subject to the Mozilla Public License Version
00006 1.1 (the "License"); you may not use this file except in compliance with
00007 the License. You may obtain a copy of the License at
00008 http://www.mozilla.org/MPL/
00009 
00010 Software distributed under the License is distributed on an "AS IS" basis,
00011 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012 for the specific language governing rights and limitations under the
00013 License.
00014 
00015 The Original Code is the entire contents of this file.
00016 
00017 The Initial Developer of the Original Code is ACCESS Systems Americas, Inc.
00018 
00019 Portions created by ACCESS Systems Americas, Inc. are Copyright © 2006. All
00020 Rights Reserved.
00021 
00022 Contributor(s): none.
00023 
00024 ********************************************************************/
00025 
00026 /***************************************************************************
00027 //
00028 //      File:                   support/KeyedVector.h
00029 //
00030 //      Description:    A templatized key/value mapping.
00031 //
00032 //      Copyright 2001, Be Incorporated, All Rights Reserved.
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 /*----- KeyedVector class -------------------------------*/
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         /* Size stats */
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         /* Value by Key */
00069 
00070                         const VALUE&    ValueFor(const KEY& key, bool* found = NULL) const;
00071                         VALUE&                  EditValueFor(const KEY& key, bool* found = NULL);
00072 
00073         /* Value/Key by index */
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         /* List manipulation */
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 // Type optimizations.
00106 template<class KEY, class VALUE>
00107 void Swap(KeyedVector<KEY, VALUE>& v1, KeyedVector<KEY, VALUE>& v2);
00108 
00109 /*-------------------------------------------------------------*/
00110 /*---- No user serviceable parts after this -------------------*/
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 }       // namespace alp
00304 
00305 #endif

Generated on Sat Dec 16 20:29:47 2006 for hiker-0.9 by  doxygen 1.4.4