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
11 #include <boost/mpl/and.hpp>
12 #include <boost/icl/type_traits/is_map.hpp>
13 #include <boost/icl/detail/notate.hpp>
14 #include <boost/icl/detail/relation_state.hpp>
15 #include <boost/icl/type_traits/identity_element.hpp>
16 #include <boost/icl/type_traits/codomain_type_of.hpp>
17 #include <boost/icl/type_traits/is_concept_equivalent.hpp>
18 #include <boost/icl/type_traits/is_element_container.hpp>
19 #include <boost/icl/concept/interval_set_value.hpp>
20 #include <boost/icl/concept/map_value.hpp>
22 namespace boost{namespace icl
27 #pragma warning(disable:4127) // conditional expression is constant
33 //------------------------------------------------------------------------------
34 template<class LeftT, class RightT>
35 struct settic_codomain_compare
37 static int apply(typename LeftT::const_iterator& left_, typename RightT::const_iterator& right_)
39 return inclusion_compare( co_value<LeftT>(left_),
40 co_value<RightT>(right_));
44 template<class LeftT, class RightT>
45 struct atomic_codomain_compare
47 static int apply(typename LeftT::const_iterator& left_, typename RightT::const_iterator& right_)
49 if(co_value<LeftT>(left_) == co_value<RightT>(right_))
50 return inclusion::equal;
52 return inclusion::unrelated;
56 template<class LeftT, class RightT>
57 struct empty_codomain_compare
59 static int apply(typename LeftT::const_iterator&, typename RightT::const_iterator&)
61 return inclusion::equal;
65 template<class LeftT, class RightT>
66 struct map_codomain_compare
68 static int apply(typename LeftT::const_iterator& left_, typename RightT::const_iterator& right_)
70 using namespace boost::mpl;
71 typedef typename LeftT::codomain_type LeftCodomainT;
72 typedef typename RightT::codomain_type RightCodomainT;
76 bool_<is_concept_equivalent<is_set,LeftCodomainT,
77 RightCodomainT>::value>,
78 settic_codomain_compare<LeftT,RightT>,
79 atomic_codomain_compare<LeftT,RightT>
81 ::type::apply(left_, right_);
86 //------------------------------------------------------------------------------
87 template<class LeftT, class RightT>
91 subset_comparer& operator = (const subset_comparer&);
93 typedef typename LeftT::const_iterator LeftIterT;
94 typedef typename RightT::const_iterator RightIterT;
96 BOOST_STATIC_CONSTANT(bool,
97 _compare_codomain = (mpl::and_<is_map<LeftT>, is_map<RightT> >::value));
99 subset_comparer(const LeftT& left,
101 const LeftIterT& left_end,
102 const RightIterT& right_end)
103 : _left(left), _right(right),
104 _left_end(left_end), _right_end(right_end), _result(equal)
107 enum{nextboth, stop};
111 unrelated = inclusion::unrelated,
112 subset = inclusion::subset, // left is_subset_of right
113 superset = inclusion::superset, // left is_superset_of right
114 equal = inclusion::equal // equal = subset | superset
117 int result()const{ return _result; }
119 int co_compare(LeftIterT& left, RightIterT& right)
121 using namespace boost::mpl;
122 typedef typename codomain_type_of<LeftT>::type LeftCodomainT;
123 typedef typename codomain_type_of<RightT>::type RightCodomainT;
127 bool_<is_concept_equivalent<is_element_map,LeftT,RightT>::value>,
128 map_codomain_compare<LeftT,RightT>,
129 empty_codomain_compare<LeftT,RightT>
131 ::type::apply(left,right);
134 int restrict_result(int state) { return _result &= state; }
136 int next_both(LeftIterT& left, RightIterT& right)
138 if(left == _left_end && right == _right_end)
140 else if(left == _left_end)
142 restrict_result(subset);
145 else if(right == _right_end)
147 restrict_result(superset);
150 else if(typename LeftT::key_compare()(key_value<LeftT>(left), key_value<RightT>(right)))
151 { // left: *left . . *joint_ left could be superset
152 // right: *right ... if joint_ exists
153 restrict_result(superset);
154 if(unrelated == _result)
158 LeftIterT joint_ = _left.lower_bound(key_value<RightT>(right));
159 if( joint_ == _left.end()
160 || typename LeftT::key_compare()(key_value<RightT>(right), key_value<LeftT>(joint_)))
169 else if(typename LeftT::key_compare()(key_value<RightT>(right), key_value<LeftT>(left)))
170 { // left: *left left could be subset
171 // right:*right . . .*joint_ if *joint_ exists
172 restrict_result(subset);
173 if(unrelated == _result)
177 RightIterT joint_ = _right.lower_bound(key_value<LeftT>(left));
178 if( joint_ == _right.end()
179 || typename LeftT::key_compare()(key_value<LeftT>(left), key_value<RightT>(joint_)))
190 if(_compare_codomain)
191 if(unrelated == restrict_result(co_compare(left,right)))
201 const RightT& _right;
203 RightIterT _right_end;
211 //------------------------------------------------------------------------------
212 // Subset/superset comparison on ranges of two interval container
213 //------------------------------------------------------------------------------
214 template<class LeftT, class RightT>
217 const LeftT& left, //sub
218 const RightT& right, //super
219 typename LeftT::const_iterator left_begin,
220 typename LeftT::const_iterator left_end,
221 typename RightT::const_iterator right_begin,
222 typename RightT::const_iterator right_end
225 typedef subset_comparer<LeftT,RightT> Step;
226 Step step(left, right, left_end, right_end);
228 typename LeftT::const_iterator left_ = left_begin;
229 typename RightT::const_iterator right_ = right_begin;
231 int state = Step::nextboth;
232 while(state != Step::stop)
233 state = step.next_both(left_, right_);
235 return step.result();
238 template<class LeftT, class RightT>
239 int subset_compare(const LeftT& left, const RightT& right)
241 return subset_compare
244 left.begin(), left.end(),
245 right.begin(), right.end()
256 }} // namespace icl boost