ScummVM API documentation
multiset.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_MULTISET_H
23 #define HPL1_STD_MULTISET_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 multiset {
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 
77  iterator insert(const T &item) {
78  return _items.insert(item);
79  }
80 
81  void erase(iterator item) {
82  _items.erase(item);
83  }
84 
85  void erase(iterator first, iterator last) {
86  _items.erase(first, last);
87  }
88 
89  size_t erase(const T &item) {
90  size_t total = 0;
91  for (auto it = _items.lowerBound(item); it != end() && CompareEq(*it, item);) {
92  _items.erase(it++);
93  ++total;
94  }
95  return total;
96  }
97 
101  size_t count(const T &item) const {
102  size_t total = 0;
103  for (auto it = _items.lowerBound(item); it != end() && CompareEq(*it, item); ++it)
104  ++total;
105  return total;
106  }
107 
108 private:
109  static bool CompareEq(const T &a, const T &b) {
110  return !CompFn()(a, b) && !CompFn()(b, a);
111  }
112 
113  TreeT _items;
114 };
115 
116 } // namespace Std
117 
118 } // namespace Hpl1
119 
120 #endif
BasicIterator lowerBound(const Key &key)
Definition: rb_tree.h:181
BasicIterator insert(const ValueType &val)
Definition: rb_tree.h:302
iterator find(const T &item)
Definition: multiset.h:69
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
size_t size() const
Definition: rb_tree.h:327
Definition: multiset.h:31
Definition: algorithm.h:37
BasicIterator begin()
Definition: rb_tree.h:166
Definition: algorithms.h:27
BasicIterator end()
Definition: rb_tree.h:172
size_t count(const T &item) const
Definition: multiset.h:101