[ create a new paste ] login | about

Link: http://codepad.org/4IB8UtH9    [ raw code | output | fork ]

scwizard - C++, pasted on Mar 1:
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#define nullptr 0
#define EVER ;;
#define IMPLEMENTS :
#define INITIALIZER_LIST :

//#pragma warning(disable : 4290)

template <typename T> class shrub {
// Types.
private:
	class Wood {
		friend class shrub<T>;

	// Types.
	private:
		enum ChildType {
			ROOT,
			LEFTCHILD,
			RIGHTCHILD
		};

		enum WoodType {
			FIRSTLEAF,
			LASTLEAF,
			FORK,
			MIDDLELEAF
		};

		union TypeOrData {
			WoodType Type;
			T* Data;
		};

		union LeafOrChild {
			Wood* Child;
			Wood* Leaf;
		};

	// Private members.
	private:
		// For everyone.
		Wood* Parent;
		ChildType TypeOfChild;
		TypeOrData TheWood;
		LeafOrChild Left;
		LeafOrChild Right;

		// For forks.
		unsigned int NumberOfLeavesToTheLeft;
		unsigned int NumberOfLeavesToTheRight;

	// Constructor, copy constructor, assignment operator, destructor.
	private:
		Wood() {
		}

		~Wood() {
			if(TheWood.Type >= MIDDLELEAF) {
				delete TheWood.Data;
			}
		}

	// Private methods.
	private:
		void Burn() {
			if(TheWood.Type == FORK) {
				Right.Child->Burn();
				Left.Child->Burn();
			}
			delete this;
		}
	};

	typedef size_t size_type;
public:
	class const_iterator IMPLEMENTS public std::iterator<std::bidirectional_iterator_tag, T> {
		friend class shrub<T>;

		// Private members.
	private:
		Wood* CurrentLeaf;

		// The public constructor.
	public:
		const_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		const_iterator(Wood* Leaf) {
			CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		T& operator*() {
			return *CurrentLeaf->TheWood.Data;
		}

		const_iterator& operator++() {
			CurrentLeaf = CurrentLeaf->Right.Leaf;
			return *this;
		}

		const_iterator& operator--() {
			CurrentLeaf = CurrentLeaf->Left.Leaf;
			return *this;		
		}

		bool operator==(const const_iterator& rhs) {
			return CurrentLeaf == rhs.CurrentLeaf;
		}

		bool operator!=(const const_iterator& rhs) {
			return CurrentLeaf != rhs.CurrentLeaf;
		}
	};

	class const_checked_iterator IMPLEMENTS virtual public const_iterator {
		friend class shrub<T>;

		// Constructor, copy constructor, assignment operator, destructor.
	public:
		const_checked_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		const_checked_iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		const T& operator*() {
			if(const_iterator::CurrentLeaf->TheWood.Type >= Wood::MIDDLELEAF) {
				return *const_iterator::CurrentLeaf->TheWood.Data;
			}
			else {
				throw iterator_exception(*this, "Attempt to dereference a past-the-end iterator.");
			}
		}

		const_checked_iterator& operator++() {
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::LASTLEAF) {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
				return *this;
			}
			else {
				throw iterator_exception(*this, "Attempt to increment an iterator past end().");
			}
		}

		const_checked_iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::FIRSTLEAF) {
				return *this;
			}
			else {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
				throw iterator_exception(*this, "Attempt to decrement an iterator past begin().");
			}
		}
	};

	class iterator IMPLEMENTS virtual public const_iterator {
		friend class shrub<T>;

		// The public constructor.
	public:
		iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		T& operator*() {
			return *const_iterator::CurrentLeaf->TheWood.Data;
		}

		iterator& operator++() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
			return *this;
		}

		iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
			return *this;		
		}
	};

	class const_reverse_iterator IMPLEMENTS virtual public const_iterator {
		friend class shrub<T>;

		// The public constructor.
	public:
		const_reverse_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		const_reverse_iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		const T& operator*() {
			return *const_iterator::CurrentLeaf->TheWood.Data;
		}

		const_reverse_iterator& operator++() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
			return *this;
		}

		const_reverse_iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
			return *this;		
		}
	};

	class const_checked_reverse_iterator IMPLEMENTS public const_checked_iterator, public const_reverse_iterator {
		friend class shrub<T>;

		// The public constructor.
	public:
		const_checked_reverse_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		const_checked_reverse_iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		const T& operator*() {
			if(const_iterator::CurrentLeaf->TheWood.Type >= Wood::MIDDLELEAF) {
				return *const_iterator::CurrentLeaf->TheWood.Data;
			}
			else {
				throw iterator_exception(*this, "Attempt to dereference a past-the-end iterator.");
			}
		}

		const_checked_reverse_iterator& operator++() {
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::FIRSTLEAF) {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
				return *this;
			}
			else {
				throw iterator_exception(*this, "Attempt to increment an iterator past rend().");
			}
		}

		const_checked_reverse_iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::LASTLEAF) {
				return *this;
			}
			else {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
				throw iterator_exception(*this, "Attempt to decrement an iterator past rbegin().");
			}
		}	
	};

	class checked_iterator IMPLEMENTS virtual public iterator, virtual public const_checked_iterator {
		friend class shrub<T>;

		// The public constructor.
	public:
		checked_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		checked_iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		T& operator*() {
			if(const_iterator::CurrentLeaf->TheWood.Type >= Wood::MIDDLELEAF) {
				return *const_iterator::CurrentLeaf->TheWood.Data;
			}
			else {
				throw iterator_exception(*this, "Attempt to dereference a past-the-end iterator.");
			}
		}

		checked_iterator& operator++() {
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::LASTLEAF) {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
				return *this;
			}
			else {
				throw iterator_exception(*this, "Attempt to increment an iterator past end().");
			}
		}

		checked_iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::FIRSTLEAF) {
				return *this;
			}
			else {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
				throw iterator_exception(*this, "Attempt to decrement an iterator past begin().");
			}
		}	
	};

	class reverse_iterator IMPLEMENTS virtual public iterator, virtual public const_reverse_iterator {
		friend class shrub<T>;

		// The public constructor.
	public:
		reverse_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		reverse_iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		T& operator*() {
			return *const_iterator::CurrentLeaf->TheWood.Data;
		}

		reverse_iterator& operator++() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
			return *this;
		}

		reverse_iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
			return *this;		
		}	
	};

	class checked_reverse_iterator IMPLEMENTS public checked_iterator, public reverse_iterator {
		friend class shrub<T>;

		// The public constructor.
	public:
		checked_reverse_iterator() {
		}

		// Only methods of shrub, which is a friend of iterator, should call this.
	private:
		checked_reverse_iterator(Wood* Leaf) {
			const_iterator::CurrentLeaf = Leaf;
		}

		// The public methods of a bidirectional iterator.
	public:
		T& operator*() {
			if(const_iterator::CurrentLeaf->TheWood.Type >= Wood::MIDDLELEAF) {
				return *const_iterator::CurrentLeaf->TheWood.Data;
			}
			else {
				throw iterator_exception(*this, "Attempt to dereference a past-the-end iterator.");
			}
		}

		checked_reverse_iterator& operator++() {
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::FIRSTLEAF) {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
				return *this;
			}
			else {
				throw iterator_exception(*this, "Attempt to increment an iterator past rend().");
			}
		}

		checked_reverse_iterator& operator--() {
			const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Right.Leaf;
			if(const_iterator::CurrentLeaf->TheWood.Type != Wood::LASTLEAF) {
				return *this;
			}
			else {
				const_iterator::CurrentLeaf = const_iterator::CurrentLeaf->Left.Leaf;
				throw iterator_exception(*this, "Attempt to decrement an iterator past rbegin().");
			}
		}
	};

	class iterator_exception {
		const_checked_iterator ThrownIterator;
		const char* Message;
		
		// Constructor, copy constructor, assignment operator, destructor.
	public:
		iterator_exception(const const_checked_iterator& PassedIterator, const char* PassedMessage)
		INITIALIZER_LIST ThrownIterator(PassedIterator), Message(PassedMessage) {
		}
	};

// Private members.
private:
	Wood* Root;
	Wood* FirstLeaf;
	Wood* LastLeaf;
	size_type ShrubSize;
// Constructor, copy constructor, assignment operator, destructor.
public:
	shrub() throw() {
		Construct();
	}

	shrub(const shrub<T>& ShrubToCopyFrom) throw() {
		Construct();
		
		iterator Iter = ShrubToCopyFrom.begin();

		while(Iter != ShrubToCopyFrom.end()) {
			push_back(*Iter);
			++Iter;
		}
	}

	void operator=(const shrub<T>& ShrubToCopyFrom) throw() {
		clear();

		iterator Iter = ShrubToCopyFrom.begin();

		while(Iter != ShrubToCopyFrom.end()) {
			push_back(*Iter);
			++Iter;
		}
	}
	~shrub() throw() {
		Root->Burn();
	}

// Private methods.
private:
	void Construct() {
		Root = new Wood;
		FirstLeaf = new Wood;
		LastLeaf = new Wood;

		Root->Parent = nullptr;
		Root->TypeOfChild = Wood::ROOT;
		Root->TheWood.Type = Wood::FORK;
		Root->Left.Child = FirstLeaf;
		Root->Right.Child = LastLeaf;
		Root->NumberOfLeavesToTheRight = 1;
		Root->NumberOfLeavesToTheLeft = 1;

		FirstLeaf->Parent = Root;
		FirstLeaf->TypeOfChild = Wood::LEFTCHILD;
		FirstLeaf->TheWood.Type = Wood::FIRSTLEAF;
		FirstLeaf->Left.Leaf = nullptr;
		FirstLeaf->Right.Leaf = LastLeaf;

		LastLeaf->Parent = Root;
		LastLeaf->TypeOfChild = Wood::RIGHTCHILD;
		LastLeaf->TheWood.Type = Wood::LASTLEAF;
		LastLeaf->Left.Leaf = FirstLeaf;
		LastLeaf->Right.Leaf = nullptr;

		ShrubSize = 0;
	}
	void UpdateShrub(Wood* Crawler) {
		for(EVER) {
			if(Crawler->TypeOfChild == Wood::LEFTCHILD) {
				Crawler = Crawler->Parent;
				Crawler->NumberOfLeavesToTheLeft = 
					Crawler->Left.Child->NumberOfLeavesToTheLeft +
					Crawler->Left.Child->NumberOfLeavesToTheRight;
			}
			else if (Crawler->TypeOfChild == Wood::RIGHTCHILD) {
				Crawler = Crawler->Parent;
				Crawler->NumberOfLeavesToTheRight = 
					Crawler->Right.Child->NumberOfLeavesToTheLeft +
					Crawler->Right.Child->NumberOfLeavesToTheRight;
			}
			else {
				break;
			}

			if(Crawler->NumberOfLeavesToTheLeft > Crawler->NumberOfLeavesToTheRight) {
				RotateRight(Crawler);
				Crawler = Crawler->Parent;
			}
			else if(Crawler->NumberOfLeavesToTheRight > Crawler->NumberOfLeavesToTheLeft) {
				RotateLeft(Crawler);
				Crawler = Crawler->Parent;
			}
			else {
				continue;
			}
		}
	}

	void RotateLeft(Wood* ForkToMoveLeft) {
		Wood* ForkToMoveUp = ForkToMoveLeft->Right.Child;

		// Step #1: Update the leaf counts.
		ForkToMoveLeft->NumberOfLeavesToTheRight -=
			ForkToMoveUp->NumberOfLeavesToTheRight;
		ForkToMoveUp->NumberOfLeavesToTheLeft +=
			ForkToMoveLeft->NumberOfLeavesToTheLeft;

		// Step #2: Update TypeOfChild for all the items that are being moved.
		ForkToMoveUp->TypeOfChild = ForkToMoveLeft->TypeOfChild;
		ForkToMoveLeft->TypeOfChild = Wood::LEFTCHILD;
		ForkToMoveUp->Left.Child->TypeOfChild = Wood::RIGHTCHILD;

		// Step #3: Move ForkToMoveUp up.
		ForkToMoveUp->Parent = ForkToMoveLeft->Parent;
		if(ForkToMoveUp->TypeOfChild == Wood::LEFTCHILD) {
			ForkToMoveLeft->Parent->Left.Child = ForkToMoveUp;
		}
		else if(ForkToMoveUp->TypeOfChild == Wood::RIGHTCHILD) {
			ForkToMoveLeft->Parent->Right.Child = ForkToMoveUp;
		}
		else {
			Root = ForkToMoveUp;
		}

		// Step #4: Have ForkToMoveLeft take ForkToMoveUp's left child.
		ForkToMoveLeft->Right.Child = ForkToMoveUp->Left.Child;
		ForkToMoveLeft->Right.Child->Parent = ForkToMoveLeft;

		// Step #5: Make ForkToMoveUp's new left child be ForkToMoveLeft.
		ForkToMoveUp->Left.Child = ForkToMoveLeft;
		ForkToMoveUp->Left.Child->Parent = ForkToMoveUp;
	}

	void RotateRight(Wood* ForkToMoveRight) {
		Wood* ForkToMoveUp = ForkToMoveRight->Left.Child;

		// Step #1: Update the leaf counts.
		ForkToMoveRight->NumberOfLeavesToTheLeft -=
			ForkToMoveUp->NumberOfLeavesToTheLeft;
		ForkToMoveUp->NumberOfLeavesToTheRight +=
			ForkToMoveRight->NumberOfLeavesToTheRight;

		// Step #2: Update TypeOfChild for all the items that are being moved.
		ForkToMoveUp->TypeOfChild = ForkToMoveRight->TypeOfChild;
		ForkToMoveRight->TypeOfChild = Wood::RIGHTCHILD;
		ForkToMoveUp->Right.Child->TypeOfChild = Wood::LEFTCHILD;

		// Step #3: Move ForkToMoveUp up.
		ForkToMoveUp->Parent = ForkToMoveRight->Parent;
		if(ForkToMoveUp->TypeOfChild == Wood::RIGHTCHILD) {
			ForkToMoveRight->Parent->Right.Child = ForkToMoveUp;
		}
		else if(ForkToMoveUp->TypeOfChild == Wood::LEFTCHILD) {
			ForkToMoveRight->Parent->Left.Child = ForkToMoveUp;
		}
		else {
			Root = ForkToMoveUp;
		}

		// Step #4: Have ForkToMoveRight take ForkToMoveUp's right child.
		ForkToMoveRight->Left.Child = ForkToMoveUp->Right.Child;
		ForkToMoveRight->Left.Child->Parent = ForkToMoveRight;

		// Step #5: Make ForkToMoveUp's new right child be ForkToMoveRight.
		ForkToMoveUp->Right.Child = ForkToMoveRight;
		ForkToMoveUp->Right.Child->Parent = ForkToMoveUp;
	}
// Public methods.
public:
	size_type size() const throw() {
		return ShrubSize;
	}
	bool empty() const throw() {
		if(ShrubSize == 0) {
			return true;
		}
		else {
			return false;
		}
	}
	const_checked_iterator position(size_type Index) const throw(iterator_exception) {
		// Will return end() if the index is out of bounds.
		Wood* Crawler = Root;
		
		// To compensate for the dummy nodes.
		++Index;

		while(Crawler->TheWood.Type == Wood::FORK) {
			if(Crawler->NumberOfLeavesToTheLeft > Index) {
				Crawler = Crawler->Left.Child;
			}
			else {
				Index -= Crawler->NumberOfLeavesToTheLeft;
				Crawler = Crawler->Right.Child;
			}
		}

		return checked_iterator(Crawler);
	}

	const_checked_iterator begin() const throw(iterator_exception) {
		return checked_iterator(FirstLeaf->Right.Leaf);
	}

	const_checked_iterator end() const throw(iterator_exception) {
		return checked_iterator(LastLeaf);
	}

	checked_iterator position(size_type Index) throw(iterator_exception) {
		// Will return end() if the index is out of bounds.
		Wood* Crawler = Root;
		
		// To compensate for the dummy nodes.
		++Index;

		while(Crawler->TheWood.Type == Wood::FORK) {
			if(Crawler->NumberOfLeavesToTheLeft > Index) {
				Crawler = Crawler->Left.Child;
			}
			else {
				Index -= Crawler->NumberOfLeavesToTheLeft;
				Crawler = Crawler->Right.Child;
			}
		}

		return checked_iterator(Crawler);
	}

	checked_iterator begin() throw(iterator_exception) {
		return checked_iterator(FirstLeaf->Right.Leaf);
	}

	checked_iterator end() throw(iterator_exception) {
		return checked_iterator(LastLeaf);
	}

	const_checked_reverse_iterator rposition(size_type Index) const throw(iterator_exception) {
		// Will return rend() if the index is out of bounds.
		Wood* Crawler = Root;

		// To compensate for the dummy nodes.
		++Index;

		while(Crawler->TheWood.Type == Wood::FORK) {
			if(Crawler->NumberOfLeavesToTheRight > Index) {
				Crawler = Crawler->Right.Child;
			}
			else {
				Index -= Crawler->NumberOfLeavesToTheRight;
				Crawler = Crawler->Left.Child;
			}
		}

		return checked_reverse_iterator(Crawler);
	}

	const_checked_reverse_iterator rbegin() const throw(iterator_exception) {
		return checked_reverse_iterator(LastLeaf->Left.Leaf);
	}
	const_checked_reverse_iterator rend() const throw(iterator_exception) {
		return checked_reverse_iterator(FirstLeaf);
	}
	checked_reverse_iterator rposition(size_type Index) throw(iterator_exception) {
		// Will return rend() if the index is out of bounds.
		Wood* Crawler = Root;

		// To compensate for the dummy nodes.
		++Index;

		while(Crawler->TheWood.Type == Wood::FORK) {
			if(Crawler->NumberOfLeavesToTheRight > Index) {
				Crawler = Crawler->Right.Child;
			}
			else {
				Index -= Crawler->NumberOfLeavesToTheRight;
				Crawler = Crawler->Left.Child;
			}
		}

		return checked_reverse_iterator(Crawler);
	}

	checked_reverse_iterator rbegin() throw(iterator_exception) {
		return checked_reverse_iterator(LastLeaf->Left.Leaf);
	}
	checked_reverse_iterator rend() throw(iterator_exception) {
		return checked_reverse_iterator(FirstLeaf);
	}
	T& at(size_type Index) throw(iterator_exception) {
		return *position(Index);
	}

	const T& at(size_type Index) const throw(iterator_exception) {
		return *position(Index);
	}

	T& front() throw(iterator_exception) {
		return *checked_iterator(FirstLeaf->Right.Leaf);
	}

	const T& front() const throw(iterator_exception) {
		return *const_checked_iterator(FirstLeaf->Right.Leaf);
	}

	T& back() throw(iterator_exception) {
		return *checked_iterator(LastLeaf->Left.Leaf);
	}

	const T& back() const throw(iterator_exception) {
		return *const_checked_iterator(LastLeaf->Left.Leaf);
	}

	iterator insert(iterator Position, const T& DataToInsert) throw(iterator_exception) {
		Wood* LeafToInsert = new Wood;
		Wood* ForkToInsert = new Wood;

		// Step #1: Fill LeafToInsert with data.
		LeafToInsert->Parent = ForkToInsert;
		LeafToInsert->TypeOfChild = Wood::LEFTCHILD;
		LeafToInsert->TheWood.Data = new T(DataToInsert);
		LeafToInsert->Right.Leaf = Position.CurrentLeaf;
		LeafToInsert->Left.Leaf = Position.CurrentLeaf->Left.Leaf;
		
		// Step #2: Make LeafToInsert's neighbors point back to LeafToInsert.
		LeafToInsert->Right.Leaf->Left.Leaf = LeafToInsert;
		LeafToInsert->Left.Leaf->Right.Leaf = LeafToInsert;

		// Step #3: Place ForkToInsert where LeafToInsert's sibling used to be.
		if(LeafToInsert->Right.Leaf->TypeOfChild == Wood::LEFTCHILD) {
			LeafToInsert->Right.Leaf->Parent->Left.Child = ForkToInsert;
		}
		else if(LeafToInsert->Right.Leaf->TypeOfChild == Wood::RIGHTCHILD) {
			LeafToInsert->Right.Leaf->Parent->Right.Child = ForkToInsert;
		}
		else {
			Root = ForkToInsert;
		}

		// Step #4: Fill ForkToInsert with data.
		ForkToInsert->Parent = LeafToInsert->Right.Leaf->Parent;
		ForkToInsert->TypeOfChild = LeafToInsert->Right.Leaf->TypeOfChild;
		ForkToInsert->TheWood.Type = Wood::FORK;
		ForkToInsert->Left.Child = LeafToInsert;
		ForkToInsert->Right.Child = LeafToInsert->Right.Leaf;
		ForkToInsert->NumberOfLeavesToTheLeft = 1;
		ForkToInsert->NumberOfLeavesToTheRight = 1;

		// Step #5: Update LeafToInsert's sibling.
		Position.CurrentLeaf->Parent = ForkToInsert;
		Position.CurrentLeaf->TypeOfChild = Wood::RIGHTCHILD;

		// Step #6: Ensure that the shrub stays balanced. 
		UpdateShrub(ForkToInsert);

		// Step #7: Update Size.
		++ShrubSize;

		return iterator(LeafToInsert);
	}

	void push_front(const T& DataToInsert) throw(iterator_exception) {
		insert(iterator(FirstLeaf->Right.Leaf), DataToInsert);
	}

	void push_back(const T& DataToInsert) throw(iterator_exception) {
		insert(iterator(LastLeaf), DataToInsert);
	}

	iterator erase(iterator Position) throw(iterator_exception) {
		Wood* LeafToDelete = Position.CurrentLeaf;
		Wood* MoveMeUp;
		iterator ReturnValue = iterator(LeafToDelete->Right.Leaf);

		// Step #1: Update the leaves pointing to LeafToDelete.
		LeafToDelete->Right.Leaf->Left.Leaf = LeafToDelete->Left.Leaf;
		LeafToDelete->Left.Leaf->Right.Leaf = LeafToDelete->Right.Leaf;

		// Step #2: The piece of wood we want to move up is LeafToDelete's sibling.
		if(LeafToDelete->TypeOfChild == Wood::LEFTCHILD) {
			MoveMeUp = LeafToDelete->Parent->Right.Child;
		}
		else if(LeafToDelete->TypeOfChild == Wood::RIGHTCHILD) {
			MoveMeUp = LeafToDelete->Parent->Left.Child;
		}
		else {
			// Will never be reached, because a leaf with data can never be the root.
			return ReturnValue;
		}

		// Step #3: Make MoveMeUp's TypeOfChild the TypeOfChild of the fork it's replacing.
		MoveMeUp->TypeOfChild = MoveMeUp->Parent->TypeOfChild;
		
		// Step #4: Make MoveMeUp's parent the parent of the fork it's replacing.
		MoveMeUp->Parent = MoveMeUp->Parent->Parent;
		
		// Step #5: MoveMeUp up and update its parent's NumberOfLeavesToThe(Left/Right).
		if(MoveMeUp->TypeOfChild == Wood::LEFTCHILD) {
			MoveMeUp->Parent->Left.Child = MoveMeUp;
			if(MoveMeUp->TheWood.Type != Wood::FORK) {
				MoveMeUp->Parent->NumberOfLeavesToTheLeft = 1;
			}
			else {
				MoveMeUp->Parent->NumberOfLeavesToTheLeft = 
					MoveMeUp->NumberOfLeavesToTheLeft +
					MoveMeUp->NumberOfLeavesToTheRight;
			}
		}
		else if(LeafToDelete->Parent->TypeOfChild == Wood::RIGHTCHILD) {
			MoveMeUp->Parent->Right.Child = MoveMeUp;
			if(MoveMeUp->TheWood.Type != Wood::FORK) {
				MoveMeUp->Parent->NumberOfLeavesToTheRight = 1;
			}
			else {
				MoveMeUp->Parent->NumberOfLeavesToTheRight = 
					MoveMeUp->NumberOfLeavesToTheLeft +
					MoveMeUp->NumberOfLeavesToTheRight;
			}
		}
		else {
			Root = MoveMeUp;
		}

		// Step #6: Delete LeafToDelete and its parent.
		delete LeafToDelete->Parent;
		delete LeafToDelete;

		// Step #7: Ensure that the shrub stays balanced.
		if(MoveMeUp->TypeOfChild != Wood::ROOT) {
			UpdateShrub(MoveMeUp->Parent);
		}

		// Step #8: Update Size.
		--ShrubSize;

		return ReturnValue;
	}

	void pop_front() throw(iterator_exception) {
		erase(iterator(FirstLeaf->Right.Leaf));
	}

	void pop_back() throw(iterator_exception) {
		erase(iterator(LastLeaf->Left.Leaf));
	}
	void clear() throw() {
		Root->Burn();
		
		Construct();
	}
	void swap(shrub<T>& ShrubToSwapWith) throw() {
		union WoodOrSize {
			Wood* Pointer;
			size_type Size;
		};

		WoodOrSize Temp; 

		Temp.Pointer = Root;
		Root = ShrubToSwapWith.Root;
		ShrubToSwapWith.Root = Temp.Pointer;

		Temp.Pointer = FirstLeaf;
		FirstLeaf = ShrubToSwapWith.FirstLeaf;
		ShrubToSwapWith.FirstLeaf = Temp.Pointer;

		Temp.Pointer = LastLeaf;
		LastLeaf = ShrubToSwapWith.LastLeaf;
		ShrubToSwapWith.LastLeaf = Temp.Pointer;

		Temp.Size = ShrubSize;
		ShrubSize = ShrubToSwapWith.ShrubSize;
		ShrubToSwapWith.ShrubSize = Temp.Size;
	}
};

int main() {
	shrub<std::string> TestShrub;

	TestShrub.push_back("do");
	TestShrub.push_back("ri");
	TestShrub.push_back("mi");
	TestShrub.push_back("fa");
	TestShrub.push_back("sol");
	TestShrub.push_back("la");
	TestShrub.push_back("ti");
	TestShrub.push_back("do");

	shrub<std::string>::iterator Iter = TestShrub.begin();

	while(Iter != TestShrub.end()) {
		std::cout << *Iter << std::endl;
		++Iter;
	}
}


Output:
1
2
3
4
5
6
7
8
do
ri
mi
fa
sol
la
ti
do


Create a new paste based on this one


Comments: