Chris@16
|
1 /*-----------------------------------------------------------------------------+
|
Chris@16
|
2 Copyright (c) 2011-2011: Joachim Faulhaber
|
Chris@16
|
3 +------------------------------------------------------------------------------+
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
5 (See accompanying file LICENCE.txt or copy at
|
Chris@16
|
6 http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 +-----------------------------------------------------------------------------*/
|
Chris@16
|
8 #ifndef BOOST_ICL_CONCEPT_INTERVAL_ASSOCIATOR_BASE_HPP_JOFA_110301
|
Chris@16
|
9 #define BOOST_ICL_CONCEPT_INTERVAL_ASSOCIATOR_BASE_HPP_JOFA_110301
|
Chris@16
|
10
|
Chris@16
|
11 #include <boost/icl/type_traits/domain_type_of.hpp>
|
Chris@16
|
12 #include <boost/icl/type_traits/interval_type_of.hpp>
|
Chris@16
|
13 #include <boost/icl/type_traits/is_combinable.hpp>
|
Chris@16
|
14 #include <boost/icl/concept/set_value.hpp>
|
Chris@16
|
15 #include <boost/icl/concept/map_value.hpp>
|
Chris@16
|
16 #include <boost/icl/concept/interval.hpp>
|
Chris@16
|
17
|
Chris@16
|
18 namespace boost{ namespace icl
|
Chris@16
|
19 {
|
Chris@16
|
20
|
Chris@16
|
21 //==============================================================================
|
Chris@16
|
22 //= Selection<IntervalSet|IntervalMap>
|
Chris@16
|
23 //==============================================================================
|
Chris@16
|
24 template<class Type> inline
|
Chris@16
|
25 typename enable_if<mpl::and_< is_interval_container<Type>
|
Chris@16
|
26 , is_discrete<typename domain_type_of<Type>::type>
|
Chris@16
|
27 >
|
Chris@16
|
28 , typename Type::const_iterator>::type
|
Chris@16
|
29 find(const Type& object, const typename domain_type_of<Type>::type& key_val)
|
Chris@16
|
30 {
|
Chris@16
|
31 typedef typename Type::const_iterator const_iterator;
|
Chris@16
|
32 typedef typename Type::interval_type interval_type;
|
Chris@16
|
33 return object.find(icl::detail::unit_trail<interval_type>(key_val));
|
Chris@16
|
34 }
|
Chris@16
|
35
|
Chris@16
|
36 template<class Type> inline
|
Chris@16
|
37 typename enable_if<mpl::and_< is_interval_container<Type>
|
Chris@16
|
38 , is_continuous<typename domain_type_of<Type>::type>
|
Chris@16
|
39 , has_dynamic_bounds<typename interval_type_of<Type>::type>
|
Chris@16
|
40 >
|
Chris@16
|
41 , typename Type::const_iterator>::type
|
Chris@16
|
42 find(const Type& object, const typename domain_type_of<Type>::type& key_val)
|
Chris@16
|
43 {
|
Chris@16
|
44 typedef typename Type::const_iterator const_iterator;
|
Chris@16
|
45 typedef typename Type::interval_type interval_type;
|
Chris@16
|
46 return object.find(icl::singleton<interval_type>(key_val));
|
Chris@16
|
47 }
|
Chris@16
|
48
|
Chris@16
|
49 template<class Type> inline
|
Chris@16
|
50 typename enable_if<mpl::and_< is_interval_container<Type>
|
Chris@16
|
51 , is_continuous<typename domain_type_of<Type>::type>
|
Chris@16
|
52 , is_static_right_open<typename interval_type_of<Type>::type>
|
Chris@16
|
53 , boost::detail::is_incrementable<typename domain_type_of<Type>::type>
|
Chris@16
|
54 >
|
Chris@16
|
55 , typename Type::const_iterator>::type
|
Chris@16
|
56 find(const Type& object, const typename domain_type_of<Type>::type& key_val)
|
Chris@16
|
57 {
|
Chris@16
|
58 typedef typename Type::const_iterator const_iterator;
|
Chris@16
|
59 typedef typename Type::interval_type interval_type;
|
Chris@16
|
60 const_iterator first_collision = object.lower_bound(icl::detail::unit_trail<interval_type>(key_val));
|
Chris@16
|
61 // A part of the unit_trail(key_value)-interval may be found in the container, that
|
Chris@16
|
62 // does not contain key_value. Therefore we have to check for its existence:
|
Chris@16
|
63 return ( first_collision == object.end()
|
Chris@16
|
64 || icl::contains(key_value<Type>(first_collision), key_val) )
|
Chris@16
|
65 ? first_collision
|
Chris@16
|
66 : object.end();
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 template<class Type> inline
|
Chris@16
|
70 typename enable_if<mpl::and_< is_interval_container<Type>
|
Chris@16
|
71 , is_continuous<typename domain_type_of<Type>::type>
|
Chris@16
|
72 , is_static_left_open<typename interval_type_of<Type>::type>
|
Chris@16
|
73 , boost::detail::is_incrementable<typename domain_type_of<Type>::type>
|
Chris@16
|
74 >
|
Chris@16
|
75 , typename Type::const_iterator>::type
|
Chris@16
|
76 find(const Type& object, const typename domain_type_of<Type>::type& key_val)
|
Chris@16
|
77 {
|
Chris@16
|
78 typedef typename Type::const_iterator const_iterator;
|
Chris@16
|
79 typedef typename Type::interval_type interval_type;
|
Chris@16
|
80 const_iterator last_collision = object.upper_bound(icl::detail::unit_trail<interval_type>(key_val));
|
Chris@16
|
81 if(last_collision != object.begin())
|
Chris@16
|
82 --last_collision;
|
Chris@16
|
83 // A part of the unit_trail(key_value)-interval may be found in the container, that
|
Chris@16
|
84 // does not contain key_value. Therefore we have to check for its existence:
|
Chris@16
|
85 return ( last_collision == object.end()
|
Chris@16
|
86 || icl::contains(key_value<Type>(last_collision), key_val) )
|
Chris@16
|
87 ? last_collision
|
Chris@16
|
88 : object.end();
|
Chris@16
|
89 }
|
Chris@16
|
90
|
Chris@16
|
91 // NOTE: find(object, key) won't compile if key is of continuous type that does
|
Chris@16
|
92 // not implement in(de)crementation (e.g. std::string).
|
Chris@16
|
93
|
Chris@16
|
94 template<class Type> inline
|
Chris@16
|
95 typename enable_if< is_interval_container<Type>
|
Chris@16
|
96 , typename Type::const_iterator>::type
|
Chris@16
|
97 find(const Type& object, const typename interval_type_of<Type>::type& inter_val)
|
Chris@16
|
98 {
|
Chris@16
|
99 return object.find(inter_val);
|
Chris@16
|
100 }
|
Chris@16
|
101
|
Chris@16
|
102 //==============================================================================
|
Chris@16
|
103 //= Morphisms
|
Chris@16
|
104 //==============================================================================
|
Chris@16
|
105 template<class Type>
|
Chris@16
|
106 typename enable_if<is_interval_container<Type>, Type>::type&
|
Chris@16
|
107 join(Type& object)
|
Chris@16
|
108 {
|
Chris@16
|
109 typedef typename Type::interval_type interval_type;
|
Chris@16
|
110 typedef typename Type::iterator iterator;
|
Chris@16
|
111
|
Chris@16
|
112 iterator it_ = object.begin();
|
Chris@16
|
113 if(it_ == object.end())
|
Chris@16
|
114 return object;
|
Chris@16
|
115
|
Chris@16
|
116 iterator next_ = it_; next_++;
|
Chris@16
|
117
|
Chris@16
|
118 while(next_ != object.end())
|
Chris@16
|
119 {
|
Chris@16
|
120 if( segmental::is_joinable<Type>(it_, next_) )
|
Chris@16
|
121 {
|
Chris@16
|
122 iterator fst_mem = it_; // hold the first member
|
Chris@16
|
123
|
Chris@16
|
124 // Go on while touching members are found
|
Chris@16
|
125 it_++; next_++;
|
Chris@16
|
126 while( next_ != object.end()
|
Chris@16
|
127 && segmental::is_joinable<Type>(it_, next_) )
|
Chris@16
|
128 { it_++; next_++; }
|
Chris@16
|
129
|
Chris@16
|
130 // finally we arrive at the end of a sequence of joinable intervals
|
Chris@16
|
131 // and it points to the last member of that sequence
|
Chris@16
|
132 const_cast<interval_type&>(key_value<Type>(it_))
|
Chris@16
|
133 = hull(key_value<Type>(it_), key_value<Type>(fst_mem));
|
Chris@16
|
134 object.erase(fst_mem, it_);
|
Chris@16
|
135
|
Chris@16
|
136 it_++; next_=it_;
|
Chris@16
|
137 if(next_!=object.end())
|
Chris@16
|
138 next_++;
|
Chris@16
|
139 }
|
Chris@16
|
140 else { it_++; next_++; }
|
Chris@16
|
141 }
|
Chris@16
|
142 return object;
|
Chris@16
|
143 }
|
Chris@16
|
144
|
Chris@16
|
145 }} // namespace boost icl
|
Chris@16
|
146
|
Chris@16
|
147 #endif
|
Chris@16
|
148
|
Chris@16
|
149
|