fork(2) download
  1.  
  2. #include <boost/icl/interval_map.hpp>
  3. #include <iostream>
  4.  
  5. using namespace boost::icl;
  6.  
  7. // interval_map combiner functor: assigns new value if key exists
  8. template <class Type>
  9. struct inplace_replace : identity_based_inplace_combine<Type> {
  10. void operator()(Type &object, const Type &operand) const { object = operand; }
  11. };
  12.  
  13. template<>
  14. inline std::string unary_template_to_string<inplace_replace>::apply() {
  15. return "=";
  16. }
  17.  
  18. /* When adding, if interval exists, replaces value.
  19.  * When subtracting, if interval exists, removes value.
  20.  */
  21. using ival_map =
  22. interval_map<unsigned, // Key
  23. unsigned, // Value
  24. partial_enricher, // Unmapped intervals have unkown value;
  25. // store identity values
  26. std::less, // Comparator
  27. inplace_replace, // Combination operator
  28. inplace_erasure, // Extraction operator
  29. closed_interval<unsigned, std::less> // Interval type
  30. >;
  31.  
  32. using ival = ival_map::interval_type;
  33.  
  34. std::ostream &operator<<(std::ostream &os, const ival_map &m) {
  35. for (auto &entry : m) {
  36. os << entry.first << " : " << entry.second << "\n";
  37. }
  38. return os;
  39. }
  40.  
  41. int main() {
  42. ival_map m;
  43.  
  44. m.add(std::make_pair(ival(0, 9), 1));
  45. std::cout << m << "\n\n";
  46.  
  47. m.add(std::make_pair(ival(3, 6), 3));
  48. std::cout << m << "\n\n";
  49.  
  50. m.add(std::make_pair(ival(3, 6), 1));
  51. std::cout << m << "\n\n";
  52.  
  53. m.subtract(std::make_pair(ival(3, 6), 3));
  54. std::cout << m << "\n\n";
  55.  
  56. m.subtract(std::make_pair(ival(3, 6), 1));
  57. std::cout << m << "\n\n";
  58.  
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
[0,9] : 1


[0,2] : 1
[3,6] : 3
[7,9] : 1


[0,9] : 1


[0,9] : 1


[0,2] : 1
[3,6] : 0
[7,9] : 1