-
Notifications
You must be signed in to change notification settings - Fork 0
/
File.scala
94 lines (79 loc) · 2.13 KB
/
File.scala
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
package memes.orderedfile
class OrderedFile {
var root : Node
var height : Int
def densityMinima : Seq[Double] = (0 until height) map (0.5 - _/(4.0 * h))
def densityMaxima : Seq[Double] = (0 until height) map (0.75 + _/(4.0 * h))
def highestViolatedNode(leaf : Leaf) : Option[Node] = {
val minima = densityMinima
val maxima = densityMaxima
var curNode : Node = leaf
var violatedNode : Option[Node] = None
}
def insert(i: Int) : Unit = {
val appropriateLeaf = findLeaf(root, i)
put
val minsDensity =
}
def putInLeaf(leaf: Leaf, value: Int) : Unit = {
var index : Int = 0
for(i <- 0 until leaf.slots) {
leaf.stuff(i) match {
case Some(thing) => if(thing < value) index = i
case _ => ()
}
}
val next = nextHole(leaf, index)
val prev = prevHole(leaf, index)
if(prev < next) {
for(i <- prev until index){
leaf.stuff(i) = leaf.stuff(i+1)
}
leaf.stuff(index) = Some(value)
}else{
for(i <- index+1 to next){
leaf.stuff(i+1) = leaf.stuff(i)
}
leaf.stuff(index+1) = Some(value)
}
}
def nextHole(leaf: Leaf, index: Int) : Option[Int] = {
var i : Int = index
while(i < leaf.slots && leaf.stuff(i) != None) i++
return i match {
case leaf.slots => None
case _ => Some(i)
}
}
def prevHole(leaf: Leaf, index: Int) : Option[Int] = {
var i : Int = index
while(i >= 0 && leaf.stuff(i) != None) i--
return i match {
case -1 => None
case _ => Some(i)
}
}
def findLeaf(node: Node, value: Int) : Leaf = {
return node match {
case InteriorNode(left, right) => if(left.max > value) findLeaf(right, value) else findLeaf(left, value)
case x : Leaf => x
}
}
}
sealed abstract class Node(parent: Option[Node]) {
var size : Int
var slots : Int
var max : Int
def density : Double = (Double)size/(Double)slots
def changeSize(s : Int) : Unit = {
size += s
parent match {
case Some(p) => p.increaseSize(s)
case None => ()
}
}
}
case class InteriorNode(left: Node, right: Node, _parent: Option[Node]) extends Node(_parent) {
}
case class Leaf[T](stuff: Seq[Option[Int]], _parent: Option[Node], prev: Option[Leaf], next: Option[Leaf]) extends Node(_parent) {
}