File: as_map.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (717 lines) | stat: -rw-r--r-- 16,023 bytes parent folder | download | duplicates (3)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/*
   AngelCode Scripting Library
   Copyright (c) 2003-2013 Andreas Jonsson

   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any
   damages arising from the use of this software.

   Permission is granted to anyone to use this software for any
   purpose, including commercial applications, and to alter it and
   redistribute it freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you
      must not claim that you wrote the original software. If you use
      this software in a product, an acknowledgment in the product
      documentation would be appreciated but is not required.

   2. Altered source versions must be plainly marked as such, and
      must not be misrepresented as being the original software.

   3. This notice may not be removed or altered from any source
      distribution.

   The original version of this library can be located at:
   http://www.angelcode.com/angelscript/

   Andreas Jonsson
   andreas@angelcode.com
*/


//
// as_map.h
//
// This class is used for mapping a value to another
//


#ifndef AS_MAP_H
#define AS_MAP_H

template <class KEY, class VAL> struct asSMapNode;

template <class KEY, class VAL> class asCMap {
public:
	asCMap();
	~asCMap();

	int   Insert(const KEY &key, const VAL &value);
	int   Insert(asSMapNode<KEY, VAL> *node);
	int   GetCount() const;

	const KEY &GetKey(const asSMapNode<KEY, VAL> *cursor) const;
	const VAL &GetValue(const asSMapNode<KEY, VAL> *cursor) const;
	VAL       &GetValue(asSMapNode<KEY, VAL> *cursor);

	void Erase(asSMapNode<KEY, VAL> *cursor);
	asSMapNode<KEY, VAL> *Remove(asSMapNode<KEY, VAL> *cursor);
	void EraseAll();

	void SwapWith(asCMap<KEY, VAL> &other);

	// Returns true as long as cursor is valid

	bool MoveTo(asSMapNode<KEY, VAL> **out, const KEY &key) const;
	bool MoveFirst(asSMapNode<KEY, VAL> **out) const;
	bool MoveLast(asSMapNode<KEY, VAL> **out) const;
	bool MoveNext(asSMapNode<KEY, VAL> **out, asSMapNode<KEY, VAL> *cursor) const;
	bool MovePrev(asSMapNode<KEY, VAL> **out, asSMapNode<KEY, VAL> *cursor) const;

	// For debugging only

	int CheckIntegrity(asSMapNode<KEY, VAL> *node) const;

protected:
	// Don't allow value assignment
	asCMap &operator=(const asCMap &) {
		return *this;
	}

	void BalanceInsert(asSMapNode<KEY, VAL> *node);
	void BalanceErase(asSMapNode<KEY, VAL> *child, asSMapNode<KEY, VAL> *parent);

	int EraseAll(asSMapNode<KEY, VAL> *node);
	int RotateLeft(asSMapNode<KEY, VAL> *node);
	int RotateRight(asSMapNode<KEY, VAL> *node);

	asSMapNode<KEY, VAL> *root;
	asSMapNode<KEY, VAL> dummy;

	int count;
};

//---------------------------------------------------------------------------
// Implementation

// Properties of a Red-Black Tree
//
// 1. The root is always black
// 2. All single paths from the root to leafs
//    contain the same amount of black nodes
// 3. No red node can have a red node as parent

#define ISRED(x) ((x != 0) && (x)->isRed)
#define ISBLACK(x) (!ISRED(x))

template <class KEY, class VAL> struct asSMapNode {
	asSMapNode() {
		parent = 0;
		left = 0;
		right = 0;
		isRed = true;
	}
	void Init(KEY k, VAL v) {
		key = k;
		value = v;
		parent = 0;
		left = 0;
		right = 0;
		isRed = true;
	}

	asSMapNode *parent;
	asSMapNode *left;
	asSMapNode *right;
	bool isRed;

	KEY key;
	VAL value;
};

template <class KEY, class VAL>
asCMap<KEY, VAL>::asCMap() {
	root = 0;
	count = 0;
}

template <class KEY, class VAL>
asCMap<KEY, VAL>::~asCMap() {
	EraseAll();
}

template <class KEY, class VAL>
void asCMap<KEY, VAL>::SwapWith(asCMap<KEY, VAL> &other) {
	asSMapNode<KEY, VAL> *tmpRoot  = root;
	int                  tmpCount = count;

	root  = other.root;
	count = other.count;

	other.root  = tmpRoot;
	other.count = tmpCount;
}

template <class KEY, class VAL>
void asCMap<KEY, VAL>::EraseAll() {
	EraseAll(root);
	root = 0;
}

template <class KEY, class VAL>
int asCMap<KEY, VAL>::EraseAll(asSMapNode<KEY, VAL> *p) {
	if (p == 0) return -1;

	EraseAll(p->left);
	EraseAll(p->right);

	typedef asSMapNode<KEY, VAL> node_t;
	asDELETE(p, node_t);

	count--;

	return 0;
}

template <class KEY, class VAL>
int asCMap<KEY, VAL>::GetCount() const {
	return count;
}

template <class KEY, class VAL>
int asCMap<KEY, VAL>::Insert(const KEY &key, const VAL &value) {
	typedef asSMapNode<KEY, VAL> node_t;
	asSMapNode<KEY, VAL> *nnode = asNEW(node_t);
	if (nnode == 0) {
		// Out of memory
		return -1;
	}

	nnode->key   = key;
	nnode->value = value;

	return Insert(nnode);
}

template <class KEY, class VAL>
int asCMap<KEY, VAL>::Insert(asSMapNode<KEY, VAL> *nnode) {
	// Insert the node
	if (root == 0)
		root = nnode;
	else {
		asSMapNode<KEY, VAL> *p = root;
		for (;;) {
			if (nnode->key < p->key) {
				if (p->left == 0) {
					nnode->parent = p;
					p->left = nnode;
					break;
				} else
					p = p->left;
			} else {
				if (p->right == 0) {
					nnode->parent = p;
					p->right = nnode;
					break;
				} else
					p = p->right;
			}
		}
	}

	BalanceInsert(nnode);

	count++;

	return 0;
}

template <class KEY, class VAL>
void asCMap<KEY, VAL>::BalanceInsert(asSMapNode<KEY, VAL> *node) {
	// The node, that is red, can't have a red parent
	while (node != root && node->parent->isRed) {
		// Check color of uncle
		if (node->parent == node->parent->parent->left) {
			asSMapNode<KEY, VAL> *uncle = node->parent->parent->right;
			if (ISRED(uncle)) {
				//    B
				//   R R
				//  N

				// Change color on parent, uncle, and grand parent
				node->parent->isRed = false;
				uncle->isRed = false;
				node->parent->parent->isRed = true;

				// Continue balancing from grand parent
				node = node->parent->parent;
			} else {
				//    B
				//   R B
				//  N

				if (node == node->parent->right) {
					// Make the node a left child
					node = node->parent;
					RotateLeft(node);
				}

				// Change color on parent and grand parent
				// Then rotate grand parent to the right
				node->parent->isRed = false;
				node->parent->parent->isRed = true;
				RotateRight(node->parent->parent);
			}
		} else {
			asSMapNode<KEY, VAL> *uncle = node->parent->parent->left;
			if (ISRED(uncle)) {
				//   B
				//  R R
				//     N

				// Change color on parent, uncle, and grand parent
				// Continue balancing from grand parent
				node->parent->isRed = false;
				uncle->isRed = false;
				node = node->parent->parent;
				node->isRed = true;
			} else {
				//   B
				//  B R
				//     N

				if (node == node->parent->left) {
					// Make the node a right child
					node = node->parent;
					RotateRight(node);
				}

				// Change color on parent and grand parent
				// Then rotate grand parent to the right
				node->parent->isRed = false;
				node->parent->parent->isRed = true;
				RotateLeft(node->parent->parent);
			}
		}
	}

	root->isRed = false;
}

// For debugging purposes only
template <class KEY, class VAL>
int asCMap<KEY, VAL>::CheckIntegrity(asSMapNode<KEY, VAL> *node) const {
	if (node == 0) {
		if (root == 0)
			return 0;
		else if (ISRED(root))
			return -1;
		else
			node = root;
	}

	int left = 0, right = 0;
	if (node->left)
		left = CheckIntegrity(node->left);
	if (node->right)
		right = CheckIntegrity(node->right);

	if (left != right || left == -1)
		return -1;

	if (ISBLACK(node))
		return left + 1;

	return left;
}

// Returns true if successful
template <class KEY, class VAL>
bool asCMap<KEY, VAL>::MoveTo(asSMapNode<KEY, VAL> **out, const KEY &key) const {
	asSMapNode<KEY, VAL> *p = root;
	while (p) {
		if (key < p->key)
			p = p->left;
		else if (key == p->key) {
			if (out) *out = p;
			return true;
		} else
			p = p->right;
	}

	if (out) *out = 0;
	return false;
}

template <class KEY, class VAL>
void asCMap<KEY, VAL>::Erase(asSMapNode<KEY, VAL> *cursor) {
	asSMapNode<KEY, VAL> *node = Remove(cursor);
	asASSERT(node == cursor);

	typedef asSMapNode<KEY, VAL> node_t;
	asDELETE(node, node_t);
}

template <class KEY, class VAL>
asSMapNode<KEY, VAL> *asCMap<KEY, VAL>::Remove(asSMapNode<KEY, VAL> *cursor) {
	if (cursor == 0) return 0;

	asSMapNode<KEY, VAL> *node = cursor;

	//---------------------------------------------------
	// Choose the node that will replace the erased one
	asSMapNode<KEY, VAL> *remove;
	if (node->left == 0 || node->right == 0)
		remove = node;
	else {
		remove = node->right;
		while (remove->left) remove = remove->left;
	}

	//--------------------------------------------------
	// Remove the node
	asSMapNode<KEY, VAL> *child;
	if (remove->left)
		child = remove->left;
	else
		child = remove->right;

	if (child) child->parent = remove->parent;
	if (remove->parent) {
		if (remove == remove->parent->left)
			remove->parent->left = child;
		else
			remove->parent->right = child;
	} else
		root = child;

	// If we remove a black node we must make sure the tree is balanced
	if (ISBLACK(remove))
		BalanceErase(child, remove->parent);

	//----------------------------------------
	// Replace the erased node with the removed one
	if (remove != node) {
		if (node->parent) {
			if (node->parent->left == node)
				node->parent->left = remove;
			else
				node->parent->right = remove;
		} else
			root = remove;

		remove->isRed  = node->isRed;
		remove->parent = node->parent;

		remove->left = node->left;
		if (remove->left) remove->left->parent = remove;
		remove->right = node->right;
		if (remove->right) remove->right->parent = remove;
	}

	count--;

	return node;
}

// Call method only if removed node was black
// child is the child of the removed node
template <class KEY, class VAL>
void asCMap<KEY, VAL>::BalanceErase(asSMapNode<KEY, VAL> *child, asSMapNode<KEY, VAL> *parent) {
	// If child is red
	//   Color child black
	//   Terminate

	// These tests assume brother is to the right.

	// 1. Brother is red
	//   Color parent red and brother black
	//   Rotate parent left
	//   Transforms to 2b
	// 2a. Parent and brother is black, brother's children are black
	//   Color brother red
	//   Continue test with parent as child
	// 2b. Parent is red, brother is black, brother's children are black
	//   Color parent black and brother red
	//   Terminate
	// 3. Brother is black, and brother's left is red and brother's right is black
	//   Color brother red and brother's left black
	//   Rotate brother to right
	//   Transforms to 4.
	// 4. Brother is black, brother's right is red
	//   Color brother's right black
	//   Color brother to color of parent
	//   Color parent black
	//   Rotate parent left
	//   Terminate

	while (child != root && ISBLACK(child)) {
		if (child == parent->left) {
			asSMapNode<KEY, VAL> *brother = parent->right;

			// Case 1
			if (ISRED(brother)) {
				brother->isRed = false;
				parent->isRed = true;
				RotateLeft(parent);
				brother = parent->right;
			}

			// Case 2
			if (brother == 0) break;
			if (ISBLACK(brother->left) && ISBLACK(brother->right)) {
				// Case 2b
				if (ISRED(parent)) {
					parent->isRed = false;
					brother->isRed = true;
					break;
				}

				brother->isRed = true;
				child = parent;
				parent = child->parent;
			} else {
				// Case 3
				if (ISBLACK(brother->right)) {
					brother->left->isRed = false;
					brother->isRed = true;
					RotateRight(brother);
					brother = parent->right;
				}

				// Case 4
				brother->isRed = parent->isRed;
				parent->isRed = false;
				brother->right->isRed = false;
				RotateLeft(parent);
				break;
			}
		} else {
			asSMapNode<KEY, VAL> *brother = parent->left;

			// Case 1
			if (ISRED(brother)) {
				brother->isRed = false;
				parent->isRed = true;
				RotateRight(parent);
				brother = parent->left;
			}

			// Case 2
			if (brother == 0) break;
			if (ISBLACK(brother->left) && ISBLACK(brother->right)) {
				// Case 2b
				if (ISRED(parent)) {
					parent->isRed = false;
					brother->isRed = true;
					break;
				}

				brother->isRed = true;
				child = parent;
				parent = child->parent;
			} else {
				// Case 3
				if (ISBLACK(brother->left)) {
					brother->right->isRed = false;
					brother->isRed = true;
					RotateLeft(brother);
					brother = parent->left;
				}

				// Case 4
				brother->isRed = parent->isRed;
				parent->isRed = false;
				brother->left->isRed = false;
				RotateRight(parent);
				break;
			}
		}
	}

	if (child)
		child->isRed = false;
}

template <class KEY, class VAL>
int asCMap<KEY, VAL>::RotateRight(asSMapNode<KEY, VAL> *node) {
	//     P             L       //
	//    / \           / \      //
	//   L   R    =>   Ll  P     //
	//  / \               / \    //
	// Ll  Lr            Lr  R   //

	if (node->left == 0) return -1;

	asSMapNode<KEY, VAL> *left = node->left;

	// Update parent
	if (node->parent) {
		asSMapNode<KEY, VAL> *parent = node->parent;
		if (parent->left == node)
			parent->left = left;
		else
			parent->right = left;

		left->parent = parent;
	} else {
		root = left;
		left->parent = 0;
	}

	// Move left's right child to node's left child
	node->left = left->right;
	if (node->left) node->left->parent = node;

	// Put node as left's right child
	left->right = node;
	node->parent = left;

	return 0;
}

template <class KEY, class VAL>
int asCMap<KEY, VAL>::RotateLeft(asSMapNode<KEY, VAL> *node) {
	//     P             R      //
	//    / \           / \     //
	//   L   R    =>   P   Rr   //
	//      / \       / \       //
	//     Rl  Rr    L   Rl     //

	if (node->right == 0) return -1;

	asSMapNode<KEY, VAL> *right = node->right;

	// Update parent
	if (node->parent) {
		asSMapNode<KEY, VAL> *parent = node->parent;
		if (parent->right == node)
			parent->right = right;
		else
			parent->left = right;

		right->parent = parent;
	} else {
		root = right;
		right->parent = 0;
	}

	// Move right's left child to node's right child
	node->right = right->left;
	if (node->right) node->right->parent = node;

	// Put node as right's left child
	right->left = node;
	node->parent = right;

	return 0;
}

template <class KEY, class VAL>
const VAL &asCMap<KEY, VAL>::GetValue(const asSMapNode<KEY, VAL> *cursor) const {
	if (cursor == 0)
		return dummy.value;

	return cursor->value;
}

template <class KEY, class VAL>
VAL &asCMap<KEY, VAL>::GetValue(asSMapNode<KEY, VAL> *cursor) {
	if (cursor == 0)
		return dummy.value;

	return cursor->value;
}

template <class KEY, class VAL>
const KEY &asCMap<KEY, VAL>::GetKey(const asSMapNode<KEY, VAL> *cursor) const {
	if (cursor == 0)
		return dummy.key;

	return cursor->key;
}

template <class KEY, class VAL>
bool asCMap<KEY, VAL>::MoveFirst(asSMapNode<KEY, VAL> **out) const {
	*out = root;
	if (root == 0) return false;

	while ((*out)->left)
		*out = (*out)->left;

	return true;
}

template <class KEY, class VAL>
bool asCMap<KEY, VAL>::MoveLast(asSMapNode<KEY, VAL> **out) const {
	*out = root;
	if (root == 0) return false;

	while ((*out)->right)
		*out = (*out)->right;

	return true;
}

template <class KEY, class VAL>
bool asCMap<KEY, VAL>::MoveNext(asSMapNode<KEY, VAL> **out, asSMapNode<KEY, VAL> *cursor) const {
	if (cursor == 0) {
		*out = 0;
		return false;
	}

	if (cursor->right == 0) {
		// Move upwards until we find a parent node to the right
		while (cursor->parent && cursor->parent->right == cursor)
			cursor = cursor->parent;

		cursor = cursor->parent;
		*out = cursor;
		if (cursor == 0)
			return false;

		return true;
	}

	cursor = cursor->right;
	while (cursor->left)
		cursor = cursor->left;

	*out = cursor;
	return true;
}

template <class KEY, class VAL>
bool asCMap<KEY, VAL>::MovePrev(asSMapNode<KEY, VAL> **out, asSMapNode<KEY, VAL> *cursor) const {
	if (cursor == 0) {
		*out = 0;
		return false;
	}

	if (cursor->left == 0) {
		// Move upwards until we find a parent node to the left
		while (cursor->parent && cursor->parent->left == cursor)
			cursor = cursor->parent;

		cursor = cursor->parent;

		*out = cursor;
		if (cursor == 0)
			return false;

		return true;
	}

	cursor = cursor->left;
	while (cursor->right)
		cursor = cursor->right;

	*out = cursor;
	return true;
}




#endif