Consider the following program:
#include <iostream>
#include <boost/icl/interval_map.hpp>
struct Value
{
explicit Value(int v) : v(v), is_empty(false) {}
Value() : v(0), is_empty(true) {}
Value& operator+=(const Value& other)
{
v += other.v;
return *this;
}
bool operator==(const Value& other) const { return is_empty == other.is_empty; }
bool operator!=(const Value& other) const { return is_empty != other.is_empty; }
int v;
bool is_empty;
};
int main()
{
boost::icl::interval_map<int, Value> m;
m.add(std::make_pair(boost::icl::interval<int>::right_open(10, 20), Value(2)));
m.add(std::make_pair(boost::icl::interval<int>::right_open(15, 30), Value(3)));
std::cout << m.iterative_size() << '\n';
std::cout << m.begin()->first.lower() << '\n';
std::cout << m.begin()->first.upper() << '\n';
std::cout << m.begin()->second.v << '\n';
}
The output is
1
10
30
2
Questons:
- Why is the last line 2?
- Isn't the interval map expected to add up the value, so that it is 5?
- How to achieve the behavior that intervals are added and the value from
+=
is preserved?
I want the following results:
{([10,20)->(2))+
([15,30)->(3))+
([40,50)->(11))}
=
{([10,30)->(5))([40,50)->(11))}
That is, when intervals are added, they are merged, and the combined value is stored for entire merged interval.
There's may be no math sense in this operation, but this is what I need in my program. (Also, I don't to subtract intervals).