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
|
/* __ *\
** ________ ___ / / ___ Scala API **
** / __/ __// _ | / / / _ | (c) 2003-2004, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
** $Id$
\* */
/* General Balanced Trees - highly efficient functional dictionaries.
**
** This is a scala version of gb_trees.erl which is
** copyrighted (C) 1999-2001 by Sven-Olof Nystrom, and Richard Carlsson
**
** An efficient implementation of Prof. Arne Andersson's General
** Balanced Trees. These have no storage overhead compared to plain
** unbalanced binary trees, and their performance is in general better
** than AVL trees.
** ---------------------------------------------------------------------
** This library is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as
** published by the Free Software Foundation; either version 2 of the
** License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA
**
** Author contact: erik.stenman@epfl.ch
** ---------------------------------------------------------------------
*/
package scala.collection.immutable;
/** General Balanced Trees - highly efficient functional dictionaries.
*
* An efficient implementation of Prof. Arne Andersson's General
* Balanced Trees. These have no storage overhead compared to plain
* unbalanced binary trees, and their performance is in general better
* than AVL trees.
* <p/>
* This implementation does not balance the trees after deletions.
* Since deletions don't increase the height of a tree, this should
* be OK in most applications. A balance method is provided for those
* cases where rebalancing is needed.
* <p/>
* The tree consists of entries conatining a key with an order.
* <p/>
* When instanciating the tree an order for the keys has to be
* supplied.
*
* @author Erik Stenman
* @author Michel Schinz
* @version 1.1, 2005-01-20
*/
//[serializable]
abstract class Tree[A <% Ordered[A], B]() extends AnyRef with java.io.Serializable {
/* Data structure:
** - size:Int - the number of elements in the tree.
** - tree:T, which is composed of nodes of the form:
** - GBNode(key: A, entry:B, smaller:T, bigger:T),
** - and the "empty tree" node GBLeaf.
**
** Original balance condition h(T) <= ceil(c * log(|T|)) has been
** changed to the similar (but not quite equivalent) condition
** 2 ^ h(T) <= |T| ^ c.
**
*/
/** The type returned when creating a new tree.
* This type should be defined by concrete implementations
* e.g. <pre>
* class C[T](...) extends Tree[A,B](...) {
* type This = C[T];
* </pre>
*/
protected type This <: Tree[A,B];
protected def getThis: This;
/**
* The type of nodes that the tree is build from.
*/
protected type aNode = GBTree[A,B];
private val empty: aNode = GBLeaf[A,B]();
/** The nodes in the tree.
*/
protected def tree: aNode = empty;
/** This abstract method should be defined by a concrete implementation
** C[T] as something like:
** <pre>
** override def New(sz:Int,t:aNode):This {
** new C[T](order) {
** override def size=sz;
** override protected def tree:aNode=t;
** }
** </pre>
** The concrete implementation should also override the def of This
** <code>override type This = C[T];</code>
**
*/
protected def New(sz: Int, t: aNode): This;
/** The size of the tree, returns 0 (zero) if the tree is empty.
** @Returns The number of nodes in the tree as an integer.
**/
def size: Int = 0;
/**
* A new tree with the entry added is returned,
* assuming that key is <em>not</em> in the tree.
*/
protected def add(key: A, entry: B): This = {
val newSize = size+1;
New(newSize, tree.insert(key, entry, newSize * newSize).node);
}
/**
* A new tree with the entry added is returned,
* if key is <em>not</em> in the tree, otherwise
* the key is updated with the new entry.
*/
protected def updateOrAdd(key: A, entry: B): This = {
if (tree.isDefinedAt(key))
New(size,tree.update(key,entry))
else
add(key,entry);
}
/** Removes the key from the tree. */
protected def deleteAny(key: A): This =
if (tree.isDefinedAt(key))
delete(key)
else
getThis;
/** Removes the key from the tree, assumimg that key is present. */
private def delete(key:A): This =
New(size - 1, tree.delete(key));
/** Check if this map maps <code>key</code> to a value and return the
* value if it exists.
*
* @param key the key of the mapping of interest
* @return the value of the mapping, if it exists
*/
protected def findValue(key:A): Option[B] =
tree.get(key);
/**
* Gives you an iterator over all elements in the tree.
* The iterator structure corresponds to
* the call stack of an in-order traversal.
*
* Note: The iterator itself has a state, i.e., it is not functional.
*/
protected def entries: Iterator[B] =
new Iterator[B] {
var iter = tree.mk_iter(scala.Nil);
def hasNext = !iter.isEmpty;
def next =
iter match {
case (GBNode(_,v,_,t)::iter_tail) => {
iter= t.mk_iter(iter_tail);
v;
}
case scala.Nil =>
error("next on empty iterator");
}
}
/**
* Create a new balanced tree from the tree. Might be useful to call
* after many deletions, since deletion does not rebalance the tree.
*/
def balance: This =
New(size, tree.balance(size));
}
protected abstract class InsertTree[A <% Ordered[A],B]() extends AnyRef {
def insertLeft(k: A, v: B, t: GBTree[A,B]): InsertTree[A,B];
def insertRight(k: A, v: B, t: GBTree[A,B]): InsertTree[A,B];
def node: GBTree[A,B];
}
private case class ITree[A <% Ordered[A],B](t: GBTree[A,B])
extends InsertTree[A,B] {
def insertLeft(key: A, value: B, bigger: GBTree[A,B]) =
ITree(GBNode(key, value, t, bigger));
def insertRight(key: A, value: B, smaller: GBTree[A,B]) =
ITree(GBNode(key, value, smaller, t));
def node = t;
}
private case class INode[A <% Ordered[A],B](t1: GBTree[A,B],
height: int,
size: int)
extends InsertTree[A,B] {
def insertLeft(key: A, value: B, bigger: GBTree[A,B]) =
balance_p(GBNode(key, value, t1, bigger), bigger);
def insertRight(key: A, value: B, smaller: GBTree[A,B]) =
balance_p(GBNode(key, value, smaller, t1),smaller);
protected def balance_p(t:GBTree[A,B],subtree:GBTree[A,B]):InsertTree[A,B] = {
val Pair(subHeight, subSize) = subtree.count;
val totalHeight = 2 * scala.runtime.compat.Math.max(height, subHeight);
val totalSize = size + subSize + 1;
val BalanceHeight = totalSize * totalSize;
if(totalHeight > BalanceHeight) ITree(t.balance(totalSize));
else INode(t, totalHeight, totalSize);
}
def node = t1;
}
/**
* GBTree is an internal class used by Tree.
*/
//[serializable]
protected abstract class GBTree[A <% Ordered[A],B] extends AnyRef with java.io.Serializable {
type aNode = GBTree[A,B];
type anInsertTree = InsertTree[A,B];
/** Calculates 2^h, and size, where h is the height of the tree
* and size is the number of nodes in the tree.
*/
def count:Pair[Int,Int];
def isDefinedAt(Key:A):Boolean;
def get(key:A):Option[B];
def apply(key:A):B;
def update(key:A, value:B):aNode;
def insert(key:A, value:B, size:int):anInsertTree;
def toList(acc: List[Pair[A,B]]): List[Pair[A,B]];
def mk_iter(iter_tail:List[aNode]): List[aNode];
def delete(key:A):aNode;
def merge(t:aNode):aNode;
def takeSmallest:Triple[A,B,aNode];
def balance(s:int):GBTree[A,B];
}
private case class GBLeaf[A <% Ordered[A],B]() extends GBTree[A,B] {
def count = Pair(1, 0);
def isDefinedAt(key:A) = false;
def get(_key:A) = None;
def apply(key:A) = error("key " + key + " not found");
def update(key:A, value:B) = error("key " + key + " not found");
def insert(key:A, value:B, s:int):anInsertTree = {
if (s == 0)
INode(GBNode(key, value, this, this), 1, 1)
else
ITree(GBNode(key, value, this, this))
}
def toList(acc: List[Pair[A,B]]): List[Pair[A,B]] = acc;
def mk_iter(iter_tail:List[GBTree[A,B]]) = iter_tail;
def merge(larger:GBTree[A,B]) = larger;
def takeSmallest:Triple[A,B,GBTree[A,B]] =
error("Take Smallest on empty tree");
def delete(_key:A) = error("Delete on empty tree.");
def balance(s:int) = this;
override def hashCode() = 0;
}
private case class GBNode[A <% Ordered[A],B](key: A,
value: B,
smaller: GBTree[A,B],
bigger: GBTree[A,B])
extends GBTree[A,B] {
def count: Pair[Int,Int] = {
val Pair(sHeight, sSize) = smaller.count;
val Pair(bHeight, bSize) = bigger.count;
val mySize = sSize + bSize + 1;
if (mySize == 1)
Pair(1, mySize)
else
Pair(2 * scala.runtime.compat.Math.max(sHeight, bHeight), mySize);
}
def isDefinedAt(sKey:A):Boolean = {
if (sKey < key) smaller.isDefinedAt(sKey)
else if (sKey > key) bigger.isDefinedAt(sKey)
else true;
}
def get(sKey:A):Option[B] =
if (sKey < key) smaller.get(sKey);
else if (sKey > key) bigger.get(sKey);
else Some(value);
def apply(sKey:A):B =
if (sKey < key) smaller.apply(sKey);
else if (sKey > key) bigger.apply(sKey);
else value;
def update(newKey:A, newValue:B):aNode =
if (newKey < key)
GBNode(key, value, smaller.update(newKey,newValue), bigger);
else if (newKey > key)
GBNode(key, value, smaller, bigger.update(newKey,newValue));
else
GBNode(newKey, newValue, smaller, bigger);
def insert(newKey:A, newValue:B, s:int): anInsertTree = {
if (newKey < key)
smaller.insert(newKey, newValue, s / 2).insertLeft(key, value, bigger);
else if (newKey > key)
bigger.insert(newKey, newValue, s / 2).insertRight(key, value, smaller);
else
error("Key exists: " + newKey);
}
def toList(acc: List[Pair[A,B]]): List[Pair[A,B]] =
smaller.toList(Pair(key, value) :: bigger.toList(acc));
def mk_iter(iter_tail:List[aNode]):List[aNode] =
smaller.mk_iter(this :: iter_tail);
def delete(sKey:A):aNode = {
if (sKey < key)
GBNode(key, value, smaller.delete(sKey), bigger);
else if (sKey > key)
GBNode(key, value, smaller, bigger.delete(sKey));
else
smaller.merge(bigger)
}
def merge(larger: aNode): GBTree[A,B] = larger match {
case GBLeaf() =>
this
case _ =>
val Triple(key1, value1, larger1) = larger.takeSmallest;
GBNode(key1, value1, this, larger1)
}
def takeSmallest: Triple[A, B, aNode] = smaller match {
case GBLeaf() =>
Triple(key, value, bigger)
case _ =>
val Triple(key1, value1, smaller1) = smaller.takeSmallest;
Triple(key1, value1, GBNode(key, value, smaller1, bigger))
}
def balance(s:int): GBTree[A,B] =
balance_list(toList(scala.Nil), s);
protected def balance_list(list: List[Pair[A,B]], s:int): GBTree[A,B] = {
val empty = GBLeaf[A,B]();
def bal(list: List[Pair[A,B]], s:int): Pair[aNode,List[Pair[A,B]]] = {
if (s > 1) {
val sm = s - 1;
val s2 = sm / 2;
val s1 = sm - s2;
val Pair(t1, Pair(k, v) :: l1) = bal(list, s1);
val Pair(t2, l2) = bal(l1, s2);
val t = GBNode(k, v, t1, t2);
Pair(t, l2)
} else
if (s == 1) {
val Pair(k,v) :: rest = list;
Pair(GBNode(k, v, empty, empty), rest)
} else
Pair(empty, list)
}
bal(list, s)._1
}
override def hashCode() =
value.hashCode() + smaller.hashCode() + bigger.hashCode();
}
|