-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.h
117 lines (93 loc) · 2.71 KB
/
map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (C) 2013 Thierry Crozat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact: [email protected]
*/
#include <assert.h>
#ifndef map_h
#define map_h
#include "list.h"
template <class Key, class Value> class Map {
public:
Map();
Map(const Map&);
~Map();
Map& operator=(const Map&);
bool contains(const Key&) const;
const Value& operator[](const Key&) const;
Value& operator[](const Key&);
bool isEmpty() const;
int size() const;
const List<Key>& keys() const;
bool remove(const Key&);
void clear();
private:
List<Key> keys_;
List<Value> values_;
};
template <class Key, class Value> Map<Key, Value>::Map() {
}
template <class Key, class Value> Map<Key, Value>::Map(const Map<Key, Value>& other) :
keys_(other.keys_), values_(other.values)
{
}
template <class Key, class Value> Map<Key, Value>::~Map() {
}
template <class Key, class Value> Map<Key, Value>& Map<Key, Value>::operator=(const Map<Key, Value>& other) {
if (&other != this) {
keys_ = other.keys_;
values_ = other.values_;
}
return *this;
}
template <class Key, class Value> bool Map<Key, Value>::contains(const Key& key) const {
return keys_.contains(key);
}
template <class Key, class Value> const Value& Map<Key, Value>::operator[](const Key& key) const {
int i = keys_.indexOf(key);
assert(i != -1);
return values_[i];
}
template <class Key, class Value> Value& Map<Key, Value>::operator[](const Key& key) {
int i = keys_.indexOf(key);
if (i != -1)
return values_[i];
keys_ << key;
values_ << Value();
return values_.last();
}
template <class Key, class Value> bool Map<Key, Value>::isEmpty() const {
return keys_.isEmpty();
}
template <class Key, class Value> int Map<Key, Value>::size() const {
return keys_.size();
}
template <class Key, class Value> const List<Key>& Map<Key, Value>::keys() const {
return keys_;
}
template <class Key, class Value> bool Map<Key, Value>::remove(const Key& key) {
int i = keys_.indexOf(key);
if (i == -1)
return false;
keys_.removeAt(i);
values_.removeAt(i);
return true;
}
template <class Key, class Value> void Map<Key, Value>::clear() {
keys_.clear();
values_.clear();
}
#endif