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+ }
0 commit comments