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  typedef T reference;
91  typedef const T const_reference;
92 
93  vector() : Common::Array<T>() {}
94  vector(size_t newSize) : Common::Array<T>(newSize) {}
95  vector(size_t newSize, const T elem) : Common::Array<T>(newSize, elem) {}
96 
97  reverse_iterator rbegin() {
98  return reverse_iterator(this, (int)Common::Array<T>::size() - 1);
99  }
100  reverse_iterator rend() {
101  return reverse_iterator(this, -1);
102  }
103  const_reverse_iterator rbegin() const {
104  return const_reverse_iterator(this, (int)Common::Array<T>::size() - 1);
105  }
106  const_reverse_iterator rend() const {
107  return const_reverse_iterator(this, -1);
108  }
109 
110  void pop_front() {
112  }
113 
114  T at(size_t index) const {
115  return (*this)[index];
116  }
117 };
118 
119 template<class T>
120 class set {
121  struct Comparitor {
122  bool operator()(const T &a, const T &b) const {
123  return a == b;
124  }
125  };
126 private:
127  Common::Array<T> _items;
128  Comparitor _comparitor;
129 public:
130  typedef T *iterator;
131  typedef const T *const_iterator;
132 
133  iterator begin() { return _items.begin(); }
134  iterator end() { return _items.end(); }
135  const_iterator begin() const { return _items.begin(); }
136  const_iterator end() const { return _items.end(); }
137 
141  void clear() {
142  _items.clear();
143  }
144 
148  void insert(T val) {
149  _items.push_back(val);
150  Common::sort(begin(), end(), _comparitor);
151  }
152 
156  void insert(iterator first, iterator last) {
157  for (; first != last; ++first)
158  _items.push_back(*first);
159  Common::sort(begin(), end(), _comparitor);
160  }
161 
165  void swap(set<T> &arr) {
166  _items.swap(arr);
167  }
168 
172  iterator find(const T item) {
173  iterator it = begin();
174  for (; it != end() && *it != item; ++it) {}
175  return it;
176  }
177  const_iterator find(const T item) const {
178  const_iterator it = begin();
179  for (; it != end() && *it != item; ++it) {
180  }
181  return it;
182  }
183 };
184 
185 template<class VAL>
186 class deque : public Common::List<VAL> {
187 public:
188  VAL operator[](uint index) {
189  for (typename Common::List<VAL>::iterator it = this->begin();
190  it != this->end(); ++it, --index) {
191  if (index == 0)
192  return *it;
193  }
194 
195  error("Invalid index");
196  }
197 };
198 
199 template<class T>
200 class list : public Common::List<T> {
201 public:
203  private:
204  typename Common::List<T>::iterator _it;
205  public:
206  reverse_iterator(typename Common::List<T>::iterator it) : _it(it) {}
207  reverse_iterator() {}
208 
209  T operator*() const { return *_it; }
210 
211  reverse_iterator &operator++() {
212  --_it;
213  return *this;
214  }
215 
216  bool operator==(const reverse_iterator &rhs) { return _it == rhs._it; }
217  bool operator!=(const reverse_iterator &rhs) { return _it != rhs._it; }
218  };
219 public:
220  reverse_iterator rbegin() {
222  }
223  reverse_iterator rend() {
225  }
226 };
227 
233 template <class _Ty, class _Container, class _Pr>
235 public:
236  priority_queue() : c(), comp() {}
237 
238  explicit priority_queue(const _Pr &_Pred) : c(), comp(_Pred) {}
239 
240  priority_queue(const _Pr &_Pred, const _Container &_Cont) : c(_Cont), comp(_Pred) {
241  make_heap(c.begin(), c.end(), comp);
242  }
243 
244  template <class _InIt>
245  priority_queue(_InIt _First, _InIt _Last, const _Pr &_Pred, const _Container &_Cont) : c(_Cont), comp(_Pred) {
246  c.insert(c.end(), _First, _Last);
247  make_heap(c.begin(), c.end(), comp);
248  }
249 
250  template <class _InIt>
251  priority_queue(_InIt _First, _InIt _Last) : c(_First, _Last), comp() {
252  make_heap(c.begin(), c.end(), comp);
253  }
254 
255  template <class _InIt>
256  priority_queue(_InIt _First, _InIt _Last, const _Pr &_Pred) : c(_First, _Last), comp(_Pred) {
257  make_heap(c.begin(), c.end(), comp);
258  }
259 
260  bool empty() const {
261  return c.empty();
262  }
263 
264  size_t size() const {
265  return c.size();
266  }
267 
268  typename _Container::const_reference top() const {
269  return c.back();
270  }
271 
272  void push(const typename _Container::value_type &_Val) {
273  c.push_back(_Val);
274  Common::sort(c.begin(), c.end(), comp);
275  }
276 
277  void pop() {
278  c.pop_back();
279  }
280 
281  void swap(priority_queue &_Right) {
282  SWAP(c, _Right.c);
283  SWAP(comp, _Right.comp);
284  }
285 
286 protected:
287  _Container c;
288  _Pr comp;
289 };
290 
291 } // End of namespace Std
292 } // End of namespace Ultima
293 
294 #endif
Definition: containers.h:120
Definition: containers.h:40
Definition: containers.h:186
Definition: array.h:52
void clear()
Definition: containers.h:141
void clear()
Definition: array.h:320
iterator end()
Definition: array.h:379
iterator begin()
Definition: array.h:374
In find(In first, In last, const T &v)
Definition: algorithm.h:225
Definition: list.h:44
T & operator[](size_type idx)
Definition: array.h:273
T * iterator
Definition: array.h:54
void SWAP(T &a, T &b)
Definition: util.h:82
void insert(T val)
Definition: containers.h:148
Definition: detection.h:27
bool empty() const
Definition: array.h:351
void push_back(const T &element)
Definition: array.h:180
iterator find(const T item)
Definition: containers.h:172
const T * const_iterator
Definition: array.h:55
Definition: containers.h:234
void insert(iterator first, iterator last)
Definition: containers.h:156
size_type size() const
Definition: array.h:315
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: containers.h:200
Definition: containers.h:202
T remove_at(size_type idx)
Definition: array.h:260
Definition: list_intern.h:51
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:165