1 /*-----------------------------------------------------------------------------+
2 Copyright (c) 2008-2010: 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_INTERVAL_MAP_ALGO_HPP_JOFA_100730
9 #define BOOST_ICL_INTERVAL_MAP_ALGO_HPP_JOFA_100730
11 #include <boost/utility/enable_if.hpp>
12 #include <boost/mpl/not.hpp>
14 #include <boost/icl/type_traits/is_total.hpp>
15 #include <boost/icl/type_traits/is_map.hpp>
16 #include <boost/icl/detail/notate.hpp>
17 #include <boost/icl/detail/relation_state.hpp>
18 #include <boost/icl/type_traits/identity_element.hpp>
19 #include <boost/icl/interval_combining_style.hpp>
20 #include <boost/icl/detail/element_comparer.hpp>
21 #include <boost/icl/detail/interval_subset_comparer.hpp>
23 namespace boost{namespace icl
27 namespace Interval_Map
29 using namespace segmental;
31 template<class IntervalMapT>
32 bool is_joinable(const IntervalMapT& container,
33 typename IntervalMapT::const_iterator first,
34 typename IntervalMapT::const_iterator past)
36 if(first == container.end())
39 typename IntervalMapT::const_iterator it_ = first, next_ = first;
42 const typename IntervalMapT::codomain_type& co_value
43 = icl::co_value<IntervalMapT>(first);
46 if(icl::co_value<IntervalMapT>(next_) != co_value)
48 if(!icl::touches(key_value<IntervalMapT>(it_++),
49 key_value<IntervalMapT>(next_++)))
56 //------------------------------------------------------------------------------
57 //- Containedness of key objects
58 //------------------------------------------------------------------------------
60 //- domain_type ----------------------------------------------------------------
61 template<class IntervalMapT>
62 typename enable_if<mpl::not_<is_total<IntervalMapT> >, bool>::type
63 contains(const IntervalMapT& container,
64 const typename IntervalMapT::domain_type& key)
66 return container.find(key) != container.end();
69 template<class IntervalMapT>
70 typename enable_if<is_total<IntervalMapT>, bool>::type
71 contains(const IntervalMapT&,
72 const typename IntervalMapT::domain_type&)
77 //- interval_type --------------------------------------------------------------
78 template<class IntervalMapT>
79 typename enable_if<mpl::not_<is_total<IntervalMapT> >, bool>::type
80 contains(const IntervalMapT& container,
81 const typename IntervalMapT::interval_type& sub_interval)
83 typedef typename IntervalMapT::const_iterator const_iterator;
84 if(icl::is_empty(sub_interval))
87 std::pair<const_iterator, const_iterator> exterior = container.equal_range(sub_interval);
88 if(exterior.first == exterior.second)
91 const_iterator last_overlap = prior(exterior.second);
94 icl::contains(hull(exterior.first->first, last_overlap->first), sub_interval)
95 && Interval_Set::is_joinable(container, exterior.first, last_overlap);
98 template<class IntervalMapT>
99 typename enable_if<is_total<IntervalMapT>, bool>::type
100 contains(const IntervalMapT&,
101 const typename IntervalMapT::interval_type&)
106 //- set_type -------------------------------------------------------------------
107 template<class IntervalMapT, class IntervalSetT>
108 typename enable_if<mpl::and_<mpl::not_<is_total<IntervalMapT> >
109 ,is_interval_set<IntervalSetT> >, bool>::type
110 contains(const IntervalMapT& super_map, const IntervalSetT& sub_set)
112 return Interval_Set::within(sub_set, super_map);
115 template<class IntervalMapT, class IntervalSetT>
116 typename enable_if<mpl::and_<is_total<IntervalMapT>
117 ,is_interval_set<IntervalSetT> >, bool>::type
118 contains(const IntervalMapT&, const IntervalSetT&)
124 //------------------------------------------------------------------------------
125 //- Containedness of sub objects
126 //------------------------------------------------------------------------------
128 template<class IntervalMapT>
129 bool contains(const IntervalMapT& container,
130 const typename IntervalMapT::element_type& key_value_pair)
132 typename IntervalMapT::const_iterator it_ = container.find(key_value_pair.key);
133 return it_ != container.end() && (*it_).second == key_value_pair.data;
136 template<class IntervalMapT>
137 bool contains(const IntervalMapT& container,
138 const typename IntervalMapT::segment_type sub_segment)
140 typedef typename IntervalMapT::const_iterator const_iterator;
141 typename IntervalMapT::interval_type sub_interval = sub_segment.first;
142 if(icl::is_empty(sub_interval))
145 std::pair<const_iterator, const_iterator> exterior = container.equal_range(sub_interval);
146 if(exterior.first == exterior.second)
149 const_iterator last_overlap = prior(exterior.second);
151 if(!(sub_segment.second == exterior.first->second) )
155 icl::contains(hull(exterior.first->first, last_overlap->first), sub_interval)
156 && Interval_Map::is_joinable(container, exterior.first, last_overlap);
160 template<class IntervalMapT>
161 bool contains(const IntervalMapT& super, const IntervalMapT& sub)
163 return Interval_Set::within(sub, super);
166 } // namespace Interval_Map
168 }} // namespace icl boost