mcsp-algorithms-0.1.0: Algorithms for Minimum Common String Partition (MCSP) in Haskell.
Safe HaskellSafe-Inferred
LanguageGHC2021

MCSP.Data.RadixTree.Map

Description

A map of String keys represented by a radix tree.

Synopsis

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.

Fields

  • value :: !(Maybe v)

    The value for a terminating node.

  • edges :: !(EdgeSet a v)

    The set of labelled edges to its children.

Instances

Instances details
Foldable (RadixTreeMap s) Source #

Delegate Foldabe to its Edges.

Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

fold :: Monoid m => RadixTreeMap s m -> m #

foldMap :: Monoid m => (a -> m) -> RadixTreeMap s a -> m #

foldMap' :: Monoid m => (a -> m) -> RadixTreeMap s a -> m #

foldr :: (a -> b -> b) -> b -> RadixTreeMap s a -> b #

foldr' :: (a -> b -> b) -> b -> RadixTreeMap s a -> b #

foldl :: (b -> a -> b) -> b -> RadixTreeMap s a -> b #

foldl' :: (b -> a -> b) -> b -> RadixTreeMap s a -> b #

foldr1 :: (a -> a -> a) -> RadixTreeMap s a -> a #

foldl1 :: (a -> a -> a) -> RadixTreeMap s a -> a #

toList :: RadixTreeMap s a -> [a] #

null :: RadixTreeMap s a -> Bool #

length :: RadixTreeMap s a -> Int #

elem :: Eq a => a -> RadixTreeMap s a -> Bool #

maximum :: Ord a => RadixTreeMap s a -> a #

minimum :: Ord a => RadixTreeMap s a -> a #

sum :: Num a => RadixTreeMap s a -> a #

product :: Num a => RadixTreeMap s a -> a #

Traversable (RadixTreeMap s) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

traverse :: Applicative f => (a -> f b) -> RadixTreeMap s a -> f (RadixTreeMap s b) #

sequenceA :: Applicative f => RadixTreeMap s (f a) -> f (RadixTreeMap s a) #

mapM :: Monad m => (a -> m b) -> RadixTreeMap s a -> m (RadixTreeMap s b) #

sequence :: Monad m => RadixTreeMap s (m a) -> m (RadixTreeMap s a) #

Functor (RadixTreeMap s) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

fmap :: (a -> b) -> RadixTreeMap s a -> RadixTreeMap s b #

(<$) :: a -> RadixTreeMap s b -> RadixTreeMap s a #

(ShowString a, Show v) => Show (RadixTreeMap a v) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

(Eq v, Eq a) => Eq (RadixTreeMap a v) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

(==) :: RadixTreeMap a v -> RadixTreeMap a v -> Bool #

(/=) :: RadixTreeMap a v -> RadixTreeMap a v -> Bool #

(Ord v, Ord a) => Ord (RadixTreeMap a v) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

data Edge a v Source #

A labelled edge.

A simple pair (label :~> subtree) representing an edge of a RadixTreeMap.

Note that the subtre pointed by an edge must never be empty!

Constructors

!(String a) :~> !(RadixTreeMap a v) 

Instances

Instances details
Foldable (Edge s) Source #

Implementation based on Foldable1 (Edge s)

Instance details

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 #

toList :: Edge s a -> [a] #

null :: Edge s a -> Bool #

length :: Edge s a -> Int #

elem :: Eq a => a -> Edge s a -> Bool #

maximum :: Ord a => Edge s a -> a #

minimum :: Ord a => Edge s a -> a #

sum :: Num a => Edge s a -> a #

product :: Num a => Edge s a -> a #

Foldable1 (Edge s) Source # 
Instance details

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 #

head :: Edge s a -> a #

last :: 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 # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

traverse :: Applicative f => (a -> f b) -> Edge s a -> f (Edge s b) #

sequenceA :: Applicative f => Edge s (f a) -> f (Edge s a) #

mapM :: Monad m => (a -> m b) -> Edge s a -> m (Edge s b) #

sequence :: Monad m => Edge s (m a) -> m (Edge s a) #

Functor (Edge s) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

fmap :: (a -> b) -> Edge s a -> Edge s b #

(<$) :: a -> Edge s b -> Edge s a #

(ShowString a, Show v) => Show (Edge a v) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

showsPrec :: Int -> Edge a v -> ShowS #

show :: Edge a v -> String #

showList :: [Edge a v] -> ShowS #

(Eq a, Eq v) => Eq (Edge a v) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

(==) :: Edge a v -> Edge a v -> Bool #

(/=) :: Edge a v -> Edge a v -> Bool #

(Ord a, Ord v) => Ord (Edge a v) Source # 
Instance details

Defined in MCSP.Data.RadixTree.Map

Methods

compare :: Edge a v -> Edge a v -> Ordering #

(<) :: Edge a v -> Edge a v -> Bool #

(<=) :: Edge a v -> Edge a v -> Bool #

(>) :: Edge a v -> Edge a v -> Bool #

(>=) :: Edge a v -> Edge a v -> Bool #

max :: Edge a v -> Edge a v -> Edge a v #

min :: Edge a v -> Edge a v -> Edge a v #

type EdgeSet a v = Map a (Edge a v) Source #

A collection of uniquely labelled edges.

This could be Map (String a) (Edge a v), 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.

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.

insertWith f key value tree will insert the pair (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.