custom_interval.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 custom_interval.cpp \file custom_interval.cpp
  12. \brief Shows how to use interval containers with own interval classes.
  13. There may be instances, where we want to use interval container with our
  14. own user defined interval classes. Boost interval containers can be adapted
  15. to your interval class by partial template specialisation. Only a few lines
  16. of code are needed to achieve this.
  17. \include custom_interval_/custom_interval.cpp
  18. */
  19. //[example_custom_interval
  20. #include <iostream>
  21. #include <boost/icl/interval_set.hpp>
  22. using namespace std;
  23. using namespace boost::icl;
  24. // Here is a typical class that may model intervals in your application.
  25. class MyInterval
  26. {
  27. public:
  28. MyInterval(): _first(), _past(){}
  29. MyInterval(int lo, int up): _first(lo), _past(up){}
  30. int first()const{ return _first; }
  31. int past ()const{ return _past; }
  32. private:
  33. int _first, _past;
  34. };
  35. namespace boost{ namespace icl
  36. {
  37. // Class template interval_traits serves as adapter to register and customize your interval class
  38. template<>
  39. struct interval_traits< MyInterval > //1. Partially specialize interval_traits for
  40. { // your class MyInterval
  41. //2. Define associated types
  42. typedef MyInterval interval_type; //2.1 MyInterval will be the interval_type
  43. typedef int domain_type; //2.2 The elements of the domain are ints
  44. typedef std::less<int> domain_compare; //2.3 This is the way our element shall be ordered.
  45. //3. Next we define the essential functions
  46. // of the specialisation
  47. //3.1 Construction of intervals
  48. static interval_type construct(const domain_type& lo, const domain_type& up)
  49. { return interval_type(lo, up); }
  50. //3.2 Selection of values
  51. static domain_type lower(const interval_type& inter_val){ return inter_val.first(); };
  52. static domain_type upper(const interval_type& inter_val){ return inter_val.past(); };
  53. };
  54. template<>
  55. struct interval_bound_type<MyInterval> //4. Finally we define the interval borders.
  56. { // Choose between static_open (lo..up)
  57. typedef interval_bound_type type; // static_left_open (lo..up]
  58. BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::static_right_open);//[lo..up)
  59. }; // and static_closed [lo..up]
  60. }} // namespace boost icl
  61. void custom_interval()
  62. {
  63. // Now we can use class MyInterval with interval containers:
  64. typedef interval_set<int, std::less, MyInterval> MyIntervalSet;
  65. MyIntervalSet mySet;
  66. mySet += MyInterval(1,9);
  67. cout << mySet << endl;
  68. mySet.subtract(3) -= 6;
  69. cout << mySet << " subtracted 3 and 6\n";
  70. mySet ^= MyInterval(2,8);
  71. cout << mySet << " flipped between 2 and 7\n";
  72. }
  73. int main()
  74. {
  75. cout << ">>Interval Container Library: Sample custom_interval.cpp <<\n";
  76. cout << "-----------------------------------------------------------\n";
  77. cout << "This program uses a user defined interval class:\n";
  78. custom_interval();
  79. return 0;
  80. }
  81. // Program output:
  82. /*-----------------------------------------------------------------------------
  83. >>Interval Container Library: Sample custom_interval.cpp <<
  84. -----------------------------------------------------------
  85. This program uses a user defined interval class:
  86. {[1, 9)}
  87. {[1, 3) [4, 6) [7, 9)} subtracted 3 and 6
  88. {[1,2) [3,4) [6,7) [8,9)} flipped between 2 and 7
  89. -----------------------------------------------------------------------------*/
  90. //]