overlap_counter.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*-----------------------------------------------------------------------------+
  2. Interval Container Library
  3. Author: Joachim Faulhaber
  4. Copyright (c) 2007-2009: 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 overlap_counter.cpp \file overlap_counter.cpp
  12. \brief The most simple application of an interval map:
  13. Counting the overlaps of added intervals.
  14. The most basic application of an interval_map is a counter counting
  15. the number of overlaps of intervals inserted into it.
  16. On could call an interval_map an aggregate on overlap machine. A very basic
  17. aggregation is summation of an integer. A interval_map<int,int> maps
  18. intervals of int to ints.
  19. If we insert a value pair (discrete_interval<int>(2,6), 1) into the interval_map, it
  20. increases the content of all value pairs in the map by 1, if their interval
  21. part overlaps with discrete_interval<int>(2,6).
  22. \include overlap_counter_/overlap_counter.cpp
  23. */
  24. //[example_overlap_counter
  25. #include <iostream>
  26. #include <boost/icl/split_interval_map.hpp>
  27. using namespace std;
  28. using namespace boost::icl;
  29. /* The most simple example of an interval_map is an overlap counter.
  30. If intervals are added that are associated with the value 1,
  31. all overlaps of added intervals are counted as a result in the
  32. associated values.
  33. */
  34. typedef interval_map<int, int> OverlapCounterT;
  35. void print_overlaps(const OverlapCounterT& counter)
  36. {
  37. for(OverlapCounterT::const_iterator it = counter.begin(); it != counter.end(); it++)
  38. {
  39. discrete_interval<int> itv = (*it).first;
  40. int overlaps_count = (*it).second;
  41. if(overlaps_count == 1)
  42. cout << "in interval " << itv << " intervals do not overlap" << endl;
  43. else
  44. cout << "in interval " << itv << ": "<< overlaps_count << " intervals overlap" << endl;
  45. }
  46. }
  47. void overlap_counter()
  48. {
  49. OverlapCounterT overlap_counter;
  50. discrete_interval<int> inter_val;
  51. inter_val = discrete_interval<int>::right_open(4,8);
  52. cout << "-- adding " << inter_val << " -----------------------------------------" << endl;
  53. overlap_counter += make_pair(inter_val, 1);
  54. print_overlaps(overlap_counter);
  55. cout << "-----------------------------------------------------------" << endl;
  56. inter_val = discrete_interval<int>::right_open(6,9);
  57. cout << "-- adding " << inter_val << " -----------------------------------------" << endl;
  58. overlap_counter += make_pair(inter_val, 1);
  59. print_overlaps(overlap_counter);
  60. cout << "-----------------------------------------------------------" << endl;
  61. inter_val = discrete_interval<int>::right_open(1,9);
  62. cout << "-- adding " << inter_val << " -----------------------------------------" << endl;
  63. overlap_counter += make_pair(inter_val, 1);
  64. print_overlaps(overlap_counter);
  65. cout << "-----------------------------------------------------------" << endl;
  66. }
  67. int main()
  68. {
  69. cout << ">>Interval Container Library: Sample overlap_counter.cpp <<\n";
  70. cout << "-----------------------------------------------------------\n";
  71. overlap_counter();
  72. return 0;
  73. }
  74. // Program output:
  75. // >>Interval Container Library: Sample overlap_counter.cpp <<
  76. // -----------------------------------------------------------
  77. // -- adding [4,8) -----------------------------------------
  78. // in interval [4,8) intervals do not overlap
  79. // -----------------------------------------------------------
  80. // -- adding [6,9) -----------------------------------------
  81. // in interval [4,6) intervals do not overlap
  82. // in interval [6,8): 2 intervals overlap
  83. // in interval [8,9) intervals do not overlap
  84. // -----------------------------------------------------------
  85. // -- adding [1,9) -----------------------------------------
  86. // in interval [1,4) intervals do not overlap
  87. // in interval [4,6): 2 intervals overlap
  88. // in interval [6,8): 3 intervals overlap
  89. // in interval [8,9): 2 intervals overlap
  90. // -----------------------------------------------------------
  91. //]