fork(1) 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 {
  10. typedef void result_type;
  11. typedef Type& first_argument_type;
  12. typedef const Type& second_argument_type;
  13. void operator()(Type &object, const Type &operand) const { object = operand; }
  14. };
  15.  
  16. template<>
  17. inline std::string unary_template_to_string<inplace_replace>::apply() {
  18. return "=";
  19. }
  20.  
  21. /* When adding, if interval exists, replaces value.
  22.  * When subtracting, if interval exists, removes value.
  23.  */
  24. using ival_map =
  25. interval_map<unsigned, // Key
  26. unsigned, // Value
  27. partial_enricher, // Unmapped intervals have unkown value;
  28. // store identity values
  29. std::less, // Comparator
  30. inplace_replace, // Combination operator
  31. inplace_erasure, // Extraction operator
  32. closed_interval<unsigned, std::less> // Interval type
  33. >;
  34.  
  35. using ival = ival_map::interval_type;
  36.  
  37. std::ostream &operator<<(std::ostream &os, const ival_map &m) {
  38. for (auto &entry : m) {
  39. os << entry.first << " : " << entry.second << "\n";
  40. }
  41. return os;
  42. }
  43.  
  44. int main() {
  45. ival_map m;
  46.  
  47. m.add(std::make_pair(ival(0, 9), 1));
  48. std::cout << m << "\n\n";
  49.  
  50. m.add(std::make_pair(ival(3, 6), 3));
  51. std::cout << m << "\n\n";
  52.  
  53. m.add(std::make_pair(ival(3, 6), 1));
  54. std::cout << m << "\n\n";
  55.  
  56. m.subtract(std::make_pair(ival(3, 6), 3));
  57. std::cout << m << "\n\n";
  58.  
  59. m.subtract(std::make_pair(ival(3, 6), 1));
  60. std::cout << m << "\n\n";
  61.  
  62. return 0;
  63. }
  64.  
Success #stdin #stdout 0s 4536KB
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