interval_set.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2010-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_CONCEPT_INTERVAL_SET_HPP_JOFA_100920
  9. #define BOOST_ICL_CONCEPT_INTERVAL_SET_HPP_JOFA_100920
  10. #include <boost/icl/type_traits/is_combinable.hpp>
  11. #include <boost/icl/type_traits/interval_type_of.hpp>
  12. #include <boost/icl/detail/set_algo.hpp>
  13. #include <boost/icl/detail/interval_set_algo.hpp>
  14. #include <boost/icl/concept/interval.hpp>
  15. namespace boost{ namespace icl
  16. {
  17. //==============================================================================
  18. //= Containedness<IntervalSet>
  19. //==============================================================================
  20. //------------------------------------------------------------------------------
  21. //- bool contains(c T&, c P&) T:{S} P:{e i S} fragment_types
  22. //------------------------------------------------------------------------------
  23. template<class Type>
  24. typename enable_if<is_interval_set<Type>, bool>::type
  25. contains(const Type& super, const typename Type::element_type& element)
  26. {
  27. return !(icl::find(super, element) == super.end());
  28. }
  29. template<class Type>
  30. typename enable_if<is_interval_set<Type>, bool>::type
  31. contains(const Type& super, const typename Type::segment_type& inter_val)
  32. {
  33. typedef typename Type::const_iterator const_iterator;
  34. if(icl::is_empty(inter_val))
  35. return true;
  36. std::pair<const_iterator, const_iterator> exterior
  37. = super.equal_range(inter_val);
  38. if(exterior.first == exterior.second)
  39. return false;
  40. const_iterator last_overlap = cyclic_prior(super, exterior.second);
  41. return
  42. icl::contains(hull(*(exterior.first), *last_overlap), inter_val)
  43. && Interval_Set::is_joinable(super, exterior.first, last_overlap);
  44. }
  45. template<class Type, class OperandT>
  46. typename enable_if<has_same_concept<is_interval_set, Type, OperandT>,
  47. bool>::type
  48. contains(const Type& super, const OperandT& sub)
  49. {
  50. return Interval_Set::contains(super, sub);
  51. }
  52. //==============================================================================
  53. //= Addition<IntervalSet>
  54. //==============================================================================
  55. //------------------------------------------------------------------------------
  56. //- T& add(T&, c P&) T:{S} P:{e i} fragment_types
  57. //------------------------------------------------------------------------------
  58. template<class Type>
  59. typename enable_if<is_interval_set<Type>, Type>::type&
  60. add(Type& object, const typename Type::segment_type& operand)
  61. {
  62. return object.add(operand);
  63. }
  64. template<class Type>
  65. inline typename enable_if<is_interval_set<Type>, Type>::type&
  66. add(Type& object, const typename Type::element_type& operand)
  67. {
  68. typedef typename Type::segment_type segment_type;
  69. return icl::add(object, icl::singleton<segment_type>(operand));
  70. }
  71. //------------------------------------------------------------------------------
  72. //- T& add(T&, J, c P&) T:{S} P:{i} interval_type
  73. //------------------------------------------------------------------------------
  74. template<class Type>
  75. inline typename enable_if<is_interval_set<Type>, typename Type::iterator>::type
  76. add(Type& object, typename Type::iterator prior,
  77. const typename Type::segment_type& operand)
  78. {
  79. return object.add(prior, operand);
  80. }
  81. //==============================================================================
  82. //= Insertion<IntervalSet>
  83. //==============================================================================
  84. //------------------------------------------------------------------------------
  85. //- T& insert(T&, c P&) T:{S} P:{e i} fragment_types
  86. //------------------------------------------------------------------------------
  87. template<class Type>
  88. inline typename enable_if<is_interval_set<Type>, Type>::type&
  89. insert(Type& object, const typename Type::segment_type& operand)
  90. {
  91. return icl::add(object, operand);
  92. }
  93. template<class Type>
  94. inline typename enable_if<is_interval_set<Type>, Type>::type&
  95. insert(Type& object, const typename Type::element_type& operand)
  96. {
  97. return icl::add(object, operand);
  98. }
  99. //------------------------------------------------------------------------------
  100. //- T& insert(T&, J, c P&) T:{S} P:{i} with hint
  101. //------------------------------------------------------------------------------
  102. template<class Type>
  103. inline typename enable_if<is_interval_set<Type>, typename Type::iterator>::type
  104. insert(Type& object, typename Type::iterator prior,
  105. const typename Type::segment_type& operand)
  106. {
  107. return icl::add(object, prior, operand);
  108. }
  109. //==============================================================================
  110. //= Subtraction<IntervalSet>
  111. //==============================================================================
  112. //------------------------------------------------------------------------------
  113. //- T& subtract(T&, c P&) T:{S} P:{e i} fragment_type
  114. //------------------------------------------------------------------------------
  115. template<class Type>
  116. typename enable_if<is_interval_set<Type>, Type>::type&
  117. subtract(Type& object, const typename Type::segment_type& operand)
  118. {
  119. return object.subtract(operand);
  120. }
  121. template<class Type>
  122. inline typename enable_if<is_interval_set<Type>, Type>::type&
  123. subtract(Type& object, const typename Type::element_type& operand)
  124. {
  125. typedef typename Type::segment_type segment_type;
  126. return icl::subtract(object, icl::singleton<segment_type>(operand));
  127. }
  128. //==============================================================================
  129. //= Erasure<IntervalSet>
  130. //==============================================================================
  131. //------------------------------------------------------------------------------
  132. //- T& erase(T&, c P&) T:{S} P:{e i} fragment_types
  133. //------------------------------------------------------------------------------
  134. template<class Type>
  135. typename enable_if<is_interval_set<Type>, Type>::type&
  136. erase(Type& object, const typename Type::segment_type& minuend)
  137. {
  138. return icl::subtract(object, minuend);
  139. }
  140. template<class Type>
  141. typename enable_if<is_interval_set<Type>, Type>::type&
  142. erase(Type& object, const typename Type::element_type& minuend)
  143. {
  144. return icl::subtract(object, minuend);
  145. }
  146. //==============================================================================
  147. //= Intersection
  148. //==============================================================================
  149. //------------------------------------------------------------------------------
  150. //- void add_intersection(T&, c T&, c P&) T:{S} P:{e i} fragment_types
  151. //------------------------------------------------------------------------------
  152. template<class Type>
  153. typename enable_if<is_interval_set<Type>, void>::type
  154. add_intersection(Type& section, const Type& object,
  155. const typename Type::element_type& operand)
  156. {
  157. typedef typename Type::const_iterator const_iterator;
  158. const_iterator found = icl::find(object, operand);
  159. if(found != object.end())
  160. icl::add(section, operand);
  161. }
  162. template<class Type>
  163. typename enable_if<is_interval_set<Type>, void>::type
  164. add_intersection(Type& section, const Type& object,
  165. const typename Type::segment_type& segment)
  166. {
  167. typedef typename Type::const_iterator const_iterator;
  168. typedef typename Type::iterator iterator;
  169. typedef typename Type::interval_type interval_type;
  170. if(icl::is_empty(segment))
  171. return;
  172. std::pair<const_iterator, const_iterator> exterior
  173. = object.equal_range(segment);
  174. if(exterior.first == exterior.second)
  175. return;
  176. iterator prior_ = section.end();
  177. for(const_iterator it_=exterior.first; it_ != exterior.second; it_++)
  178. {
  179. interval_type common_interval = key_value<Type>(it_) & segment;
  180. if(!icl::is_empty(common_interval))
  181. prior_ = section.insert(prior_, common_interval);
  182. }
  183. }
  184. //==============================================================================
  185. //= Symmetric difference<IntervalSet>
  186. //==============================================================================
  187. //------------------------------------------------------------------------------
  188. //- T& flip(T&, c P&) T:{S} P:{e i S'} fragment_types
  189. //------------------------------------------------------------------------------
  190. template<class Type>
  191. typename enable_if<is_interval_set<Type>, Type>::type&
  192. flip(Type& object, const typename Type::element_type& operand)
  193. {
  194. if(icl::contains(object, operand))
  195. return object -= operand;
  196. else
  197. return object += operand;
  198. }
  199. template<class Type>
  200. typename enable_if<is_interval_set<Type>, Type>::type&
  201. flip(Type& object, const typename Type::segment_type& segment)
  202. {
  203. typedef typename Type::const_iterator const_iterator;
  204. typedef typename Type::interval_type interval_type;
  205. // That which is common shall be subtracted
  206. // That which is not shall be added
  207. // So x has to be 'complementary added' or flipped
  208. interval_type span = segment;
  209. std::pair<const_iterator, const_iterator> exterior
  210. = object.equal_range(span);
  211. const_iterator fst_ = exterior.first;
  212. const_iterator end_ = exterior.second;
  213. interval_type covered, left_over;
  214. const_iterator it_ = fst_;
  215. while(it_ != end_)
  216. {
  217. covered = *it_++;
  218. //[a ... : span
  219. // [b ... : covered
  220. //[a b) : left_over
  221. left_over = right_subtract(span, covered);
  222. icl::subtract(object, span & covered); //That which is common shall be subtracted
  223. icl::add(object, left_over); //That which is not shall be added
  224. //... d) : span
  225. //... c) : covered
  226. // [c d) : span'
  227. span = left_subtract(span, covered);
  228. }
  229. //If span is not empty here, it_ is not in the set so it_ shall be added
  230. icl::add(object, span);
  231. return object;
  232. }
  233. template<class Type, class OperandT>
  234. typename enable_if<is_concept_compatible<is_interval_set, Type, OperandT>, Type>::type&
  235. flip(Type& object, const OperandT& operand)
  236. {
  237. typedef typename OperandT::const_iterator const_iterator;
  238. if(operand.empty())
  239. return object;
  240. const_iterator common_lwb, common_upb;
  241. if(!Set::common_range(common_lwb, common_upb, operand, object))
  242. return object += operand;
  243. const_iterator it_ = operand.begin();
  244. // All elements of operand left of the common range are added
  245. while(it_ != common_lwb)
  246. icl::add(object, *it_++);
  247. // All elements of operand in the common range are symmertrically subtracted
  248. while(it_ != common_upb)
  249. icl::flip(object, *it_++);
  250. // All elements of operand right of the common range are added
  251. while(it_ != operand.end())
  252. icl::add(object, *it_++);
  253. return object;
  254. }
  255. //==============================================================================
  256. //= Set selection
  257. //==============================================================================
  258. template<class Type>
  259. typename enable_if<is_interval_set<Type>, Type>::type&
  260. domain(Type& dom, const Type& object)
  261. {
  262. typedef typename Type::const_iterator const_iterator;
  263. typedef typename Type::iterator iterator;
  264. dom.clear();
  265. const_iterator it_ = object.begin();
  266. iterator prior_ = dom.end();
  267. while(it_ != object.end())
  268. prior_ = icl::insert(dom, prior_, *it_++);
  269. return dom;
  270. }
  271. template<class Type>
  272. typename enable_if<is_interval_set<Type>, Type>::type&
  273. between(Type& in_between, const Type& object)
  274. {
  275. typedef typename Type::const_iterator const_iterator;
  276. typedef typename Type::iterator iterator;
  277. in_between.clear();
  278. const_iterator it_ = object.begin(), pred_;
  279. iterator prior_ = in_between.end();
  280. if(it_ != object.end())
  281. pred_ = it_++;
  282. while(it_ != object.end())
  283. prior_ = icl::insert(in_between, prior_,
  284. icl::between(*pred_++, *it_++));
  285. return in_between;
  286. }
  287. //==============================================================================
  288. //= Streaming
  289. //==============================================================================
  290. template<class CharType, class CharTraits, class Type>
  291. typename enable_if<is_interval_set<Type>,
  292. std::basic_ostream<CharType, CharTraits> >::type&
  293. operator << (std::basic_ostream<CharType, CharTraits>& stream, const Type& object)
  294. {
  295. stream << "{";
  296. ICL_const_FORALL(typename Type, it_, object)
  297. stream << (*it_);
  298. return stream << "}";
  299. }
  300. }} // namespace boost icl
  301. #endif