Boost C++ Libraries Home Libraries People FAQ More


Std copy

The standard algorithm std::copy can be used to fill interval containers from standard containers of intervals or interval value pairs (segments). Because intervals do not represent elements but sets, that can be empty or contain more than one element, the usage of std::copy differs from what we are familiar with using containers of elements.

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/itl/interval_map.hpp>
#include <boost/itl/iterator.hpp> // needed for itl::inserter and
                                  // itl::adder.

using namespace std;
using namespace boost;
using namespace boost::itl;

// 'make_segments' returns a vector of interval value pairs, which
// are not sorted. The values are taken from the minimal example
// in section 'interval combining styles'.
vector<pair<interval<int>, int> > make_segments()
    vector<pair<interval<int>, int> > segment_vec;
    segment_vec.push_back(make_pair(interval<int>::rightopen(2,4), 1));
    segment_vec.push_back(make_pair(interval<int>::rightopen(4,5), 1));
    segment_vec.push_back(make_pair(interval<int>::rightopen(1,3), 1));
    return segment_vec;

// 'show_segments' displays the source segements.
void show_segments(const vector<pair<interval<int>, int> >& segments)
    vector<pair<interval<int>, int> >::const_iterator iter = segments.begin();
    while(iter != segments.end())
        cout << "(" << iter->first << "," << iter->second << ")";

void std_copy()
    // So we have some segments stored in an std container.
    vector<pair<interval<int>, int> > segments = make_segments(); 
    // Display the input
    cout << "input sequence: "; show_segments(segments); cout << "\n\n";

    // We are going to 'std::copy' those segments into an interval_map:
    interval_map<int,int> segmap;

    // Use an 'itl::inserter' from <boost/itl/iterator.hpp> to call 
    // insertion on the interval container.
    std::copy(segments.begin(), segments.end(), 
              itl::inserter(segmap, segmap.end()));
    cout << "itl::inserting: " << segmap << endl;

    // When we are feeding data into interval_maps, most of the time we are
    // intending to compute an aggregation result. So we are not interested
    // the std::insert semantincs but the aggregating itl::addition semantics.
    // To achieve this there is an itl::add_iterator and an itl::adder function 
    // provided in <boost/itl/iterator.hpp>.
    std::copy(segments.begin(), segments.end(), 
              itl::adder(segmap, segmap.end())); //Aggregating associated values
    cout << "itl::adding   : " << segmap << endl; 

    // In this last case, the semantics of 'std::copy' transforms to the 
    // generalized addition operation, that is implemented by operator
    // += or + on itl maps and sets.

int main()
    cout << ">>    Interval Template Library: Example std_copy.cpp    <<\n";
    cout << "-----------------------------------------------------------\n";
    cout << "Using std::copy to fill an interval_map:\n\n";

    return 0;

// Program output:
>>    Interval Template Library: Example std_copy.cpp    <<
Using std::copy to fill an interval_map:

input sequence: ([2,4),1)([4,5),1)([1,3),1)

itl::inserting: {([1,5)->1)}
itl::adding   : {([1,2)->1)([2,3)->2)([3,5)->1)}