interval.hpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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_HPP_JOFA_100323
  9. #define BOOST_ICL_CONCEPT_INTERVAL_HPP_JOFA_100323
  10. #include <boost/assert.hpp>
  11. #include <boost/utility/enable_if.hpp>
  12. #include <boost/mpl/and.hpp>
  13. #include <boost/mpl/or.hpp>
  14. #include <boost/mpl/not.hpp>
  15. #include <boost/icl/detail/design_config.hpp>
  16. #include <boost/icl/type_traits/unit_element.hpp>
  17. #include <boost/icl/type_traits/identity_element.hpp>
  18. #include <boost/icl/type_traits/infinity.hpp>
  19. #include <boost/icl/type_traits/succ_pred.hpp>
  20. #include <boost/icl/type_traits/is_numeric.hpp>
  21. #include <boost/icl/type_traits/is_discrete.hpp>
  22. #include <boost/icl/type_traits/is_continuous.hpp>
  23. #include <boost/icl/type_traits/is_asymmetric_interval.hpp>
  24. #include <boost/icl/type_traits/is_discrete_interval.hpp>
  25. #include <boost/icl/type_traits/is_continuous_interval.hpp>
  26. #include <boost/icl/concept/interval_bounds.hpp>
  27. #include <boost/icl/interval_traits.hpp>
  28. #include <boost/icl/dynamic_interval_traits.hpp>
  29. #include <algorithm>
  30. namespace boost{namespace icl
  31. {
  32. //==============================================================================
  33. //= Ordering
  34. //==============================================================================
  35. template<class Type>
  36. inline typename enable_if<is_interval<Type>, bool>::type
  37. domain_less(const typename interval_traits<Type>::domain_type& left,
  38. const typename interval_traits<Type>::domain_type& right)
  39. {
  40. return typename interval_traits<Type>::domain_compare()(left, right);
  41. }
  42. template<class Type>
  43. inline typename enable_if<is_interval<Type>, bool>::type
  44. domain_less_equal(const typename interval_traits<Type>::domain_type& left,
  45. const typename interval_traits<Type>::domain_type& right)
  46. {
  47. return !(typename interval_traits<Type>::domain_compare()(right, left));
  48. }
  49. template<class Type>
  50. inline typename enable_if<is_interval<Type>, bool>::type
  51. domain_equal(const typename interval_traits<Type>::domain_type& left,
  52. const typename interval_traits<Type>::domain_type& right)
  53. {
  54. typedef typename interval_traits<Type>::domain_compare domain_compare;
  55. return !(domain_compare()(left, right)) && !(domain_compare()(right, left));
  56. }
  57. template<class Type>
  58. inline typename enable_if< is_interval<Type>
  59. , typename interval_traits<Type>::domain_type>::type
  60. domain_next(const typename interval_traits<Type>::domain_type value)
  61. {
  62. typedef typename interval_traits<Type>::domain_type domain_type;
  63. typedef typename interval_traits<Type>::domain_compare domain_compare;
  64. return icl::successor<domain_type,domain_compare>::apply(value);
  65. }
  66. template<class Type>
  67. inline typename enable_if< is_interval<Type>
  68. , typename interval_traits<Type>::domain_type>::type
  69. domain_prior(const typename interval_traits<Type>::domain_type value)
  70. {
  71. typedef typename interval_traits<Type>::domain_type domain_type;
  72. typedef typename interval_traits<Type>::domain_compare domain_compare;
  73. return icl::predecessor<domain_type,domain_compare>::apply(value);
  74. }
  75. //==============================================================================
  76. //= Construct<Interval> singleton
  77. //==============================================================================
  78. template<class Type>
  79. typename enable_if
  80. <
  81. mpl::and_< is_static_right_open<Type>
  82. , is_discrete<typename interval_traits<Type>::domain_type> >
  83. , Type
  84. >::type
  85. singleton(const typename interval_traits<Type>::domain_type& value)
  86. {
  87. //ASSERT: This always creates an interval with exactly one element
  88. return interval_traits<Type>::construct(value, domain_next<Type>(value));
  89. }
  90. template<class Type>
  91. typename enable_if
  92. <
  93. mpl::and_< is_static_left_open<Type>
  94. , is_discrete<typename interval_traits<Type>::domain_type> >
  95. , Type
  96. >::type
  97. singleton(const typename interval_traits<Type>::domain_type& value)
  98. {
  99. //ASSERT: This always creates an interval with exactly one element
  100. typedef typename interval_traits<Type>::domain_type domain_type;
  101. typedef typename interval_traits<Type>::domain_compare domain_compare;
  102. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  103. ::is_less_than(value) ));
  104. return interval_traits<Type>::construct(domain_prior<Type>(value), value);
  105. }
  106. template<class Type>
  107. typename enable_if<is_discrete_static_open<Type>, Type>::type
  108. singleton(const typename interval_traits<Type>::domain_type& value)
  109. {
  110. //ASSERT: This always creates an interval with exactly one element
  111. typedef typename interval_traits<Type>::domain_type domain_type;
  112. typedef typename interval_traits<Type>::domain_compare domain_compare;
  113. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  114. ::is_less_than(value)));
  115. return interval_traits<Type>::construct( domain_prior<Type>(value)
  116. , domain_next<Type>(value));
  117. }
  118. template<class Type>
  119. typename enable_if<is_discrete_static_closed<Type>, Type>::type
  120. singleton(const typename interval_traits<Type>::domain_type& value)
  121. {
  122. //ASSERT: This always creates an interval with exactly one element
  123. return interval_traits<Type>::construct(value, value);
  124. }
  125. template<class Type>
  126. typename enable_if<has_dynamic_bounds<Type>, Type>::type
  127. singleton(const typename interval_traits<Type>::domain_type& value)
  128. {
  129. return dynamic_interval_traits<Type>::construct(value, value, interval_bounds::closed());
  130. }
  131. namespace detail
  132. {
  133. //==============================================================================
  134. //= Construct<Interval> unit_trail == generalized singleton
  135. // The smallest interval on an incrementable (and decrementable) type that can
  136. // be constructed using ++ and -- and such that it contains a given value.
  137. // If 'Type' is discrete, 'unit_trail' and 'singleton' are identical. So we
  138. // can view 'unit_trail' as a generalized singleton for static intervals of
  139. // continuous types.
  140. //==============================================================================
  141. template<class Type>
  142. typename enable_if
  143. <
  144. mpl::and_< is_static_right_open<Type>
  145. , boost::detail::is_incrementable<typename interval_traits<Type>::domain_type> >
  146. , Type
  147. >::type
  148. unit_trail(const typename interval_traits<Type>::domain_type& value)
  149. {
  150. return interval_traits<Type>::construct(value, domain_next<Type>(value));
  151. }
  152. template<class Type>
  153. typename enable_if
  154. <
  155. mpl::and_< is_static_left_open<Type>
  156. , boost::detail::is_incrementable<typename interval_traits<Type>::domain_type> >
  157. , Type
  158. >::type
  159. unit_trail(const typename interval_traits<Type>::domain_type& value)
  160. {
  161. typedef typename interval_traits<Type>::domain_type domain_type;
  162. typedef typename interval_traits<Type>::domain_compare domain_compare;
  163. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  164. ::is_less_than(value) ));
  165. return interval_traits<Type>::construct(domain_prior<Type>(value), value);
  166. }
  167. template<class Type>
  168. typename enable_if
  169. <
  170. mpl::and_< is_static_open<Type>
  171. , is_discrete<typename interval_traits<Type>::domain_type> >
  172. , Type
  173. >::type
  174. unit_trail(const typename interval_traits<Type>::domain_type& value)
  175. {
  176. typedef typename interval_traits<Type>::domain_type domain_type;
  177. typedef typename interval_traits<Type>::domain_compare domain_compare;
  178. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  179. ::is_less_than(value)));
  180. return interval_traits<Type>::construct( domain_prior<Type>(value)
  181. , domain_next<Type>(value));
  182. }
  183. template<class Type>
  184. typename enable_if
  185. <
  186. mpl::and_< is_static_closed<Type>
  187. , is_discrete<typename interval_traits<Type>::domain_type> >
  188. , Type
  189. >::type
  190. unit_trail(const typename interval_traits<Type>::domain_type& value)
  191. {
  192. return interval_traits<Type>::construct(value, value);
  193. }
  194. //NOTE: statically bounded closed or open intervals of continuous domain types
  195. // are NOT supported by ICL. They can not be used with interval containers
  196. // consistently.
  197. template<class Type>
  198. typename enable_if<has_dynamic_bounds<Type>, Type>::type
  199. unit_trail(const typename interval_traits<Type>::domain_type& value)
  200. {
  201. return dynamic_interval_traits<Type>::construct(value, value, interval_bounds::closed());
  202. }
  203. } //namespace detail
  204. //==============================================================================
  205. //= Construct<Interval> multon
  206. //==============================================================================
  207. template<class Type>
  208. typename enable_if<has_static_bounds<Type>, Type>::type
  209. construct(const typename interval_traits<Type>::domain_type& low,
  210. const typename interval_traits<Type>::domain_type& up )
  211. {
  212. return interval_traits<Type>::construct(low, up);
  213. }
  214. template<class Type>
  215. typename enable_if<has_dynamic_bounds<Type>, Type>::type
  216. construct(const typename interval_traits<Type>::domain_type& low,
  217. const typename interval_traits<Type>::domain_type& up,
  218. interval_bounds bounds = interval_bounds::right_open())
  219. {
  220. return dynamic_interval_traits<Type>::construct(low, up, bounds);
  221. }
  222. //- construct form bounded values ----------------------------------------------
  223. template<class Type>
  224. typename enable_if<has_dynamic_bounds<Type>, Type>::type
  225. construct(const typename Type::bounded_domain_type& low,
  226. const typename Type::bounded_domain_type& up)
  227. {
  228. return dynamic_interval_traits<Type>::construct_bounded(low, up);
  229. }
  230. template<class Type>
  231. typename enable_if<is_interval<Type>, Type>::type
  232. span(const typename interval_traits<Type>::domain_type& left,
  233. const typename interval_traits<Type>::domain_type& right)
  234. {
  235. typedef typename interval_traits<Type>::domain_compare domain_compare;
  236. if(domain_compare()(left,right))
  237. return construct<Type>(left, right);
  238. else
  239. return construct<Type>(right, left);
  240. }
  241. //==============================================================================
  242. template<class Type>
  243. typename enable_if<is_static_right_open<Type>, Type>::type
  244. hull(const typename interval_traits<Type>::domain_type& left,
  245. const typename interval_traits<Type>::domain_type& right)
  246. {
  247. typedef typename interval_traits<Type>::domain_compare domain_compare;
  248. if(domain_compare()(left,right))
  249. return construct<Type>(left, domain_next<Type>(right));
  250. else
  251. return construct<Type>(right, domain_next<Type>(left));
  252. }
  253. template<class Type>
  254. typename enable_if<is_static_left_open<Type>, Type>::type
  255. hull(const typename interval_traits<Type>::domain_type& left,
  256. const typename interval_traits<Type>::domain_type& right)
  257. {
  258. typedef typename interval_traits<Type>::domain_type domain_type;
  259. typedef typename interval_traits<Type>::domain_compare domain_compare;
  260. if(domain_compare()(left,right))
  261. {
  262. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  263. ::is_less_than(left) ));
  264. return construct<Type>(domain_prior<Type>(left), right);
  265. }
  266. else
  267. {
  268. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  269. ::is_less_than(right) ));
  270. return construct<Type>(domain_prior<Type>(right), left);
  271. }
  272. }
  273. template<class Type>
  274. typename enable_if<is_static_closed<Type>, Type>::type
  275. hull(const typename interval_traits<Type>::domain_type& left,
  276. const typename interval_traits<Type>::domain_type& right)
  277. {
  278. typedef typename interval_traits<Type>::domain_compare domain_compare;
  279. if(domain_compare()(left,right))
  280. return construct<Type>(left, right);
  281. else
  282. return construct<Type>(right, left);
  283. }
  284. template<class Type>
  285. typename enable_if<is_static_open<Type>, Type>::type
  286. hull(const typename interval_traits<Type>::domain_type& left,
  287. const typename interval_traits<Type>::domain_type& right)
  288. {
  289. typedef typename interval_traits<Type>::domain_type domain_type;
  290. typedef typename interval_traits<Type>::domain_compare domain_compare;
  291. if(domain_compare()(left,right))
  292. {
  293. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  294. ::is_less_than(left) ));
  295. return construct<Type>( domain_prior<Type>(left)
  296. , domain_next<Type>(right));
  297. }
  298. else
  299. {
  300. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  301. ::is_less_than(right) ));
  302. return construct<Type>( domain_prior<Type>(right)
  303. , domain_next<Type>(left));
  304. }
  305. }
  306. template<class Type>
  307. typename enable_if<has_dynamic_bounds<Type>, Type>::type
  308. hull(const typename interval_traits<Type>::domain_type& left,
  309. const typename interval_traits<Type>::domain_type& right)
  310. {
  311. typedef typename interval_traits<Type>::domain_compare domain_compare;
  312. if(domain_compare()(left,right))
  313. return construct<Type>(left, right, interval_bounds::closed());
  314. else
  315. return construct<Type>(right, left, interval_bounds::closed());
  316. }
  317. //==============================================================================
  318. //= Selection
  319. //==============================================================================
  320. template<class Type>
  321. inline typename enable_if<is_interval<Type>,
  322. typename interval_traits<Type>::domain_type>::type
  323. lower(const Type& object)
  324. {
  325. return interval_traits<Type>::lower(object);
  326. }
  327. template<class Type>
  328. inline typename enable_if<is_interval<Type>,
  329. typename interval_traits<Type>::domain_type>::type
  330. upper(const Type& object)
  331. {
  332. return interval_traits<Type>::upper(object);
  333. }
  334. //- first ----------------------------------------------------------------------
  335. template<class Type>
  336. inline typename
  337. enable_if< mpl::or_<is_static_right_open<Type>, is_static_closed<Type> >
  338. , typename interval_traits<Type>::domain_type>::type
  339. first(const Type& object)
  340. {
  341. return lower(object);
  342. }
  343. template<class Type>
  344. inline typename
  345. enable_if< mpl::and_< mpl::or_<is_static_left_open<Type>, is_static_open<Type> >
  346. , is_discrete<typename interval_traits<Type>::domain_type> >
  347. , typename interval_traits<Type>::domain_type>::type
  348. first(const Type& object)
  349. {
  350. return domain_next<Type>(lower(object));
  351. }
  352. template<class Type>
  353. inline typename enable_if<is_discrete_interval<Type>,
  354. typename interval_traits<Type>::domain_type>::type
  355. first(const Type& object)
  356. {
  357. return is_left_closed(object.bounds()) ?
  358. lower(object) :
  359. domain_next<Type>(lower(object));
  360. }
  361. //- last -----------------------------------------------------------------------
  362. template<class Type>
  363. inline typename
  364. enable_if< mpl::or_<is_static_left_open<Type>, is_static_closed<Type> >
  365. , typename interval_traits<Type>::domain_type>::type
  366. last(const Type& object)
  367. {
  368. return upper(object);
  369. }
  370. template<class Type>
  371. inline typename
  372. enable_if< mpl::and_< mpl::or_<is_static_right_open<Type>, is_static_open<Type> >
  373. , is_discrete<typename interval_traits<Type>::domain_type> >
  374. , typename interval_traits<Type>::domain_type>::type
  375. last(const Type& object)
  376. {
  377. typedef typename interval_traits<Type>::domain_type domain_type;
  378. typedef typename interval_traits<Type>::domain_compare domain_compare;
  379. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  380. ::is_less_than(upper(object)) ));
  381. return domain_prior<Type>(upper(object));
  382. }
  383. template<class Type>
  384. inline typename enable_if<is_discrete_interval<Type>,
  385. typename interval_traits<Type>::domain_type>::type
  386. last(const Type& object)
  387. {
  388. typedef typename interval_traits<Type>::domain_type domain_type;
  389. typedef typename interval_traits<Type>::domain_compare domain_compare;
  390. BOOST_ASSERT((numeric_minimum<domain_type, domain_compare, is_numeric<domain_type>::value>
  391. ::is_less_than_or(upper(object), is_right_closed(object.bounds())) ));
  392. return is_right_closed(object.bounds()) ?
  393. upper(object) :
  394. domain_prior<Type>(upper(object));
  395. }
  396. //- last_next ------------------------------------------------------------------
  397. template<class Type>
  398. inline typename
  399. enable_if< mpl::and_< mpl::or_<is_static_left_open<Type>, is_static_closed<Type> >
  400. , is_discrete<typename interval_traits<Type>::domain_type> >
  401. , typename interval_traits<Type>::domain_type>::type
  402. last_next(const Type& object)
  403. {
  404. return domain_next<Type>(upper(object));
  405. }
  406. template<class Type>
  407. inline typename
  408. enable_if< mpl::and_< mpl::or_<is_static_right_open<Type>, is_static_open<Type> >
  409. , is_discrete<typename interval_traits<Type>::domain_type> >
  410. , typename interval_traits<Type>::domain_type>::type
  411. last_next(const Type& object)
  412. {
  413. //CL typedef typename interval_traits<Type>::domain_type domain_type;
  414. return upper(object); // NOTE: last_next is implemented to avoid calling pred(object)
  415. } // For unsigned integral types this may cause underflow.
  416. template<class Type>
  417. inline typename enable_if<is_discrete_interval<Type>,
  418. typename interval_traits<Type>::domain_type>::type
  419. last_next(const Type& object)
  420. {
  421. return is_right_closed(object.bounds()) ?
  422. domain_next<Type>(upper(object)):
  423. upper(object) ;
  424. }
  425. //------------------------------------------------------------------------------
  426. template<class Type>
  427. typename enable_if<has_dynamic_bounds<Type>,
  428. typename Type::bounded_domain_type>::type
  429. bounded_lower(const Type& object)
  430. {
  431. return typename
  432. Type::bounded_domain_type(lower(object), object.bounds().left());
  433. }
  434. template<class Type>
  435. typename enable_if<has_dynamic_bounds<Type>,
  436. typename Type::bounded_domain_type>::type
  437. reverse_bounded_lower(const Type& object)
  438. {
  439. return typename
  440. Type::bounded_domain_type(lower(object),
  441. object.bounds().reverse_left());
  442. }
  443. template<class Type>
  444. typename enable_if<has_dynamic_bounds<Type>,
  445. typename Type::bounded_domain_type>::type
  446. bounded_upper(const Type& object)
  447. {
  448. return typename
  449. Type::bounded_domain_type(upper(object),
  450. object.bounds().right());
  451. }
  452. template<class Type>
  453. typename enable_if<has_dynamic_bounds<Type>,
  454. typename Type::bounded_domain_type>::type
  455. reverse_bounded_upper(const Type& object)
  456. {
  457. return typename
  458. Type::bounded_domain_type(upper(object),
  459. object.bounds().reverse_right());
  460. }
  461. //- bounds ---------------------------------------------------------------------
  462. template<class Type>
  463. inline typename enable_if<has_dynamic_bounds<Type>, interval_bounds>::type
  464. bounds(const Type& object)
  465. {
  466. return object.bounds();
  467. }
  468. template<class Type>
  469. inline typename enable_if<has_static_bounds<Type>, interval_bounds>::type
  470. bounds(const Type&)
  471. {
  472. return interval_bounds(interval_bound_type<Type>::value);
  473. }
  474. //==============================================================================
  475. //= Emptieness
  476. //==============================================================================
  477. /** Is the interval empty? */
  478. template<class Type>
  479. typename boost::enable_if<is_asymmetric_interval<Type>, bool>::type
  480. is_empty(const Type& object)
  481. {
  482. return domain_less_equal<Type>(upper(object), lower(object));
  483. }
  484. template<class Type>
  485. typename boost::enable_if<is_static_closed<Type>, bool>::type
  486. is_empty(const Type& object)
  487. {
  488. return domain_less<Type>(upper(object), lower(object));
  489. }
  490. template<class Type>
  491. typename boost::enable_if<is_static_open<Type>, bool>::type
  492. is_empty(const Type& object)
  493. {
  494. return domain_less_equal<Type>(upper(object), lower(object) )
  495. || domain_less_equal<Type>(upper(object), domain_next<Type>(lower(object)));
  496. }
  497. template<class Type>
  498. typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  499. is_empty(const Type& object)
  500. {
  501. if(object.bounds() == interval_bounds::closed())
  502. return domain_less<Type>(upper(object), lower(object));
  503. else if(object.bounds() == interval_bounds::open())
  504. return domain_less_equal<Type>(upper(object), lower(object) )
  505. || domain_less_equal<Type>(upper(object), domain_next<Type>(lower(object)));
  506. else
  507. return domain_less_equal<Type>(upper(object), lower(object));
  508. }
  509. template<class Type>
  510. typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  511. is_empty(const Type& object)
  512. {
  513. return domain_less<Type>(upper(object), lower(object))
  514. || ( domain_equal<Type>(upper(object), lower(object))
  515. && object.bounds() != interval_bounds::closed() );
  516. }
  517. //==============================================================================
  518. //= Equivalences and Orderings
  519. //==============================================================================
  520. //- exclusive_less -------------------------------------------------------------
  521. /** Maximal element of <tt>left</tt> is less than the minimal element of
  522. <tt>right</tt> */
  523. template<class Type>
  524. inline typename boost::enable_if<is_asymmetric_interval<Type>, bool>::type
  525. exclusive_less(const Type& left, const Type& right)
  526. {
  527. return icl::is_empty(left) || icl::is_empty(right)
  528. || domain_less_equal<Type>(upper(left), lower(right));
  529. }
  530. template<class Type>
  531. inline typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  532. exclusive_less(const Type& left, const Type& right)
  533. {
  534. return icl::is_empty(left) || icl::is_empty(right)
  535. || domain_less<Type>(last(left), first(right));
  536. }
  537. template<class Type>
  538. inline typename boost::
  539. enable_if<has_symmetric_bounds<Type>, bool>::type
  540. exclusive_less(const Type& left, const Type& right)
  541. {
  542. return icl::is_empty(left) || icl::is_empty(right)
  543. || domain_less<Type>(last(left), first(right));
  544. }
  545. template<class Type>
  546. inline typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  547. exclusive_less(const Type& left, const Type& right)
  548. {
  549. return icl::is_empty(left) || icl::is_empty(right)
  550. || domain_less<Type>(upper(left), lower(right))
  551. || ( domain_equal<Type>(upper(left), lower(right))
  552. && inner_bounds(left,right) != interval_bounds::open() );
  553. }
  554. //------------------------------------------------------------------------------
  555. template<class Type>
  556. typename boost::enable_if<has_static_bounds<Type>, bool>::type
  557. lower_less(const Type& left, const Type& right)
  558. {
  559. return domain_less<Type>(lower(left), lower(right));
  560. }
  561. template<class Type>
  562. typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  563. lower_less(const Type& left, const Type& right)
  564. {
  565. return domain_less<Type>(first(left), first(right));
  566. }
  567. template<class Type>
  568. typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  569. lower_less(const Type& left, const Type& right)
  570. {
  571. if(left_bounds(left,right) == interval_bounds::right_open()) //'[(' == 10
  572. return domain_less_equal<Type>(lower(left), lower(right));
  573. else
  574. return domain_less<Type>(lower(left), lower(right));
  575. }
  576. //------------------------------------------------------------------------------
  577. template<class Type>
  578. typename boost::enable_if<has_static_bounds<Type>, bool>::type
  579. upper_less(const Type& left, const Type& right)
  580. {
  581. return domain_less<Type>(upper(left), upper(right));
  582. }
  583. template<class Type>
  584. typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  585. upper_less(const Type& left, const Type& right)
  586. {
  587. return domain_less<Type>(last(left), last(right));
  588. }
  589. template<class Type>
  590. typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  591. upper_less(const Type& left, const Type& right)
  592. {
  593. if(right_bounds(left,right) == interval_bounds::left_open())
  594. return domain_less_equal<Type>(upper(left), upper(right));
  595. else
  596. return domain_less<Type>(upper(left), upper(right));
  597. }
  598. //------------------------------------------------------------------------------
  599. template<class Type>
  600. typename boost::enable_if<has_dynamic_bounds<Type>,
  601. typename Type::bounded_domain_type >::type
  602. lower_min(const Type& left, const Type& right)
  603. {
  604. return lower_less(left, right) ? bounded_lower(left) : bounded_lower(right);
  605. }
  606. //------------------------------------------------------------------------------
  607. template<class Type>
  608. typename boost::enable_if<has_dynamic_bounds<Type>,
  609. typename Type::bounded_domain_type >::type
  610. lower_max(const Type& left, const Type& right)
  611. {
  612. return lower_less(left, right) ? bounded_lower(right) : bounded_lower(left);
  613. }
  614. //------------------------------------------------------------------------------
  615. template<class Type>
  616. typename boost::enable_if<has_dynamic_bounds<Type>,
  617. typename Type::bounded_domain_type >::type
  618. upper_max(const Type& left, const Type& right)
  619. {
  620. return upper_less(left, right) ? bounded_upper(right) : bounded_upper(left);
  621. }
  622. //------------------------------------------------------------------------------
  623. template<class Type>
  624. typename boost::enable_if<has_dynamic_bounds<Type>,
  625. typename Type::bounded_domain_type >::type
  626. upper_min(const Type& left, const Type& right)
  627. {
  628. return upper_less(left, right) ? bounded_upper(left) : bounded_upper(right);
  629. }
  630. //------------------------------------------------------------------------------
  631. template<class Type>
  632. typename boost::enable_if<is_asymmetric_interval<Type>, bool>::type
  633. lower_equal(const Type& left, const Type& right)
  634. {
  635. return domain_equal<Type>(lower(left), lower(right));
  636. }
  637. template<class Type>
  638. typename boost::enable_if<has_symmetric_bounds<Type>, bool>::type
  639. lower_equal(const Type& left, const Type& right)
  640. {
  641. return domain_equal<Type>(first(left), first(right));
  642. }
  643. template<class Type>
  644. typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  645. lower_equal(const Type& left, const Type& right)
  646. {
  647. return domain_equal<Type>(first(left), first(right));
  648. }
  649. template<class Type>
  650. typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  651. lower_equal(const Type& left, const Type& right)
  652. {
  653. return (left.bounds().left()==right.bounds().left())
  654. && domain_equal<Type>(lower(left), lower(right));
  655. }
  656. //------------------------------------------------------------------------------
  657. template<class Type>
  658. typename boost::enable_if<is_asymmetric_interval<Type>, bool>::type
  659. upper_equal(const Type& left, const Type& right)
  660. {
  661. return domain_equal<Type>(upper(left), upper(right));
  662. }
  663. template<class Type>
  664. typename boost::enable_if<has_symmetric_bounds<Type>, bool>::type
  665. upper_equal(const Type& left, const Type& right)
  666. {
  667. return domain_equal<Type>(last(left), last(right));
  668. }
  669. template<class Type>
  670. typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  671. upper_equal(const Type& left, const Type& right)
  672. {
  673. return domain_equal<Type>(last(left), last(right));
  674. }
  675. template<class Type>
  676. typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  677. upper_equal(const Type& left, const Type& right)
  678. {
  679. return (left.bounds().right()==right.bounds().right())
  680. && domain_equal<Type>(upper(left), upper(right));
  681. }
  682. //------------------------------------------------------------------------------
  683. template<class Type>
  684. typename boost::enable_if<is_interval<Type>, bool>::type
  685. lower_less_equal(const Type& left, const Type& right)
  686. {
  687. return lower_less(left,right) || lower_equal(left,right);
  688. }
  689. template<class Type>
  690. typename boost::enable_if<is_interval<Type>, bool>::type
  691. upper_less_equal(const Type& left, const Type& right)
  692. {
  693. return upper_less(left,right) || upper_equal(left,right);
  694. }
  695. //==============================================================================
  696. //= Orderings, containedness (non empty)
  697. //==============================================================================
  698. namespace non_empty
  699. {
  700. template<class Type>
  701. inline typename boost::enable_if<is_asymmetric_interval<Type>, bool>::type
  702. exclusive_less(const Type& left, const Type& right)
  703. {
  704. BOOST_ASSERT(!(icl::is_empty(left) || icl::is_empty(right)));
  705. return domain_less_equal<Type>(upper(left), lower(right));
  706. }
  707. template<class Type>
  708. inline typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  709. exclusive_less(const Type& left, const Type& right)
  710. {
  711. BOOST_ASSERT(!(icl::is_empty(left) || icl::is_empty(right)));
  712. return domain_less<Type>(last(left), first(right));
  713. }
  714. template<class Type>
  715. inline typename boost::
  716. enable_if<has_symmetric_bounds<Type>, bool>::type
  717. exclusive_less(const Type& left, const Type& right)
  718. {
  719. BOOST_ASSERT(!(icl::is_empty(left) || icl::is_empty(right)));
  720. return domain_less<Type>(last(left), first(right));
  721. }
  722. template<class Type>
  723. inline typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  724. exclusive_less(const Type& left, const Type& right)
  725. {
  726. BOOST_ASSERT(!(icl::is_empty(left) || icl::is_empty(right)));
  727. return domain_less <Type>(upper(left), lower(right))
  728. || ( domain_equal<Type>(upper(left), lower(right))
  729. && inner_bounds(left,right) != interval_bounds::open() );
  730. }
  731. template<class Type>
  732. inline typename boost::enable_if<is_interval<Type>, bool>::type
  733. contains(const Type& super, const Type& sub)
  734. {
  735. return lower_less_equal(super,sub) && upper_less_equal(sub,super);
  736. }
  737. } //namespace non_empty
  738. //- contains -------------------------------------------------------------------
  739. template<class Type>
  740. inline typename boost::enable_if<is_interval<Type>, bool>::type
  741. contains(const Type& super, const Type& sub)
  742. {
  743. return icl::is_empty(sub) || non_empty::contains(super, sub);
  744. }
  745. template<class Type>
  746. typename boost::enable_if<is_discrete_static<Type>, bool>::type
  747. contains(const Type& super, const typename interval_traits<Type>::domain_type& element)
  748. {
  749. return domain_less_equal<Type>(icl::first(super), element )
  750. && domain_less_equal<Type>( element, icl::last(super));
  751. }
  752. template<class Type>
  753. typename boost::enable_if<is_continuous_left_open<Type>, bool>::type
  754. contains(const Type& super, const typename interval_traits<Type>::domain_type& element)
  755. {
  756. return domain_less <Type>(icl::lower(super), element )
  757. && domain_less_equal<Type>( element, icl::upper(super));
  758. }
  759. template<class Type>
  760. typename boost::enable_if<is_continuous_right_open<Type>, bool>::type
  761. contains(const Type& super, const typename interval_traits<Type>::domain_type& element)
  762. {
  763. return domain_less_equal<Type>(icl::lower(super), element )
  764. && domain_less <Type>( element, icl::upper(super));
  765. }
  766. template<class Type>
  767. typename boost::enable_if<has_dynamic_bounds<Type>, bool>::type
  768. contains(const Type& super, const typename interval_traits<Type>::domain_type& element)
  769. {
  770. return
  771. (is_left_closed(super.bounds())
  772. ? domain_less_equal<Type>(lower(super), element)
  773. : domain_less<Type>(lower(super), element))
  774. &&
  775. (is_right_closed(super.bounds())
  776. ? domain_less_equal<Type>(element, upper(super))
  777. : domain_less<Type>(element, upper(super)));
  778. }
  779. //- within ---------------------------------------------------------------------
  780. template<class Type>
  781. inline typename boost::enable_if<is_interval<Type>, bool>::type
  782. within(const Type& sub, const Type& super)
  783. {
  784. return contains(super,sub);
  785. }
  786. //- operator == ----------------------------------------------------------------
  787. template<class Type>
  788. typename boost::enable_if<is_interval<Type>, bool>::type
  789. operator == (const Type& left, const Type& right)
  790. {
  791. return (icl::is_empty(left) && icl::is_empty(right))
  792. || (lower_equal(left,right) && upper_equal(left,right));
  793. }
  794. template<class Type>
  795. typename boost::enable_if<is_interval<Type>, bool>::type
  796. operator != (const Type& left, const Type& right)
  797. {
  798. return !(left == right);
  799. }
  800. //- operator < -----------------------------------------------------------------
  801. template<class Type>
  802. typename boost::enable_if<is_interval<Type>, bool>::type
  803. operator < (const Type& left, const Type& right)
  804. {
  805. if(icl::is_empty(left))
  806. return !icl::is_empty(right);
  807. else
  808. return lower_less(left,right)
  809. || (lower_equal(left,right) && upper_less(left,right));
  810. }
  811. template<class Type>
  812. inline typename boost::enable_if<is_interval<Type>, bool>::type
  813. operator > (const Type& left, const Type& right)
  814. {
  815. return right < left;
  816. }
  817. //------------------------------------------------------------------------------
  818. template<class Type>
  819. typename boost::enable_if<is_asymmetric_interval<Type>, bool>::type
  820. touches(const Type& left, const Type& right)
  821. {
  822. return domain_equal<Type>(upper(left), lower(right));
  823. }
  824. template<class Type>
  825. typename boost::enable_if<has_symmetric_bounds<Type>, bool>::type
  826. touches(const Type& left, const Type& right)
  827. {
  828. return domain_equal<Type>(last_next(left), first(right));
  829. }
  830. template<class Type>
  831. typename boost::enable_if<is_discrete_interval<Type>, bool>::type
  832. touches(const Type& left, const Type& right)
  833. {
  834. return domain_equal<Type>(domain_next<Type>(last(left)), first(right));
  835. }
  836. template<class Type>
  837. typename boost::enable_if<is_continuous_interval<Type>, bool>::type
  838. touches(const Type& left, const Type& right)
  839. {
  840. return is_complementary(inner_bounds(left,right))
  841. && domain_equal<Type>(upper(left), lower(right));
  842. }
  843. //==============================================================================
  844. //= Size
  845. //==============================================================================
  846. //- cardinality ----------------------------------------------------------------
  847. template<class Type>
  848. typename boost::enable_if<is_continuous_interval<Type>,
  849. typename size_type_of<interval_traits<Type> >::type>::type
  850. cardinality(const Type& object)
  851. {
  852. typedef typename size_type_of<interval_traits<Type> >::type SizeT;
  853. if(icl::is_empty(object))
  854. return icl::identity_element<SizeT>::value();
  855. else if( object.bounds() == interval_bounds::closed()
  856. && domain_equal<Type>(lower(object), upper(object)))
  857. return icl::unit_element<SizeT>::value();
  858. else
  859. return icl::infinity<SizeT>::value();
  860. }
  861. template<class Type>
  862. typename boost::enable_if<is_discrete_interval<Type>,
  863. typename size_type_of<interval_traits<Type> >::type>::type
  864. cardinality(const Type& object)
  865. {
  866. typedef typename size_type_of<interval_traits<Type> >::type SizeT;
  867. return icl::is_empty(object) ? identity_element<SizeT>::value()
  868. : static_cast<SizeT>(last_next(object) - first(object));
  869. }
  870. template<class Type>
  871. typename boost::enable_if<is_continuous_asymmetric<Type>,
  872. typename size_type_of<interval_traits<Type> >::type>::type
  873. cardinality(const Type& object)
  874. {
  875. typedef typename size_type_of<interval_traits<Type> >::type SizeT;
  876. if(icl::is_empty(object))
  877. return icl::identity_element<SizeT>::value();
  878. else
  879. return icl::infinity<SizeT>::value();
  880. }
  881. template<class Type>
  882. typename boost::enable_if<is_discrete_asymmetric<Type>,
  883. typename size_type_of<interval_traits<Type> >::type>::type
  884. cardinality(const Type& object)
  885. {
  886. typedef typename size_type_of<interval_traits<Type> >::type SizeT;
  887. return icl::is_empty(object) ? identity_element<SizeT>::value()
  888. : static_cast<SizeT>(last_next(object) - first(object));
  889. }
  890. template<class Type>
  891. typename boost::enable_if<has_symmetric_bounds<Type>,
  892. typename size_type_of<interval_traits<Type> >::type>::type
  893. cardinality(const Type& object)
  894. {
  895. typedef typename size_type_of<interval_traits<Type> >::type SizeT;
  896. return icl::is_empty(object) ? identity_element<SizeT>::value()
  897. : static_cast<SizeT>(last_next(object) - first(object));
  898. }
  899. //- size -----------------------------------------------------------------------
  900. template<class Type>
  901. inline typename enable_if<is_interval<Type>,
  902. typename size_type_of<interval_traits<Type> >::type>::type
  903. size(const Type& object)
  904. {
  905. return cardinality(object);
  906. }
  907. //- length ---------------------------------------------------------------------
  908. template<class Type>
  909. inline typename boost::enable_if<is_continuous_interval<Type>,
  910. typename difference_type_of<interval_traits<Type> >::type>::type
  911. length(const Type& object)
  912. {
  913. typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
  914. return icl::is_empty(object) ? identity_element<DiffT>::value()
  915. : upper(object) - lower(object);
  916. }
  917. template<class Type>
  918. inline typename boost::enable_if<is_discrete_interval<Type>,
  919. typename difference_type_of<interval_traits<Type> >::type>::type
  920. length(const Type& object)
  921. {
  922. typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
  923. return icl::is_empty(object) ? identity_element<DiffT>::value()
  924. : last_next(object) - first(object);
  925. }
  926. template<class Type>
  927. typename boost::enable_if<is_continuous_asymmetric<Type>,
  928. typename difference_type_of<interval_traits<Type> >::type>::type
  929. length(const Type& object)
  930. {
  931. typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
  932. return icl::is_empty(object) ? identity_element<DiffT>::value()
  933. : upper(object) - lower(object);
  934. }
  935. template<class Type>
  936. inline typename boost::enable_if<is_discrete_static<Type>,
  937. typename difference_type_of<interval_traits<Type> >::type>::type
  938. length(const Type& object)
  939. {
  940. typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
  941. return icl::is_empty(object) ? identity_element<DiffT>::value()
  942. : last_next(object) - first(object);
  943. }
  944. //- iterative_size -------------------------------------------------------------
  945. template<class Type>
  946. inline typename enable_if<is_interval<Type>,
  947. typename size_type_of<interval_traits<Type> >::type>::type
  948. iterative_size(const Type&)
  949. {
  950. return 2;
  951. }
  952. //==============================================================================
  953. //= Addition
  954. //==============================================================================
  955. //- hull -----------------------------------------------------------------------
  956. /** \c hull returns the smallest interval containing \c left and \c right. */
  957. template<class Type>
  958. typename boost::enable_if<has_static_bounds<Type>, Type>::type
  959. hull(Type left, const Type& right)
  960. {
  961. typedef typename interval_traits<Type>::domain_compare domain_compare;
  962. if(icl::is_empty(right))
  963. return left;
  964. else if(icl::is_empty(left))
  965. return right;
  966. return
  967. construct<Type>
  968. (
  969. (std::min)(lower(left), lower(right), domain_compare()),
  970. (std::max)(upper(left), upper(right), domain_compare())
  971. );
  972. }
  973. template<class Type>
  974. typename boost::enable_if<has_dynamic_bounds<Type>, Type>::type
  975. hull(Type left, const Type& right)
  976. {
  977. if(icl::is_empty(right))
  978. return left;
  979. else if(icl::is_empty(left))
  980. return right;
  981. return dynamic_interval_traits<Type>::construct_bounded
  982. (
  983. lower_min(left, right),
  984. upper_max(left, right)
  985. );
  986. }
  987. //==============================================================================
  988. //= Subtraction
  989. //==============================================================================
  990. //- left_subtract --------------------------------------------------------------
  991. /** subtract \c left_minuend from the \c right interval on it's left side.
  992. Return the difference: The part of \c right right of \c left_minuend.
  993. \code
  994. right_over = right - left_minuend; //on the left.
  995. ... d) : right
  996. ... c) : left_minuend
  997. [c d) : right_over
  998. \endcode
  999. */
  1000. template<class Type>
  1001. typename boost::enable_if<is_asymmetric_interval<Type>, Type>::type
  1002. left_subtract(Type right, const Type& left_minuend)
  1003. {
  1004. if(exclusive_less(left_minuend, right))
  1005. return right;
  1006. return construct<Type>(upper(left_minuend), upper(right));
  1007. }
  1008. template<class Type>
  1009. typename boost::enable_if<is_static_closed<Type>, Type>::type
  1010. left_subtract(Type right, const Type& left_minuend)
  1011. {
  1012. if(exclusive_less(left_minuend, right))
  1013. return right;
  1014. else if(upper_less_equal(right, left_minuend))
  1015. return identity_element<Type>::value();
  1016. return construct<Type>(domain_next<Type>(upper(left_minuend)), upper(right));
  1017. }
  1018. template<class Type>
  1019. typename boost::enable_if<is_static_open<Type>, Type>::type
  1020. left_subtract(Type right, const Type& left_minuend)
  1021. {
  1022. if(exclusive_less(left_minuend, right))
  1023. return right;
  1024. return construct<Type>(domain_prior<Type>(upper(left_minuend)), upper(right));
  1025. }
  1026. template<class Type>
  1027. typename boost::enable_if<has_dynamic_bounds<Type>, Type>::type
  1028. left_subtract(Type right, const Type& left_minuend)
  1029. {
  1030. if(exclusive_less(left_minuend, right))
  1031. return right;
  1032. return dynamic_interval_traits<Type>::construct_bounded
  1033. ( reverse_bounded_upper(left_minuend), bounded_upper(right) );
  1034. }
  1035. //- right_subtract -------------------------------------------------------------
  1036. /** subtract \c right_minuend from the \c left interval on it's right side.
  1037. Return the difference: The part of \c left left of \c right_minuend.
  1038. \code
  1039. left_over = left - right_minuend; //on the right side.
  1040. [a ... : left
  1041. [b ... : right_minuend
  1042. [a b) : left_over
  1043. \endcode
  1044. */
  1045. template<class Type>
  1046. typename boost::enable_if<is_asymmetric_interval<Type>, Type>::type
  1047. right_subtract(Type left, const Type& right_minuend)
  1048. {
  1049. if(exclusive_less(left, right_minuend))
  1050. return left;
  1051. return construct<Type>(lower(left), lower(right_minuend));
  1052. }
  1053. template<class Type>
  1054. typename boost::enable_if<is_static_closed<Type>, Type>::type
  1055. right_subtract(Type left, const Type& right_minuend)
  1056. {
  1057. if(exclusive_less(left, right_minuend))
  1058. return left;
  1059. else if(lower_less_equal(right_minuend, left))
  1060. return identity_element<Type>::value();
  1061. return construct<Type>(lower(left), domain_prior<Type>(lower(right_minuend)));
  1062. }
  1063. template<class Type>
  1064. typename boost::enable_if<is_static_open<Type>, Type>::type
  1065. right_subtract(Type left, const Type& right_minuend)
  1066. {
  1067. if(exclusive_less(left, right_minuend))
  1068. return left;
  1069. return construct<Type>(lower(left), domain_next<Type>(lower(right_minuend)));
  1070. }
  1071. template<class Type>
  1072. typename boost::enable_if<has_dynamic_bounds<Type>, Type>::type
  1073. right_subtract(Type left, const Type& right_minuend)
  1074. {
  1075. if(exclusive_less(left, right_minuend))
  1076. return left;
  1077. return dynamic_interval_traits<Type>::construct_bounded
  1078. ( bounded_lower(left), reverse_bounded_lower(right_minuend) );
  1079. }
  1080. //==============================================================================
  1081. //= Intersection
  1082. //==============================================================================
  1083. //- operator & -----------------------------------------------------------------
  1084. /** Returns the intersection of \c left and \c right interval. */
  1085. template<class Type>
  1086. typename boost::enable_if<is_asymmetric_interval<Type>, Type>::type
  1087. operator & (Type left, const Type& right)
  1088. {
  1089. typedef typename interval_traits<Type>::domain_compare domain_compare;
  1090. if(icl::is_empty(left) || icl::is_empty(right))
  1091. return identity_element<Type>::value();
  1092. else
  1093. return
  1094. construct<Type>
  1095. (
  1096. (std::max)(icl::lower(left), icl::lower(right), domain_compare()),
  1097. (std::min)(icl::upper(left), icl::upper(right), domain_compare())
  1098. );
  1099. }
  1100. template<class Type>
  1101. typename boost::enable_if<has_symmetric_bounds<Type>, Type>::type
  1102. operator & (Type left, const Type& right)
  1103. {
  1104. typedef typename interval_traits<Type>::domain_compare domain_compare;
  1105. if(icl::is_empty(left) || icl::is_empty(right))
  1106. return identity_element<Type>::value();
  1107. else
  1108. return
  1109. construct<Type>
  1110. (
  1111. (std::max)(icl::lower(left), icl::lower(right), domain_compare()),
  1112. (std::min)(icl::upper(left), icl::upper(right), domain_compare())
  1113. );
  1114. }
  1115. template<class Type>
  1116. typename boost::enable_if<has_dynamic_bounds<Type>, Type>::type
  1117. operator & (Type left, const Type& right)
  1118. {
  1119. if(icl::is_empty(left) || icl::is_empty(right))
  1120. return identity_element<Type>::value();
  1121. else
  1122. return dynamic_interval_traits<Type>::construct_bounded
  1123. (
  1124. lower_max(left, right),
  1125. upper_min(left, right)
  1126. );
  1127. }
  1128. //- intersects -----------------------------------------------------------------
  1129. template<class Type>
  1130. typename boost::enable_if<is_interval<Type>, bool>::type
  1131. intersects(const Type& left, const Type& right)
  1132. {
  1133. return !( icl::is_empty(left) || icl::is_empty(right)
  1134. || exclusive_less(left,right) || exclusive_less(right,left));
  1135. }
  1136. //- disjoint -------------------------------------------------------------------
  1137. template<class Type>
  1138. typename boost::enable_if<is_interval<Type>, bool>::type
  1139. disjoint(const Type& left, const Type& right)
  1140. {
  1141. return icl::is_empty(left) || icl::is_empty(right)
  1142. || exclusive_less(left,right) || exclusive_less(right,left);
  1143. }
  1144. //==============================================================================
  1145. //= Complement
  1146. //==============================================================================
  1147. template<class Type>
  1148. typename boost::enable_if<is_asymmetric_interval<Type>, Type>::type
  1149. inner_complement(const Type& left, const Type& right)
  1150. {
  1151. if(icl::is_empty(left) || icl::is_empty(right))
  1152. return identity_element<Type>::value();
  1153. else if(exclusive_less(left, right))
  1154. return construct<Type>(upper(left), lower(right));
  1155. else if(exclusive_less(right, left))
  1156. return construct<Type>(upper(right), lower(left));
  1157. else
  1158. return identity_element<Type>::value();
  1159. }
  1160. template<class Type>
  1161. typename boost::enable_if<is_discrete_static_closed<Type>, Type>::type
  1162. inner_complement(const Type& left, const Type& right)
  1163. {
  1164. if(icl::is_empty(left) || icl::is_empty(right))
  1165. return identity_element<Type>::value();
  1166. else if(exclusive_less(left, right))
  1167. return construct<Type>(domain_next<Type>(upper(left)), domain_prior<Type>(lower(right)));
  1168. else if(exclusive_less(right, left))
  1169. return construct<Type>(domain_next<Type>(upper(right)), domain_prior<Type>(lower(left)));
  1170. else
  1171. return identity_element<Type>::value();
  1172. }
  1173. template<class Type>
  1174. typename boost::enable_if<is_discrete_static_open<Type>, Type>::type
  1175. inner_complement(const Type& left, const Type& right)
  1176. {
  1177. if(icl::is_empty(left) || icl::is_empty(right))
  1178. return identity_element<Type>::value();
  1179. else if(exclusive_less(left, right))
  1180. return construct<Type>(last(left), first(right));
  1181. else if(exclusive_less(right, left))
  1182. return construct<Type>(last(right), first(left));
  1183. else
  1184. return identity_element<Type>::value();
  1185. }
  1186. template<class Type>
  1187. typename boost::enable_if<has_dynamic_bounds<Type>, Type>::type
  1188. inner_complement(const Type& left, const Type& right)
  1189. {
  1190. if(icl::is_empty(left) || icl::is_empty(right))
  1191. return identity_element<Type>::value();
  1192. else if(exclusive_less(left, right))
  1193. return right_subtract(left_subtract(hull(left, right), left), right);
  1194. else if(exclusive_less(right, left))
  1195. return right_subtract(left_subtract(hull(right, left), right), left);
  1196. else
  1197. return identity_element<Type>::value();
  1198. }
  1199. template<class Type>
  1200. inline typename boost::enable_if<is_interval<Type>, Type>::type
  1201. between(const Type& left, const Type& right)
  1202. {
  1203. return inner_complement(left, right);
  1204. }
  1205. //==============================================================================
  1206. //= Distance
  1207. //==============================================================================
  1208. template<class Type>
  1209. typename boost::
  1210. enable_if< mpl::and_< is_interval<Type>
  1211. , has_difference<typename interval_traits<Type>::domain_type>
  1212. , is_discrete<typename interval_traits<Type>::domain_type>
  1213. >
  1214. , typename difference_type_of<interval_traits<Type> >::type>::type
  1215. distance(const Type& x1, const Type& x2)
  1216. {
  1217. typedef typename difference_type_of<interval_traits<Type> >::type difference_type;
  1218. if(icl::is_empty(x1) || icl::is_empty(x2))
  1219. return icl::identity_element<difference_type>::value();
  1220. else if(domain_less<Type>(last(x1), first(x2)))
  1221. return static_cast<difference_type>(icl::pred(first(x2) - last(x1)));
  1222. else if(domain_less<Type>(last(x2), first(x1)))
  1223. return static_cast<difference_type>(icl::pred(first(x1) - last(x2)));
  1224. else
  1225. return icl::identity_element<difference_type>::value();
  1226. }
  1227. template<class Type>
  1228. typename boost::
  1229. enable_if< mpl::and_< is_interval<Type>
  1230. , has_difference<typename interval_traits<Type>::domain_type>
  1231. , is_continuous<typename interval_traits<Type>::domain_type>
  1232. >
  1233. , typename difference_type_of<interval_traits<Type> >::type>::type
  1234. distance(const Type& x1, const Type& x2)
  1235. {
  1236. typedef typename difference_type_of<interval_traits<Type> >::type DiffT;
  1237. if(icl::is_empty(x1) || icl::is_empty(x2))
  1238. return icl::identity_element<DiffT>::value();
  1239. else if(domain_less<Type>(upper(x1), lower(x2)))
  1240. return lower(x2) - upper(x1);
  1241. else if(domain_less<Type>(upper(x2), lower(x1)))
  1242. return lower(x1) - upper(x2);
  1243. else
  1244. return icl::identity_element<DiffT>::value();
  1245. }
  1246. //==============================================================================
  1247. //= Streaming, representation
  1248. //==============================================================================
  1249. template<class Type>
  1250. typename boost::
  1251. enable_if< mpl::or_< is_static_left_open<Type>
  1252. , is_static_open<Type> >, std::string>::type
  1253. left_bracket(const Type&) { return "("; }
  1254. template<class Type>
  1255. typename boost::
  1256. enable_if< mpl::or_< is_static_right_open<Type>
  1257. , is_static_closed<Type> >, std::string>::type
  1258. left_bracket(const Type&) { return "["; }
  1259. template<class Type>
  1260. typename boost::enable_if<has_dynamic_bounds<Type>, std::string>::type
  1261. left_bracket(const Type& object)
  1262. {
  1263. return left_bracket(object.bounds());
  1264. }
  1265. //------------------------------------------------------------------------------
  1266. template<class Type>
  1267. typename boost::
  1268. enable_if< mpl::or_< is_static_right_open<Type>
  1269. , is_static_open<Type> >, std::string>::type
  1270. right_bracket(const Type&) { return ")"; }
  1271. template<class Type>
  1272. typename boost::
  1273. enable_if< mpl::or_< is_static_left_open<Type>
  1274. , is_static_closed<Type> >, std::string>::type
  1275. right_bracket(const Type&) { return "]"; }
  1276. template<class Type>
  1277. typename boost::enable_if<has_dynamic_bounds<Type>, std::string>::type
  1278. right_bracket(const Type& object)
  1279. {
  1280. return right_bracket(object.bounds());
  1281. }
  1282. //------------------------------------------------------------------------------
  1283. template<class CharType, class CharTraits, class Type>
  1284. typename boost::enable_if<is_interval<Type>,
  1285. std::basic_ostream<CharType, CharTraits> >::type&
  1286. operator << (std::basic_ostream<CharType, CharTraits> &stream, Type const& object)
  1287. {
  1288. if(boost::icl::is_empty(object))
  1289. return stream << left_bracket<Type>(object) << right_bracket<Type>(object);
  1290. else
  1291. return stream << left_bracket<Type>(object)
  1292. << interval_traits<Type>::lower(object)
  1293. << ","
  1294. << interval_traits<Type>::upper(object)
  1295. << right_bracket<Type>(object) ;
  1296. }
  1297. }} // namespace icl boost
  1298. #endif