-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringSet.ml
416 lines (350 loc) · 11.9 KB
/
stringSet.ml
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
(*TODO
- sort
- range search
- longest_prefix
- wildcard match
- neighrest neighbor search (k difference)
- multi set (a set with list as value)
- patricia trie/tree (crit bit tree)
- ex: T9
*)
(* Notes
- most of the ops not tail recursive (that could be done through continuations if needed)
*)
open StringUtil
module Tst = struct
type 'a t =
| E
| N of 'a t * 'a t * 'a t * char
let create () =
E
let rec _insert t s i len =
if i = len then t
else match t with
| E ->
N(E, _insert E s (i + 1) len, E, s.[i])
| N(left, mid, right, c) ->
let cur = s.[i] in
if cur < c then
N(_insert left s i len, mid, right, c)
else if cur > c then
N(left, mid, _insert right s i len, c)
else (*cur = c*)
N(left, _insert mid s (i + 1) len, right, c)
let insert t s =
let len = String.length s in
_insert t s 0 len
let rec _has_prefix t s i len =
if i = len then true
else match t with
| E ->
false
| N(l,m,r,c) ->
let cur = s.[i] in
if cur < c then
_has_prefix l s i len
else if cur > c then
_has_prefix r s i len
else (*cur = c*)
_has_prefix m s (i + 1) len
let has_prefix t s =
_has_prefix t s 0 (String.length s)
end (* module Tst *)
module CTst = struct (* collapsed TST *)
type 'a t =
| E
| N of 'a t * 'a t * 'a t * string
(* semantic: N(l,m,r,"abc") = N(E,N(E,N(E,E,E,'c'),E,'b'),e,'a') *)
let create () =
E
let rec _insert t s i len =
if i = len then t
else match t with
| E ->
let v = if i = 0 then s else String.sub s i (len - i) in
N(E, E, E, v)
| N(left, mid, right, c) as n ->
let clen = String.length c in
match string_cmp_i s i len c 0 clen with
| Eq ->
n
| Prefix (* s+i is prefix of c *) ->
n (* do not even need to mark the end of the key *)
| Contain ->
N(left, _insert mid s (i + clen) len, right, c)
| Inf p (* p < len *) ->
(* s and c may share a common prefix *)
if p = i
then (* empty prefix, s < c *)
N(_insert left s i len, mid, right, c)
else
let prefix = String.sub s i (p - i) in
let rest = String.sub c (p - i) (clen - p - i) in
let sub = _insert (N(left, mid, right, rest)) s (i + p) len in
N(E, sub, E, prefix)
| Sup p ->
(* s and c may share a common prefix *)
if p = i
then (* empty prefix, s > c *)
N(left, mid, _insert right s i len, c)
else
let prefix = String.sub s i (p - i) in
let rest = String.sub c (p - i) (clen - p - i) in
let sub = _insert (N(left, mid, right, rest)) s (i + p) len in
N(E, sub, E, prefix)
let insert t s =
let len = String.length s in
_insert t s 0 len
let rec _has_prefix t s i len =
if i = len then true
else match t with
| E ->
false
| N(l,m,r,c) ->
let clen = String.length c in
match string_cmp_i s i len c 0 clen with
| Eq ->
true
| Prefix (* s+i is prefix of c *) ->
true
| Contain ->
_has_prefix m s (i + clen) len
| Inf p (* p < len *) ->
if p = i then
_has_prefix l s i len
else
false
| Sup p ->
if p = i then
_has_prefix r s i len
else
false
let has_prefix t s =
_has_prefix t s 0 (String.length s)
end (* module CTst *)
module Radix = struct
(* not sure about the algo ... maybe a mix of patricia, radix, critbit *)
type 'a _n_t = (* node: Leaf, Trunk (for prefix inputs), Branch *)
| L of string * 'a
| T of 'a _n_t * string * 'a
| B of 'a _n_t * 'a _n_t * int * int (* left, right, critical char index, critical bit index (this is bit index in char ... from right to left) *)
type 'a t = 'a _n_t option
let create () =
None
let rec _first_key t =
match t with
| L(k, _) ->
k
| T(_, k, _) ->
k
| B(l, r, _, _) ->
_first_key l (* favor left??? ... could take the shortest path if B node embed the size of tree *)
let rec _bind t s sl v =
match t with
| L(k, d) ->
begin
let kl = String.length k in
match string_cmp_i s 0 sl k 0 kl with
| Eq ->
L(k, v) (* replace *)
| Prefix ->
T(t, s, v)
| Contain ->
T(L(s, v), k, d)
| Inf p ->
let b = critbit s.[p] k.[p] in (* can raise but this would be unexpected here *)
B(L(s, v), t, p, b)
| Sup p ->
let b = critbit s.[p] k.[p] in (* can raise but this would be unexpected here *)
B(t, L(s, v), p, b)
end
| T(m, k, v) ->
let kl = String.length k in
if kl = sl then
T(m, k, v) (* replace *)
else
_bind m s sl v
| B(l, r, i, b) ->
if sl > i then
if ((int_of_char s.[i]) land (1 lsl b)) = 0 then
B(_bind l s sl v, r, i, b)
else
B(l, _bind r s sl v, i, b)
else
begin
let k = _first_key l in (* get the first string in tree (at least size i) *)
match string_cmp_i s 0 sl k 0 sl with
| Eq | Prefix ->
T(t, s, v)
| Contain ->
failwith "unexpected data structure state (for real ... this is a bug!)"
| Inf p ->
let bn = critbit s.[p] k.[p] in (* can raise but this would be unexpected here *)
B(L(s, v), t, p, bn)
| Sup p ->
let bn = critbit s.[p] k.[p] in (* can raise but this would be unexpected here *)
B(t, L(s, v), p, bn)
end
let bind radix s v =
match radix with
| None -> Some(L(s, v))
| Some t ->
let sl = String.length s in
Some(_bind t s sl v)
let rec _lookup t s sl =
match t with
| L(k, v) ->
if s = k then Some v else None
| T(m, k, v) ->
let kl = String.length k in
if kl < sl then
_lookup m s sl
else if kl > sl then
None
else (* kl == sl *)
(if s = k then Some v else None)
| B(l, r, i, b) ->
if sl > i then
let dir = if ((int_of_char s.[i]) land (1 lsl b)) = 0 then l else r in
_lookup dir s sl
else
None
let lookup radix s =
match radix with
| None -> None
| Some t ->
let sl = String.length s in
_lookup t s sl
end (* module Radix *)
module type Map = sig
(* dict or map or finiteMap *)
type key_t
type 'a t
val create: unit -> 'a t
val lookup: 'a t -> key_t -> 'a option
val bind: 'a t -> key_t -> 'a -> 'a t
end (* module type Map *)
module TstMap : Map with type key_t = string = struct
type key_t = string
type 'a t =
| E
| N of 'a t * 'a t * 'a t * char * 'a option
let create () =
E
let rec _bind t s i len v =
if i = len - 1 then
begin
match t with
| E ->
N(E, E, E, s.[i], Some v)
| N(l, m, r, c, o) ->
let cur = s.[i] in
if cur < c then
N(_bind l s i len v, m, r, c, o)
else if cur > c then
N(l, m, _bind r s i len v, c, o)
else (*cur = c*)
N(l, m, r, c, Some v) (* REPLACE existing value *)
end
else match t with
| E ->
N(E, _bind E s (i + 1) len v, E, s.[i], None)
| N(left, mid, right, c, o) ->
let cur = s.[i] in
if cur < c then
N(_bind left s i len v, mid, right, c, o)
else if cur > c then
N(left, mid, _bind right s i len v, c, o)
else (*cur = c*)
N(left, _bind mid s (i + 1) len v, right, c, o)
let bind t s v =
let len = String.length s in
if len > 0 then _bind t s 0 len v
else t
let rec _lookup t s i len =
if i = len - 1 then
begin
match t with
| E ->
None
| N(l, m, r, c, o) ->
let cur = s.[i] in
if cur < c then
_lookup l s i len
else if cur > c then
_lookup r s i len
else (*cur = c*)
o
end
else match t with
| E ->
None
| N(l,m,r,c,_) ->
let cur = s.[i] in
if cur < c then
_lookup l s i len
else if cur > c then
_lookup r s i len
else (*cur = c*)
_lookup m s (i + 1) len
let lookup t s =
let len = String.length s in
if len > 0 then _lookup t s 0 len
else None
end (* module TstMap *)
(* adapted from "Purely functional data structure" [Chris Okasaki, 1998]
bootstraped a Map of char to create a Map of string
*)
module TrieFtor (M : Map with type key_t = char) : Map with type key_t = string = struct
type key_t = string
type 'a t = Trie of 'a option * 'a t M.t
let create () = Trie(None, M.create ())
let rec _lookup t s i len =
match t with
| Trie(maybe, m) ->
if i = len then
maybe
else
begin
match M.lookup m s.[i] with
| Some trie ->
_lookup trie s (i + 1) len
| None ->
None
end
let lookup t s =
_lookup t s 0 (String.length s)
let rec _bind t s i len v =
match t with
| Trie(o, m) ->
begin
if i = len then Trie(Some v, m) (* discard o *)
else
begin
let trie = match M.lookup m s.[i] with
| Some mt ->
mt
| None ->
create ()
in
let tt = _bind trie s (i + 1) len v in
Trie(o, M.bind m s.[i] tt)
end
end
let bind t s v =
_bind t s 0 (String.length s) v
end
(* Build a Trie based on a Hashtbl *)
module HashtblTrie = TrieFtor (struct
(* Need to wrap Hastbl to match our Map definition *)
type key_t = char
type 'a t = (char, 'a) Hashtbl.t
let create () =
Hashtbl.create 128
let bind t s v =
let _ = Hashtbl.replace t s v in
t
let lookup t s =
try let v = Hashtbl.find t s in Some v
with Not_found -> None
end)