ScummVM API documentation
containers.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 ULTIMA_STD_CONTAINERS_H
23 #define ULTIMA_STD_CONTAINERS_H
24 
25 #include "common/algorithm.h"
26 #include "common/array.h"
27 #include "common/hashmap.h"
28 #include "common/hash-str.h"
29 #include "common/list.h"
30 #include "common/queue.h"
31 #include "common/stack.h"
32 #include "common/util.h"
33 
34 namespace Ultima {
35 namespace Std {
36 
37 template<class T>
38 class vector : public Common::Array<T> {
39 public:
41  private:
42  vector<T> *_owner;
43  int _index;
44  public:
45  reverse_iterator(vector<T> *owner, int index) : _owner(owner), _index(index) {}
46  reverse_iterator() : _owner(0), _index(-1) {}
47 
48  T &operator*() { return (*_owner)[_index]; }
49 
50  reverse_iterator &operator++() {
51  --_index;
52  return *this;
53  }
54 
55  bool operator==(const reverse_iterator &rhs) {
56  return _owner == rhs._owner && _index == rhs._index;
57  }
58  bool operator!=(const reverse_iterator &rhs) {
59  return !operator==(rhs);
60  }
61  };
62 
64  private:
65  const vector<T> *_owner;
66  int _index;
67  public:
68  const_reverse_iterator(const vector<T> *owner, int index) : _owner(owner), _index(index) {
69  }
70  const_reverse_iterator() : _owner(0), _index(-1) {
71  }
72 
73  const T operator*() const {
74  return (*_owner)[_index];
75  }
76 
77  const_reverse_iterator &operator++() {
78  --_index;
79  return *this;
80  }
81 
82  bool operator==(const const_reverse_iterator &rhs) {
83  return _owner == rhs._owner && _index == rhs._index;
84  }
85  bool operator!=(const const_reverse_iterator &rhs) {
86  return !operator==(rhs);
87  }
88  };
89 public:
90  constexpr vector() : Common::Array<T>() {}
91  vector(size_t newSize) : Common::Array<T>(newSize) {}
92  vector(size_t newSize, const T elem) : Common::Array<T>(newSize, elem) {}
93 
94  reverse_iterator rbegin() {
95  return reverse_iterator(this, (int)Common::Array<T>::size() - 1);
96  }
97  reverse_iterator rend() {
98  return reverse_iterator(this, -1);
99  }
100  const_reverse_iterator rbegin() const {
101  return const_reverse_iterator(this, (int)Common::Array<T>::size() - 1);
102  }
103  const_reverse_iterator rend() const {
104  return const_reverse_iterator(this, -1);
105  }
106 };
107 
108 template<class T>
109 class set {
110  struct Comparitor {
111  bool operator()(const T &a, const T &b) const {
112  return a == b;
113  }
114  };
115 private:
116  Common::Array<T> _items;
117  Comparitor _comparitor;
118 public:
119  typedef T *iterator;
120  typedef const T *const_iterator;
121 
122  iterator begin() { return _items.begin(); }
123  iterator end() { return _items.end(); }
124  const_iterator begin() const { return _items.begin(); }
125  const_iterator end() const { return _items.end(); }
126 
130  void clear() {
131  _items.clear();
132  }
133 
137  void insert(T val) {
138  _items.push_back(val);
139  Common::sort(begin(), end(), _comparitor);
140  }
141 
145  void insert(iterator first, iterator last) {
146  for (; first != last; ++first)
147  _items.push_back(*first);
148  Common::sort(begin(), end(), _comparitor);
149  }
150 
154  void swap(set<T> &arr) {
155  _items.swap(arr);
156  }
157 
161  iterator find(const T item) {
162  iterator it = begin();
163  for (; it != end() && *it != item; ++it) {}
164  return it;
165  }
166  const_iterator find(const T item) const {
167  const_iterator it = begin();
168  for (; it != end() && *it != item; ++it) {
169  }
170  return it;
171  }
172 };
173 
174 template<class T>
175 class list : public Common::List<T> {
176 public:
178  private:
179  typename Common::List<T>::iterator _it;
180  public:
181  reverse_iterator(typename Common::List<T>::iterator it) : _it(it) {}
182  reverse_iterator() {}
183 
184  T operator*() const { return *_it; }
185 
186  reverse_iterator &operator++() {
187  --_it;
188  return *this;
189  }
190 
191  bool operator==(const reverse_iterator &rhs) { return _it == rhs._it; }
192  bool operator!=(const reverse_iterator &rhs) { return _it != rhs._it; }
193  };
194 public:
195  reverse_iterator rbegin() {
197  }
198  reverse_iterator rend() {
200  }
201 };
202 
208 template <class _Ty, class _Container, class _Pr>
210 public:
211  priority_queue() : c(), comp() {}
212 
213  explicit priority_queue(const _Pr &_Pred) : c(), comp(_Pred) {}
214 
215  priority_queue(const _Pr &_Pred, const _Container &_Cont) : c(_Cont), comp(_Pred) {
216  make_heap(c.begin(), c.end(), comp);
217  }
218 
219  template <class _InIt>
220  priority_queue(_InIt _First, _InIt _Last, const _Pr &_Pred, const _Container &_Cont) : c(_Cont), comp(_Pred) {
221  c.insert(c.end(), _First, _Last);
222  make_heap(c.begin(), c.end(), comp);
223  }
224 
225  template <class _InIt>
226  priority_queue(_InIt _First, _InIt _Last) : c(_First, _Last), comp() {
227  make_heap(c.begin(), c.end(), comp);
228  }
229 
230  template <class _InIt>
231  priority_queue(_InIt _First, _InIt _Last, const _Pr &_Pred) : c(_First, _Last), comp(_Pred) {
232  make_heap(c.begin(), c.end(), comp);
233  }
234 
235  bool empty() const {
236  return c.empty();
237  }
238 
239  size_t size() const {
240  return c.size();
241  }
242 
243  typename _Container::const_reference top() const {
244  return c.back();
245  }
246 
247  void push(const typename _Container::value_type &_Val) {
248  c.push_back(_Val);
249  Common::sort(c.begin(), c.end(), comp);
250  }
251 
252  void pop() {
253  c.pop_back();
254  }
255 
256  void swap(priority_queue &_Right) {
257  SWAP(c, _Right.c);
258  SWAP(comp, _Right.comp);
259  }
260 
261 protected:
262  _Container c;
263  _Pr comp;
264 };
265 
266 } // End of namespace Std
267 } // End of namespace Ultima
268 
269 #endif
Definition: containers.h:109
Definition: containers.h:40
Definition: array.h:52
void clear()
Definition: containers.h:130
void clear()
Definition: array.h:323
iterator end()
Definition: array.h:382
iterator begin()
Definition: array.h:377
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: list.h:44
T * iterator
Definition: array.h:54
void SWAP(T &a, T &b)
Definition: util.h:84
void insert(T val)
Definition: containers.h:137
Definition: detection.h:27
bool empty() const
Definition: array.h:354
void push_back(const T &element)
Definition: array.h:183
iterator find(const T item)
Definition: containers.h:161
const T * const_iterator
Definition: array.h:55
Definition: containers.h:209
void insert(iterator first, iterator last)
Definition: containers.h:145
size_type size() const
Definition: array.h:318
Definition: containers.h:175
Definition: containers.h:177
Definition: list_intern.h:54
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349
Definition: containers.h:38
Definition: algorithm.h:37
void swap(set< T > &arr)
Definition: containers.h:154