1 /* ------------------------------------------------------------------ 2 * Copyright (C) 1998-2009 PacketVideo 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 * express or implied. 14 * See the License for the specific language governing permissions 15 * and limitations under the License. 16 * ------------------------------------------------------------------- 17 */ 18 // -*- c++ -*- 19 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 20 21 // O S C L _ M A P 22 23 // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 24 25 /*! \addtogroup osclbase OSCL Base 26 * 27 * @{ 28 */ 29 30 31 /*! \file oscl_map.h 32 \brief The file oscl_map.h defines the template class Oscl_Map which has a very similar API as the STL Map class (it basically provides a subset of the STL functionality). Memory allocation is abstracted through the use of an allocator template parameter. 33 */ 34 35 #ifndef OSCL_MAP_H_INCLUDED 36 #define OSCL_MAP_H_INCLUDED 37 38 #ifndef OSCL_BASE_H_INCLUDED 39 #include "oscl_base.h" 40 #endif 41 42 #ifndef OSCL_TREE_H_INCLUDED 43 #include "oscl_tree.h" 44 #endif 45 46 #define OSCL_DISABLE_WARNING_TRUNCATE_DEBUG_MESSAGE 47 #include "osclconfig_compiler_warnings.h" 48 49 template <class T> 50 struct Oscl_Less 51 { operatorOscl_Less52 bool operator()(const T& x, const T& y) const 53 { 54 return x < y ? true : false ; 55 } 56 }; 57 58 template <class V, class U> 59 struct Oscl_Select1st 60 { operatorOscl_Select1st61 const U& operator()(const V& x) const 62 { 63 return x.first; 64 } 65 }; 66 67 /** 68 * Oscl_Map Class. A subset of STL::Map methods. 69 * Oscl_Map is a sorted associative container that associates objects of type Key with objects of type T. 70 * It is also a unique associative container, meaning that no two elements have the same key. Oscl_Map 71 * uses the key to speed lookup, insertion, and deletion of elements. It is often superior to all other 72 * containers when you need to lookup an element by key value. Due to the underlying tree structure, 73 * inserts and erases can be performed in logarithmic time, where a vector would take linear time in 74 * some cases. 75 */ 76 77 template < class Key, class T, class Alloc, class Compare = Oscl_Less<Key> > 78 class Oscl_Map 79 { 80 81 public: 82 typedef Key key_type; 83 typedef Compare key_compare; 84 typedef Oscl_Pair<const Key, T> value_type; 85 typedef Oscl_Map<Key, T, Alloc, Compare> self; 86 87 private: 88 typedef Oscl_Rb_Tree < key_type, value_type, 89 Oscl_Select1st<value_type, key_type>, 90 key_compare, Alloc > rep_type; 91 rep_type t; // red-black tree representing map 92 93 public: 94 typedef typename rep_type::pointer pointer; 95 typedef typename rep_type::reference reference; 96 typedef typename rep_type::const_reference const_reference; 97 typedef typename rep_type::iterator iterator; 98 typedef typename rep_type::const_iterator const_iterator; 99 typedef typename rep_type::size_type size_type; 100 101 class value_compare 102 { 103 friend class Oscl_Map<Key, T, Alloc, Compare>; 104 protected : 105 Compare comp; value_compare(Compare c)106 value_compare(Compare c) : comp(c) {} 107 public: operator()108 bool operator()(const value_type& x, const value_type& y) const 109 { 110 return comp(x.first, y.first); 111 } 112 }; 113 114 public: 115 116 /** 117 * Creates an empty map using comp as the key compare object 118 */ t(comp)119 Oscl_Map(const Compare& comp = Compare()) : t(comp) {} 120 // Oscl_Map(const value_type* first, const value_type* last, 121 // const Compare& comp = Compare()) : t(first, last, comp, false) {} 122 123 /** 124 * Oscl_Map copy constructor 125 */ Oscl_Map(const self & x)126 Oscl_Map(const self& x) : t(x.t) {} 127 /** 128 * Oscl_Map assignment operator 129 */ 130 self& operator=(const self& x) 131 { 132 t = x.t; 133 return *this; 134 } 135 /** 136 * Returns the key compare object used by the map 137 */ key_comp()138 key_compare key_comp() const 139 { 140 return t.key_comp(); 141 } 142 /** 143 * Returns the value compare object used by the map 144 */ value_comp()145 value_compare value_comp() const 146 { 147 return value_compare(t.key_comp()); 148 } 149 /** 150 * Returns an iterator pointing to the beginning of the map 151 */ begin()152 iterator begin() 153 { 154 return t.begin(); 155 } 156 /** 157 * Returns a const iterator pointing to the beginning of the map 158 */ begin()159 const_iterator begin() const 160 { 161 return t.begin(); 162 } 163 /** 164 * Returns an iterator pointing to the end of the map. 165 */ end()166 iterator end() 167 { 168 return t.end(); 169 } 170 /** 171 * Returns a const iterator pointing to the end of the map. 172 */ end()173 const_iterator end() const 174 { 175 return t.end(); 176 } 177 /** 178 * Returns true if map size is 0 179 */ empty()180 bool empty() const 181 { 182 return t.empty(); 183 } 184 /** 185 * Returns the size of the map 186 */ size()187 size_type size() const 188 { 189 return t.size(); 190 } 191 /** 192 * Returns the maximum possible size of the map 193 */ max_size()194 size_type max_size() const 195 { 196 return t.max_size(); 197 } 198 /** 199 * Returns a reference to the object that is associated with a particular key. 200 * If the map does not already contain such an object, operator[] inserts the default object value_type(). 201 */ 202 T& operator[](const key_type& k) 203 { 204 return (*((insert(value_type(k, T()))).first)).second; 205 } 206 // void swap(map<Key, T, Compare>& x) { t.swap(x.t); } 207 208 209 typedef Oscl_Pair<iterator, bool> pair_iterator_bool; 210 /** 211 * Inserts x into the map 212 */ insert(const value_type & x)213 pair_iterator_bool insert(const value_type& x) 214 { 215 return t.insert_unique(x); 216 } 217 /** 218 * Inserts x into the map using position as a hint as to where it should be inserted 219 */ insert(iterator position,const value_type & x)220 iterator insert(iterator position, const value_type& x) 221 { 222 return t.insert_unique(position, x); 223 } 224 /** 225 * Inserts the range [first,last) into the map 226 */ insert(const value_type * first,const value_type * last)227 void insert(const value_type* first, const value_type* last) 228 { 229 t.insert_unique(first, last); 230 } 231 /** 232 * Erases the element pointed to by position 233 */ erase(iterator position)234 void erase(iterator position) 235 { 236 t.erase(position); 237 } 238 /** 239 * Erases the element with key x 240 */ erase(const key_type & x)241 size_type erase(const key_type& x) 242 { 243 return t.erase(x); 244 } 245 /** 246 * Erases all elements in the range [first,last) 247 */ erase(iterator first,iterator last)248 void erase(iterator first, iterator last) 249 { 250 t.erase(first, last); 251 } 252 /** 253 * Erases all elements 254 */ clear()255 void clear() 256 { 257 t.clear(); 258 } 259 /** 260 * Finds an element whose key is x 261 */ find(const key_type & x)262 iterator find(const key_type& x) 263 { 264 return t.find(x); 265 } 266 /** 267 * Finds an element whose key is x 268 */ find(const key_type & x)269 const_iterator find(const key_type& x) const 270 { 271 return t.find(x); 272 } 273 /** 274 * Returns the number of elements with key x. For map this will either be 0 or 1. 275 */ count(const key_type & x)276 size_type count(const key_type& x) const 277 { 278 return t.count(x); 279 } 280 /** 281 * Finds the first element whose key is not less than x 282 */ lower_bound(const key_type & x)283 iterator lower_bound(const key_type& x) 284 { 285 return t.lower_bound(x); 286 } 287 /** 288 * Finds the first element whose key is not less than x 289 */ lower_bound(const key_type & x)290 const_iterator lower_bound(const key_type& x) const 291 { 292 return t.lower_bound(x); 293 } 294 /** 295 * Finds the first element whose key is not greater than x 296 */ upper_bound(const key_type & x)297 iterator upper_bound(const key_type& x) 298 { 299 return t.upper_bound(x); 300 } 301 /** 302 * Finds the first element whose key is not greater than x 303 */ upper_bound(const key_type & x)304 const_iterator upper_bound(const key_type& x) const 305 { 306 return t.upper_bound(x); 307 } 308 typedef Oscl_Pair<iterator, iterator> pair_iterator_iterator; 309 /** 310 * Finds a range containing all elements whose key is x 311 */ equal_range(const key_type & x)312 pair_iterator_iterator equal_range(const key_type& x) 313 { 314 return t.equal_range(x); 315 } 316 typedef Oscl_Pair<const_iterator, const_iterator> pair_citerator_citerator; 317 /** 318 * Finds a range containing all elements whose key is x 319 */ equal_range(const key_type & x)320 pair_citerator_citerator equal_range(const key_type& x) const 321 { 322 return t.equal_range(x); 323 } 324 325 private: 326 327 }; 328 329 //template <class Key, class T, class Compare> 330 //inline bool operator==(const map<Key, T, Compare>& x, 331 // const map<Key, T, Compare>& y) { 332 // return x.size() == y.size() && equal(x.begin(), x.end(), y.begin()); 333 //} 334 335 //template <class Key, class T, class Compare> 336 //inline bool operator<(const map<Key, T, Compare>& x, 337 // const map<Key, T, Compare>& y) { 338 // return lexicographical_compare(x.begin(), x.end(), y.begin(), y.end()); 339 //} 340 341 342 /*! @} */ 343 344 345 #endif 346