22 #ifndef __M2ETIS_UTIL_SEGMENTTREE_H__
23 #define __M2ETIS_UTIL_SEGMENTTREE_H__
54 if (_elements.empty()) {
55 _elements.push_back(std::make_pair(value, value));
57 if (value < _elements[0].first) {
58 if (_elements[0].first - value == 1) {
59 _elements[0].first = value;
61 _elements.insert(_elements.begin(), std::make_pair(value, value));
63 }
else if (value > _elements[_elements.size() - 1].second) {
64 if (value - _elements[_elements.size() - 1].second == 1) {
65 _elements[_elements.size() - 1].second = value;
67 _elements.push_back(std::make_pair(value, value));
70 for (
unsigned int i = 0; i < _elements.size() - 1; ++i) {
71 if (value - _elements[i].second == 1) {
72 _elements[i].second = value;
74 if (_elements[i + 1].first - value == 1) {
75 _elements[i].second = _elements[i + 1].second;
76 _elements.erase(_elements.begin() + i + 1);
79 }
else if (_elements[i + 1].first - value == 1) {
80 _elements[i + 1].first = value;
82 }
else if (value > _elements[i].second && value < _elements[i + 1].first) {
83 _elements.insert(_elements.begin() + i + 1, std::make_pair(value, value));
95 if (_elements.empty()) {
99 if (value < _elements[0].first || value > _elements[_elements.size() - 1].second) {
103 for (
unsigned int i = 0; i < _elements.size(); ++i) {
104 if (value >= _elements[i].first && value <= _elements[i].second) {
116 return _elements.size();
125 for (
size_t i = 0; i < _elements.size(); ++i) {
126 counter += size_t(_elements[i].second - _elements[i].first + 1);
133 std::vector<std::pair<T, T>> _elements;
SegmentTree handles integer values and stores segments of set values e.g. inserting values 2...
size_t count() const
returns the amount of stored elements
size_t size() const
returns the amount of segments, is always <= count()
bool contains(const T &value)
returns whether value is stored in the tree or not
void insert(const T &value)
inserts a value into the tree