std_copy.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 std_copy.cpp \file std_copy.cpp
  12. \brief Fill interval containers using std::copy.
  13. Example std_copy shows how algorithm std::copy can be used to
  14. fill interval containers from other std::containers and how copying
  15. to interval containers differs from other uses of std::copy.
  16. \include std_copy_/std_copy.cpp
  17. */
  18. //[example_std_copy
  19. #include <iostream>
  20. #include <vector>
  21. #include <algorithm>
  22. #include <boost/icl/interval_map.hpp>
  23. using namespace std;
  24. using namespace boost;
  25. using namespace boost::icl;
  26. // 'make_segments' returns a vector of interval value pairs, which
  27. // are not sorted. The values are taken from the minimal example
  28. // in section 'interval combining styles'.
  29. vector<pair<discrete_interval<int>, int> > make_segments()
  30. {
  31. vector<pair<discrete_interval<int>, int> > segment_vec;
  32. segment_vec.push_back(make_pair(discrete_interval<int>::right_open(2,4), 1));
  33. segment_vec.push_back(make_pair(discrete_interval<int>::right_open(4,5), 1));
  34. segment_vec.push_back(make_pair(discrete_interval<int>::right_open(1,3), 1));
  35. return segment_vec;
  36. }
  37. // 'show_segments' displays the source segements.
  38. void show_segments(const vector<pair<discrete_interval<int>, int> >& segments)
  39. {
  40. vector<pair<discrete_interval<int>, int> >::const_iterator iter = segments.begin();
  41. while(iter != segments.end())
  42. {
  43. cout << "(" << iter->first << "," << iter->second << ")";
  44. ++iter;
  45. }
  46. }
  47. void std_copy()
  48. {
  49. // So we have some segments stored in an std container.
  50. vector<pair<discrete_interval<int>, int> > segments = make_segments();
  51. // Display the input
  52. cout << "input sequence: "; show_segments(segments); cout << "\n\n";
  53. // We are going to 'std::copy' those segments into an interval_map:
  54. interval_map<int,int> segmap;
  55. // Use an 'icl::inserter' from <boost/icl/iterator.hpp> to call
  56. // insertion on the interval container.
  57. std::copy(segments.begin(), segments.end(),
  58. icl::inserter(segmap, segmap.end()));
  59. cout << "icl::inserting: " << segmap << endl;
  60. segmap.clear();
  61. // When we are feeding data into interval_maps, most of the time we are
  62. // intending to compute an aggregation result. So we are not interested
  63. // the std::insert semantincs but the aggregating icl::addition semantics.
  64. // To achieve this there is an icl::add_iterator and an icl::adder function
  65. // provided in <boost/icl/iterator.hpp>.
  66. std::copy(segments.begin(), segments.end(),
  67. icl::adder(segmap, segmap.end())); //Aggregating associated values
  68. cout << "icl::adding : " << segmap << endl;
  69. // In this last case, the semantics of 'std::copy' transforms to the
  70. // generalized addition operation, that is implemented by operator
  71. // += or + on itl maps and sets.
  72. }
  73. int main()
  74. {
  75. cout << ">> Interval Container Library: Example std_copy.cpp <<\n";
  76. cout << "-----------------------------------------------------------\n";
  77. cout << "Using std::copy to fill an interval_map:\n\n";
  78. std_copy();
  79. return 0;
  80. }
  81. // Program output:
  82. /*---------------------------------------------------------
  83. >> Interval Container Library: Example std_copy.cpp <<
  84. -----------------------------------------------------------
  85. Using std::copy to fill an interval_map:
  86. input sequence: ([2,4),1)([4,5),1)([1,3),1)
  87. icl::inserting: {([1,5)->1)}
  88. icl::adding : {([1,2)->1)([2,3)->2)([3,5)->1)}
  89. ---------------------------------------------------------*/
  90. //]