f***ing ropes

  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
/**
* Created by Nehoroshiy.
* const.belev@ya.ru
*/
object Color extends Enumeration {
type Color = Value
val Black, Red = Value
}
import Color._
import scala.reflect.ClassTag
import scala.util.Random
import java.io._
object Node {
val sliceSize = 16
def apply[A](nodes: Array[Node[A]])(implicit m: ClassTag[A]): Node[A] = nodes match {
case arr if arr.length == 0 => EmptyLeaf
case Array(_) => nodes(0)
case x if x.length % 2 == 0 => apply(x.sliding(2, 2).toList map { x => InternalNode(Black, x(0), x(1)) } toArray)
case _ => {
val arr = nodes.takeRight(3)
if (Random.nextBoolean()) {
apply(nodes.dropRight(3) :+ InternalNode(Black, arr(0), InternalNode(Red, arr(1), arr(2))))
} else {
apply(nodes.dropRight(3) :+ InternalNode(Black, InternalNode(Red, arr(0), arr(1)), arr(2)))
}
apply(nodes.dropRight(3) ++ nodes.takeRight(3))
}
}
def apply[A](values: Array[A])(implicit m: ClassTag[A]): Node[A] = apply(values.sliding(sliceSize, sliceSize).toList map { x => LeafNode(x) } toArray)
}
sealed trait Node[+A] {
import Node._
def len : Int
def blackHeight: Int
def color: Color
def left: Node[A]
def right: Node[A]
def isEmpty: Boolean = { len == 0 }
def balance(): Node[A] = this match {
case InternalNode(Black, InternalNode(Red, InternalNode(Red, a, b), c), d) => {
InternalNode(Red, InternalNode(Black, a, b), InternalNode(Black, c, d))
}
case InternalNode(Black, InternalNode(Red, a, InternalNode(Red, b, c)), d) => {
InternalNode(Red, InternalNode(Black, a, b), InternalNode(Black, c, d))
}
case InternalNode(Black, a, InternalNode(Red, InternalNode(Red, b, c), d)) => {
InternalNode(Red, InternalNode(Black, a, b), InternalNode(Black, c, d))
}
case InternalNode(Black, a, InternalNode(Red, b, InternalNode(Red, c, d))) => {
InternalNode(Red, InternalNode(Black, a, b), InternalNode(Black, c, d))
}
case _ => this
}
def append(other: Node[A])(implicit m: ClassTag[A]): Node[A] = {
if (blackHeight == other.blackHeight) {
InternalNode(Black, this, other)
} else if (blackHeight < other.blackHeight) {
other.prependLow(this)
} else {
prependLow(other)
}
}
def appendLow(other: Node[A])(implicit m: ClassTag[A]) : Node[A] = this match {
case LeafNode(_) => {
other match {
case LeafNode(_) => {
if (this.len + other.len <= Node.sliceSize)
LeafNode(this.values ++ other.values)
else InternalNode(Black, this, other)
}
case _ => throw new IllegalArgumentException("Internal node cannot be appendedLow to leaf node")
}
}
case InternalNode(_,_,_) if blackHeight == other.blackHeight => {
InternalNode(Red, this, other)
}
case InternalNode(_,_,_) if blackHeight > other.blackHeight => {
InternalNode(color, left, right.appendLow(other)).balance()
}
}
def prependLow(other: Node[A])(implicit m: ClassTag[A]) : Node[A] = this match {
case LeafNode(_) => {
right match {
case LeafNode(_) => {
if (this.len + other.len <= Node.sliceSize)
LeafNode(other.values ++ this.values)
else InternalNode(Black, other, this)
}
}
}
case InternalNode(_,_,_) if blackHeight == other.blackHeight => {
InternalNode(Red, other, this)
}
case InternalNode(_,_,_) if blackHeight > other.blackHeight => {
InternalNode(color, left.prependLow(other), right).balance()
}
}
def dotPrint()(implicit m: ClassTag[A]) {
val p = new java.io.PrintWriter(new File("test.dot"))
p.write("digraph graphname {\n")
val thisString = "xxx" + this.toString.replaceAll("[$ @]+", "")
p.write(thisString + "[label=" + len + "];\n")
if (color == Red)
p.write(thisString + "[color=red];\n")
dotPrintRecursive(p, this)
p.write("}\n")
p.close()
}
def dotPrintRecursive(p: java.io.PrintWriter, node: Node[A])(implicit m: ClassTag[A]):Unit = node match {
case LeafNode(values) => {
val thisString = "xxx" + node.toString.replaceAll("[$ @]+", "")
p.write(thisString + " [shape=box];\n")
p.write(thisString + " [label=\"[" + this.values().head.toString + ":" + this.values().takeRight(1).toString + "]\\nsize:" + len + "\\nbh:[" + blackHeight + "]\"];\n")
}
case InternalNode(_,_,_) => {
val thisString = "xxx" + node.toString.replaceAll("[$ @]+", "")
p.write(thisString + " [label=\"len:" + len + "\\nbh:[" + blackHeight + "]\"];\n")
if (node.color == Red) {
p.write(thisString + "[color=red];\n")
}
if (node.left != null) {
val leftString = "xxx" + node.left.toString.replaceAll("[$ @]+", "")
dotPrintRecursive(p, node.left)
p.write(thisString + "->" + leftString + "\n")
}
if (node.right != null) {
val rightString = "xxx" + node.right.toString.replaceAll("[$ @]+", "")
dotPrintRecursive(p, node.right)
p.write(thisString + "->" + rightString + "\n")
}
}
}
def values()(implicit m: ClassTag[A]) : Array[A]
}
case class InternalNode[A](color: Color, left: Node[A], right: Node[A]) extends Node[A] {
val len = left.len + right.len
val blackHeight = if (color == Black) left.blackHeight + 1 else left.blackHeight
def values()(implicit m: ClassTag[A]): Array[A] = {
left.values ++ right.values
}
}
case class LeafNode[A](vals: Array[A]) extends Node[A] {
val color = Black
val len = vals.length
val blackHeight = 1
val left, right = null
def values()(implicit m: ClassTag[A]) = values
}
case object EmptyLeaf extends Node[Nothing] {
val isEmpty = true
}