m2etis  0.4
SegmentTree.h
Go to the documentation of this file.
1 /*
2  Copyright (2016) Michael Baer, Daniel Bonrath, All rights reserved.
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15  */
16 
22 #ifndef __M2ETIS_UTIL_SEGMENTTREE_H__
23 #define __M2ETIS_UTIL_SEGMENTTREE_H__
24 
25 #include <queue>
26 
27 #include "m2etis/util/Exceptions.h"
28 
29 namespace m2etis {
30 namespace util {
31 
36  template<typename T>
37  class SegmentTree {
38  public:
39  SegmentTree() : _elements() {
40  }
41 
43  _elements.clear();
44  }
45 
49  void insert(const T & value) {
50  if (contains(value)) {
51  return;
52  }
53 
54  if (_elements.empty()) {
55  _elements.push_back(std::make_pair(value, value));
56  } else {
57  if (value < _elements[0].first) {
58  if (_elements[0].first - value == 1) {
59  _elements[0].first = value;
60  } else {
61  _elements.insert(_elements.begin(), std::make_pair(value, value));
62  }
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;
66  } else {
67  _elements.push_back(std::make_pair(value, value));
68  }
69  } else {
70  for (unsigned int i = 0; i < _elements.size() - 1; ++i) {
71  if (value - _elements[i].second == 1) {
72  _elements[i].second = value;
73 
74  if (_elements[i + 1].first - value == 1) {
75  _elements[i].second = _elements[i + 1].second;
76  _elements.erase(_elements.begin() + i + 1);
77  }
78  break;
79  } else if (_elements[i + 1].first - value == 1) {
80  _elements[i + 1].first = value;
81  break;
82  } else if (value > _elements[i].second && value < _elements[i + 1].first) {
83  _elements.insert(_elements.begin() + i + 1, std::make_pair(value, value));
84  break;
85  }
86  }
87  }
88  }
89  }
90 
94  bool contains(const T & value) {
95  if (_elements.empty()) {
96  return false;
97  }
98 
99  if (value < _elements[0].first || value > _elements[_elements.size() - 1].second) {
100  return false;
101  }
102 
103  for (unsigned int i = 0; i < _elements.size(); ++i) {
104  if (value >= _elements[i].first && value <= _elements[i].second) {
105  return true;
106  }
107  }
108 
109  return false;
110  }
111 
115  size_t size() const {
116  return _elements.size();
117  }
118 
122  size_t count() const {
123  size_t counter = 0;
124 
125  for (size_t i = 0; i < _elements.size(); ++i) {
126  counter += size_t(_elements[i].second - _elements[i].first + 1);
127  }
128 
129  return counter;
130  }
131 
132  private:
133  std::vector<std::pair<T, T>> _elements;
134  };
135 
136 } /* namespace util */
137 } /* namespace m2etis */
138 
139 #endif /* __M2ETIS_UTIL_SEGMENTTREE_H__ */
140 
SegmentTree handles integer values and stores segments of set values e.g. inserting values 2...
Definition: SegmentTree.h:37
size_t count() const
returns the amount of stored elements
Definition: SegmentTree.h:122
size_t size() const
returns the amount of segments, is always <= count()
Definition: SegmentTree.h:115
bool contains(const T &value)
returns whether value is stored in the tree or not
Definition: SegmentTree.h:94
void insert(const T &value)
inserts a value into the tree
Definition: SegmentTree.h:49