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