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.

182 lines
5.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/pair.hpp (originally MTPair.h, ::SynesisStl)
  3. *
  4. * Purpose: Contains the pair template.
  5. *
  6. * Created: 19th November 1998
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 1998-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/util/pair.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::pair class template
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_PAIR
  45. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_PAIR
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PAIR_MAJOR 5
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PAIR_MINOR 0
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PAIR_REVISION 2
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_PAIR_EDIT 54
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Includes
  54. */
  55. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  56. # include <stlsoft/stlsoft.h>
  57. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Namespace
  60. */
  61. #ifndef _STLSOFT_NO_NAMESPACE
  62. namespace stlsoft
  63. {
  64. #endif /* _STLSOFT_NO_NAMESPACE */
  65. /* /////////////////////////////////////////////////////////////////////////
  66. * Classes
  67. */
  68. /** \brief Represents a pair
  69. *
  70. * \ingroup group__library__utility
  71. *
  72. * \param T1 The type of the \c first member
  73. * \param T2 The type of the \c second member
  74. */
  75. template< ss_typename_param_k T1
  76. , ss_typename_param_k T2
  77. >
  78. struct pair
  79. {
  80. public:
  81. /// The first type
  82. typedef T1 first_type;
  83. /// The second type
  84. typedef T2 second_type;
  85. /// \name Construction
  86. /// @{
  87. public:
  88. /// Default constructor
  89. pair()
  90. : first(first_type())
  91. , second(second_type())
  92. {}
  93. /// Converstion constructor
  94. pair(first_type const& v1, second_type const& v2)
  95. : first(v1)
  96. , second(v2)
  97. {}
  98. /// @}
  99. /// \name Members
  100. /// @{
  101. public:
  102. first_type first; //!< The first member
  103. second_type second; //!< The second member
  104. /// @}
  105. };
  106. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  107. template<ss_typename_param_k T1, ss_typename_param_k T2>
  108. inline ss_bool_t operator ==(pair<T1, T2> const& x, pair<T1, T2> const& y)
  109. {
  110. return x.first == y.first && x.second == y.second;
  111. }
  112. template<ss_typename_param_k T1, ss_typename_param_k T2>
  113. inline ss_bool_t operator !=(pair<T1, T2> const& x, pair<T1, T2> const& y)
  114. {
  115. return !(x == y);
  116. }
  117. template<ss_typename_param_k T1, ss_typename_param_k T2>
  118. inline ss_bool_t operator <(pair<T1, T2> const& x, pair<T1, T2> const& y)
  119. {
  120. return x.first < y.first || !(y.first < x.first) && x.second < y.second;
  121. }
  122. template<ss_typename_param_k T1, ss_typename_param_k T2>
  123. inline ss_bool_t operator >(pair<T1, T2> const& x, pair<T1, T2> const& y)
  124. {
  125. return y < x;
  126. }
  127. template<ss_typename_param_k T1, ss_typename_param_k T2>
  128. inline ss_bool_t operator <=(pair<T1, T2> const& x, pair<T1, T2> const& y)
  129. {
  130. return !(y < x);
  131. }
  132. template<ss_typename_param_k T1, ss_typename_param_k T2>
  133. inline ss_bool_t operator >=(pair<T1, T2> const& x, pair<T1, T2> const& y)
  134. {
  135. return !(x < y);
  136. }
  137. template<ss_typename_param_k T1, ss_typename_param_k T2>
  138. inline pair<T1, T2> make_pair(T1 const& x, T2 const& y)
  139. {
  140. return pair<T1, T2>(x, y);
  141. }
  142. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  143. ////////////////////////////////////////////////////////////////////////////
  144. // Unit-testing
  145. #ifdef STLSOFT_UNITTEST
  146. # include "./unittest/pair_unittest_.h"
  147. #endif /* STLSOFT_UNITTEST */
  148. /* ////////////////////////////////////////////////////////////////////// */
  149. #ifndef _STLSOFT_NO_NAMESPACE
  150. } // namespace stlsoft
  151. #endif /* _STLSOFT_NO_NAMESPACE */
  152. /* ////////////////////////////////////////////////////////////////////// */
  153. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_PAIR */
  154. /* ///////////////////////////// end of file //////////////////////////// */