aboutsummaryrefslogtreecommitdiff
path: root/snippets/discretization.h
blob: 25da7a2b2c24698ece96722ca19203b5681477c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <algorithm>
#include <vector>

template <typename T, typename Compare = std::less<T>>
struct Discretization : public std::vector<T> {
  explicit Discretization() = default;

  explicit Discretization(const std::vector<T> &a_) : std::vector<T>(a_) {
    normalize();
  }

  void normalize() {
    std::ranges::sort(*this, Compare{});
    auto [first, last] = std::ranges::unique(*this);
    this->erase(first, last);
  }

  int index(T x) const {
    return std::ranges::lower_bound(*this, x, Compare{}) - this->begin();
  }
};