-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearized_search_trees.h
162 lines (150 loc) · 6.02 KB
/
linearized_search_trees.h
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
/*
* MIT License
*
* Copyright (c) 2018 Lucas Lersch
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/* Implementation derived from:
* "k-Ary Search on Modern Processors", Benjamin Schlegel et al, DaMoN 2009
*/
#include <cmath>
#include <cassert>
/**
* @brief Maps an index in a sorted array to an index in a linearized perfect
* K-ary search tree.
*
* @param size total size of the array/tree.
* @param K arity of the linearized search tree being converted to.
* @param array_idx index in the sorted array to be converted to a tree index.
* @param H height of the tree.
* @return size_t corresponding index in a linearized perfect K-ary search tree.
*/
size_t array_to_perfect_tree(size_t size,
int K,
size_t array_idx,
size_t H) {
assert(K > 1);
assert(size > 0);
assert(array_idx >= 0 && array_idx < size);
const size_t idx = array_idx+1;
size_t d = 0;
for(int i=1; i<H; ++i) {
d += ((idx % static_cast<size_t>(std::pow(K, H-i))) > 0) ? 1 : 0;
}
const size_t o = std::floor(((K-1)*idx) / std::pow(K, H-d));
return std::pow(K, d) + o - 1;
}
/**
* @brief Maps an index in a sorted array to an index in a linearized perfect
* K-ary search tree.
*
* @param size total size of the array/tree.
* @param K arity of the linearized search tree being converted to.
* @param array_idx index in the sorted array to be converted to a tree index.
* @return size_t corresponding index in a linearized perfect K-ary search tree.
*/
size_t array_to_perfect_tree(size_t size, int k, size_t array_idx) {
const size_t H = std::ceil(std::log(size+1) / std::log(k));
return array_to_perfect_tree(size, k, array_idx, H);
}
/**
* @brief Maps an index in a linearized perfect K-ary search tree to an
* index in a sorted array.
*
* @param size total size of the array/tree.
* @param K arity of the linearized search tree being converted from.
* @param tree_idx index in the tree to be converted to an array index.
* @param H height of the tree.
* @return size_t corresponding index in the sorted array.
*/
size_t perfect_tree_to_array(size_t size,
int K,
size_t tree_idx,
size_t H) {
assert(K > 1);
assert(size > 0);
assert(tree_idx >= 0 && tree_idx < size);
const size_t idx = tree_idx + 1;
const size_t d = std::floor(std::log(idx) / std::log(K));
const size_t o = idx - std::pow(K,d);
return std::pow(K, H-d-1) * std::floor((K*o)/(K-1)+1) - 1;
}
/**
* @brief Maps an index in a linearized perfect K-ary search tree to an
* index in a sorted array.
*
* @param size total size of the array/tree.
* @param K arity of the linearized search tree being converted from.
* @param tree_idx index in the tree to be converted to an array index.
* @return size_t corresponding index in the sorted array.
*/
size_t perfect_tree_to_array(size_t size, int K, size_t tree_idx) {
const size_t H = std::ceil(std::log(size+1) / std::log(K));
return perfect_tree_to_array(size, K, tree_idx, H);
}
/**
* @brief Maps an index in a sorted array to an index in a linearized complete
* K-ary search tree.
*
* @param size total size of the array/tree.
* @param K arity of the linearized search tree being converted to.
* @param array_idx index in the sorted array to be converted to a tree index.
* @return size_t corresponding index in a complete linearized K-ary search tree.
*/
size_t array_to_complete_tree(size_t size, int K, size_t array_idx) {
assert(K > 1);
assert(size > 0);
assert(array_idx >= 0 && array_idx < size);
const size_t H = std::ceil(std::log(size+1) / std::log(K));
const size_t fringe = perfect_tree_to_array(size, K, size-1, H);
if(array_idx <= fringe) {
return array_to_perfect_tree(size, K, array_idx, H);
}
else {
const size_t d = std::floor(std::log(size) / std::log(K));
const size_t o = size - std::pow(K,d);
return array_to_perfect_tree(size, K, array_idx-o-1, H-1);
}
}
/**
* @brief Maps an index in a linearized complete K-ary search tree to an index
* in a sorted array.
*
* @param size total size of the array/tree.
* @param K arity of the linearized search tree being converted to.
* @param tree_idx index in the tree to be converted to an array index.
* @return size_t corresponding index in the sorted array.
*/
size_t complete_tree_to_array(size_t size, int K, size_t tree_idx) {
assert(K > 1);
assert(size > 0);
assert(tree_idx >= 0 && tree_idx < size);
const size_t H = std::ceil(std::log(size+1) / std::log(K));
const size_t fringe = perfect_tree_to_array(size, K, size-1, H);
const size_t array_idx = perfect_tree_to_array(size, K, tree_idx, H);
if(array_idx <= fringe) {
return array_idx;
}
else {
const size_t d = std::floor(std::log(size) / std::log(K));
const size_t o = size - std::pow(K,d);
return perfect_tree_to_array(size, K, tree_idx, H-1) + o + 1;
}
}