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_FREQUENCY_MAP
00049 #define STLSOFT_INCL_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_MAJOR 2
00053 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_MINOR 1
00054 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_REVISION 1
00055 # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_EDIT 23
00056 #endif
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00067 # include <stlsoft/stlsoft.h>
00068 #endif
00069 #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
00070 # include <stlsoft/collections/util/collections.hpp>
00071 #endif
00072 #ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE
00073 # include <stlsoft/meta/is_integral_type.hpp>
00074 #endif
00075 #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP
00076 # include <stlsoft/util/std_swap.hpp>
00077 #endif
00078
00079 #ifndef STLSOFT_INCL_MAP
00080 # define STLSOFT_INCL_MAP
00081 # include <map>
00082 #endif
00083
00084
00085
00086
00087
00088 #ifndef _STLSOFT_NO_NAMESPACE
00089 namespace stlsoft
00090 {
00091 #endif
00092
00093
00094
00095
00096
00105 template< ss_typename_param_k T
00106 #if defined(STLSOFT_COMPILER_IS_MSVC) && \
00107 _MSC_VER < 1300
00108 , ss_typename_param_k N = unsigned int
00109 #else
00110 , ss_typename_param_k N = uint32_t
00111 #endif
00112 >
00113 class frequency_map
00114 : public stl_collection_tag
00115 {
00116 private:
00117 typedef stlsoft_ns_qual_std(map)<T, N> map_type_;
00118 public:
00120 typedef frequency_map<T, N> class_type;
00122 typedef ss_typename_param_k map_type_::value_type value_type;
00124 typedef ss_typename_param_k map_type_::const_iterator const_iterator;
00125
00127 typedef ss_typename_param_k map_type_::const_reference const_reference;
00128
00130 typedef ss_typename_param_k map_type_::key_type key_type;
00131
00133 typedef N count_type;
00135 typedef ss_size_t size_type;
00137 typedef ss_ptrdiff_t difference_type;
00139 typedef ss_bool_t bool_type;
00140
00141 public:
00143 frequency_map()
00144 {
00145 STLSOFT_STATIC_ASSERT(0 != stlsoft::is_integral_type<N>::value);
00146 }
00147
00148 public:
00157 count_type push(key_type const& key)
00158 {
00159 return ++m_map[key];
00160
00161 #if 0
00162
00163
00164 ss_typename_param_k map_type_::iterator it = m_map.find(key);
00165 if(m_map.end() == it)
00166 {
00167 value_type value(key, 1);
00168
00169 m_map.insert(value);
00170
00171 return 1;
00172 }
00173 else
00174 {
00175 value_type& value = *it;
00176
00177 return ++(*it).second;
00178 }
00179 #endif
00180 }
00181
00183 void clear()
00184 {
00185 m_map.clear();
00186 }
00187
00189 class_type& merge(class_type const& rhs)
00190 {
00191 class_type t(*this);
00192
00193 { for(const_iterator i = rhs.begin(); i != rhs.end(); ++i)
00194 {
00195 t.m_map[(*i).first] += (*i).second;
00196 }}
00197
00198 t.swap(*this);
00199
00200 return *this;
00201 }
00202
00204 void swap(class_type& rhs) stlsoft_throw_0()
00205 {
00206 std_swap(m_map, rhs.m_map);
00207 }
00208
00209 public:
00212 const_iterator find(key_type const& key) const
00213 {
00214 return m_map.find(key);
00215 }
00216
00217 public:
00220 count_type operator [](key_type const& key) const
00221 {
00222 return count(key);
00223 }
00224
00227 count_type count(key_type const& key) const
00228 {
00229 const_iterator it = m_map.find(key);
00230
00231 return (m_map.end() != it) ? (*it).second : 0;
00232 }
00233
00234 public:
00236 bool_type empty() const
00237 {
00238 return m_map.empty();
00239 }
00240
00245 size_type size() const
00246 {
00247 return m_map.size();
00248 }
00249
00250 public:
00252 const_iterator begin() const
00253 {
00254 return m_map.begin();
00255 }
00257 const_iterator end() const
00258 {
00259 return m_map.end();
00260 }
00261
00262 private:
00263 map_type_ m_map;
00264 };
00265
00266
00267
00268
00269
00270 #if !defined(STLSOFT_COMPILER_IS_WATCOM)
00271
00272 template< ss_typename_param_k T
00273 , ss_typename_param_k N
00274 >
00275 inline void swap(
00276 frequency_map<T, N>& lhs
00277 , frequency_map<T, N>& rhs
00278 )
00279 {
00280 lhs.swap(rhs);
00281 }
00282 #endif
00283
00284
00285
00286
00287
00288 #ifdef STLSOFT_UNITTEST
00289 # include "./unittest/frequency_map_unittest_.h"
00290 #endif
00291
00292
00293
00294 #ifndef _STLSOFT_NO_NAMESPACE
00295 }
00296 #endif
00297
00298
00299
00300 #endif
00301
00302