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 ULTIMA8_MISC_SET_H
23#define ULTIMA8_MISC_SET_H
24
25#include "common/algorithm.h"
26#include "common/array.h"
27
28namespace Ultima {
29namespace Ultima8 {
30
31template<class T>
32class Set {
33 struct Comparitor {
34 bool operator()(const T &a, const T &b) const {
35 return a == b;
36 }
37 };
38private:
39 Common::Array<T> _items;
40 Comparitor _comparitor;
41public:
42 typedef T *iterator;
43 typedef const T *const_iterator;
44
45 iterator begin() { return _items.begin(); }
46 iterator end() { return _items.end(); }
47 const_iterator begin() const { return _items.begin(); }
48 const_iterator end() const { return _items.end(); }
49
53 void clear() {
54 _items.clear();
55 }
56
60 void insert(T val) {
61 _items.push_back(val);
62 Common::sort(begin(), end(), _comparitor);
63 }
64
68 void insert(iterator first, iterator last) {
69 for (; first != last; ++first)
70 _items.push_back(*first);
71 Common::sort(begin(), end(), _comparitor);
72 }
73
77 void swap(Set<T> &arr) {
78 _items.swap(arr);
79 }
80
84 iterator find(const T item) {
85 iterator it = begin();
86 for (; it != end() && *it != item; ++it) {}
87 return it;
88 }
89 const_iterator find(const T item) const {
90 const_iterator it = begin();
91 for (; it != end() && *it != item; ++it) {
92 }
93 return it;
94 }
95};
96
97} // End of namespace Ultima8
98} // End of namespace Ultima
99
100#endif
Definition: array.h:52
iterator end()
Definition: array.h:380
void clear()
Definition: array.h:321
void push_back(const T &element)
Definition: array.h:181
iterator begin()
Definition: array.h:375
Definition: set.h:32
void insert(iterator first, iterator last)
Definition: set.h:68
void insert(T val)
Definition: set.h:60
void clear()
Definition: set.h:53
void swap(Set< T > &arr)
Definition: set.h:77
iterator find(const T item)
Definition: set.h:84
void sort(T first, T last, StrictWeakOrdering comp)
Definition: algorithm.h:349