One of the interval map's template parameters is the combination operator type.
The typical example consists on map using std::set
or similar as a value and then it uses the addition or the identity (keep the existing value) as operations.
Since the overwrite example is not there by default, you can create yours and pass it to the map yourself:
#include <boost/icl/interval_map.hpp>
using namespace boost::icl;
// interval_map combiner functor: assigns new value if key exists
template <class Type>
struct inplace_replace : identity_based_inplace_combine<Type> {
void operator()(Type &object, const Type &operand) const {
object = operand;
}
};
template<>
inline std::string unary_template_to_string<inplace_replace>::apply() {
return "=";
}
// When adding, if interval exists, replaces value.
// When subtracting, if interval exists, removes value.
using ival_map =
interval_map<unsigned, // Key
unsigned, // Value
partial_enricher, // Unmapped intervals have unkown value; store identity values
std::less, // Comparator
inplace_replace, // Combination operator
inplace_erasure, // Extraction operator
>;
See complete example: https://ideone.com/C49bDM
Edit: deriving from identity_based_inplace_combine<Type>
(boost/icl/functors.hpp
) does the following:
- Defines
first_argument_type
,second_argument_type
andresult_type
. - It is used to create the
identity_element
value for that type (a static function). For example the identity value for a set is an empty set, for an integer it is0
(useful when you do a sum), etc.
It is not mandatory to use it if your map is partial_enricher
or total_enricher
, since in this case the map will contain entries for any values, including identity values. You will need it for the absorber
types, since the map needs to know whether it can drop the interval or not in that case.
An alternative:
// interval_map combiner functor: assigns new value if key exists
template <class Type>
struct inplace_replace {
typedef void result_type;
typedef Type& first_argument_type;
typedef const Type& second_argument_type;
void operator()(Type &object, const Type &operand) const {
object = operand;
}
};
Note: older boost ICL implementations derive from std::binary_function
instead of using these typedefs. Unfortunately this is deprecated in C++11 and removed in C++17, so I would try not to use it in your own code. The latest versions implement the functors like the snippet above.
Same example: https://ideone.com/lMLEDw