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
00037
00038
00039
00040
00048 #ifndef STLSOFT_INCL_STLSOFT_CONTAINERS_HPP_UNSORTED_MAP
00049 #define STLSOFT_INCL_STLSOFT_CONTAINERS_HPP_UNSORTED_MAP
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_UNSORTED_MAP_MAJOR 1
00053 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_UNSORTED_MAP_MINOR 3
00054 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_UNSORTED_MAP_REVISION 2
00055 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_UNSORTED_MAP_EDIT 20
00056 #endif
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00073 # include <stlsoft/stlsoft.h>
00074 #endif
00075 #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
00076 # include <stlsoft/collections/util/collections.hpp>
00077 #endif
00078 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_SELECTOR
00079 # include <stlsoft/memory/allocator_selector.hpp>
00080 #endif
00081 #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
00082 # include <stlsoft/util/std/iterator_helper.hpp>
00083 #endif
00084 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_SAP_CAST
00085 # include <stlsoft/conversion/sap_cast.hpp>
00086 #endif
00087 #ifndef STLSOFT_INCL_STLSOFT_ITERATORS_HPP_MEMBER_SELECTOR_ITERATOR
00088 # include <stlsoft/iterators/member_selector_iterator.hpp>
00089 #endif
00090
00091 #ifndef STLSOFT_INCL_ALGORITHM
00092 # define STLSOFT_INCL_ALGORITHM
00093 # include <algorithm>
00094 #endif
00095 #ifndef STLSOFT_INCL_FUNCTIONAL
00096 # define STLSOFT_INCL_FUNCTIONAL
00097 # include <functional>
00098 #endif
00099 #ifndef STLSOFT_INCL_UTILITY
00100 # define STLSOFT_INCL_UTILITY
00101 # include <utility>
00102 #endif
00103 #ifndef STLSOFT_INCL_VECTOR
00104 # define STLSOFT_INCL_VECTOR
00105 # include <vector>
00106 #endif
00107
00108 #ifdef STLSOFT_UNITTEST
00109 # include <string>
00110 #endif
00111
00112
00113
00114
00115
00116 #ifndef _STLSOFT_NO_NAMESPACE
00117 namespace stlsoft
00118 {
00119 #endif
00120
00121
00122
00123
00124
00139 template< ss_typename_param_k K
00140 , ss_typename_param_k T
00141 , ss_typename_param_k P = stlsoft_ns_qual_std(less)<K>
00142 , ss_typename_param_k A = ss_typename_type_def_k allocator_selector<T>::allocator_type
00143 >
00144 class unsorted_map
00145 : public stl_collection_tag
00146 {
00147 public:
00149 typedef K key_type;
00151 typedef T mapped_type;
00153 typedef P key_compare;
00155 typedef stlsoft_ns_qual_std(pair)<const K, T> value_type;
00157 typedef A allocator_type;
00159 typedef unsorted_map<K, T, P, A> class_type;
00160 private:
00161 typedef stlsoft_ns_qual_std(pair)<K, T> internal_value_type_;
00162 typedef stlsoft_ns_qual_std(vector)<internal_value_type_
00163 , ss_typename_type_k allocator_selector<internal_value_type_>::allocator_type
00164 > container_type_;
00165 public:
00166 typedef ss_typename_type_k container_type_::size_type size_type;
00167 typedef ss_typename_type_k container_type_::difference_type difference_type;
00168 typedef ss_bool_t bool_type;
00169 typedef value_type* pointer;
00170 typedef value_type const* const_pointer;
00171 typedef value_type& reference;
00172 typedef value_type const& const_reference;
00173 private:
00174 typedef pointer_iterator<value_type, pointer, reference> iterator_gen_;
00175 typedef pointer_iterator<value_type, const_pointer, const_reference> const_iterator_gen_;
00176 public:
00177 typedef ss_typename_type_k iterator_gen_::type iterator;
00178 typedef ss_typename_type_k const_iterator_gen_::type const_iterator;
00179 #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
00180 typedef stlsoft_ns_qual(const_reverse_iterator_base)< iterator
00181 , value_type
00182 , reference
00183 , pointer
00184 , difference_type
00185 > reverse_iterator;
00186 typedef stlsoft_ns_qual(const_reverse_iterator_base)< const_iterator
00187 , value_type
00188 , const_reference
00189 , const_pointer
00190 , difference_type
00191 > const_reverse_iterator;
00192 #endif
00193
00194 public:
00196 size_type size() const
00197 {
00198 return m_elements.size();
00199 }
00200
00202 bool_type empty() const
00203 {
00204 return m_elements.empty();
00205 }
00206
00207 #if 1
00208 reference operator [](size_type index)
00209 {
00210 return *sap_cast<value_type*>(&m_elements[index]);
00211 }
00212 const_reference operator [](size_type index) const
00213 {
00214 return *sap_cast<value_type*>(&m_elements[index]);
00215 }
00216 #else
00217 reference operator [](mapped_type const& key)
00218 {
00219 return m_elements[key]);
00220 }
00221 const_reference operator [](mapped_type const& key) const
00222 {
00223 return m_elements[key]);
00224 }
00225 #endif
00226
00227 reference front()
00228 {
00229 return *sap_cast<value_type*>(&m_elements.front());
00230 }
00231 reference back()
00232 {
00233 return *sap_cast<value_type*>(&m_elements.back());
00234 }
00235 const_reference front() const
00236 {
00237 return *sap_cast<value_type const*>(&m_elements.front());
00238 }
00239 const_reference back() const
00240 {
00241 return *sap_cast<value_type const*>(&m_elements.back());
00242 }
00243
00244 public:
00245 iterator begin()
00246 {
00247 return sap_cast<value_type*>(&*m_elements.begin());
00248 }
00249 iterator end()
00250 {
00251 return sap_cast<value_type*>(&*m_elements.end());
00252 }
00253 const_iterator begin() const
00254 {
00255 return sap_cast<value_type const*>(&*m_elements.begin());
00256 }
00257 const_iterator end() const
00258 {
00259 return sap_cast<value_type const*>(&*m_elements.end());
00260 }
00261 #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
00262 reverse_iterator rbegin()
00263 {
00264 return const_reverse_iterator(end());
00265 }
00266 reverse_iterator rend()
00267 {
00268 return const_reverse_iterator(begin());
00269 }
00270 const_reverse_iterator rbegin() const
00271 {
00272 return const_reverse_iterator(end());
00273 }
00274 const_reverse_iterator rend() const
00275 {
00276 return const_reverse_iterator(begin());
00277 }
00278 #endif
00279
00280 public:
00281 size_type erase(key_type const& key)
00282 {
00283 ss_typename_type_k container_type_::iterator it;
00284
00285 it = stlsoft_ns_qual_std(find)( member_selector(m_elements.begin(), &internal_value_type_::first)
00286 , member_selector(m_elements.end(), &internal_value_type_::first)
00287 , key).base();
00288
00289 if(m_elements.end() != it)
00290 {
00291 m_elements.erase(it);
00292
00293 return 1;
00294 }
00295
00296 return 0;
00297 }
00298
00299 public:
00300 void push_back(value_type const& value)
00301 {
00302 push_back(value.first, value.second);
00303 }
00304 void push_back(key_type const& key, mapped_type const& value)
00305 {
00306 ss_typename_type_k container_type_::iterator it;
00307
00308 it = stlsoft_ns_qual_std(find)( member_selector(m_elements.begin(), &internal_value_type_::first)
00309 , member_selector(m_elements.end(), &internal_value_type_::first)
00310 , key).base();
00311
00312 if(m_elements.end() == it)
00313 {
00314 m_elements.push_back(value_type(key, value));
00315 }
00316 else
00317 {
00318 (*it).second = value;
00319 }
00320 }
00321
00322 private:
00323 container_type_ m_elements;
00324 };
00325
00326
00327
00328
00329
00330 #ifdef STLSOFT_UNITTEST
00331 # include "./unittest/unsorted_map_unittest_.h"
00332 #endif
00333
00334
00335
00336 #ifndef _STLSOFT_NO_NAMESPACE
00337 }
00338 #endif
00339
00340
00341
00342 #endif
00343
00344