1
- mutable struct Trie{T }
2
- value:: T
3
- children:: Dict{Char ,Trie{T }}
1
+ mutable struct Trie{K,V }
2
+ value:: V
3
+ children:: Dict{K ,Trie{K,V }}
4
4
is_key:: Bool
5
5
6
- function Trie {T } () where T
7
- self = new {T } ()
8
- self. children = Dict {Char ,Trie{T }} ()
6
+ function Trie {K,V } () where {K,V}
7
+ self = new {K,V } ()
8
+ self. children = Dict {K ,Trie{K,V }} ()
9
9
self. is_key = false
10
10
return self
11
11
end
12
12
13
- function Trie {T} (ks, vs) where T
14
- t = Trie {T} ()
15
- for (k, v) in zip (ks, vs)
16
- t[k] = v
17
- end
18
- return t
13
+ function Trie {K,V} (ks, vs) where {K,V}
14
+ return Trie {K,V} (zip (ks, vs))
19
15
end
20
16
21
- function Trie {T } (kv) where T
22
- t = Trie {T } ()
17
+ function Trie {K,V } (kv) where {K,V}
18
+ t = Trie {K,V } ()
23
19
for (k,v) in kv
24
20
t[k] = v
25
21
end
26
22
return t
27
23
end
28
24
end
29
25
30
- Trie () = Trie {Any} ()
31
- Trie (ks:: AbstractVector{K} , vs:: AbstractVector{V} ) where {K<: AbstractString ,V} = Trie {V} (ks, vs)
32
- Trie (kv:: AbstractVector{Tuple{K,V}} ) where {K<: AbstractString ,V} = Trie {V} (kv)
33
- Trie (kv:: AbstractDict{K,V} ) where {K<: AbstractString ,V} = Trie {V} (kv)
34
- Trie (ks:: AbstractVector{K} ) where {K<: AbstractString } = Trie {Nothing} (ks, similar (ks, Nothing))
26
+ Trie () = Trie {Any,Any } ()
27
+ Trie (ks:: AbstractVector{K} , vs:: AbstractVector{V} ) where {K,V} = Trie {eltype(K), V} (ks, vs)
28
+ Trie (kv:: AbstractVector{Tuple{K,V}} ) where {K,V} = Trie {eltype(K), V} (kv)
29
+ Trie (kv:: AbstractDict{K,V} ) where {K,V} = Trie {eltype(K), V} (kv)
30
+ Trie (ks:: AbstractVector{K} ) where {K} = Trie {eltype(K), Nothing} (ks, similar (ks, Nothing))
35
31
36
- function Base. setindex! (t:: Trie{T } , val, key:: AbstractString ) where T
37
- value = convert (T , val) # we don't want to iterate before finding out it fails
32
+ function Base. setindex! (t:: Trie{K,V } , val, key) where {K,V}
33
+ value = convert (V , val) # we don't want to iterate before finding out it fails
38
34
node = t
39
35
for char in key
40
36
if ! haskey (node. children, char)
41
- node. children[char] = Trie {T } ()
37
+ node. children[char] = Trie {K,V } ()
42
38
end
43
39
node = node. children[char]
44
40
end
45
41
node. is_key = true
46
42
node. value = value
47
43
end
48
44
49
- function Base. getindex (t:: Trie , key:: AbstractString )
45
+ function Base. getindex (t:: Trie , key)
50
46
node = subtrie (t, key)
51
47
if node != nothing && node. is_key
52
48
return node. value
53
49
end
54
50
throw (KeyError (" key not found: $key " ))
55
51
end
56
52
57
- function subtrie (t:: Trie , prefix:: AbstractString )
53
+ function subtrie (t:: Trie , prefix)
58
54
node = t
59
55
for char in prefix
60
56
if ! haskey (node. children, char)
@@ -66,30 +62,38 @@ function subtrie(t::Trie, prefix::AbstractString)
66
62
return node
67
63
end
68
64
69
- function Base. haskey (t:: Trie , key:: AbstractString )
65
+ function Base. haskey (t:: Trie , key)
70
66
node = subtrie (t, key)
71
67
node != nothing && node. is_key
72
68
end
73
69
74
- function Base. get (t:: Trie , key:: AbstractString , notfound)
70
+ function Base. get (t:: Trie , key, notfound)
75
71
node = subtrie (t, key)
76
72
if node != nothing && node. is_key
77
73
return node. value
78
74
end
79
75
return notfound
80
76
end
81
77
82
- function Base. keys (t:: Trie , prefix:: AbstractString = " " , found= AbstractString[])
78
+ _concat (prefix:: String , char:: Char ) = string (prefix, char)
79
+ _concat (prefix:: Vector{T} , char:: T ) where {T} = vcat (prefix, char)
80
+
81
+ _empty_prefix (:: Trie{Char,V} ) where {V} = " "
82
+ _empty_prefix (:: Trie{K,V} ) where {K,V} = K[]
83
+
84
+ function Base. keys (t:: Trie{K,V} ,
85
+ prefix= _empty_prefix (t),
86
+ found= Vector {typeof(prefix)} ()) where {K,V}
83
87
if t. is_key
84
88
push! (found, prefix)
85
89
end
86
90
for (char,child) in t. children
87
- keys (child, string (prefix,char), found)
91
+ keys (child, _concat (prefix, char), found)
88
92
end
89
93
return found
90
94
end
91
95
92
- function keys_with_prefix (t:: Trie , prefix:: AbstractString )
96
+ function keys_with_prefix (t:: Trie , prefix)
93
97
st = subtrie (t, prefix)
94
98
st != nothing ? keys (st,prefix) : []
95
99
end
101
105
# see the comments and implementation below for details.
102
106
struct TrieIterator
103
107
t:: Trie
104
- str:: AbstractString
108
+ str
105
109
end
106
110
107
111
# At the start, there is no previous iteration,
@@ -120,11 +124,11 @@ function Base.iterate(it::TrieIterator, (t, i) = (it.t, 0))
120
124
end
121
125
end
122
126
123
- partial_path (t:: Trie , str:: AbstractString ) = TrieIterator (t, str)
127
+ partial_path (t:: Trie , str) = TrieIterator (t, str)
124
128
Base. IteratorSize (:: Type{TrieIterator} ) = Base. SizeUnknown ()
125
129
126
130
"""
127
- find_prefixes(t::Trie, str::AbstractString )
131
+ find_prefixes(t::Trie, str)
128
132
129
133
Find all keys from the `Trie` that are prefix of the given string
130
134
@@ -137,10 +141,24 @@ julia> find_prefixes(t, "ABCDE")
137
141
"A"
138
142
"ABC"
139
143
"ABCD"
144
+
145
+ julia> t′ = Trie([1:1, 1:3, 1:4, 2:4]);
146
+
147
+ julia> find_prefixes(t′, 1:5)
148
+ 3-element Vector{UnitRange{Int64}}:
149
+ 1:1
150
+ 1:3
151
+ 1:4
152
+
153
+ julia> find_prefixes(t′, [1,2,3,4,5])
154
+ 3-element Vector{Vector{Int64}}:
155
+ [1]
156
+ [1, 2, 3]
157
+ [1, 2, 3, 4]
140
158
```
141
159
"""
142
- function find_prefixes (t:: Trie , str:: AbstractString )
143
- prefixes = AbstractString []
160
+ function find_prefixes (t:: Trie , str:: T ) where {T}
161
+ prefixes = T []
144
162
it = partial_path (t, str)
145
163
idx = 0
146
164
for t in it
@@ -150,4 +168,4 @@ function find_prefixes(t::Trie, str::AbstractString)
150
168
idx = nextind (str, idx)
151
169
end
152
170
return prefixes
153
- end
171
+ end
0 commit comments