Skip to content

Commit 8fb65a4

Browse files
committed
feat(util.kt,tree.kt): First pass full api - WIP
1 parent 15806f5 commit 8fb65a4

3 files changed

Lines changed: 216 additions & 46 deletions

File tree

iterator.kt

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,5 @@
11
package flat_tree
22

3-
fun twoPow(n: Long): Long {
4-
var int_n: Int = n.toString().toInt()
5-
if (n < 31L) {
6-
val pow: Long = 1L shl int_n
7-
return pow
8-
} else {
9-
var int_b: Int = int_n - 30
10-
val powwow: Long = ((1L shl 30) * (1L shl int_b))
11-
return powwow
12-
}
13-
}
14-
15-
fun rightShift(n: Long): Long {
16-
return (n - (n and 1)) / 2L
17-
}
18-
19-
fun ft_offset(index: Long, depth: Long?): Long {
20-
var os: Long
21-
var local_depth: Long
22-
if ( null == depth ) {
23-
local_depth = ft_depth(index)
24-
} else {
25-
local_depth = depth
26-
}
27-
os = ((index + 1L) / twoPow(local_depth) - 1L) / 2L
28-
if (0L == index % 2L) {
29-
os = index / 2L
30-
}
31-
return os
32-
}
33-
34-
fun ft_depth(index: Long): Long {
35-
var local_index = index
36-
var depth: Long = 0
37-
38-
local_index += 1
39-
while ( 0L == local_index % 2L ) {
40-
depth++
41-
local_index = rightShift(local_index)
42-
}
43-
44-
return depth
45-
46-
}
47-
483
class Iterator {
494
// data class Iterator(...) ?
505
var index: Long = 0
@@ -108,7 +63,7 @@ class Iterator {
10863
}
10964

11065
fun parent(): Long {
111-
if (0L ==this.offset % 2L) {
66+
if (0L == this.offset % 2L) {
11267
this.index += this.factor / 2L
11368
this.offset /= 2L
11469
} else {

tree.kt

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
package flat_tree
2+
3+
class tree {
4+
5+
fun fullRoots(index: Long, result: Array<Long>?): Array<Long> {
6+
if (0L != index % 2L) {
7+
// TODO: throw error
8+
}
9+
var local_result = arrayOf<Long>()
10+
if (null != result) {
11+
local_result = result
12+
}
13+
14+
var local_index: Long = index / 2
15+
var offset: Long = 0
16+
var factor: Long = 1
17+
18+
while (true) {
19+
if (0L == index) {
20+
return local_result
21+
}
22+
while (factor * 2L <= factor - 1L) {
23+
factor *= 2
24+
}
25+
local_result.plusElement(offset + factor - 1L)
26+
offset = offset + 2L * factor
27+
local_index -= factor
28+
factor = 1L
29+
}
30+
}
31+
32+
fun count(index: Long, depth: Long?): Long {
33+
if (0L == index % 2L) {
34+
return 1L
35+
}
36+
var local_depth: Long
37+
if (null == depth) {
38+
local_depth = ft_depth(index)
39+
} else {
40+
local_depth = depth
41+
}
42+
return twoPow(local_depth + 1L) - 1L
43+
}
44+
45+
fun parent(index: Long, depth: Long?): Long {
46+
var local_depth: Long
47+
if (null == depth) {
48+
local_depth = ft_depth(index)
49+
} else {
50+
local_depth = depth
51+
}
52+
val offset = ft_offset(index, local_depth)
53+
54+
return ft_index(local_depth + 1L, rightShift(offset))
55+
}
56+
57+
fun children(index: Long, depth: Long?): Array<Long> {
58+
if (0L == index % 2L) {
59+
return arrayOf<Long>()
60+
}
61+
var local_depth: Long
62+
if (null == depth) {
63+
local_depth = ft_depth(index)
64+
} else {
65+
local_depth = depth
66+
}
67+
val offset = ft_offset(index, local_depth) * 2L
68+
return arrayOf<Long>(
69+
ft_index(local_depth - 1L, offset),
70+
ft_index(local_depth - 1L, offset + 1L)
71+
)
72+
}
73+
74+
fun sibling(index: Long, depth: Long?): Long {
75+
var local_depth: Long
76+
if (null == depth) {
77+
local_depth = ft_depth(index)
78+
} else {
79+
local_depth = depth
80+
}
81+
var offset = ft_offset(index, local_depth)
82+
var local_offset: Long
83+
if (0L == offset) {
84+
local_offset = offset + 1
85+
} else {
86+
local_offset = offset - 1
87+
}
88+
return ft_index(local_depth, local_offset)
89+
}
90+
91+
fun leftChild(index: Long, depth: Long?): Long {
92+
if (0L == index % 2L) {
93+
return -1L
94+
}
95+
var local_depth: Long
96+
if (null == depth) {
97+
local_depth = ft_depth(index)
98+
} else {
99+
local_depth = depth
100+
}
101+
val offset = ft_offset(index, local_depth) * 2L
102+
return ft_index(local_depth - 1L, offset)
103+
}
104+
105+
fun rightChild(index: Long, depth: Long?): Long {
106+
if (0L == index % 2L) {
107+
return -1L
108+
}
109+
var local_depth: Long
110+
if (null == depth) {
111+
local_depth = ft_depth(index)
112+
} else {
113+
local_depth = depth
114+
}
115+
val offset = ft_offset(index, local_depth) * 2L
116+
return ft_index(local_depth - 1L, offset + 1L)
117+
}
118+
119+
fun leftSpan(index: Long, depth: Long?): Long {
120+
if (0L == index % 2L) {
121+
return index
122+
}
123+
var local_depth: Long
124+
if (null == depth) {
125+
local_depth = ft_depth(index)
126+
} else {
127+
local_depth = depth
128+
}
129+
val offset = ft_offset(index, local_depth)
130+
return offset * twoPow(local_depth + 1L)
131+
}
132+
133+
fun rightSpan(index: Long, depth: Long?): Long {
134+
if (0L == index % 2L) {
135+
return index
136+
}
137+
var local_depth: Long
138+
if (null == depth) {
139+
local_depth = ft_depth(index)
140+
} else {
141+
local_depth = depth
142+
}
143+
val offset = ft_offset(index, local_depth) + 1L
144+
return offset * twoPow(local_depth + 1L) - 2L
145+
}
146+
147+
fun spans(index: Long, depth: Long?): Array<Long> {
148+
if (0L == index % 2L) {
149+
return arrayOf<Long>(index, index)
150+
}
151+
var local_depth: Long
152+
if (null == depth) {
153+
local_depth = ft_depth(index)
154+
} else {
155+
local_depth = depth
156+
}
157+
158+
val offset = ft_offset(index, local_depth)
159+
val width = twoPow(local_depth + 1L)
160+
val left = offset * width
161+
val right = (offset + 1L) * width - 2L
162+
163+
return arrayOf<Long>(left, right)
164+
}
165+
166+
}

util.kt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package flat_tree
2+
3+
fun ft_index(depth: Long, offset: Long): Long {
4+
return (1L + 2L * offset) * twoPow(depth) - 1L
5+
}
6+
7+
fun ft_depth(index: Long): Long {
8+
var local_index = index
9+
var depth: Long = 0
10+
11+
local_index += 1
12+
while ( 0L == local_index % 2L ) {
13+
depth++
14+
local_index = rightShift(local_index)
15+
}
16+
17+
return depth
18+
}
19+
20+
fun ft_offset(index: Long, depth: Long?): Long {
21+
var os: Long
22+
var local_depth: Long
23+
if ( null == depth ) {
24+
local_depth = ft_depth(index)
25+
} else {
26+
local_depth = depth
27+
}
28+
os = ((index + 1L) / twoPow(local_depth) - 1L) / 2L
29+
if (0L == index % 2L) {
30+
os = index / 2L
31+
}
32+
return os
33+
}
34+
35+
fun twoPow(n: Long): Long {
36+
var int_n: Int = n.toString().toInt()
37+
if (n < 31L) {
38+
val pow: Long = 1L shl int_n
39+
return pow
40+
} else {
41+
var int_b: Int = int_n - 30
42+
val powwow: Long = ((1L shl 30) * (1L shl int_b))
43+
return powwow
44+
}
45+
}
46+
47+
fun rightShift(n: Long): Long {
48+
return (n - (n and 1)) / 2L
49+
}

0 commit comments

Comments
 (0)