set_algo.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2010: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  5. +------------------------------------------------------------------------------+
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENCE.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. +-----------------------------------------------------------------------------*/
  10. #ifndef BOOST_ICL_SET_ALGO_HPP_JOFA_990225
  11. #define BOOST_ICL_SET_ALGO_HPP_JOFA_990225
  12. #include <boost/type_traits/remove_const.hpp>
  13. #include <boost/icl/detail/notate.hpp>
  14. #include <boost/icl/concept/container.hpp>
  15. #include <boost/icl/concept/set_value.hpp>
  16. #include <boost/icl/concept/map_value.hpp>
  17. namespace boost{namespace icl
  18. {
  19. namespace Set
  20. {
  21. template<class ObjectT, class ConstObjectT, class IteratorT>
  22. bool common_range(IteratorT& lwb, IteratorT& upb, ObjectT& x1, const ConstObjectT& x2)
  23. {
  24. // lwb and upb are iterators of x1 marking the lower and upper bound of
  25. // the common range of x1 and x2.
  26. typedef typename ConstObjectT::const_iterator ConstObject_iterator;
  27. // ObjectT may be const or non const.
  28. typedef typename remove_const<ObjectT>::type PureObjectT;
  29. lwb = x1.end();
  30. upb = x1.end();
  31. if(icl::is_empty(x1) || icl::is_empty(x2))
  32. return false;
  33. IteratorT x1_fst_ = x1.begin();
  34. IteratorT x1_lst_ = x1.end(); x1_lst_--;
  35. ConstObject_iterator x2_fst_ = x2.begin();
  36. ConstObject_iterator x2_lst_ = x2.end(); x2_lst_--;
  37. typename ObjectT::key_compare key_less;
  38. if(key_less(icl::key_value< PureObjectT>(x1_lst_),
  39. icl::key_value<ConstObjectT>(x2_fst_))) // {x1} {x2}
  40. return false;
  41. if(key_less(icl::key_value<ConstObjectT>(x2_lst_),
  42. icl::key_value< PureObjectT>(x1_fst_))) // {x2} {x1}
  43. return false;
  44. // We do have a common range
  45. lwb = x1.lower_bound(icl::key_value<ConstObjectT>(x2_fst_));
  46. upb = x1.upper_bound(icl::key_value<ConstObjectT>(x2_lst_));
  47. return true;
  48. }
  49. /** Function template <tt>contained_in</tt> implements the subset relation.
  50. <tt>contained_in(sub, super)</tt> is true if <tt>sub</tt> is contained in <tt>super</tt> */
  51. template<class SetType>
  52. inline bool within(const SetType& sub, const SetType& super)
  53. {
  54. if(&super == &sub) return true;
  55. if(icl::is_empty(sub)) return true;
  56. if(icl::is_empty(super)) return false;
  57. typename SetType::const_iterator common_lwb_, common_upb_;
  58. if(!common_range(common_lwb_, common_upb_, sub, super))
  59. return false;
  60. typename SetType::const_iterator sub_ = common_lwb_, super_;
  61. while(sub_ != common_upb_)
  62. {
  63. super_ = super.find(*sub_++);
  64. if(super_ == super.end())
  65. return false;
  66. }
  67. return true;
  68. }
  69. template<class SetType>
  70. bool intersects(const SetType& left, const SetType& right)
  71. {
  72. typename SetType::const_iterator common_lwb_right_, common_upb_right_;
  73. if(!common_range(common_lwb_right_, common_upb_right_, right, left))
  74. return false;
  75. typename SetType::const_iterator right_ = common_lwb_right_, found_;
  76. while(right_ != common_upb_right_)
  77. {
  78. found_ = left.find(*right_++);
  79. if(found_ != left.end())
  80. return true; // found a common element
  81. }
  82. // found no common element
  83. return false;
  84. }
  85. #ifdef BOOST_MSVC
  86. #pragma warning(push)
  87. #pragma warning(disable:4996) //'std::equal': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
  88. #endif // I do guarantee here that I am using the parameters correctly :)
  89. /** Function template <tt>lexicographical_equal</tt> implements
  90. lexicographical equality. */
  91. template<class SetType>
  92. inline bool lexicographical_equal(const SetType& left, const SetType& right)
  93. {
  94. if(&left == &right)
  95. return true;
  96. else return left.iterative_size() == right.iterative_size()
  97. && std::equal(left.begin(), left.end(), right.begin());
  98. }
  99. #ifdef BOOST_MSVC
  100. #pragma warning(pop)
  101. #endif
  102. } // namespace Set
  103. }} // namespace icl boost
  104. #endif