subset_comparer.hpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2008-2009: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENCE.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. +-----------------------------------------------------------------------------*/
  8. #ifndef BOOST_ICL_SUBSET_COMPARER_HPP_JOFA_090202
  9. #define BOOST_ICL_SUBSET_COMPARER_HPP_JOFA_090202
  10. #include <boost/mpl/and.hpp>
  11. #include <boost/icl/type_traits/is_map.hpp>
  12. #include <boost/icl/detail/notate.hpp>
  13. #include <boost/icl/detail/relation_state.hpp>
  14. #include <boost/icl/type_traits/identity_element.hpp>
  15. #include <boost/icl/type_traits/codomain_type_of.hpp>
  16. #include <boost/icl/type_traits/is_concept_equivalent.hpp>
  17. #include <boost/icl/type_traits/is_element_container.hpp>
  18. #include <boost/icl/concept/interval_set_value.hpp>
  19. #include <boost/icl/concept/map_value.hpp>
  20. namespace boost{namespace icl
  21. {
  22. #ifdef BOOST_MSVC
  23. #pragma warning(push)
  24. #pragma warning(disable:4127) // conditional expression is constant
  25. #endif
  26. namespace Set
  27. {
  28. //------------------------------------------------------------------------------
  29. template<class LeftT, class RightT>
  30. struct settic_codomain_compare
  31. {
  32. static int apply(typename LeftT::const_iterator& left_, typename RightT::const_iterator& right_)
  33. {
  34. return inclusion_compare( co_value<LeftT>(left_),
  35. co_value<RightT>(right_));
  36. }
  37. };
  38. template<class LeftT, class RightT>
  39. struct atomic_codomain_compare
  40. {
  41. static int apply(typename LeftT::const_iterator& left_, typename RightT::const_iterator& right_)
  42. {
  43. if(co_value<LeftT>(left_) == co_value<RightT>(right_))
  44. return inclusion::equal;
  45. else
  46. return inclusion::unrelated;
  47. }
  48. };
  49. template<class LeftT, class RightT>
  50. struct empty_codomain_compare
  51. {
  52. static int apply(typename LeftT::const_iterator&, typename RightT::const_iterator&)
  53. {
  54. return inclusion::equal;
  55. }
  56. };
  57. template<class LeftT, class RightT>
  58. struct map_codomain_compare
  59. {
  60. static int apply(typename LeftT::const_iterator& left_, typename RightT::const_iterator& right_)
  61. {
  62. using namespace boost::mpl;
  63. typedef typename LeftT::codomain_type LeftCodomainT;
  64. typedef typename RightT::codomain_type RightCodomainT;
  65. return
  66. if_<
  67. bool_<is_concept_equivalent<is_set,LeftCodomainT,
  68. RightCodomainT>::value>,
  69. settic_codomain_compare<LeftT,RightT>,
  70. atomic_codomain_compare<LeftT,RightT>
  71. >
  72. ::type::apply(left_, right_);
  73. }
  74. };
  75. //------------------------------------------------------------------------------
  76. template<class LeftT, class RightT>
  77. class subset_comparer
  78. {
  79. private:
  80. subset_comparer& operator = (const subset_comparer&);
  81. public:
  82. typedef typename LeftT::const_iterator LeftIterT;
  83. typedef typename RightT::const_iterator RightIterT;
  84. BOOST_STATIC_CONSTANT(bool,
  85. _compare_codomain = (mpl::and_<is_map<LeftT>, is_map<RightT> >::value));
  86. subset_comparer(const LeftT& left,
  87. const RightT& right,
  88. const LeftIterT& left_end,
  89. const RightIterT& right_end)
  90. : _left(left), _right(right),
  91. _left_end(left_end), _right_end(right_end), _result(equal)
  92. {}
  93. enum{nextboth, stop};
  94. enum
  95. {
  96. unrelated = inclusion::unrelated,
  97. subset = inclusion::subset, // left is_subset_of right
  98. superset = inclusion::superset, // left is_superset_of right
  99. equal = inclusion::equal // equal = subset | superset
  100. };
  101. int result()const{ return _result; }
  102. int co_compare(LeftIterT& left, RightIterT& right)
  103. {
  104. using namespace boost::mpl;
  105. //CL typedef typename codomain_type_of<LeftT>::type LeftCodomainT;
  106. //CL typedef typename codomain_type_of<RightT>::type RightCodomainT;
  107. return
  108. if_<
  109. bool_<is_concept_equivalent<is_element_map,LeftT,RightT>::value>,
  110. map_codomain_compare<LeftT,RightT>,
  111. empty_codomain_compare<LeftT,RightT>
  112. >
  113. ::type::apply(left,right);
  114. }
  115. int restrict_result(int state) { return _result &= state; }
  116. int next_both(LeftIterT& left, RightIterT& right)
  117. {
  118. if(left == _left_end && right == _right_end)
  119. return stop;
  120. else if(left == _left_end)
  121. {
  122. restrict_result(subset);
  123. return stop;
  124. }
  125. else if(right == _right_end)
  126. {
  127. restrict_result(superset);
  128. return stop;
  129. }
  130. else if(typename LeftT::key_compare()(key_value<LeftT>(left), key_value<RightT>(right)))
  131. { // left: *left . . *joint_ left could be superset
  132. // right: *right ... if joint_ exists
  133. restrict_result(superset);
  134. if(unrelated == _result)
  135. return stop;
  136. else
  137. {
  138. LeftIterT joint_ = _left.lower_bound(key_value<RightT>(right));
  139. if( joint_ == _left.end()
  140. || typename LeftT::key_compare()(key_value<RightT>(right), key_value<LeftT>(joint_)))
  141. {
  142. _result = unrelated;
  143. return stop;
  144. }
  145. else
  146. left = joint_;
  147. }
  148. }
  149. else if(typename LeftT::key_compare()(key_value<RightT>(right), key_value<LeftT>(left)))
  150. { // left: *left left could be subset
  151. // right:*right . . .*joint_ if *joint_ exists
  152. restrict_result(subset);
  153. if(unrelated == _result)
  154. return stop;
  155. else
  156. {
  157. RightIterT joint_ = _right.lower_bound(key_value<LeftT>(left));
  158. if( joint_ == _right.end()
  159. || typename LeftT::key_compare()(key_value<LeftT>(left), key_value<RightT>(joint_)))
  160. {
  161. _result = unrelated;
  162. return stop;
  163. }
  164. else
  165. right = joint_;
  166. }
  167. }
  168. // left =key= right
  169. if(_compare_codomain)
  170. if(unrelated == restrict_result(co_compare(left,right)))
  171. return stop;
  172. ++left;
  173. ++right;
  174. return nextboth;
  175. }
  176. private:
  177. const LeftT& _left;
  178. const RightT& _right;
  179. LeftIterT _left_end;
  180. RightIterT _right_end;
  181. int _result;
  182. };
  183. //------------------------------------------------------------------------------
  184. // Subset/superset comparison on ranges of two interval container
  185. //------------------------------------------------------------------------------
  186. template<class LeftT, class RightT>
  187. int subset_compare
  188. (
  189. const LeftT& left, //sub
  190. const RightT& right, //super
  191. typename LeftT::const_iterator left_begin,
  192. typename LeftT::const_iterator left_end,
  193. typename RightT::const_iterator right_begin,
  194. typename RightT::const_iterator right_end
  195. )
  196. {
  197. typedef subset_comparer<LeftT,RightT> Step;
  198. Step step(left, right, left_end, right_end);
  199. typename LeftT::const_iterator left_ = left_begin;
  200. typename RightT::const_iterator right_ = right_begin;
  201. int state = Step::nextboth;
  202. while(state != Step::stop)
  203. state = step.next_both(left_, right_);
  204. return step.result();
  205. }
  206. template<class LeftT, class RightT>
  207. int subset_compare(const LeftT& left, const RightT& right)
  208. {
  209. return subset_compare
  210. (
  211. left, right,
  212. left.begin(), left.end(),
  213. right.begin(), right.end()
  214. );
  215. }
  216. } // namespace Set
  217. #ifdef BOOST_MSVC
  218. #pragma warning(pop)
  219. #endif
  220. }} // namespace icl boost
  221. #endif