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 AGS_STD_SET_H
23 #define AGS_STD_SET_H
24 
25 #include "common/array.h"
26 
27 namespace AGS3 {
28 namespace std {
29 
33 template<class T, class Comparitor = Common::Less<T> >
34 class set : public Common::SortedArray<T, const T &> {
35 private:
36  static int ComparatorFn(const T &a, const T &b) {
37  return Comparitor().operator()(a, b) ? -1 : 0;
38  }
39 
40  static bool CompareEq(const T &a, const T &b) {
41  return !ComparatorFn(a, b) && !ComparatorFn(b, a);
42  }
43 
44 public:
45  struct Entry {
46  const T &_value;
47  Entry(const T &item) : _value(item) {
48  }
49  };
50 public:
51  using iterator = typename Common::SortedArray<T, const T &>::iterator;
53 
57  set() : Common::SortedArray<T, const T & >(ComparatorFn) {}
58 
62  iterator find(const T &item) {
63  iterator begin = this->begin();
64  iterator end = this->end();
65  while (begin < end) {
66  iterator mid = begin + (Common::distance(begin, end) / 2);
67  if (ComparatorFn(item, *mid))
68  end = mid;
69  else if (ComparatorFn(*mid, item))
70  begin = mid + 1;
71  else
72  return mid;
73  }
74  return this->end();
75  }
76 
80  Entry insert(const T &item) {
82  return Entry(item);
83  }
84 
88  void erase(iterator item) {
90  }
91 
95  void erase(iterator first, iterator last) {
97  }
98 
103  size_t erase(const T &item) {
104  iterator first = find(item);
105  if (first == this->end())
106  return 0;
107  iterator end = first + 1;
108  while (end != this->end() && CompareEq(*first, *end)) {
109  ++end;
110  }
111  size_t erased = Common::distance(first, end);
112  this->erase(first, end);
113  return erased;
114  }
115 
119  size_t count(const T item) const {
120  size_t total = 0;
121  for (const_iterator it = this->begin(); it != this->end(); ++it) {
122  if (CompareEq(*it, item))
123  ++total;
124  else if (!ComparatorFn(item, *it))
125  // Passed beyond possibility of matches
126  break;
127  }
128 
129  return total;
130  }
131 };
132 
133 } // namespace std
134 } // namespace AGS3
135 
136 #endif
iterator end()
Definition: array.h:339
iterator begin()
Definition: array.h:334
T * iterator
Definition: array.h:54
iterator find(const T &item)
Definition: set.h:62
const T * const_iterator
Definition: array.h:55
size_t erase(const T &item)
Definition: set.h:103
Entry insert(const T &item)
Definition: set.h:80
Definition: set.h:34
Definition: array.h:518
Definition: string.h:62
size_t count(const T item) const
Definition: set.h:119
void erase(iterator first, iterator last)
Definition: set.h:95
void insert(const T &element)
Definition: array.h:531
void erase(iterator item)
Definition: set.h:88
Definition: ags.h:40
Definition: set.h:45
iterator erase(iterator pos)
Definition: array.h:288