forked from ciberst/MP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap009.cpp
455 lines (438 loc) · 10.3 KB
/
map009.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//Александр Жиров, ККС-1-12
//map v0.0.9
#include <iostream>
#include <map>
//#define TEST
#ifdef TEST
size_t MEMORY=0;
#endif // TEST
using namespace std;
template<typename T>
struct node
{
static node<T>*NIL;
T data;
node<T> *left;
node<T> *right;
node<T> *parent;
node():left(NIL),right(NIL) {}
node(T d, node<T> *p):data(d),parent(p),left(NIL),right(NIL) {}
const bool is_right_son( ) const
{
return (parent != NIL) && (parent->right == this ) ;
}
const bool is_left_son( ) const {
return (parent != NIL ) && (parent->left == this ) ;
}
private:
static node SENTINEL_OBJECT ;
};
template< typename T >
node<T> node<T>::SENTINEL_OBJECT = node<T>() ;
template< typename T >
node<T> * node<T>::NIL = &node<T>::SENTINEL_OBJECT ;
template<typename K>
class default_comparator
{
public:
bool operator()(const K &a, const K &b)
{
return a<b;
}
};
template<typename K, typename V, typename CMP=default_comparator<K> >
class my_map
{
private:
typedef pair<const K,V> node_data;
typedef node<node_data> Node;
Node *root;
static Node *NIL;
size_t SIZE;
CMP C;
private:
Node* find (Node*p, const K &x)
{
if (p == Node::NIL)
return Node::NIL;
if (p->data.first == x)
return p;
else if (x < p->data.first)
return(find (p->left, x));
else
return(find (p->right,x));
}
void clear(Node *p)
{
if (p==Node::NIL) return;
clear(p->left);
clear(p->right);
#ifdef TEST
cout<<"-";
#endif
delete p;
#ifdef TEST
MEMORY=MEMORY-1;
#endif // TEST
if (p==root) root=Node::NIL;
#ifdef TEST
cout<<"Memory: "<<MEMORY;
#endif // TEST
}
Node* insert(Node *parent, const node_data &data)
{
if (parent==Node::NIL) return Node::NIL;
if (C(data.first,parent->data.first)) //parent->data.first>data.first)
{
if (parent->left==Node::NIL)
{
SIZE++;
parent->left=new Node(data,parent);
#ifdef TEST
MEMORY++;
#endif // TEST
return parent->left;
}
else return insert(parent->left,data);
}
else if (C(parent->data.first,data.first)) //(parent->data.first<data.first)
{
if (parent->right==Node::NIL)
{
SIZE++;
parent->right=new Node(data,parent);
#ifdef TEST
MEMORY++;
#endif // TEST
return parent->right;
}
else return insert(parent->right,data);
}
else return parent;
}
Node *at(Node *parent,const K &key)
{
if (parent==NIL) return NIL;
if (C(key,parent->data.first)) //(key<parent->data.first)
{
return at(parent->left, key);
}
else if (C(parent->data.first, key)) //(key>parent->data.first)
{
return at(parent->right, key);
}
else return parent;
}
void copy(Node* p, Node* x)
{
#ifdef TEST
cout<<"+";
#endif
if (x==Node::NIL)
return;
if (p==Node::NIL)
{
#ifdef TEST
cout<<"addROOT "<<x->data.first<<", " <<endl;
#endif
SIZE++;
root=new Node(x->data,Node::NIL);//insert(root,x->data);
#ifdef TEST
MEMORY++;
#endif // TEST
p = root;
// p==root;
}
if (x->left!=Node::NIL)
{
#ifdef TEST
cout<<"LEFT "<<x->left->data.first<<", "<<endl;
#endif
SIZE++;
p->left=new Node(x->left->data, p);//insert( p,x->left->data);
#ifdef TEST
MEMORY++;
#endif // TEST
copy(p->left, x->left);
}
if (x->right!=Node::NIL)
{
#ifdef TEST
cout<<"RIGHT "<<x->right->data.first<<", "<<endl;
#endif
SIZE++;
p->right=new Node(x->right->data, p); // insert( p,x->right->data);
#ifdef TEST
MEMORY++;
#endif // TEST
copy(p->right, x->right);
}
}
bool boolcompare(Node* x, Node* y)
{
if ((x==Node::NIL)&&(y!=Node::NIL)) return false;
if ((x!=Node::NIL)&&(y==Node::NIL)) return false;
if ((x!=Node::NIL)&&(y!=Node::NIL))
{
if (x->data!=y->data) return false;
if (!boolcompare(x->left, y->left)) return false;
if (!boolcompare(x->right, y->right)) return false;
}
//boolcompare(x->right, y->right);
return true;
}
public:
///Ctors & Dtors
my_map()
{
root=Node::NIL;
SIZE=0;
}
my_map(const my_map& p)
{
SIZE=0;
C=p.C;
// Node *q=0;
copy(Node::NIL,p.root);
root=root;
//delete q;
}
~my_map()
{
clear(root);
}
///OPERATORS
V& operator[](const K &key)
{
return (*(insert(node_data(key,V())))).second;
}
bool operator==(const my_map &x)
{
if (SIZE!=x.SIZE) return false;
if (root==x.root) return true;
return boolcompare(root,x.root);
//return (root==x.root);
}
my_map& operator=(const my_map &p)
{
if (root==p.root) return *this;
clear(root);
SIZE=0;
copy(Node::NIL, p.root);
return *this;
}
///PUBLIC FUNTIONS
V& at(const K &key)
{
Node *temp;
temp=at(root,key);
if (temp==0)
throw exception();
else return temp->data.second;
}
size_t size()
{
#ifdef TEST
cout<<"size: ";
#endif // defined
return SIZE;
}
///ITERATOR
class iterator
{
friend class my_map;
Node *it;
public:
iterator(Node *i=Node::NIL):it(i)
{
}
private:
Node* getNext(Node *i)
{
if ((i==Node::NIL)) return Node::NIL;
if (i->right!=Node::NIL)
{
Node *t=i->right;
while(t->left!=Node::NIL)
t=t->left;
return t;
}
else
{
Node *t=i->parent;
while (t!=Node::NIL && t->right==i)
{
t=t->parent;
i=i->parent;
}
return t;
}
}
Node* getPrev(Node *i)
{
if (i==Node::NIL) return Node::NIL->parent;
if (i->left!=Node::NIL)
{
Node *t=i->left;
while(t->right!=Node::NIL)
t=t->right;
return t;
};
if (i->is_right_son()) return i->parent ;
Node* pred = it ;
do
{
pred = pred->parent ;
}
while ((pred != Node::NIL ) && pred->is_left_son() ) ;
if (pred != Node::NIL )
return pred->parent ;
else
return Node::NIL ;
}
public:
bool operator!=(const iterator &r)
{
return it!=r.it;
}
bool operator==(const iterator &r)
{
return it==r.it;
}
iterator& operator++()
{
it=getNext(it);
return *this;
}
iterator operator++(int)
{
Node* t=it;
operator++();
return iterator(t);
}
iterator& operator--()
{
it=getPrev(it);
return *this;
}
iterator operator--(int)
{
Node* t=it;
operator--();
// t=getPrev(it);
return iterator(it);
}
node_data& operator*()
{
if (it==0) throw exception();
if (it==Node::NIL)
{
it=it->parent;
return it->data;
};
return it->data;
}
node_data* operator->()
{
if (it==0) throw exception();
if (it==Node::NIL)
{
it=it->parent;
return &it->data;
};
return &it->data;
}
};
iterator begin()
{
if (root==Node::NIL) return iterator(Node::NIL);
Node *t=root;
while(t->left!=Node::NIL) t=t->left;
return iterator(t);
}
iterator end()
{
if (root==Node::NIL) return iterator(Node::NIL);
Node *t=root;
while(t->right!=Node::NIL) t=t->right;
Node::NIL->parent=t;
return iterator();
}
iterator find(const K& x)
{
return iterator(find(root, x) );
}
iterator insert(const node_data &data)
{
if (root==Node::NIL)
{
root=new Node(data,Node::NIL);
#ifdef TEST
MEMORY++;
#endif // TEST
SIZE++;
return iterator(root);
}
return iterator(insert(root, data));
}
};
void print(my_map<int,int> a)
{
for(my_map<int,int>::iterator it=a.begin(); it!=a.end(); it++)
{
cout<<(*it).first<<" "<<(*it).second<<endl;
}
}
int main()
{
my_map<int,int> a;
a.insert(make_pair(4,4));
a.insert(make_pair(2,5));
a.insert(make_pair(6,2));
a.insert(make_pair(5,1));
a.insert(make_pair(7,3));
a.insert(make_pair(1,2));
a.insert(make_pair(3,4));
my_map<int,int> a1;
a1.insert(make_pair(4,4));
a1.insert(make_pair(2,5));
a1.insert(make_pair(6,2));
a1.insert(make_pair(5,1));
a1.insert(make_pair(7,4));
a1.insert(make_pair(1,2));
a1.insert(make_pair(3,4));
cout<<(a==a1) <<endl;
a1[7]=3;
cout<<(a==a1) <<endl;
a1[7]=4;
cout<<a.size()<<endl;
print(a);
cout<<endl;
print(a1);
a=a1;
print(a);
cout<<endl;
print(a1);
cout<<endl;
print(a);
cout<<endl;
for(my_map<int,int>::iterator it=a.begin(); it!=a.end(); it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
for(my_map<int,int>::iterator it=a.end(); it!=a.begin(); --it)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
for(my_map<int,int>::iterator it=a.end(); it!=a.find(4); --it)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
print(a);
cout<<endl;
cout<<a.size()<<endl;
return 0;
}