You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

445 lines
13 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/containers/frequency_map.hpp
  3. *
  4. * Purpose: A container that measures the frequency of the unique elements it contains.
  5. *
  6. * Created: 1st October 2005
  7. * Updated: 27th June 2011
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2011, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/containers/frequency_map.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::frequency_map container
  42. * class template
  43. * (\ref group__library__containers "Containers" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP
  46. #define STLSOFT_INCL_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_MINOR 6
  50. # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP_EDIT 29
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Auto-generation and compatibility
  55. */
  56. /* /////////////////////////////////////////////////////////////////////////
  57. * Includes
  58. */
  59. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  60. # include <stlsoft/stlsoft.h>
  61. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  62. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  63. # include <stlsoft/collections/util/collections.hpp>
  64. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  65. #ifndef STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE
  66. # include <stlsoft/meta/is_integral_type.hpp>
  67. #endif /* !STLSOFT_INCL_STLSOFT_META_HPP_IS_INTEGRAL_TYPE */
  68. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP
  69. # include <stlsoft/util/std_swap.hpp>
  70. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP */
  71. #ifndef STLSOFT_INCL_MAP
  72. # define STLSOFT_INCL_MAP
  73. # include <map>
  74. #endif /* !STLSOFT_INCL_MAP */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * Namespace
  77. */
  78. #ifndef _STLSOFT_NO_NAMESPACE
  79. namespace stlsoft
  80. {
  81. #endif /* _STLSOFT_NO_NAMESPACE */
  82. /* /////////////////////////////////////////////////////////////////////////
  83. * Classes
  84. */
  85. /** A container that measures the frequency of the unique elements it
  86. * contains.
  87. *
  88. * \ingroup group__library__containers
  89. *
  90. * \param T The value type of the container
  91. * \param N The count integer type. Defaults to uint32_t
  92. */
  93. template<
  94. ss_typename_param_k T
  95. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  96. _MSC_VER < 1300
  97. , ss_typename_param_k N = unsigned int
  98. #else
  99. , ss_typename_param_k N = uint32_t
  100. #endif
  101. , ss_typename_param_k P = stlsoft_ns_qual_std(less)<T>
  102. >
  103. class frequency_map
  104. : public stl_collection_tag
  105. {
  106. private: // Member Types
  107. typedef stlsoft_ns_qual_std(map)<T, N, P> map_type_;
  108. public:
  109. /// This type
  110. typedef frequency_map<T, N, P> class_type;
  111. /// The value type
  112. typedef ss_typename_param_k map_type_::value_type value_type;
  113. /// The non-mutating (const) iterator type
  114. typedef ss_typename_param_k map_type_::const_iterator const_iterator;
  115. // typedef ss_typename_param_k map_type_::const_pointer const_pointer;
  116. /// The non-mutating (const) reverse iterator type
  117. typedef ss_typename_param_k map_type_::const_reverse_iterator const_reverse_iterator;
  118. /// The non-mutating (const) reference type
  119. typedef ss_typename_param_k map_type_::const_reference const_reference;
  120. /// The key type
  121. typedef ss_typename_param_k map_type_::key_type key_type;
  122. // typedef ss_typename_param_k map_type_::mapped_type mapped_type
  123. /// The count type
  124. typedef N count_type;
  125. /// The size type
  126. typedef ss_size_t size_type;
  127. /// The difference type
  128. typedef ss_ptrdiff_t difference_type;
  129. /// The boolean type
  130. typedef ss_bool_t bool_type;
  131. public: // Construction
  132. /// Creates an instance of the map
  133. frequency_map()
  134. : m_map()
  135. , m_total(0)
  136. {
  137. STLSOFT_STATIC_ASSERT(0 != stlsoft::is_integral_type<N>::value);
  138. STLSOFT_ASSERT(is_valid());
  139. }
  140. public: // Modifiers
  141. /// Pushes an entry onto the map
  142. ///
  143. /// If the entry already exists in the map, its count will be increased
  144. /// by 1. If it does not previously exist, it will be added with an
  145. /// initial count of one
  146. ///
  147. /// \note <b>Thread-safety</b>: it is strongly exception-safe - if an
  148. /// entry cannot be added,
  149. count_type push(key_type const& key)
  150. {
  151. STLSOFT_ASSERT(is_valid());
  152. count_type r = ++m_map[key];
  153. #if 0
  154. // NOTE: Because the count type N must be an integer, the code above
  155. // is equivalent to the following "full" exception-safe implementation.
  156. ss_typename_param_k map_type_::iterator it = m_map.find(key);
  157. if(m_map.end() == it)
  158. {
  159. value_type value(key, 1);
  160. m_map.insert(value);
  161. return 1;
  162. }
  163. else
  164. {
  165. value_type& value = *it;
  166. return ++(*it).second;
  167. }
  168. #endif /* 0 */
  169. ++m_total;
  170. STLSOFT_ASSERT(is_valid());
  171. return r;
  172. }
  173. void push_n(
  174. key_type const& key
  175. , count_type n
  176. )
  177. {
  178. // TODO: update this to a single action
  179. { for(count_type i = 0; i != n; ++i)
  180. {
  181. push(key);
  182. }}
  183. }
  184. /// Removes all entries from the map
  185. void clear()
  186. {
  187. STLSOFT_ASSERT(is_valid());
  188. m_map.clear();
  189. m_total = 0u;
  190. STLSOFT_ASSERT(is_valid());
  191. }
  192. /// Merges in all entries from the given map
  193. class_type& merge(class_type const& rhs)
  194. {
  195. STLSOFT_ASSERT(is_valid());
  196. class_type t(*this);
  197. { for(const_iterator i = rhs.begin(); i != rhs.end(); ++i)
  198. {
  199. t.m_map[(*i).first] += (*i).second;
  200. }}
  201. t.m_total += rhs.m_total;
  202. t.swap(*this);
  203. STLSOFT_ASSERT(is_valid());
  204. return *this;
  205. }
  206. class_type& operator +=(class_type const& rhs)
  207. {
  208. return merge(rhs);
  209. }
  210. /// Swaps the state of the given instance with this instance
  211. void swap(class_type& rhs) stlsoft_throw_0()
  212. {
  213. STLSOFT_ASSERT(is_valid());
  214. std_swap(m_map, rhs.m_map);
  215. std_swap(m_total, rhs.m_total);
  216. STLSOFT_ASSERT(is_valid());
  217. }
  218. public: // Search
  219. /// Returns an iterator for the entry representing the given key, or
  220. /// <code>end()</code> if no such entry exists.
  221. const_iterator find(key_type const& key) const
  222. {
  223. STLSOFT_ASSERT(is_valid());
  224. return m_map.find(key);
  225. }
  226. public: // Element Access
  227. /// Returns the count associated with the entry representing the given
  228. /// key, or 0 if no such entry exists.
  229. count_type operator [](key_type const& key) const
  230. {
  231. STLSOFT_ASSERT(is_valid());
  232. return count(key);
  233. }
  234. /// Returns the count associated with the entry representing the given
  235. /// key, or 0 if no such entry exists.
  236. count_type count(key_type const& key) const
  237. {
  238. STLSOFT_ASSERT(is_valid());
  239. const_iterator it = m_map.find(key);
  240. return (m_map.end() != it) ? (*it).second : 0;
  241. }
  242. public: // Size
  243. /// Indicates whether the map is empty
  244. bool_type empty() const
  245. {
  246. STLSOFT_ASSERT(is_valid());
  247. return m_map.empty();
  248. }
  249. /// The number of unique entries in the map
  250. ///
  251. /// \remarks This may not be the same as the number of calls to
  252. /// <code>push()</code>
  253. size_type size() const
  254. {
  255. STLSOFT_ASSERT(is_valid());
  256. return m_map.size();
  257. }
  258. /// The number of non-unique entries in the map
  259. ///
  260. /// \remarks This may not be the same as the number of calls to
  261. /// <code>push()</code>
  262. count_type total() const
  263. {
  264. STLSOFT_ASSERT(is_valid());
  265. return m_total;
  266. }
  267. public: // Iteration
  268. /// A non-mutating (const) iterator representing the start of the sequence
  269. const_iterator begin() const
  270. {
  271. STLSOFT_ASSERT(is_valid());
  272. return m_map.begin();
  273. }
  274. /// A non-mutating (const) iterator representing the end-point of the sequence
  275. const_iterator end() const
  276. {
  277. STLSOFT_ASSERT(is_valid());
  278. return m_map.end();
  279. }
  280. /// A non-mutating (const) iterator representing the start of the reverse sequence
  281. const_reverse_iterator rbegin() const
  282. {
  283. STLSOFT_ASSERT(is_valid());
  284. return m_map.rbegin();
  285. }
  286. /// A non-mutating (const) iterator representing the end of the reverse sequence
  287. const_reverse_iterator rend() const
  288. {
  289. STLSOFT_ASSERT(is_valid());
  290. return m_map.rend();
  291. }
  292. private:
  293. bool is_valid() const
  294. {
  295. return m_map.empty() == (0u == m_total);
  296. }
  297. private: // Member Variables
  298. map_type_ m_map;
  299. count_type m_total;
  300. };
  301. /* /////////////////////////////////////////////////////////////////////////
  302. * Operators
  303. */
  304. template<
  305. ss_typename_param_k T
  306. , ss_typename_param_k N
  307. , ss_typename_param_k P
  308. >
  309. inline
  310. frequency_map<T, N, P>
  311. operator +(
  312. frequency_map<T, N, P> const& lhs
  313. , frequency_map<T, N, P> const& rhs
  314. )
  315. {
  316. frequency_map<T, N, P> r(lhs);
  317. r += rhs;
  318. return r;
  319. }
  320. /* /////////////////////////////////////////////////////////////////////////
  321. * Swapping
  322. */
  323. #if !defined(STLSOFT_COMPILER_IS_WATCOM)
  324. template<
  325. ss_typename_param_k T
  326. , ss_typename_param_k N
  327. , ss_typename_param_k P
  328. >
  329. inline void swap(
  330. frequency_map<T, N, P>& lhs
  331. , frequency_map<T, N, P>& rhs
  332. )
  333. {
  334. lhs.swap(rhs);
  335. }
  336. #endif /* compiler */
  337. /* /////////////////////////////////////////////////////////////////////////
  338. * Unit-testing
  339. */
  340. #ifdef STLSOFT_UNITTEST
  341. # include "./unittest/frequency_map_unittest_.h"
  342. #endif /* STLSOFT_UNITTEST */
  343. /* ////////////////////////////////////////////////////////////////////// */
  344. #ifndef _STLSOFT_NO_NAMESPACE
  345. } // namespace stlsoft
  346. #endif /* _STLSOFT_NO_NAMESPACE */
  347. #if defined(STLSOFT_CF_std_NAMESPACE)
  348. namespace std
  349. {
  350. # if !defined(STLSOFT_COMPILER_IS_MSVC) || \
  351. _MSC_VER >= 1310
  352. template<
  353. ss_typename_param_k T
  354. , ss_typename_param_k N
  355. , ss_typename_param_k P
  356. >
  357. inline void swap(
  358. stlsoft_ns_qual(frequency_map)<T, N, P>& lhs
  359. , stlsoft_ns_qual(frequency_map)<T, N, P>& rhs
  360. )
  361. {
  362. lhs.swap(rhs);
  363. }
  364. # endif
  365. } /* namespace std */
  366. #endif /* STLSOFT_CF_std_NAMESPACE */
  367. /* ////////////////////////////////////////////////////////////////////// */
  368. #endif /* !STLSOFT_INCL_STLSOFT_CONTAINERS_HPP_FREQUENCY_MAP */
  369. /* ///////////////////////////// end of file //////////////////////////// */