Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
MCSP.Data.RadixTree.Map
Description
A map of String
keys represented by a radix tree.
Synopsis
- data RadixTreeMap a v = Tree {}
- data Edge a v = !(String a) :~> !(RadixTreeMap a v)
- type EdgeSet a v = Map a (Edge a v)
- debug :: (ShowString a, Show v) => RadixTreeMap a v -> String
- empty :: RadixTreeMap a v
- construct :: Ord a => [(String a, v)] -> RadixTreeMap a v
- lookup :: Ord a => String a -> RadixTreeMap a v -> Maybe v
- lookupMin :: RadixTreeMap a v -> Maybe v
- lookupMinWithKey :: Unbox a => RadixTreeMap a v -> Maybe (String a, v)
- lookupMax :: RadixTreeMap a v -> Maybe v
- lookupMaxWithKey :: Unbox a => RadixTreeMap a v -> Maybe (String a, v)
- member :: Ord a => String a -> RadixTreeMap a v -> Bool
- insert :: Ord a => String a -> v -> RadixTreeMap a v -> RadixTreeMap a v
- insertWith :: Ord a => (v -> v -> v) -> String a -> v -> RadixTreeMap a v -> RadixTreeMap a v
- union :: Ord a => RadixTreeMap a v -> RadixTreeMap a v -> RadixTreeMap a v
- unionWith :: Ord a => (v -> v -> v) -> RadixTreeMap a v -> RadixTreeMap a v -> RadixTreeMap a v
- delete :: Ord a => String a -> RadixTreeMap a v -> RadixTreeMap a v
- updatePath :: Ord a => (String a -> Maybe v -> Maybe v) -> String a -> RadixTreeMap a v -> RadixTreeMap a v
Data Types
data RadixTreeMap a v Source #
A map of String
keys represented by a radix tree.
The tree structure uses the maximal substring possible to label edges so that each internal node
has at least two children. Even though a substrings are used, the trie property is maintained,
that is, a node has at most r
children, where r
is the number of possible values for a
.
Given this property, substring are stored using a sorted Map
which may improve performance.
Constructors
Tree | A subtree or node in the map. Each node may contain a value and is considered a terminating node in that case, or it may be only a radix for its children. Note that all leaves must be terminating, except for empty tree maps. |
Instances
A labelled edge.
A simple pair (label
representing an edge of a :~>
subtree)RadixTreeMap
.
Note that the subtre pointed by an edge must never be empty!
Constructors
!(String a) :~> !(RadixTreeMap a v) |
Instances
Foldable (Edge s) Source # | Implementation based on |
Defined in MCSP.Data.RadixTree.Map Methods fold :: Monoid m => Edge s m -> m # foldMap :: Monoid m => (a -> m) -> Edge s a -> m # foldMap' :: Monoid m => (a -> m) -> Edge s a -> m # foldr :: (a -> b -> b) -> b -> Edge s a -> b # foldr' :: (a -> b -> b) -> b -> Edge s a -> b # foldl :: (b -> a -> b) -> b -> Edge s a -> b # foldl' :: (b -> a -> b) -> b -> Edge s a -> b # foldr1 :: (a -> a -> a) -> Edge s a -> a # foldl1 :: (a -> a -> a) -> Edge s a -> a # elem :: Eq a => a -> Edge s a -> Bool # maximum :: Ord a => Edge s a -> a # minimum :: Ord a => Edge s a -> a # | |
Foldable1 (Edge s) Source # | |
Defined in MCSP.Data.RadixTree.Map Methods fold1 :: Semigroup m => Edge s m -> m # foldMap1 :: Semigroup m => (a -> m) -> Edge s a -> m # foldMap1' :: Semigroup m => (a -> m) -> Edge s a -> m # toNonEmpty :: Edge s a -> NonEmpty a # maximum :: Ord a => Edge s a -> a # minimum :: Ord a => Edge s a -> a # foldrMap1 :: (a -> b) -> (a -> b -> b) -> Edge s a -> b # foldlMap1' :: (a -> b) -> (b -> a -> b) -> Edge s a -> b # foldlMap1 :: (a -> b) -> (b -> a -> b) -> Edge s a -> b # foldrMap1' :: (a -> b) -> (a -> b -> b) -> Edge s a -> b # | |
Traversable (Edge s) Source # | |
Functor (Edge s) Source # | |
(ShowString a, Show v) => Show (Edge a v) Source # | |
(Eq a, Eq v) => Eq (Edge a v) Source # | |
(Ord a, Ord v) => Ord (Edge a v) Source # | |
Defined in MCSP.Data.RadixTree.Map |
type EdgeSet a v = Map a (Edge a v) Source #
A collection of uniquely labelled edges.
This could be
, using the entire label as key, but the
trie property ensures that the first character of the
label must also be unique. This enables us to use a faster implementation and also helps to
ensure the trie property.Map
(String
a) (Edge
a v)
debug :: (ShowString a, Show v) => RadixTreeMap a v -> String Source #
Show RadixTreeMap
in a readable, idented format.
Construction
empty :: RadixTreeMap a v Source #
O(1) The empty map.
>>>
import Prelude (Char, Int)
>>>
empty @Char @Int
Tree []
construct :: Ord a => [(String a, v)] -> RadixTreeMap a v Source #
O(?) Build a map from a list of key/value pairs.
>>>
import Prelude (Char, Int)
>>>
construct [("abc", 1), ("def", 3), ("abb", 5)]
Tree [ab :~> Tree [b :~> Tree (5) [],c :~> Tree (1) []],def :~> Tree (3) []]
Query
lookup :: Ord a => String a -> RadixTreeMap a v -> Maybe v Source #
O(n log r) Lookup the value at a key in the map.
>>>
import Prelude (Int)
>>>
lookup "abc" (construct [("abc", 1), ("def", 3), ("abb", 5)])
Just 1
>>>
lookup "xyz" (construct [("abc", 1), ("def", 3), ("abb", 5)])
Nothing
>>>
lookupMax (construct [("abc", 1), ("def", 3), ("abb", 5)])
Just 3
lookupMin :: RadixTreeMap a v -> Maybe v Source #
O(n log r) Extract the value associated with the minimal key in the map.
>>>
lookupMin @Char @Int (construct [("abc", 1), ("def", 3), ("abb", 5)])
Just 5
>>>
lookupMin @Char @Int empty
Nothing
lookupMinWithKey :: Unbox a => RadixTreeMap a v -> Maybe (String a, v) Source #
O(n log r) Extract the minimal key in the map.
>>>
lookupMinWithKey (construct [("abc", 1), ("def", 3), ("abb", 5)])
Just (abb,5)
>>>
lookupMinWithKey @Char @Int empty
Nothing
lookupMax :: RadixTreeMap a v -> Maybe v Source #
O(n log r) Extract the value associated with the maximal key in the map.
>>>
lookupMax @Char @Int (construct [("abc", 1), ("def", 3), ("abb", 5)])
Just 3
>>>
lookupMax @Char @Int empty
Nothing
lookupMaxWithKey :: Unbox a => RadixTreeMap a v -> Maybe (String a, v) Source #
O(n log r) Extract the maximal key in the map.
>>>
lookupMaxWithKey (construct [("abc", 1), ("def", 3), ("abb", 5)])
Just (def,3)
>>>
lookupMaxWithKey @Char @Int empty
Nothing
member :: Ord a => String a -> RadixTreeMap a v -> Bool Source #
O(n log r) Check if there is an associated value for the key.
>>>
member "abc" (construct [("abc", 1), ("def", 3), ("abb", 5)])
True
>>>
member "xyz" (construct [("abc", 1), ("def", 3), ("abb", 5)])
False
Modification
insert :: Ord a => String a -> v -> RadixTreeMap a v -> RadixTreeMap a v Source #
O(?) Insert a new key and value in the map.
If the key is already present in the map, the associated value is replaced with the supplied value.
insertWith :: Ord a => (v -> v -> v) -> String a -> v -> RadixTreeMap a v -> RadixTreeMap a v Source #
Insert with a function, combining new value and old value.
will insert the pair insertWith
f key value tree(key, value)
into tree
if key does
not exist in the map. If the key does exist, the function will insert the pair
(key, f new_value old_value)
.
union :: Ord a => RadixTreeMap a v -> RadixTreeMap a v -> RadixTreeMap a v Source #
O(?) The expression union t1 t2
takes the left-biased union of t1
and t2
.
It prefers t1
when duplicate keys are encountered.
unionWith :: Ord a => (v -> v -> v) -> RadixTreeMap a v -> RadixTreeMap a v -> RadixTreeMap a v Source #
O(?) Union with a combining function.
delete :: Ord a => String a -> RadixTreeMap a v -> RadixTreeMap a v Source #
O(?) Delete a key and its value from the map.
When the key is not a member of the map, the original map is returned.
updatePath :: Ord a => (String a -> Maybe v -> Maybe v) -> String a -> RadixTreeMap a v -> RadixTreeMap a v Source #
O(n log r) Update values for all prefixes of key in the map, present or not.