annotate src/container.d @ 5:496dfd8f7342 default tip

added: -repeat option for "in", "ov" -run until a line option -run until a function option -break on a function start -n is an alias for ov
author marton@basel.hu
date Sun, 17 Apr 2011 11:05:31 +0200
parents 4a9dcbd9e54f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
1 /* Ddbg - Win32 Debugger for the D programming language
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
2 * Copyright (c) 2007 Jascha Wetzel
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
3 * All rights reserved. See LICENSE.TXT for details.
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
4 */
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
5
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
6 import std.string;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
7 debug import std.stdio;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
8
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
9 /*******************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
10 Double linked list
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
11 *******************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
12 class List(T)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
13 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
14 class Element
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
15 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
16 T value;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
17 Element prev,
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
18 next;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
19
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
20 this(T v)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
21 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
22 value = v;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
23 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
24 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
25
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
26 Element head,
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
27 tail;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
28
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
29 List opCatAssign(T v)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
30 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
31 if ( tail is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
32 head = tail = new Element(v);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
33 else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
34 tail.next = new Element(v);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
35 tail.next.prev = tail;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
36 tail = tail.next;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
37 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
38 return this;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
39 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
40
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
41 bool empty()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
42 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
43 return head is null;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
44 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
45
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
46 void clear()
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
47 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
48 head = null;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
49 tail = null;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
50 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
51
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
52 void remove(Element e)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
53 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
54 if ( e is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
55 return;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
56 assert(e !is null);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
57 if ( e.prev is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
58 head = e.next;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
59 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
60 e.prev.next = e.next;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
61 if ( e.next is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
62 tail = e.prev;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
63 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
64 e.next.prev = e.prev;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
65 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
66
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
67 int opApply(int delegate(inout Element) dg)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
68 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
69 for ( Element e=head; e !is null; e = e.next )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
70 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
71 int ret = dg(e);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
72 if ( ret )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
73 return ret;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
74 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
75 return 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
76 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
77
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
78 int opApplyReverse(int delegate(inout Element) dg)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
79 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
80 for ( Element e=tail; e !is null; e = e.prev )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
81 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
82 int ret = dg(e);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
83 if ( ret )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
84 return ret;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
85 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
86 return 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
87 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
88 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
89
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
90 /**************************************************************************************************
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
91 AVL Tree that balances itself after each insertion
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
92 **************************************************************************************************/
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
93 class AVLTree(T)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
94 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
95 alias AVLNode!(T) node_t;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
96 node_t root;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
97 bool rebalance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
98
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
99 bool insert(T v)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
100 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
101 if ( root is null ) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
102 root = new AVLNode!(T)(v);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
103 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
104 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
105 else {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
106 rebalance = false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
107 return insert(root, v);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
108 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
109 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
110
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
111 bool find(VT,RT)(VT v, out RT res)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
112 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
113 if ( root is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
114 return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
115 return root.find(v, res);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
116 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
117
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
118 void traverseDepthLeft(RT)(bool delegate(RT v) proc)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
119 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
120 if ( root !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
121 root.traverseDepthLeft(proc);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
122 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
123
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
124 private bool insert(node_t n, T v)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
125 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
126 void updateParents(node_t top=null)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
127 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
128 with ( n )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
129 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
130 if ( top is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
131 top = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
132
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
133 balance = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
134 parent.balance = 0;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
135
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
136 node_t pp = parent.parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
137 if ( pp is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
138 root = top;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
139 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
140 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
141 if ( parent is pp.left )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
142 pp.left = top;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
143 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
144 pp.right = top;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
145 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
146 parent.parent = top;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
147 top.parent = pp;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
148 if ( top !is n )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
149 parent = top;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
150 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
151 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
152
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
153 with ( n )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
154 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
155 if ( v < value )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
156 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
157 if ( left is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
158 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
159 left = new node_t(v, n);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
160 --balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
161 if ( balance != 0 )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
162 rebalance = true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
163 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
164 else if ( !insert(left, v) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
165 return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
166 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
167 else if ( v > value )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
168 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
169 if ( right is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
170 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
171 right = new node_t(v, n);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
172 ++balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
173 if ( balance != 0 )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
174 rebalance = true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
175 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
176 else if ( !insert(right, v) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
177 return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
178 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
179 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
180 return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
181
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
182 if ( rebalance && parent !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
183 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
184 assert(balance != 0);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
185 if ( n is parent.left )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
186 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
187 if ( parent.balance > 0 )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
188 --parent.balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
189 else if ( parent.balance == 0 ) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
190 --parent.balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
191 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
192 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
193 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
194 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
195 // single rotation to the right
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
196 if ( balance < 0 )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
197 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
198 parent.left = right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
199 if ( right !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
200 right.parent = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
201 right = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
202 updateParents;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
203 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
204 // double rotation to the right
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
205 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
206 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
207 assert(right !is null);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
208 node_t r = right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
209 parent.left = r.right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
210 if ( parent.left !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
211 parent.left.parent = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
212 right = r.left;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
213 if ( right !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
214 right.parent = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
215 r.right = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
216 r.left = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
217 updateParents(r);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
218 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
219 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
220 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
221 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
222 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
223 if ( parent.balance < 0 )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
224 ++parent.balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
225 else if ( parent.balance == 0 ) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
226 ++parent.balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
227 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
228 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
229 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
230 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
231 // single rotation to the left
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
232 if ( balance > 0 )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
233 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
234 parent.right = left;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
235 if ( left !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
236 left.parent = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
237 left = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
238 updateParents;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
239 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
240 // double rotation to the left
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
241 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
242 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
243 assert(left !is null);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
244 node_t l = left;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
245 parent.right = l.left;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
246 if ( parent.right !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
247 parent.right.parent = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
248 left = l.right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
249 if ( left !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
250 left.parent = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
251
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
252 l.left = parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
253 l.right = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
254 updateParents(l);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
255 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
256 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
257 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
258 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
259 rebalance = false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
260 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
261 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
262 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
263 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
264
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
265 class AVLNode(T)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
266 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
267 alias AVLNode!(T) node_t;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
268 node_t parent, left, right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
269 byte balance;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
270
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
271 T value;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
272
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
273 this(T v, node_t p = null)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
274 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
275 value = v;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
276 parent = p;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
277 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
278
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
279 void traverseDepthLeft(RT)(bool delegate(RT v) proc)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
280 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
281 if ( left !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
282 left.traverseDepthLeft(proc);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
283
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
284 static if ( is(RT == AVLNode!(T)) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
285 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
286 if ( !proc(this) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
287 return;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
288 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
289 static if ( is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
290 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
291 if ( !proc(value) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
292 return;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
293 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
294 static if ( !is(RT == AVLNode!(T)) && !is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
295 static assert(0, "invalid result type for tree traversal");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
296
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
297 if ( right !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
298 right.traverseDepthLeft(proc);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
299 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
300
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
301 bool findNext(RT)(inout RT res)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
302 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
303 node_t n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
304 if ( right is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
305 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
306 bool found=false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
307 for ( n = this; n.parent !is null; n = n.parent )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
308 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
309 if ( n.parent.left is n )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
310 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
311 static if ( is(T : Object) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
312 assert(n.parent.value > n.value, "\n"~n.parent.value.toString~" parent of "~n.value.toString~"\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
313 assert(n.parent.value > n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
314 n = n.parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
315 found = true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
316 break;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
317 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
318 assert(n.parent.value <= n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
319 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
320 if ( !found )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
321 return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
322 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
323 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
324 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
325 assert(right.value >= value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
326 n = right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
327 while ( n.left !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
328 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
329 static if ( is(T : Object) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
330 assert(n.left.value < n.value, "\n"~n.left.value.toString~"\tleft of\t"~n.value.toString~"\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
331 assert(n.left.value < n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
332 n = n.left;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
333 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
334 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
335
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
336 static if ( is(RT == AVLNode!(T)) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
337 res = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
338 static if ( is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
339 res = n.value;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
340 static if ( !is(RT == AVLNode!(T)) && !is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
341 static assert(0, "invalid result type for next node search");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
342 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
343 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
344
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
345 bool findPrev(RT)(inout RT res)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
346 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
347 node_t n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
348 if ( left is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
349 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
350 bool found=false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
351 for ( n = this; n.parent !is null; n = n.parent )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
352 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
353 if ( n.parent.right is n )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
354 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
355 static if ( is(T : Object) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
356 assert(n.parent.value > n.value, "\n"~n.parent.value.toString~" parent of "~n.value.toString~"\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
357 assert(n.parent.value <= n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
358 n = n.parent;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
359 found = true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
360 break;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
361 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
362 assert(n.parent.value <= n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
363 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
364 if ( !found )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
365 return false;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
366 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
367 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
368 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
369 assert(left.value < value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
370 n = left;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
371 while ( n.right !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
372 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
373 static if ( is(T : Object) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
374 assert(n.right.value < n.value, "\n"~n.right.value.toString~"\tleft of\t"~n.value.toString~"\n");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
375 assert(n.right.value < n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
376 n = n.right;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
377 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
378 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
379
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
380 static if ( is(RT == AVLNode!(T)) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
381 res = n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
382 static if ( is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
383 res = n.value;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
384 static if ( !is(RT == AVLNode!(T)) && !is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
385 static assert(0, "invalid result type for prev node search");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
386 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
387 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
388
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
389 bool find(VT,RT)(VT v, inout RT res)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
390 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
391 bool suc;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
392 if ( value > v )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
393 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
394 if ( left !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
395 return left.find(v, res);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
396 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
397 else if ( value < v )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
398 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
399 if ( right !is null )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
400 return right.find(v, res);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
401 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
402 else
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
403 suc = true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
404 static if ( is(RT == AVLNode!(T)) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
405 res = this;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
406 static if ( is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
407 res = value;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
408 static if ( !is(RT == AVLNode!(T)) && !is(RT == T) )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
409 static assert(0, "invalid result type for tree search");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
410 return suc;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
411 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
412 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
413
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
414 unittest
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
415 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
416 AVLTree!(int) tree = new AVLTree!(int);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
417 for ( int i = 0; i < 100; ++i )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
418 tree.insert(i);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
419
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
420 bool checkOrder(AVLNode!(int) n)
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
421 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
422 if ( n.left !is null ) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
423 assert(n.left.parent is n);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
424 assert(n.left.value < n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
425 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
426 if ( n.right !is null ) {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
427 assert(n.right.parent is n);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
428 assert(n.right.value >= n.value);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
429 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
430 return true;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
431 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
432
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
433 tree.traverseDepthLeft(&checkOrder);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
434
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
435 // check next
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
436 for ( int i = 0; i < 99; ++i )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
437 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
438 AVLNode!(int) n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
439 assert(tree.find(i, n));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
440 assert(n.value == i);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
441 assert(n.findNext(n));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
442 assert(n.value == i+1, .toString(i+1)~" expected, "~.toString(n.value)~" found");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
443 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
444
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
445 tree = new AVLTree!(int);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
446 for ( int i = 99; i >= 0; --i )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
447 tree.insert(i);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
448 tree.traverseDepthLeft(&checkOrder);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
449
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
450 // check next
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
451 for ( int i = 0; i < 99; ++i )
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
452 {
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
453 AVLNode!(int) n;
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
454 assert(tree.find(i, n));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
455 assert(n.value == i);
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
456 assert(n.findNext(n));
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
457 assert(n.value == i+1, .toString(i+1)~" expected, "~.toString(n.value)~" found");
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
458 }
4a9dcbd9e54f -files of 0.13 beta
marton@basel.hu
parents:
diff changeset
459 }