static_interval.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 static_interval.cpp \file static_interval.cpp
  12. \brief Intervals with static interval bounds.
  13. Intervals types with static or fixed interval bounds. Statically
  14. bounded intervals use up to 33% less memory than dynamically
  15. bounded ones. Of the four possible statically bounded intervals types
  16. right_open_intervals are the most important ones. We can switch the
  17. library default to statically bounded intervals by defining
  18. BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS.
  19. \include static_interval_/static_interval.cpp
  20. */
  21. //[example_static_interval
  22. #include <iostream>
  23. #include <string>
  24. #include <math.h>
  25. #include <boost/type_traits/is_same.hpp>
  26. // We can change the library default for the interval types by defining
  27. #define BOOST_ICL_USE_STATIC_BOUNDED_INTERVALS
  28. // prior to other inluces from the icl.
  29. // The interval type that is automatically used with interval
  30. // containers then is the statically bounded right_open_interval.
  31. #include <boost/icl/interval_set.hpp>
  32. #include <boost/icl/split_interval_set.hpp>
  33. // The statically bounded interval type 'right_open_interval'
  34. // is indirectly included via interval containers.
  35. #include "../toytime.hpp"
  36. #include <boost/icl/rational.hpp>
  37. using namespace std;
  38. using namespace boost;
  39. using namespace boost::icl;
  40. int main()
  41. {
  42. cout << ">> Interval Container Library: Sample static_interval.cpp <<\n";
  43. cout << "------------------------------------------------------------\n";
  44. // Statically bounded intervals are the user defined library default for
  45. // interval parameters in interval containers now.
  46. BOOST_STATIC_ASSERT((
  47. boost::is_same< interval_set<int>::interval_type
  48. , right_open_interval<int> >::value
  49. ));
  50. BOOST_STATIC_ASSERT((
  51. boost::is_same< interval_set<float>::interval_type
  52. , right_open_interval<float> >::value
  53. ));
  54. // As we can see the library default both for discrete and continuous
  55. // domain_types T is 'right_open_interval<T>'.
  56. // The user defined library default for intervals is also available via
  57. // the template 'interval':
  58. BOOST_STATIC_ASSERT((
  59. boost::is_same< interval<int>::type
  60. , right_open_interval<int> >::value
  61. ));
  62. // Again we are declaring and initializing the four test intervals that have been used
  63. // in the example 'interval' and 'dynamic_interval'
  64. interval<int>::type int_interval = interval<int>::right_open(3, 8); // shifted the upper bound
  65. interval<double>::type sqrt_interval = interval<double>::right_open(1/sqrt(2.0), sqrt(2.0));
  66. // Interval ("Barcelona", "Boston"] can not be represented because there is no 'steppable next' on
  67. // lower bound "Barcelona". Ok. this is a different interval:
  68. interval<string>::type city_interval = interval<string>::right_open("Barcelona", "Boston");
  69. // Toy Time is discrete again so we can transfrom open(Time(monday,8,30), Time(monday,17,20))
  70. // to right_open(Time(monday,8,31), Time(monday,17,20))
  71. interval<Time>::type time_interval = interval<Time>::right_open(Time(monday,8,31), Time(monday,17,20));
  72. cout << "----- Statically bounded intervals ----------------------------------------\n";
  73. cout << "right_open_interval<int> : " << int_interval << endl;
  74. cout << "right_open_interval<double>: " << sqrt_interval << " does "
  75. << string(contains(sqrt_interval, sqrt(2.0))?"":"NOT")
  76. << " contain sqrt(2)" << endl;
  77. cout << "right_open_interval<string>: " << city_interval << " does "
  78. << string(contains(city_interval,"Barcelona")?"":"NOT")
  79. << " contain 'Barcelona'" << endl;
  80. cout << "right_open_interval<string>: " << city_interval << " does "
  81. << string(contains(city_interval, "Boston")?"":"NOT")
  82. << " contain 'Boston'" << endl;
  83. cout << "right_open_interval<Time> : " << time_interval << "\n\n";
  84. // Using statically bounded intervals does not allows to apply operations
  85. // with elements on all interval containers, if their domain_type is continuous.
  86. // The code that follows is identical to example 'dynamic_interval'. Only 'internally'
  87. // the library default for the interval template now is 'right_open_interval'
  88. interval<rational<int> >::type unit_interval
  89. = interval<rational<int> >::right_open(rational<int>(0), rational<int>(1));
  90. interval_set<rational<int> > unit_set(unit_interval);
  91. interval_set<rational<int> > ratio_set(unit_set);
  92. // ratio_set -= rational<int>(1,3); // This line will not compile, because we can not
  93. // represent a singleton interval as right_open_interval.
  94. return 0;
  95. }
  96. // Program output:
  97. //>> Interval Container Library: Sample static_interval.cpp <<
  98. //------------------------------------------------------------
  99. //----- Statically bounded intervals ----------------------------------------
  100. //right_open_interval<int> : [3,8)
  101. //right_open_interval<double>: [0.707107,1.41421) does NOT contain sqrt(2)
  102. //right_open_interval<string>: [Barcelona,Boston) does contain 'Barcelona'
  103. //right_open_interval<string>: [Barcelona,Boston) does NOT contain 'Boston'
  104. //right_open_interval<Time> : [mon:08:31,mon:17:20)
  105. //]