1
#pragma once
2

            
3
#include <utility>
4
#include <vector>
5

            
6
#include "envoy/common/pure.h"
7

            
8
namespace Envoy {
9

            
10
/**
11
 * Maintains sets of numeric intervals. As new intervals are added, existing ones in the
12
 * set are combined so that no overlapping intervals remain in the representation.
13
 *
14
 * Value can be any type that is comparable with <, ==, and >.
15
 */
16
template <typename Value> class IntervalSet {
17
public:
18
10962765
  virtual ~IntervalSet() = default;
19

            
20
  using Interval = std::pair<Value, Value>;
21

            
22
  /**
23
   * Inserts a new interval into the set, merging any overlaps. The intervals are in
24
   * the form [left_inclusive, right_exclusive). E.g. an interval [3, 5) includes the
25
   * numbers 3 and 4, but not 5.
26
   * @param left_inclusive Value the left-bound, inclusive.
27
   * @param right_exclusive Value the right-bound, which is exclusive.
28
   */
29
  virtual void insert(Value left_inclusive, Value right_exclusive) PURE;
30

            
31
  /**
32
   * @return std::vector<Interval> the interval-set as a vector.
33
   */
34
  virtual std::vector<Interval> toVector() const PURE;
35

            
36
  /**
37
   * Clears the contents of the interval set.
38
   */
39
  virtual void clear() PURE;
40

            
41
  /**
42
   * Determines whether the specified Value is in any of the intervals.
43
   *
44
   * @param value the value
45
   * @return true if value is covered in the inteval set.
46
   */
47
  virtual bool test(Value value) const PURE;
48
};
49

            
50
} // namespace Envoy