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.

199 lines
6.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/collections/functions.hpp
  3. *
  4. * Purpose: Collection manipulation functions.
  5. *
  6. * Created: 11th November 2004
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2004-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/functions.hpp
  40. *
  41. * \brief [C++ only] Definition of collection manipulation functions
  42. * (\ref group__library__collections "Collections" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_FUNCTIONS
  45. #define STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_FUNCTIONS_MAJOR 2
  48. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_FUNCTIONS_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_FUNCTIONS_REVISION 3
  50. # define STLSOFT_VER_STLSOFT_COLLECTIONS_HPP_FUNCTIONS_EDIT 34
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Compatibility
  54. */
  55. /*
  56. [Incompatibilies-start]
  57. STLSOFT_COMPILER_IS_WATCOM:
  58. [Incompatibilies-end]
  59. */
  60. /* /////////////////////////////////////////////////////////////////////////
  61. * Includes
  62. */
  63. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  64. # include <stlsoft/stlsoft.h>
  65. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  66. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  67. # include <stlsoft/util/std/iterator_helper.hpp>
  68. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  69. #ifndef STLSOFT_INCL_STDEXCEPT
  70. # define STLSOFT_INCL_STDEXCEPT
  71. # include <stdexcept>
  72. #endif /* !STLSOFT_INCL_STDEXCEPT */
  73. #ifdef STLSOFT_UNITTEST
  74. # include <map>
  75. #endif /* STLSOFT_UNITTEST */
  76. /* /////////////////////////////////////////////////////////////////////////
  77. * Namespace
  78. */
  79. #ifndef _STLSOFT_NO_NAMESPACE
  80. namespace stlsoft
  81. {
  82. #endif /* _STLSOFT_NO_NAMESPACE */
  83. /* /////////////////////////////////////////////////////////////////////////
  84. * Compiler / library compatibility
  85. */
  86. #ifdef STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE
  87. # undef STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE
  88. #endif /* STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE */
  89. #if defined(STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC)
  90. # if STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION <= STLSOFT_CF_DINKUMWARE_VC_VERSION_6_0
  91. # define STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE
  92. # endif /* STLSOFT_CF_STD_LIBRARY_DINKUMWARE_VC_VERSION */
  93. #endif /* STLSOFT_CF_STD_LIBRARY_IS_DINKUMWARE_VC */
  94. /* /////////////////////////////////////////////////////////////////////////
  95. * Functions
  96. */
  97. /** \brief Looks up and returns the matching referent for a given key, or throws
  98. * std::out_of_range if the key is not found in the map
  99. *
  100. * \ingroup group__library__collections
  101. *
  102. * \param m The map to be searched
  103. * \param key The key to be searched for
  104. *
  105. * \return The matching referent
  106. *
  107. * \note If \c key does not refer to an entry in the map \c m, then
  108. * std::out_of_range is thrown
  109. */
  110. #if ( !defined(STLSOFT_COMPILER_IS_INTEL) && \
  111. !defined(STLSOFT_COMPILER_IS_MSVC)) || \
  112. _MSC_VER >= 1310
  113. template< ss_typename_param_k M
  114. , ss_typename_param_k K
  115. >
  116. # ifdef STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE
  117. inline ss_typename_type_ret_k M::referent_type &lookup(M &m, K const& key)
  118. # else /* ? STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE */
  119. inline ss_typename_type_ret_k M::mapped_type &lookup(M &m, K const& key)
  120. # endif /* STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE */
  121. {
  122. ss_typename_type_k M::iterator it = m.find(key);
  123. if(m.end() == it)
  124. {
  125. STLSOFT_THROW_X(stlsoft_ns_qual_std(out_of_range)("invalid key"));
  126. }
  127. return (*it).second;
  128. }
  129. #endif /* compiler */
  130. /** \brief Looks up and returns the matching referent for a given key, or throws
  131. * std::out_of_range if the key is not found in the map
  132. *
  133. * \ingroup group__library__collections
  134. *
  135. * \param m The map to be searched
  136. * \param key The key to be searched for
  137. *
  138. * \return The matching referent
  139. *
  140. * \note If \c key does not refer to an entry in the map \c m, then
  141. * std::out_of_range is thrown
  142. */
  143. template< ss_typename_param_k M
  144. , ss_typename_param_k K
  145. >
  146. #ifdef STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE
  147. inline ss_typename_type_ret_k M::referent_type const& lookup(M const& m, K const& key)
  148. #else /* ? STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE */
  149. inline ss_typename_type_ret_k M::mapped_type const& lookup(M const& m, K const& key)
  150. #endif /* STLSOFT_CONTAINER_ACCESS_MAPPED_TYPE_IS_REFERENT_TYPE */
  151. {
  152. ss_typename_type_k M::const_iterator it = m.find(key);
  153. if(m.end() == it)
  154. {
  155. STLSOFT_THROW_X(stlsoft_ns_qual_std(out_of_range)("invalid key"));
  156. }
  157. return (*it).second;
  158. }
  159. ////////////////////////////////////////////////////////////////////////////
  160. // Unit-testing
  161. #ifdef STLSOFT_UNITTEST
  162. # include "./unittest/functions_unittest_.h"
  163. #endif /* STLSOFT_UNITTEST */
  164. /* ////////////////////////////////////////////////////////////////////// */
  165. #ifndef _STLSOFT_NO_NAMESPACE
  166. } // namespace stlsoft
  167. #endif /* _STLSOFT_NO_NAMESPACE */
  168. /* ////////////////////////////////////////////////////////////////////// */
  169. #endif /* !STLSOFT_INCL_STLSOFT_COLLECTIONS_HPP_FUNCTIONS */
  170. /* ///////////////////////////// end of file //////////////////////////// */