about summary refs log tree commit diff stats
path: root/lib/Octans/RangeSearch.rakumod
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-01-19 21:53:16 +0530
committerAndinus <andinus@nand.sh>2021-01-19 21:53:16 +0530
commita5c540608c469a2797262facb766e175b932f0e2 (patch)
tree1e0eb79678d66f5ba657c63003592029757aefe7 /lib/Octans/RangeSearch.rakumod
parent5bb0f224483fbc1d57fd1c5a2f4a22dd7263ecd6 (diff)
downloadoctans-a5c540608c469a2797262facb766e175b932f0e2.tar.gz
Re-structure for CPAN upload, include a dictionary file v0.1.0
bin/octans calls lib/Octans/CLI.rakumod which has the MAIN subroutine.
Diffstat (limited to 'lib/Octans/RangeSearch.rakumod')
-rw-r--r--lib/Octans/RangeSearch.rakumod70
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/Octans/RangeSearch.rakumod b/lib/Octans/RangeSearch.rakumod
new file mode 100644
index 0000000..e287d93
--- /dev/null
+++ b/lib/Octans/RangeSearch.rakumod
@@ -0,0 +1,70 @@
+unit module Octans::RangeSearch;
+
+# range-starts-with returns a subset of given @dict list that start
+# with $str. It should be faster than:
+#
+#   @dict.grep: *.starts-with($str)
+#
+# @dict should be a sorted list of words. It performs binary lookup on
+# the list.
+sub range-starts-with (
+    @dict, Str $str --> List
+) is export {
+    # $lower, $upper hold the lower and upper index of the range
+    # respectively.
+    my Int ($lower, $upper);
+
+    # Lookup the whole dictionary.
+    my Int ($start, $end) = (0, @dict.end);
+
+    # Loop until we end up on the lower index of range.
+    while $start < $end {
+        # Divide the list into 2 parts.
+        my Int $mid = ($start + $end) div 2;
+
+        # Check if $mid word is le (less than or equal to) $str. If
+        # true then discard the bottom end of the list, if not then
+        # discard the top end.
+        if $str le @dict[$mid].substr(0, $str.chars).lc {
+            $end = $mid;
+        } else {
+            $start = $mid + 1;
+        }
+    }
+
+    # Found the lower index.
+    $lower = $start;
+
+    # Set $end to the end of list but keep $start at the lower index.
+    $end = @dict.end;
+
+    # Loop until we end up on the upper index of range.
+    while $start < $end {
+        # Divide the list into 2 parts. Adds 1 because we have to find
+        # the upper index in this part. `div' performs Interger
+        # division, output is floor'ed.
+        my Int $mid = (($start + $end) div 2) + 1;
+
+        # Check if $mid word is lt (less than) $str. If true then
+        # discard the bottom end of the list, if not then discard the
+        # top end.
+        if $str lt @dict[$mid].substr(0, $str.chars).lc {
+            $end = $mid - 1;
+        } else {
+            $start = $mid;
+        }
+    }
+
+    # Found the upper index.
+    $upper = $end;
+
+    with @dict[$lower..$upper] -> @list {
+        # Maybe the word doesn't exist in the list, in that case there
+        # will be a single element in @list. We return an empty list
+        # unless that single element starts with $str.
+        if @list.elems == 1 {
+            return () unless @list[0].starts-with($str);
+        }
+        return @list;
+    }
+}
ision' href='/akkartik/mu/blame/041name.cc?h=hlt&id=a1968ebb48c06e1cbc2b813a73e4be235da7b3ee'>^
286d7620 ^
ac0e9db5 ^
05d17773 ^
052535fd ^
80b781cc ^

363be37f ^
ce2b0aec ^

ac0e9db5 ^
de49fb42 ^
ac0e9db5 ^
de49fb42 ^
ac0e9db5 ^
05d17773 ^
f1a6f323 ^
06b3eb96 ^
ac0e9db5 ^
ce2b0aec ^

235958e8 ^
ce2b0aec ^
35064671 ^
80b781cc ^
827898fc ^
80b781cc ^
ac0e9db5 ^
ce2b0aec ^

235958e8 ^
ce2b0aec ^
35064671 ^
ce2b0aec ^

80b781cc ^
827898fc ^
80b781cc ^

363be37f ^
35064671 ^
ce2b0aec ^

235958e8 ^
a5840f21 ^
235958e8 ^
35064671 ^
235958e8 ^

ce2b0aec ^
ab6ed192 ^
0f125d5f ^
047296d8 ^
ce2b0aec ^

80b781cc ^
fc55fea0 ^
ac0e9db5 ^
82ac0b7e ^


363be37f ^
82ac0b7e ^


363be37f ^
ac0e9db5 ^
363be37f ^
a3d9828c ^
35064671 ^
a3d9828c ^


363be37f ^
db5c9550 ^
ac0e9db5 ^

05d17773 ^
a3d9828c ^
35064671 ^
a3d9828c ^


ce2b0aec ^
ab6ed192 ^
ce2b0aec ^

0f125d5f ^
ce2b0aec ^


ab6ed192 ^
ce2b0aec ^

0f125d5f ^
ce2b0aec ^

198ad741 ^
ac0e9db5 ^
05d17773 ^
198ad741 ^



ce2b0aec ^

ce2b0aec ^
047296d8 ^
ce2b0aec ^


3b0aa1ac ^
fc55fea0 ^

bc643692 ^
fc55fea0 ^


ad68bbce ^
198ad741 ^
3b0aa1ac ^
1fbebe73 ^
198ad741 ^
bc643692 ^
198ad741 ^

1fbebe73 ^
198ad741 ^
3b0aa1ac ^
ce2b0aec ^

5497090a ^
ce2b0aec ^


3b0aa1ac ^
ce2b0aec ^

bc643692 ^
5497090a ^
ce2b0aec ^


3b0aa1ac ^
ce2b0aec ^

5497090a ^
ce2b0aec ^

5877ef32 ^
ce2b0aec ^
3b0aa1ac ^
ce2b0aec ^

bc643692 ^
ce2b0aec ^

5877ef32 ^
ce2b0aec ^
7284d503 ^
caec3c16 ^
db5c9550 ^
a3d9828c ^


3b0aa1ac ^
a3d9828c ^
bc643692 ^
10ffce98 ^

a3d9828c ^
db5c9550 ^

72cc3ae1 ^
06b3eb96 ^
db5c9550 ^
363be37f ^

e4630643 ^
09bd1e32 ^
e4630643 ^

8b9e9fb1 ^
35064671 ^
6179649e ^

363be37f ^
6179649e ^
35064671 ^
6179649e ^
06b3eb96 ^

72cc3ae1 ^
8c9e97ae ^
3b0aa1ac ^
72cc3ae1 ^
bc643692 ^

72cc3ae1 ^


caec3c16 ^
7284d503 ^
caec3c16 ^

3b0aa1ac ^
caec3c16 ^
bc643692 ^


afa42503 ^
caec3c16 ^
5497090a ^
caec3c16 ^



363be37f ^
e4630643 ^



ab6ed192 ^
6179649e ^

363be37f ^
6179649e ^
35064671 ^
6179649e ^
caec3c16 ^
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252