import qualified Data.List.NonEmpty
import Data.List.NonEmpty (NonEmpty(..))
import Data.Map.Strict (Map)
-import qualified Data.Map.Strict as Data.Map
+import qualified Data.Map.Strict as Map
import Data.Maybe (Maybe(..), maybe)
import Data.Monoid (Monoid(..))
import Data.Ord (Ord(..))
node_find [] n = Strict.Just n
node_find (k:ks) Node{node_descendants=TreeMap m} =
maybe Strict.Nothing (node_find ks) $
- Data.Map.lookup k m
+ Map.lookup k m
-- * Construct
-- | Return the empty 'TreeMap'.
-empty :: Ord k => TreeMap k x
-empty = TreeMap Data.Map.empty
+empty :: TreeMap k x
+empty = TreeMap Map.empty
-- | Return a 'TreeMap' only mapping the given 'Path' to the given value.
singleton :: Ord k => Path k -> x -> TreeMap k x
singleton ks x = insert const ks x empty
-- | Return a 'Node' only containing the given value.
-leaf :: Ord k => x -> Node k x
+leaf :: x -> Node k x
leaf x =
Node
{ node_value = Strict.Just x
insert :: Ord k => (x -> x -> x) -> Path k -> x -> TreeMap k x -> TreeMap k x
insert merge (k:|[]) x (TreeMap m) =
TreeMap $
- Data.Map.insertWith
+ Map.insertWith
(\_ Node{node_value = x1, node_descendants = m1, node_size = s1} ->
Node
{ node_value = Strict.maybe (Strict.Just x) (Strict.Just . merge x) x1
k (leaf x) m
insert merge (k:|k':ks) x (TreeMap m) =
TreeMap $
- Data.Map.insertWith
+ Map.insertWith
(\_ Node{node_value = x1, node_descendants = m1} ->
let m' = insert merge (path k' ks) x m1 in
let s' = size m' + Strict.maybe 0 (const 1) x1 in
-- the 'Path' to the value,
-- merging values of identical 'Path's (in respective order).
from_Map :: Ord k => (x -> x -> x) -> Map (Path k) x -> TreeMap k x
-from_Map merge = Data.Map.foldlWithKey (\acc p x -> insert merge p x acc) empty
+from_Map merge = Map.foldlWithKey (\acc p x -> insert merge p x acc) empty
-- * Size
-- | Return the 'Map' in the given 'TreeMap'.
-nodes :: Ord k => TreeMap k x -> Map k (Node k x)
+nodes :: TreeMap k x -> Map k (Node k x)
nodes (TreeMap m) = m
-- | Return 'True' iif. the given 'TreeMap' is 'empty'.
-null :: Ord k => TreeMap k x -> Bool
-null (TreeMap m) = Data.Map.null m
+null :: TreeMap k x -> Bool
+null (TreeMap m) = Map.null m
-- | Return the number of non-'Strict.Nothing' 'node_value's in the given 'TreeMap'.
--
-- * Complexity: O(r) where r is the size of the root 'Map'.
-size :: Ord k => TreeMap k x -> Int
-size = Data.Map.foldr ((+) . node_size) 0 . nodes
+size :: TreeMap k x -> Int
+size = Map.foldr ((+) . node_size) 0 . nodes
-- * Find
-- | Return the value (if any) associated with the given 'Path'.
find :: Ord k => Path k -> TreeMap k x -> Strict.Maybe x
-find (k:|[]) (TreeMap m) = maybe Strict.Nothing node_value $ Data.Map.lookup k m
+find (k:|[]) (TreeMap m) = maybe Strict.Nothing node_value $ Map.lookup k m
find (k:|k':ks) (TreeMap m) =
maybe Strict.Nothing (find (path k' ks) . node_descendants) $
- Data.Map.lookup k m
+ Map.lookup k m
-- | Return the values (if any) associated with the prefixes of the given 'Path' (included).
find_along :: Ord k => Path k -> TreeMap k x -> [x]
go :: Ord k => [k] -> Map k (Node k x) -> [x]
go [] _m = []
go (k:ks) m =
- case Data.Map.lookup k m of
+ case Map.lookup k m of
Nothing -> []
Just node ->
Strict.maybe id (:) (node_value node) $
go ks $ nodes (node_descendants node)
find_node :: Ord k => Path k -> TreeMap k x -> Strict.Maybe (Node k x)
-find_node (k:|[]) (TreeMap m) = maybe Strict.Nothing Strict.Just $ Data.Map.lookup k m
+find_node (k:|[]) (TreeMap m) = maybe Strict.Nothing Strict.Just $ Map.lookup k m
find_node (k:|k':ks) (TreeMap m) =
maybe Strict.Nothing (find_node (path k' ks) . node_descendants) $
- Data.Map.lookup k m
+ Map.lookup k m
-- * Union
union :: Ord k => (x -> x -> x) -> TreeMap k x -> TreeMap k x -> TreeMap k x
union merge (TreeMap tm0) (TreeMap tm1) =
TreeMap $
- Data.Map.unionWith
+ Map.unionWith
(\Node{node_value=x0, node_descendants=m0}
Node{node_value=x1, node_descendants=m1} ->
let node_descendants = union merge m0 m1 in
map :: Ord k => (x -> y) -> TreeMap k x -> TreeMap k y
map f =
TreeMap .
- Data.Map.map
+ Map.map
(\n@Node{node_value=x, node_descendants=m} ->
n{ node_value = fmap f x
, node_descendants = map f m
-- mapped by the given functions.
--
-- WARNING: the function mapping 'Path' sections must be monotonic,
--- like in 'Data.Map.mapKeysMonotonic'.
+-- like in 'Map.mapKeysMonotonic'.
map_monotonic :: (Ord k, Ord l) => (k -> l) -> (x -> y) -> TreeMap k x -> TreeMap l y
map_monotonic fk fx =
TreeMap .
- Data.Map.mapKeysMonotonic fk .
- Data.Map.map
+ Map.mapKeysMonotonic fk .
+ Map.map
(\n@Node{node_value=x, node_descendants=m} ->
n{ node_value = fmap fx x
, node_descendants = map_monotonic fk fx m
map_by_depth_first :: Ord k => (TreeMap k y -> Strict.Maybe x -> y) -> TreeMap k x -> TreeMap k y
map_by_depth_first f =
TreeMap .
- Data.Map.map
+ Map.map
(\Node{node_value, node_descendants} ->
let m = map_by_depth_first f node_descendants in
Node
go _f [] m = m
go f (k:p) (TreeMap m) =
TreeMap $
- Data.Map.alter
+ Map.alter
(\c ->
let (cv, cm) =
case c of
=> [k] -> (a -> Path k -> x -> a)
-> a -> TreeMap k x -> a
foldp p fct a (TreeMap m) =
- Data.Map.foldlWithKey
+ Map.foldlWithKey
(\acc k Node{node_value, node_descendants} ->
let acc' = Strict.maybe acc (fct acc (reverse $ path k p)) node_value in
foldp (k:p) fct acc' node_descendants) a m
=> [k] -> (a -> Node k x -> Path k -> x -> a)
-> a -> TreeMap k x -> a
foldp p fct a (TreeMap m) =
- Data.Map.foldlWithKey
+ Map.foldlWithKey
(\acc k n@Node{node_value, node_descendants} ->
let acc' = Strict.maybe acc (fct acc n (reverse $ path k p)) node_value in
foldp (k:p) fct acc' node_descendants) a m
=> [k] -> (Path k -> x -> a -> a)
-> a -> TreeMap k x -> a
foldp p fct a (TreeMap m) =
- Data.Map.foldrWithKey
+ Map.foldrWithKey
(\k Node{node_value, node_descendants} acc ->
let acc' = foldp (k:p) fct acc node_descendants in
Strict.maybe acc' (\x -> fct (reverse $ path k p) x acc') node_value) a m
=> [k] -> (Node k x -> Path k -> x -> a -> a)
-> a -> TreeMap k x -> a
foldp p fct a (TreeMap m) =
- Data.Map.foldrWithKey
+ Map.foldrWithKey
(\k n@Node{node_value, node_descendants} acc ->
let acc' = foldp (k:p) fct acc node_descendants in
Strict.maybe acc' (\x -> fct n (reverse $ path k p) x acc') node_value) a m
-> TreeMap k x -> a -> a
go _f _ [] _t a = a
go f p (k:n) (TreeMap t) a =
- case Data.Map.lookup k t of
+ case Map.lookup k t of
Nothing -> a
Just Node{node_value=v, node_descendants=d} ->
case v of
-> TreeMap k x -> a -> a
go _f _ [] _t a = a
go f p (k:n) (TreeMap t) a =
- case Data.Map.lookup k t of
+ case Map.lookup k t of
Nothing -> a
Just Node{node_value=v, node_descendants=d} ->
case v of
-> TreeMap k x
-> Map (Path k) y
flat_map p f (TreeMap m) =
- Data.Map.unions $
- Data.Map.mapKeysMonotonic (reverse . flip path p) (
- Data.Map.mapMaybeWithKey (\k Node{node_value} ->
+ Map.unions $
+ Map.mapKeysMonotonic (reverse . flip path p) (
+ Map.mapMaybeWithKey (\k Node{node_value} ->
case node_value of
Strict.Nothing -> Nothing
Strict.Just x -> Just $ f (reverse $ path k p) x) m
) :
- Data.Map.foldrWithKey
+ Map.foldrWithKey
(\k -> (:) . flat_map (k:p) f . node_descendants)
[] m
-> TreeMap k y
go p test (TreeMap m) =
TreeMap $
- Data.Map.mapMaybeWithKey
+ Map.mapMaybeWithKey
(\k node@Node{node_value=v, node_descendants=ns} ->
let node_descendants = go (k:p) test ns in
let node_size = size node_descendants in