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.

308 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/collections/associative_key_sequence.hpp
  3. *
  4. * Purpose: Adapts associative containers into a sequence of their values.
  5. *
  6. * Created: 28th January 2005
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2009, 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/collections/associative_key_sequence.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::associative_key_sequence
  42. * class template
  43. * (\ref group__library__collections "Collections" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE
  46. #define STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE_MINOR 1
  50. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE_EDIT 24
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_WATCOM:
  59. [Incompatibilies-end]
  60. */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Includes
  63. */
  64. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  65. # include <stlsoft/stlsoft.h>
  66. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  67. #ifndef STLSOFT_INCL_STLSOFT_ITERATORS_HPP_ASSOCIATIVE_SELECT_ITERATOR
  68. # include <stlsoft/iterators/associative_select_iterator.hpp>
  69. #endif /* !STLSOFT_INCL_STLSOFT_ITERATORS_HPP_ASSOCIATIVE_SELECT_ITERATOR */
  70. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS
  71. # include <stlsoft/collections/util/collections.hpp>
  72. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_UTIL_HPP_COLLECTIONS */
  73. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  74. # include <stlsoft/util/std/iterator_helper.hpp>
  75. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  76. #ifndef STLSOFT_INCL_STDEXCEPT
  77. # define STLSOFT_INCL_STDEXCEPT
  78. # include <stdexcept>
  79. #endif /* !STLSOFT_INCL_STDEXCEPT */
  80. #ifdef STLSOFT_UNITTEST
  81. # include <map>
  82. #endif /* STLSOFT_UNITTEST */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Namespace
  85. */
  86. #ifndef _STLSOFT_NO_NAMESPACE
  87. namespace stlsoft
  88. {
  89. #endif /* _STLSOFT_NO_NAMESPACE */
  90. /* /////////////////////////////////////////////////////////////////////////
  91. * Classes
  92. */
  93. /** \brief Adapts an associative container to provide a sequence over the element keys
  94. *
  95. * \ingroup group__library__collections
  96. */
  97. template <ss_typename_param_k A>
  98. class associative_key_sequence
  99. : public stl_collection_tag
  100. {
  101. /// \name Types
  102. /// @{
  103. public:
  104. /// The current parameterisation of the type
  105. typedef associative_key_sequence<A> class_type;
  106. /// The value type
  107. #if 0
  108. typedef ss_typename_type_k A::mapped_type value_type;
  109. #else /* ? 0 */
  110. typedef ss_typename_type_k A::value_type::first_type value_type;
  111. #endif /* 0 */
  112. /// The associative container type
  113. typedef A associative_container_type;
  114. /// The allocator type
  115. typedef ss_typename_type_k A::allocator_type allocator_type;
  116. #if 0
  117. /// The pointer type
  118. ///
  119. /// \note This is identical to the const_pointer, since we will not be using
  120. /// this adaptor sequence to alter the contents of the underlying sequence
  121. typedef ss_typename_type_k A::const_pointer pointer;
  122. /// The non-mutable (const) pointer type
  123. typedef ss_typename_type_k A::const_pointer const_pointer;
  124. #endif /* 0 */
  125. /// The reference type
  126. ///
  127. /// \note This is identical to the const_reference, since we will not be using
  128. /// this adaptor sequence to alter the contents of the underlying sequence
  129. typedef ss_typename_type_k A::const_reference reference;
  130. /// The non-mutable (const) reference type
  131. typedef ss_typename_type_k A::const_reference const_reference;
  132. /// The size type
  133. typedef ss_typename_type_k A::size_type size_type;
  134. /// The difference type
  135. typedef ss_typename_type_k A::difference_type difference_type;
  136. /// The iterator type
  137. private:
  138. typedef ss_typename_type_k A::iterator A_iterator_type;
  139. typedef ss_typename_type_k A::const_iterator A_const_iterator_type;
  140. typedef ss_typename_type_k A::value_type A_value_type;
  141. typedef ::stlsoft::select_first<A_value_type> select_first_type;
  142. // typedef ::stlsoft::select_first<ss_typename_type_k A::value_type>
  143. public:
  144. typedef associative_select_iterator < A_iterator_type
  145. , select_first_type
  146. > iterator;
  147. /// The non-mutating (const) iterator type
  148. typedef associative_select_iterator < A_const_iterator_type
  149. , select_first_type
  150. > const_iterator;
  151. #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  152. /// The reverse iterator type
  153. typedef reverse_iterator_base < iterator
  154. , value_type
  155. , reference
  156. , value_type*
  157. , difference_type
  158. > reverse_iterator;
  159. /// The non-mutating (const) reverse iterator type
  160. typedef const_reverse_iterator_base < const_iterator
  161. , value_type const
  162. , const_reference
  163. , value_type const*
  164. , difference_type
  165. > const_reverse_iterator;
  166. #endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  167. /// @}
  168. /// \name Construction
  169. /// @{
  170. public:
  171. ss_explicit_k associative_key_sequence(associative_container_type const& container);
  172. /// @}
  173. /// \name Attributes
  174. /// @{
  175. public:
  176. /// The number of elements in the container
  177. size_type size() const;
  178. /// The maximum number of elements that can be stored in the container
  179. size_type max_size() const;
  180. /// Indicates whether the container is empty
  181. ss_bool_t empty() const;
  182. /// @}
  183. /// \name Iteration
  184. /// @{
  185. public:
  186. /// Begins the iteration
  187. ///
  188. /// \return A non-mutable (const) iterator representing the start of the sequence
  189. const_iterator begin() const;
  190. /// Ends the iteration
  191. ///
  192. /// \return A non-mutable (const) iterator representing the end of the sequence
  193. const_iterator end() const;
  194. #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  195. /// Begins the reverse iteration
  196. ///
  197. /// \return A non-mutable (const) iterator representing the start of the reverse sequence
  198. const_reverse_iterator rbegin() const;
  199. /// Ends the reverse iteration
  200. ///
  201. /// \return A non-mutable (const) iterator representing the end of the reverse sequence
  202. const_reverse_iterator rend() const;
  203. #endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  204. /// @}
  205. /// \name Members
  206. /// @{
  207. private:
  208. associative_container_type const &m_container;
  209. /// @}
  210. };
  211. /* /////////////////////////////////////////////////////////////////////////
  212. * Implementation
  213. */
  214. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  215. // Construction
  216. template <ss_typename_param_k A>
  217. inline associative_key_sequence<A>::associative_key_sequence(ss_typename_type_k associative_key_sequence<A>::associative_container_type const& container)
  218. : m_container(container)
  219. {}
  220. // Attributes
  221. template <ss_typename_param_k A>
  222. inline ss_typename_type_ret_k associative_key_sequence<A>::size_type associative_key_sequence<A>::size() const
  223. {
  224. return m_container.size();
  225. }
  226. template <ss_typename_param_k A>
  227. inline ss_typename_type_ret_k associative_key_sequence<A>::size_type associative_key_sequence<A>::max_size() const
  228. {
  229. return m_container.max_size();
  230. }
  231. template <ss_typename_param_k A>
  232. inline ss_bool_t associative_key_sequence<A>::empty() const
  233. {
  234. return m_container.empty();
  235. }
  236. // Iteration
  237. template <ss_typename_param_k A>
  238. inline ss_typename_type_ret_k associative_key_sequence<A>::const_iterator associative_key_sequence<A>::begin() const
  239. {
  240. return const_iterator(m_container.begin());
  241. }
  242. template <ss_typename_param_k A>
  243. inline ss_typename_type_ret_k associative_key_sequence<A>::const_iterator associative_key_sequence<A>::end() const
  244. {
  245. return const_iterator(m_container.end());
  246. }
  247. # if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  248. template <ss_typename_param_k A>
  249. inline ss_typename_type_ret_k associative_key_sequence<A>::const_reverse_iterator associative_key_sequence<A>::rbegin() const
  250. {
  251. return const_reverse_iterator(m_container.rbegin());
  252. }
  253. template <ss_typename_param_k A>
  254. inline ss_typename_type_ret_k associative_key_sequence<A>::const_reverse_iterator associative_key_sequence<A>::rend() const
  255. {
  256. return const_reverse_iterator(m_container.rend());
  257. }
  258. # endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  259. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  260. /* ////////////////////////////////////////////////////////////////////// */
  261. #ifndef _STLSOFT_NO_NAMESPACE
  262. } // namespace stlsoft
  263. #endif /* _STLSOFT_NO_NAMESPACE */
  264. /* ////////////////////////////////////////////////////////////////////// */
  265. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_KEY_SEQUENCE */
  266. /* ///////////////////////////// end of file //////////////////////////// */