-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cpp
More file actions
168 lines (150 loc) · 4.24 KB
/
Map.cpp
File metadata and controls
168 lines (150 loc) · 4.24 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "Map.h"
#include "MapIterator.h"
// Bester Fall: θ(1)
// Schlechster Fall: θ(1)
// Durchschnittlicher Fall: θ(1)
// Allgemeine Komplexitat: θ(1)
Map::Map() {
this->cap = 5;
this->nrElems = 0;
this->elements = new TElem[cap];
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(1)
// Durchschnittlicher Fall: θ(1)
// Allgemeine Komplexitat: θ(1)
Map::~Map() {
delete[] this->elements;
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(1)
// Durchschnittlicher Fall: θ(1)
// Allgemeine Komplexitat: θ(1)
Map::Map(const Map& original){
this->cap = original.cap;
this->nrElems = original.nrElems;
this->elements = new TElem[original.cap];
for(int index=0;index<this->cap;index++) {
this->elements[index] = original.elements[index];
}
}
// Bester Fall: θ(n)
// Schlechster Fall: θ(n^2)
// Allgemeine Komplexitat: O(n^2)
Map Map::filter(TKey index1, TKey index2) {
if(index1 > index2){
TKey aux = index2;
index2 = index1;
index1 = aux;
}
Map new_map=Map(*this);
for(int index=0;index<new_map.nrElems;index++){
if(new_map.elements[index].first < index1 || new_map.elements[index].first > index2) {
int pos_remove = index;
while (pos_remove < new_map.nrElems) {
new_map.elements[pos_remove] = new_map.elements[pos_remove + 1];
pos_remove++;
}
new_map.nrElems--;
index--;
}
}
return new_map;
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(n)
// Durchschnittlicher Fall: θ(n)
// Allgemeine Komplexitat: O(n)
TValue Map::add(TKey c, TValue v) {
// make more place
if (this->nrElems == this->cap) {
this->cap = this->cap * 2;
TElem *newvec = new TElem[this->cap];
int index = 0;
while (index < this->nrElems) {
newvec[index] = this->elements[index];
index++;
}
delete[] this->elements;
this->elements = newvec;
}
int index = 0;
while (index < this->nrElems) {
if (this->elements[index].first == c) {
TValue old = this->elements[index].second;
this->elements[index].second = v;
return old;
}
index++;
}
TElem newpair(c, v);
this->elements[this->nrElems] = newpair;
this->nrElems++;
return NULL_TVALUE;
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(n)
// Durchschnittlicher Fall: θ(n)
// Allgemeine Komplexitat: O(n)
TValue Map::search(TKey c) const {
int index = 0;
while (index < this->nrElems) {
if (this->elements[index].first == c)
return this->elements[index].second;
index++;
}
return NULL_TVALUE;
}
// Bester Fall: θ(n)
// Schlechster Fall: θ(n)
// Durchschnittlicher Fall: θ(n)
// Allgemeine Komplexitat: θ(n)
TValue Map::remove(TKey c) {
int index = 0;
while (index < this->nrElems) {
if (this->elements[index].first == c) {
TValue old = this->elements[index].second;
while (index < this->nrElems) {
this->elements[index] = this->elements[index + 1];
index++;
}
this->nrElems--;
// make less space
if (this->nrElems <= this->cap / 4) {
this->cap = this->cap / 2;
TElem *newvec = new TElem[this->cap];
int index_newvec = 0;
while (index_newvec < this->nrElems) {
newvec[index_newvec] = this->elements[index_newvec];
index_newvec++;
}
delete[] this->elements;
this->elements = newvec;
}
return old;
}
index++;
}
return NULL_TVALUE;
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(1)
// Durchschnittlicher Fall: θ(1)
// Allgemeine Komplexitat: θ(1)
int Map::size() const {
return this->nrElems;
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(1)
// Durchschnittlicher Fall: θ(1)
// Allgemeine Komplexitat: θ(1)
bool Map::isEmpty() const {
return this->nrElems == 0;
}
// Bester Fall: θ(1)
// Schlechster Fall: θ(1)
// Durchschnittlicher Fall: θ(1)
// Allgemeine Komplexitat: θ(1)
MapIterator Map::iterator() const {
return MapIterator(*this);
}