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.

303 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/collections/associative_value_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_value_sequence.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::associative_value_sequence
  42. * class template
  43. * (\ref group__library__collections "Collections" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE
  46. #define STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE_MINOR 1
  50. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE_REVISION 1
  51. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE_EDIT 25
  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 Instance adaptor that represents an associative collection as a
  94. * sequence of its values.
  95. *
  96. * \ingroup group__library__collections
  97. *
  98. * \param A The associative collection type
  99. */
  100. template <ss_typename_param_k A>
  101. class associative_value_sequence
  102. : public stl_collection_tag
  103. {
  104. /// \name Types
  105. /// @{
  106. public:
  107. /// \brief The current parameterisation of the type
  108. typedef associative_value_sequence<A> class_type;
  109. /// \brief The value type
  110. #if 0
  111. typedef ss_typename_type_k A::mapped_type value_type;
  112. #else /* ? 0 */
  113. typedef ss_typename_type_k A::value_type::second_type value_type;
  114. #endif /* 0 */
  115. /// \brief The associative container type
  116. typedef A associative_container_type;
  117. /// \brief The allocator type
  118. typedef ss_typename_type_k A::allocator_type allocator_type;
  119. #if 0
  120. /// \brief The pointer type
  121. ///
  122. /// \note This is identical to the const_pointer, since we will not be using
  123. /// this adaptor sequence to alter the contents of the underlying sequence
  124. typedef ss_typename_type_k A::const_pointer pointer;
  125. /// \brief The non-mutable (const) pointer type
  126. typedef ss_typename_type_k A::const_pointer const_pointer;
  127. #endif /* 0 */
  128. /// \brief The reference type
  129. ///
  130. /// \note This is identical to the const_reference, since we will not be using
  131. /// this adaptor sequence to alter the contents of the underlying sequence
  132. typedef ss_typename_type_k A::const_reference reference;
  133. /// \brief The non-mutable (const) reference type
  134. typedef ss_typename_type_k A::const_reference const_reference;
  135. /// \brief The size type
  136. typedef ss_typename_type_k A::size_type size_type;
  137. /// \brief The difference type
  138. typedef ss_typename_type_k A::difference_type difference_type;
  139. /// \brief The iterator type
  140. typedef associative_select_iterator < ss_typename_type_k A::iterator
  141. , ::stlsoft::select_second<ss_typename_type_k A::value_type>
  142. > iterator;
  143. /// \brief The non-mutating (const) iterator type
  144. typedef associative_select_iterator < ss_typename_type_k A::const_iterator
  145. , ::stlsoft::select_second_const<ss_typename_type_k A::value_type>
  146. > const_iterator;
  147. #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  148. /// \brief The reverse iterator type
  149. typedef reverse_iterator_base < iterator
  150. , value_type
  151. , reference
  152. , value_type*
  153. , difference_type
  154. > reverse_iterator;
  155. /// \brief The non-mutating (const) reverse iterator type
  156. typedef const_reverse_iterator_base < const_iterator
  157. , value_type const
  158. , const_reference
  159. , value_type const*
  160. , difference_type
  161. > const_reverse_iterator;
  162. #endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  163. /// @}
  164. /// \name Construction
  165. /// @{
  166. public:
  167. ss_explicit_k associative_value_sequence(associative_container_type const& container);
  168. /// @}
  169. /// \name Attributes
  170. /// @{
  171. public:
  172. /// \brief The number of elements in the container
  173. size_type size() const;
  174. /// \brief The maximum number of elements that can be stored in the container
  175. size_type max_size() const;
  176. /// \brief Indicates whether the container is empty
  177. ss_bool_t empty() const;
  178. /// @}
  179. /// \name Iteration
  180. /// @{
  181. public:
  182. /// \brief Begins the iteration
  183. ///
  184. /// \return A non-mutable (const) iterator representing the start of the sequence
  185. const_iterator begin() const;
  186. /// \brief Ends the iteration
  187. ///
  188. /// \return A non-mutable (const) iterator representing the end of the sequence
  189. const_iterator end() const;
  190. #if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  191. /// \brief Begins the reverse iteration
  192. ///
  193. /// \return A non-mutable (const) iterator representing the start of the reverse sequence
  194. const_reverse_iterator rbegin() const;
  195. /// \brief Ends the reverse iteration
  196. ///
  197. /// \return A non-mutable (const) iterator representing the end of the reverse sequence
  198. const_reverse_iterator rend() const;
  199. #endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  200. /// @}
  201. /// \name Members
  202. /// @{
  203. private:
  204. associative_container_type const &m_container;
  205. /// @}
  206. };
  207. /* /////////////////////////////////////////////////////////////////////////
  208. * Implementation
  209. */
  210. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  211. // Construction
  212. template <ss_typename_param_k A>
  213. inline associative_value_sequence<A>::associative_value_sequence(ss_typename_type_k associative_value_sequence<A>::associative_container_type const& container)
  214. : m_container(container)
  215. {}
  216. // Attributes
  217. template <ss_typename_param_k A>
  218. inline ss_typename_type_ret_k associative_value_sequence<A>::size_type associative_value_sequence<A>::size() const
  219. {
  220. return m_container.size();
  221. }
  222. template <ss_typename_param_k A>
  223. inline ss_typename_type_ret_k associative_value_sequence<A>::size_type associative_value_sequence<A>::max_size() const
  224. {
  225. return m_container.max_size();
  226. }
  227. template <ss_typename_param_k A>
  228. inline ss_bool_t associative_value_sequence<A>::empty() const
  229. {
  230. return m_container.empty();
  231. }
  232. // Iteration
  233. template <ss_typename_param_k A>
  234. inline ss_typename_type_ret_k associative_value_sequence<A>::const_iterator associative_value_sequence<A>::begin() const
  235. {
  236. return const_iterator(m_container.begin());
  237. }
  238. template <ss_typename_param_k A>
  239. inline ss_typename_type_ret_k associative_value_sequence<A>::const_iterator associative_value_sequence<A>::end() const
  240. {
  241. return const_iterator(m_container.end());
  242. }
  243. # if defined(STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT)
  244. template <ss_typename_param_k A>
  245. inline ss_typename_type_ret_k associative_value_sequence<A>::const_reverse_iterator associative_value_sequence<A>::rbegin() const
  246. {
  247. return const_reverse_iterator(m_container.rbegin());
  248. }
  249. template <ss_typename_param_k A>
  250. inline ss_typename_type_ret_k associative_value_sequence<A>::const_reverse_iterator associative_value_sequence<A>::rend() const
  251. {
  252. return const_reverse_iterator(m_container.rend());
  253. }
  254. # endif /* STLSOFT_LF_BIDIRECTIONAL_ITERATOR_SUPPORT */
  255. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  256. /* ////////////////////////////////////////////////////////////////////// */
  257. #ifndef _STLSOFT_NO_NAMESPACE
  258. } // namespace stlsoft
  259. #endif /* _STLSOFT_NO_NAMESPACE */
  260. /* ////////////////////////////////////////////////////////////////////// */
  261. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_ASSOCIATIVE_VALUE_SEQUENCE */
  262. /* ///////////////////////////// end of file //////////////////////////// */