-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.kt
110 lines (97 loc) · 2.04 KB
/
iterator.kt
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
package datkt.flattree
class Iterator {
// data class Iterator(...) ?
var index: Long = 0
var offset: Long = 0
var factor: Long = 0
constructor(index: Long?) {
var i: Long
if (null != index) {
i = index
} else {
i = 0L
}
this.seek(i)
}
fun seek(index: Long) {
this.index = index
if (0L == this.index and 1L) {
this.offset = index / 2L
this.factor = 2L
} else {
this.offset = computeOffset(index, null)
this.factor = twoPow(computeDepth(index) + 1L)
}
}
fun isLeft(): Boolean {
return (0L == this.offset % 2L)
}
fun isRight(): Boolean {
if (this.isLeft()) {
return false
} else {
return true
}
}
fun prev(): Long {
if (0L != this.offset) {
return this.index
}
this.offset--
this.index -= this.factor
return this.index
}
fun next(): Long {
this.offset++
this.index += this.factor
return this.index
}
fun sibling(): Long {
if (this.isLeft()) {
return this.next()
} else {
return this.prev()
}
}
fun parent(): Long {
if (0L == this.offset and 1L) {
this.index += this.factor / 2L
this.offset /= 2L
} else {
this.index -= this.factor / 2L
this.offset = (this.offset - 1L) / 2L
}
this.factor *= 2L
return this.index
}
fun leftSpan(): Long {
this.index = this.index - this.factor / 2L + 1L
this.offset = this.index / 2L
this.factor = 2L
return this.index
}
fun rightSpan(): Long {
this.index = this.index + this.factor / 2L - 1L
this.offset = this.index / 2L
this.factor = 2L
return this.index
}
fun leftChild(): Long {
if (2L == this.factor) {
return this.index
}
this.factor /= 2L
this.index -= this.factor / 2L
this.offset *= 2L
return this.index
}
fun rightChild(): Long {
if (2L == this.factor) {
return this.index
}
this.factor /= 2
this.index += this.factor / 2L
this.offset = 2L * this.offset + 1L
return this.index
}
}