var trie = new Trie[Int]
trie["foo"] = 1
trie["fooo"] = 2
trie["bar"] = 3
# Search by key
assert trie.has_key("foo")
assert trie["foo"] == 1
# Search by prefix
assert trie.find_by_prefix("") == [1, 3, 2]
assert trie.find_by_prefix("foo") == [1, 2]
assert trie.find_by_prefix("baz").is_empty
trees :: Trie :: _cache_key
Cache key used betweenhas_key
and []
trees :: Trie :: cache_key=
Cache key used betweenhas_key
and []
trees :: Trie :: defaultinit
trees :: Trie :: find_by_prefix
Find values stored underprefix
trees :: Trie :: search_node
Search a node by a key or prefixtrees :: Trie :: _cache_key
Cache key used betweenhas_key
and []
serialization :: Serializable :: accept_inspect_serializer_core
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
trees :: Trie :: cache_key=
Cache key used betweenhas_key
and []
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Map :: defaultinit
core :: Object :: defaultinit
core :: MapRead :: defaultinit
trees :: Trie :: defaultinit
core :: MapRead :: filter_keys
Return all elements ofkeys
that have a value.
trees :: Trie :: find_by_prefix
Find values stored underprefix
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
core :: MapRead :: get_or_default
Get the item atkey
or return default
if not in map
core :: MapRead :: get_or_null
Get the item atkey
or null if key
is not in the map.
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: MapRead :: keys_sorted_by_values
Return an array of all keys sorted with their values usingcomparator
.
core :: MapRead :: lookup_all_values
Search all the values inpe.greaters
.
core :: MapRead :: lookup_values
Combine the values inpe.greaters
from the most smaller elements that have a value.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).core :: MapRead :: provide_default_value
Called by the underling implementation of[]
to provide a default value when a key
has no value
trees :: Trie :: search_node
Search a node by a key or prefixserialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: serialize_to_or_delay
Accept references or force direct serialization (usingserialize_to
)
core :: MapRead :: to_map_comparator
A comparator that compares things with their values in self.serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
core :: MapRead :: values_sorted_by_key
Return an array of all values sorted with their keys usingcomparator
.
Serializer::serialize
# Trie data structure for prefix searches
#
# ~~~
# # Associate some integers to Map keys
# var trie = new Trie[Int]
# trie["foo"] = 1
# trie["fooo"] = 2
# trie["bar"] = 3
#
# # Search by key
# assert trie.has_key("foo")
# assert trie["foo"] == 1
#
# # Search by prefix
# assert trie.find_by_prefix("") == [1, 3, 2]
# assert trie.find_by_prefix("foo") == [1, 2]
# assert trie.find_by_prefix("baz").is_empty
# ~~~
class Trie[E]
super Map[String, E]
# Trie root
#
# Root children are used to store the first letters.
private var root = new TrieNode[E]
redef fun []=(key, value) do
var children = root.children
for i in [0..key.length[ do
var c = key.chars[i]
var node
if children.has_key(c) then
node = children[c]
else
node = new TrieNode[E](c)
children[c] = node
end
children = node.children
if i == key.length - 1 then
node.is_leaf = true
node.value = value
end
end
end
# Cache node used between `has_key` and `[]`
private var cache: nullable TrieNode[E] = null
# Cache key used between `has_key` and `[]`
private var cache_key: nullable String = null
redef fun [](key) do
if cache_key == key then return cache.as(not null).value.as(not null)
var node = search_node(key)
assert node != null
return node.value
end
redef fun has_key(key) do
var node = search_node(key)
if node == null then return false
var res = node.is_leaf
if res then
cache = node
cache_key = key.as(String)
end
return res
end
# Find values stored under `prefix`
#
# ~~~
# # Associate some integers to Map keys
# var trie = new Trie[Int]
# trie["foo"] = 1
# trie["fooo"] = 2
# trie["foooo"] = 3
# trie["bar"] = 4
#
# assert trie.find_by_prefix("") == [1, 4, 2, 3]
# assert trie.find_by_prefix("foo") == [1, 2, 3]
# assert trie.find_by_prefix("bar") == [4]
# assert trie.find_by_prefix("baz").is_empty
# ~~~
fun find_by_prefix(prefix: String): Array[E] do
var node
if prefix == "" then
node = root
else
node = search_node(prefix)
end
if node == null then return new Array[E]
return node.collect_values
end
# Find values stored under `prefix`
#
# ~~~
# # Associate some integers to Map keys
# var trie = new Trie[Int]
# trie["foo"] = 1
# trie["bar"] = 4
# trie["baz"] = 5
#
# assert trie.has_prefix("")
# assert trie.has_prefix("f")
# assert not trie.has_prefix("z")
# ~~~
fun has_prefix(prefix: String): Bool do
if prefix == "" then return true
return search_node(prefix) != null
end
# Search a node by a key or prefix
#
# Returns `null` if no node matches `string`.
private fun search_node(string: nullable Object): nullable TrieNode[E] do
if string == null then return null
string = string.to_s
var children = root.children
var node = null
for i in [0..string.length[ do
var c = string.chars[i]
if children.has_key(c) then
node = children[c]
children = node.children
else
node = null
break
end
end
return node
end
end
lib/trees/trie.nit:45,1--181,3