ScummVM API documentation
set.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef HPL1_STD_SET_H
23 #define HPL1_STD_SET_H
24 
25 #include "common/rb_tree.h"
26 
27 namespace Hpl1 {
28 namespace Std {
29 
30 template<class T, class CompFn = Common::Less<T> >
31 class set {
33 
34 public:
35  using iterator = typename TreeT::BasicIterator;
36  using const_iterator = typename TreeT::ConstIterator;
37 
38  iterator begin() {
39  return _items.begin();
40  }
41 
42  iterator end() {
43  return _items.end();
44  }
45 
46  const_iterator begin() const {
47  return _items.begin();
48  }
49 
50  const_iterator end() const {
51  return _items.end();
52  }
53 
54  void clear() {
55  _items.clear();
56  }
57 
58  bool empty() {
59  return _items.size() == 0;
60  }
61 
62  size_t size() {
63  return _items.size();
64  }
65 
69  iterator find(const T &item) {
70  const auto it = _items.lowerBound(item);
71  if (it != _items.end() && CompareEq(*it, item)) {
72  return it;
73  }
74  return _items.end();
75  }
76 
81  const auto it = _items.lowerBound(item);
82  if (it == _items.end() || !CompareEq(*it, item)) {
83  const auto position = _items.insert(it, item);
84  return {position, true};
85  }
86  return {it, false};
87  }
88 
89  void erase(iterator item) {
90  _items.erase(item);
91  }
92 
93  void erase(iterator first, iterator last) {
94  _items.erase(first, last);
95  }
96 
97  size_t erase(const T &item) {
98  iterator it = find(item);
99  if (it != end()) {
100  _items.erase(it);
101  return 1;
102  }
103  return 0;
104  }
105 
109  size_t count(const T &item) const {
110  size_t total = 0;
111  for (auto it = _items.lowerBound(item); it != end() && CompareEq(*it, item); ++it)
112  ++total;
113  return total;
114  }
115 
116 private:
117  static bool CompareEq(const T &a, const T &b) {
118  return !CompFn()(a, b) && !CompFn()(b, a);
119  }
120 
121  TreeT _items;
122 };
123 
124 } // namespace Std
125 } // namespace Hpl1
126 
127 #endif
Definition: util.h:213
BasicIterator lowerBound(const Key &key)
Definition: rb_tree.h:181
BasicIterator insert(const ValueType &val)
Definition: rb_tree.h:302
size_t count(const T &item) const
Definition: set.h:109
iterator find(const T &item)
Definition: set.h:69
Definition: set.h:31
Iterator< const T &, const T *> ConstIterator
Definition: rb_tree.h:123
Iterator< T &, T *> BasicIterator
Definition: rb_tree.h:122
BasicIterator erase(BasicIterator it)
Definition: rb_tree.h:250
void clear()
Definition: rb_tree.h:158
Common::Pair< iterator, bool > insert(const T &item)
Definition: set.h:80
size_t size() const
Definition: rb_tree.h:327
Definition: algorithm.h:37
BasicIterator begin()
Definition: rb_tree.h:166
Definition: algorithms.h:27
BasicIterator end()
Definition: rb_tree.h:172