dynamic_interval.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*-----------------------------------------------------------------------------+
  2. Interval Container Library
  3. Author: Joachim Faulhaber
  4. Copyright (c) 2007-2010: Joachim Faulhaber
  5. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  6. +------------------------------------------------------------------------------+
  7. Distributed under the Boost Software License, Version 1.0.
  8. (See accompanying file LICENCE.txt or copy at
  9. http://www.boost.org/LICENSE_1_0.txt)
  10. +-----------------------------------------------------------------------------*/
  11. /** Example dynamic_interval.cpp \file dynamic_interval.cpp
  12. \brief Intervals with dynamic interval bounds that can be changed at runtime.
  13. Intervals types with dynamic interval bounds can represent closed and
  14. open interval borders. Interval borders are not static or fixed for
  15. the type but may change due to computations in interval containers.
  16. Dynamically bounded intervals are the library default for interval
  17. parameters in interval containers.
  18. \include dynamic_interval_/dynamic_interval.cpp
  19. */
  20. //[example_dynamic_interval
  21. #include <iostream>
  22. #include <string>
  23. #include <math.h>
  24. #include <boost/type_traits/is_same.hpp>
  25. #include <boost/icl/interval_set.hpp>
  26. #include <boost/icl/split_interval_set.hpp>
  27. // Dynamically bounded intervals 'discrete_interval' and 'continuous_interval'
  28. // are indirectly included via interval containers as library defaults.
  29. #include "../toytime.hpp"
  30. #include <boost/icl/rational.hpp>
  31. using namespace std;
  32. using namespace boost;
  33. using namespace boost::icl;
  34. int main()
  35. {
  36. cout << ">>Interval Container Library: Sample interval.cpp <<\n";
  37. cout << "----------------------------------------------------\n";
  38. // Dynamically bounded intervals are the library default for
  39. // interval parameters in interval containers.
  40. BOOST_STATIC_ASSERT((
  41. boost::is_same< interval_set<int>::interval_type
  42. , discrete_interval<int> >::value
  43. ));
  44. BOOST_STATIC_ASSERT((
  45. boost::is_same< interval_set<float>::interval_type
  46. , continuous_interval<float> >::value
  47. ));
  48. // As we can see the library default chooses the appropriate
  49. // class template instance discrete_interval<T> or continuous_interval<T>
  50. // dependent on the domain_type T. The library default for intervals
  51. // is also available via the template 'interval':
  52. BOOST_STATIC_ASSERT((
  53. boost::is_same< interval<int>::type
  54. , discrete_interval<int> >::value
  55. ));
  56. BOOST_STATIC_ASSERT((
  57. boost::is_same< interval<float>::type
  58. , continuous_interval<float> >::value
  59. ));
  60. // template interval also provides static functions for the four border types
  61. interval<int>::type int_interval = interval<int>::closed(3, 7);
  62. interval<double>::type sqrt_interval = interval<double>::right_open(1/sqrt(2.0), sqrt(2.0));
  63. interval<string>::type city_interval = interval<string>::left_open("Barcelona", "Boston");
  64. interval<Time>::type time_interval = interval<Time>::open(Time(monday,8,30), Time(monday,17,20));
  65. cout << "----- Dynamically bounded intervals ----------------------------------------\n";
  66. cout << " discrete_interval<int> : " << int_interval << endl;
  67. cout << "continuous_interval<double>: " << sqrt_interval << " does "
  68. << string(contains(sqrt_interval, sqrt(2.0))?"":"NOT")
  69. << " contain sqrt(2)" << endl;
  70. cout << "continuous_interval<string>: " << city_interval << " does "
  71. << string(contains(city_interval,"Barcelona")?"":"NOT")
  72. << " contain 'Barcelona'" << endl;
  73. cout << "continuous_interval<string>: " << city_interval << " does "
  74. << string(contains(city_interval, "Berlin")?"":"NOT")
  75. << " contain 'Berlin'" << endl;
  76. cout << " discrete_interval<Time> : " << time_interval << "\n\n";
  77. // Using dynamically bounded intervals allows to apply operations
  78. // with intervals and also with elements on all interval containers
  79. // including interval containers of continuous domain types:
  80. interval<rational<int> >::type unit_interval
  81. = interval<rational<int> >::right_open(rational<int>(0), rational<int>(1));
  82. interval_set<rational<int> > unit_set(unit_interval);
  83. interval_set<rational<int> > ratio_set(unit_set);
  84. ratio_set -= rational<int>(1,3); // Subtract 1/3 from the set
  85. cout << "----- Manipulation of single values in continuous sets ---------------------\n";
  86. cout << "1/3 subtracted from [0..1) : " << ratio_set << endl;
  87. cout << "The set does " << string(contains(ratio_set, rational<int>(1,3))?"":"NOT")
  88. << " contain '1/3'" << endl;
  89. ratio_set ^= unit_set;
  90. cout << "Flipping the holey set : " << ratio_set << endl;
  91. cout << "yields the subtracted : 1/3\n\n";
  92. // Of course we can use interval types that are different from the
  93. // library default by explicit instantiation:
  94. split_interval_set<int, std::less, closed_interval<Time> > intuitive_times;
  95. // Interval set 'intuitive_times' uses statically bounded closed intervals
  96. intuitive_times += closed_interval<Time>(Time(monday, 9,00), Time(monday, 10,59));
  97. intuitive_times += closed_interval<Time>(Time(monday, 10,00), Time(monday, 11,59));
  98. cout << "----- Here we are NOT using the library default for intervals --------------\n";
  99. cout << intuitive_times << endl;
  100. return 0;
  101. }
  102. // Program output:
  103. //>>Interval Container Library: Sample interval.cpp <<
  104. //----------------------------------------------------
  105. //----- Dynamically bounded intervals ----------------------------------------
  106. // discrete_interval<int> : [3,7]
  107. //continuous_interval<double>: [0.707107,1.41421) does NOT contain sqrt(2)
  108. //continuous_interval<string>: (Barcelona,Boston] does NOT contain 'Barcelona'
  109. //continuous_interval<string>: (Barcelona,Boston] does contain 'Berlin'
  110. // discrete_interval<Time> : (mon:08:30,mon:17:20)
  111. //
  112. //----- Manipulation of single values in continuous sets ---------------------
  113. //1/3 subtracted from [0..1) : {[0/1,1/3)(1/3,1/1)}
  114. //The set does NOT contain '1/3'
  115. //Flipping the holey set : {[1/3,1/3]}
  116. //yields the subtracted : 1/3
  117. //
  118. //----- Here we are NOT using the library default for intervals --------------
  119. //{[mon:09:00,mon:09:59][mon:10:00,mon:10:59][mon:11:00,mon:11:59]}
  120. //]