1
+ /*
2
+ * Scala (https://www.scala-lang.org)
3
+ *
4
+ * Copyright EPFL and Lightbend, Inc.
5
+ *
6
+ * Licensed under Apache License 2.0
7
+ * (http://www.apache.org/licenses/LICENSE-2.0).
8
+ *
9
+ * See the NOTICE file distributed with this work for
10
+ * additional information regarding copyright ownership.
11
+ */
12
+
13
+ package scala .collection .immutable
14
+
15
+ import java .io .IOException
16
+ import scala .annotation .meta .getter
17
+
18
+ private [immutable] object RedBlackTree {
19
+
20
+ // Trees for serialisation compat with 2.12 legacy format
21
+ // allow the format to be written in that manner for 2.12.11 and before
22
+ // on write this is the same format as before
23
+ // on read the `readResolve` will convert to the NewRedBlackTree format
24
+
25
+ // the Tree children must be AnyRef as during construction then are RedBlackTree.Tree
26
+ // due to the readResolve the tree is migrated to new format and the children will be converted to
27
+ // NewRedBlackTree as they are read
28
+
29
+ @ SerialVersionUID (7757490705548110898L )
30
+ sealed abstract class Tree [A , + B ](
31
+ @ (inline@ getter) final val key : A ,
32
+ @ (inline@ getter) final val value : B ,
33
+ @ (inline@ getter) final val left : AnyRef ,
34
+ @ (inline@ getter) final val right : AnyRef )
35
+ extends Serializable {
36
+ private def _count (tree : AnyRef ) = if (tree eq null ) 0 else tree.asInstanceOf [Tree [A , B ]].count
37
+ @ (inline @ getter) final val count : Int = 1 + _count(left) + _count(right)
38
+ }
39
+
40
+ @ SerialVersionUID (6516527240275040268L )
41
+ final class RedTree [A , + B ](key : A ,
42
+ value : B ,
43
+ left : AnyRef ,
44
+ right : AnyRef ) extends Tree [A , B ](key, value, left, right) {
45
+ @ throws[IOException ]
46
+ private [this ] def readResolve (): AnyRef =
47
+ NewRedBlackTree .RedTree (key, value,
48
+ this .left.asInstanceOf [NewRedBlackTree .Tree [A , B ]],
49
+ this .right.asInstanceOf [NewRedBlackTree .Tree [A , B ]])
50
+
51
+ override def toString : String = " RedTree(" + key + " , " + value + " , " + left + " , " + right + " )"
52
+ }
53
+
54
+ @ SerialVersionUID (- 3666942709716265983L )
55
+ final class BlackTree [A , + B ](key : A ,
56
+ value : B ,
57
+ left : AnyRef ,
58
+ right : AnyRef ) extends Tree [A , B ](key, value, left, right) {
59
+ @ throws[IOException ]
60
+ private [this ] def readResolve (): AnyRef =
61
+ NewRedBlackTree .BlackTree (key, value,
62
+ this .left.asInstanceOf [NewRedBlackTree .Tree [A , B ]],
63
+ this .right.asInstanceOf [NewRedBlackTree .Tree [A , B ]])
64
+
65
+ override def toString : String = " BlackTree(" + key + " , " + value + " , " + left + " , " + right + " )"
66
+ }
67
+
68
+ def from [A , B ](tree : NewRedBlackTree .Tree [A , B ]): Tree [A , B ] = {
69
+ if (tree eq null ) null
70
+ else {
71
+ val left = from(tree.left)
72
+ val right = from(tree.right)
73
+ if (NewRedBlackTree .isBlack(tree))
74
+ new BlackTree (tree.key, tree.value, left, right)
75
+ else
76
+ new RedTree (tree.key, tree.value, left, right)
77
+ }
78
+ }
79
+
80
+ }
0 commit comments