ScummVM API documentation
map.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_MAP_H
32#define COMMON_STD_MAP_H
33
34#include "common/hashmap.h"
35#include "common/std/utility.h"
36
37namespace Std {
38
39template<class Key, class Val, class CompFunc = Common::Less<Key> >
40class map {
41 struct KeyValue {
42 Key _key;
43 Val _value;
44 };
45private:
47 CompFunc _comp;
48 public:
49 using iterator = typename Common::Array<KeyValue>::iterator;
50 using const_iterator = typename Common::Array<KeyValue>::const_iterator;
51
55 void clear() {
56 _items.clear();
57 }
58
62 iterator begin() {
63 return _items.begin();
64 }
65
69 iterator end() {
70 return _items.end();
71 }
72
76 const_iterator begin() const {
77 return _items.begin();
78 }
79
83 const_iterator end() const {
84 return _items.end();
85 }
86
91 const_iterator lower_bound(const Key &theKey) const {
92 const_iterator first = this->begin();
93 const_iterator it;
94 int count_ = _items.size(), step;
95
96 while (count_ > 0) {
97 it = first;
98 step = count_ / 2;
99 it += step;
100
101 if (_comp(it->_key, theKey)) {
102 first = ++it;
103 count_ -= step + 1;
104 } else {
105 count_ = step;
106 }
107 }
108
109 return first;
110 }
111
112 iterator lower_bound(const Key &theKey) {
113 iterator first = this->begin();
114 iterator it;
115 int count_ = _items.size(), step;
116
117 while (count_ > 0) {
118 it = first;
119 step = count_ / 2;
120 it += step;
121
122 if (_comp(it->_key, theKey)) {
123 first = ++it;
124 count_ -= step + 1;
125 } else {
126 count_ = step;
127 }
128 }
129
130 return first;
131 }
132
136 iterator find(const Key &theKey) {
137 iterator it = this->lower_bound(theKey);
138
139 if (it != this->end() && it->_key == theKey)
140 return it;
141 return this->end();
142 }
143
144 const_iterator find(const Key &theKey) const {
145 const_iterator it = this->lower_bound(theKey);
146
147 if (it != this->end() && it->_key == theKey)
148 return it;
149 return this->end();
150 }
151
155 Val &operator[](const Key &theKey) {
156 iterator it = this->lower_bound(theKey);
157 if (it == this->end() || it->_key != theKey) {
158 size_t idx = it - this->begin();
159 _items.insert_at(idx, KeyValue());
160 _items[idx]._key = theKey;
161 return _items[idx]._value;
162 } else {
163 return _items[it - this->begin()]._value;
164 }
165 }
166
170 iterator erase(iterator it) {
171 iterator next = it;
172 ++next;
173 _items.remove_at(it - begin());
174 return next;
175 }
176
177 iterator erase(const Key &theKey) {
178 return erase(find(theKey));
179 }
180
184 size_t size() const {
185 return _items.size();
186 }
187
191 size_t count(const Key &theKey) {
192 int count_ = 0;
193 for (iterator it = this->begin(); it != this->end(); ++it) {
194 if (it->_key == theKey)
195 ++count_;
196 }
197
198 return count_;
199 }
200};
201
202template<class Key, class Val, class HashFunc = Common::Hash<Key>,
203 class EqualFunc = Common::EqualTo<Key> >
204class unordered_map : public Common::HashMap<Key, Val, HashFunc, EqualFunc> {
205public:
206 pair<Key, Val> insert(pair<Key, Val> elem) {
207 // unordered_map doesn't replace already existing keys
208 if (this->contains(elem.first))
209 return pair<Key, Val>(elem.first, this->operator[](elem.first));
210
211 // Add item to map
212 this->operator[](elem.first) = elem.second;
213 return elem;
214 }
215
216 void reserve(size_t size) {
217 // No implementation
218 }
219};
220
221} // namespace Std
222
223#endif
iterator end()
Definition: array.h:380
T remove_at(size_type idx)
Definition: array.h:261
T * iterator
Definition: array.h:54
size_type size() const
Definition: array.h:316
void clear()
Definition: array.h:321
iterator begin()
Definition: array.h:375
const T * const_iterator
Definition: array.h:55
void insert_at(size_type idx, const T &element)
Definition: array.h:242
Definition: hashmap.h:85
Definition: map.h:40
iterator end()
Definition: map.h:69
iterator begin()
Definition: map.h:62
iterator find(const Key &theKey)
Definition: map.h:136
void clear()
Definition: map.h:55
const_iterator lower_bound(const Key &theKey) const
Definition: map.h:91
Val & operator[](const Key &theKey)
Definition: map.h:155
size_t size() const
Definition: map.h:184
const_iterator begin() const
Definition: map.h:76
iterator erase(iterator it)
Definition: map.h:170
const_iterator end() const
Definition: map.h:83
size_t count(const Key &theKey)
Definition: map.h:191
Definition: map.h:204
bool contains(const Key &key) const
Definition: hashmap.h:598
Val & operator[](const Key &key)
Definition: hashmap.h:608
Definition: utility.h:39