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 /********************************************
23  DISCLAIMER:
24 
25  This is a wrapper code to mimic the relevant std:: class
26  Please use it ONLY when porting an existing code e.g. from the original sources
27 
28  For all new development please use classes from Common::
29  *********************************************/
30 
31 #ifndef COMMON_STD_SET_H
32 #define COMMON_STD_SET_H
33 
34 #include "common/array.h"
35 
36 namespace Std {
37 
41 template<class T, class Comparitor = Common::Less<T> >
42 class set : public Common::SortedArray<T, const T &> {
43 private:
44  static int ComparatorFn(const T &a, const T &b) {
45  return Comparitor().operator()(a, b) ? -1 : 0;
46  }
47 
48  static bool CompareEq(const T &a, const T &b) {
49  return !ComparatorFn(a, b) && !ComparatorFn(b, a);
50  }
51 
52 public:
53  struct Entry {
54  const T &_value;
55  Entry(const T &item) : _value(item) {
56  }
57  };
58 public:
59  using iterator = typename Common::SortedArray<T, const T &>::iterator;
61 
65  set() : Common::SortedArray<T, const T & >(ComparatorFn) {}
66 
70  iterator find(const T &item) {
71  iterator begin = this->begin();
72  iterator end = this->end();
73  while (begin < end) {
74  iterator mid = begin + (Common::distance(begin, end) / 2);
75  if (ComparatorFn(item, *mid))
76  end = mid;
77  else if (ComparatorFn(*mid, item))
78  begin = mid + 1;
79  else
80  return mid;
81  }
82  return this->end();
83  }
84 
88  Entry insert(const T &item) {
90  return Entry(item);
91  }
92 
96  void erase(iterator item) {
98  }
99 
103  void erase(iterator first, iterator last) {
105  }
106 
111  size_t erase(const T &item) {
112  iterator first = find(item);
113  if (first == this->end())
114  return 0;
115  iterator end = first + 1;
116  while (end != this->end() && CompareEq(*first, *end)) {
117  ++end;
118  }
119  size_t erased = Common::distance(first, end);
120  this->erase(first, end);
121  return erased;
122  }
123 
127  size_t count(const T item) const {
128  size_t total = 0;
129  for (const_iterator it = this->begin(); it != this->end(); ++it) {
130  if (CompareEq(*it, item))
131  ++total;
132  else if (!ComparatorFn(item, *it))
133  // Passed beyond possibility of matches
134  break;
135  }
136 
137  return total;
138  }
139 };
140 
141 } // namespace Std
142 
143 #endif
size_t erase(const T &item)
Definition: set.h:111
iterator find(const T &item)
Definition: set.h:70
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
T * iterator
Definition: array.h:54
Entry insert(const T &item)
Definition: set.h:88
const T * const_iterator
Definition: array.h:55
void erase(iterator first, iterator last)
Definition: set.h:103
void erase(iterator item)
Definition: set.h:96
Definition: set.h:42
Definition: array.h:558
size_t count(const T item) const
Definition: set.h:127
Definition: string.h:62
Definition: set.h:53
void insert(const T &element)
Definition: array.h:571
Definition: algorithm.h:37
iterator erase(iterator pos)
Definition: array.h:328