/
libdatrie.pm6
448 lines (333 loc) · 10.2 KB
/
libdatrie.pm6
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
use v6;
=begin pod
=head1 NAME
Algorithm::Trie::libdatrie - a character keyed trie using the datrie library.
=head1 SYNOPSIS
use Algorithm::Trie::libdatrie;
my Trie $t .= new: 'a'..'z', 'A'..'Z';
my @words = <pool prize preview prepare produce progress>;
for @words.kv -> $data, $word {
$t.store( $word, $data );
}
$data = $t.retrieve($word);
my $iter = $t.iterator;
while $iter.next {
$key = $iter.key;
$data = $iter.value;
}
=head1 WARNING
More documentation and maybe a few more features and tests are planned. For
now the tests are probably the best documentation.
=head1 DESCRIPTION
Algorithm::Trie::libdatrie is an implementation of a character keyed L<trie|http://en.wikipedia.org/wiki/Trie> using the L<datrie|http://linux.thai.net/~thep/datrie/datrie.html> library.
As the author of the datrie library states:
=begin para :nested
Trie is a kind of digital search tree, an efficient indexing method with
O(1) time complexity for searching. Comparably as efficient as hashing,
trie also provides flexibility on incremental matching and key spelling
manipulation. This makes it ideal for lexical analyzers, as well as
spelling dictionaries.
This library is an implementation of double-array structure for representing
trie, as proposed by Junichi Aoe. The details of the implementation can be
found at L<http://linux.thai.net/~thep/datrie/datrie.html>
=end para
=head1 Classes and Methods
=head2 Trie
multi method new(**@ranges) returns Trie
multi method new(Str $file) returns Trie
method save(Str $file) returns Bool
method is-dirty() returns Bool
method store(Str $key, Int $data) returns Bool
method store-if-absent(Str $key, Int $data) returns Bool
method retrieve(Str $key) returns Int
method delete(Str $key) returns Bool
method root() returns TrieState
method iterator() returns TrieIterator
method free()
/* NYI
sub enum_func(Str $key, Int $value, Pointer[void] $stash) returns Bool { * }
method enumerate(&enum_func, Pointer[void] $stash) returns Bool
*/
=item new
my Trie $t .= new: 'a'..'z', 'A'..'Z', '0'..'9';
The set of characters used in C<key>s has a maximum size of 255. The
characters themselves may be any unicode character who's code will fit in a
32 bit uint. The C<new> function will map the input ranges into C<0..254>
internally.
my Trie $t .= new: $file;
A Trie may be loaded from a R<$file> created by the C<save> method.
=item root, iterator
These methods return objects of class C<TrieState> and C<TrieIterator> that
are positioned at the root of the Trie.
=item the rest
Should be mostly self-explanatory. See the tests.
=head2 TrieState
A TrieState object is used to walk through the Trie character by character.
A TrieState object may also be used to create a TrieIterator in order to
iterate over the nodes beneath the TrieStat's current position.
method clone() returns TrieState
method rewind()
method walk(Str $c where *.chars == 1) returns Bool
method is-walkable(Str $c where *.chars == 1) returns Bool
method walkable-chars() returns Array[Str]
method is-terminal() returns Bool
method is-single() returns Bool
method is-leaf() returns Bool
method value() returns Int
method free()
=head2 TrieIterator
A TrieIterator can be created from the Trie directly via C<$trie.iterator>
or from a TrieState via C<TrieIterator.new($trie-state)>.
method new(TrieState $state) returns TrieIterator
method next() returns Bool
method key() returns Str
method value() returns Int
method free()
=end pod
unit class Algorithm::Trie::libdatrie:ver<0.2>:auth<github:zengargoyle>;
use NativeCall;
use LibraryMake;
sub library {
my $so = get-vars('')<SO>;
my $libname = "lib/libdatrie$so";
my $lib = %?RESOURCES{$libname}.Str;
if not $lib.defined {
die "Unable to find library";
}
$lib;
}
# for freeing returned key from TrieIterator.key
sub free(CArray[uint32]) is native(Str) { * }
class AlphaMap is repr('CPointer') { }
class TrieState is export is repr('CPointer') {
method clone() returns TrieState {
trie_state_clone(self) || fail 'could not clone';
}
# XXX: not implemented, use .clone instead
# sub copy(TriState $dst, Tristate $src) {
# trie_state_copy($dst,$src);
# }
method free() {
trie_state_free(self);
}
method rewind() {
trie_state_rewind(self);
}
method walk(Str $c where *.chars == 1) returns Bool {
trie_state_walk(self, $c.ord) !== 0;
}
method is-walkable(Str $c where *.chars == 1) returns Bool {
trie_state_is_walkable(self, $c.ord) !== 0;
}
method walkable-chars() returns Array[Str] {
my $walkable = CArray[uint32].new;
$walkable[256] = 0;
trie_state_walkable_chars(self, $walkable, 256)
|| fail 'could not get walkable chars';
my Str @w;
loop {
state $i = 0;
last if $walkable[$i] == 0;
@w.push: $walkable[$i++].chr;
}
@w;
}
# NOTE: is-terminal is MACRO (TRIE_CHAR_TERM = '\0'
method is-terminal() returns Bool {
trie_state_is_walkable(self, 0) !== 0;
}
method is-single() returns Bool {
trie_state_is_single(self) !== 0;
}
# NOTE: is-leaf is MACRO
method is-leaf() returns Bool {
self.is-single && self.is-terminal();
}
method value() returns Int {
Int( trie_state_get_data(self) );
}
}
class TrieIterator is export is repr('CPointer') {
method new(TrieState $state) returns TrieIterator {
trie_iterator_new($state);
}
method next() returns Bool {
trie_iterator_next(self) !== 0;
}
method key() returns Str {
my Str $key;
my $k = trie_iterator_get_key(self) || fail 'could not get key';
loop { state $i = 0; last if $k[$i] == 0; $key ~= $k[$i++].chr }
free($k);
$key;
}
method value() returns Int {
Int( trie_iterator_get_data(self) );
}
method free() {
trie_iterator_free(self);
}
}
class Trie is export is repr('CPointer') {
multi method new(**@ranges) returns Trie {
my AlphaMap $map = alpha_map_new();
for @ranges -> $range {
alpha_map_add_range($map, |$range.bounds.map(*.ord)) == 0
or fail "could not add range: $range";
}
my $t = trie_new($map) || fail "could not create trie";
alpha_map_free($map);
return $t;
}
multi method new(Str $file) returns Trie {
trie_new_from_file($file) || fail "could not load trie from '$file'"
}
method save(Str $file) returns Bool {
trie_save(self,$file) == 0 || fail "could not save trie to '$file'";
}
method free() {
trie_free(self);
}
method is-dirty() {
trie_is_dirty(self);
}
method store(Str $key, Int $data) returns Bool {
my $c = CArray[uint32].new($key.ords, 0);
trie_store(self, $c, $data) !== 0;
}
method store-if-absent(Str $key, Int $data) returns Bool {
my $c = CArray[uint32].new($key.ords, 0);
trie_store_if_absent(self, $c, $data) !== 0;
}
method retrieve(Str $key) returns Int {
my $c = CArray[uint32].new($key.ords, 0);
my $ret = CArray[uint32].new(0);
my $rc = trie_retrieve(self, $c, $ret);
return Int($ret[0]) but ($rc ?? True !! False);
}
method delete(Str $key) returns Bool {
my $c = CArray[uint32].new($key.ords, 0);
trie_delete(self, $c) !== 0;
}
method root() returns TrieState {
trie_root(self) || fail 'could not get root state';
}
method iterator() returns TrieIterator {
trie_iterator_new(trie_root(self));
}
}
#
# AlphaMap
#
sub alpha_map_new() returns AlphaMap
is native(&library)
{ * }
sub alpha_map_add_range(AlphaMap,int32,int32) returns int32
is native(&library)
{ * }
sub alpha_map_free(AlphaMap)
is native(&library)
{ * }
#
# Trie
#
sub trie_new(AlphaMap) returns Trie
is native(&library)
{ * }
sub trie_new_from_file(Str) returns Trie
is native(&library)
{ * }
sub trie_save(Trie,Str) returns int32
is native(&library)
{ * }
sub trie_free(Trie)
is native(&library)
{ * }
sub trie_store(Trie,CArray[uint32],int32) returns int32
is native(&library)
{ * }
sub trie_store_if_absent(Trie,CArray[uint32],int32) returns int32
is native(&library)
{ * }
sub trie_delete(Trie,CArray[uint32]) returns int32
is native(&library)
{ * }
sub trie_retrieve(Trie,CArray[uint32],CArray[uint32]) returns int32
is native(&library)
{ * }
sub trie_root(Trie) returns TrieState
is native(&library)
{ * }
sub trie_is_dirty(Trie) returns uint32
is native(&library)
{ * }
# TODO: callbacks into Perl 6 make brain hurt.
# sub trie_enumerate(Trie,TrieEnumFunc,Pointer[void]) returns uint32
# is native(&library)
# { * }
# XXX: not implemented raw I/O via IO::Handle?
# sub trie_fwrite(Trie,FILE) returns uint32
# is native(&library)
# { * }
# sub trie_fread(FILE) returns Trie
# is native(&library)
# { * }
#
# TrieIterator
#
sub trie_iterator_new(TrieState) returns TrieIterator
is native(&library)
{ * }
sub trie_iterator_next(TrieIterator) returns Bool
is native(&library)
{ * }
sub trie_iterator_get_key(TrieIterator) returns CArray[uint32]
is native(&library)
{ * }
sub trie_iterator_get_data(TrieIterator) returns uint32
is native(&library)
{ * }
sub trie_iterator_free(TrieIterator)
is native(&library)
{ * }
#
# TrieState
#
sub trie_state_clone(TrieState) returns TrieState
is native(&library)
{ * }
# XXX: no copy, use clone
# sub trie_state_copy(TrieState) returns TrieState
# is native(&library)
# { * }
sub trie_state_free(TrieState)
is native(&library)
{ * }
sub trie_state_rewind(TrieState)
is native(&library)
{ * }
sub trie_state_walk(TrieState,uint32) returns uint32
is native(&library)
{ * }
sub trie_state_is_walkable(TrieState,uint32) returns uint32
is native(&library)
{ * }
sub trie_state_walkable_chars(TrieState,CArray[uint32],uint32) returns uint32
is native(&library)
{ * }
sub trie_state_is_single(TrieState) returns uint32
is native(&library)
{ * }
sub trie_state_get_data(TrieState) returns uint32
is native(&library)
{ * }
=begin pod
=head1 SEE ALSO
The datrie library: L<http://linux.thai.net/~thep/datrie/datrie.html>
Wikipedia entry for: L<Trie|http://en.wikipedia.org/wiki/Trie>
=head1 AUTHOR
zengargoyle <zengargoyle@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright 2015 zengargoyle
This library is free software; you can redistribute it and/or modify it under the Artistic License 2.0.
=end pod