2020-05-18 15:59:07 +03:00
|
|
|
module Data.SortedMap
|
|
|
|
|
2021-08-26 17:00:45 +03:00
|
|
|
import Data.SortedMap.Dependent
|
2021-05-11 10:26:00 +03:00
|
|
|
|
2021-08-26 17:00:45 +03:00
|
|
|
%hide Prelude.toList
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
2021-08-26 17:00:45 +03:00
|
|
|
record SortedMap k v where
|
|
|
|
constructor M
|
|
|
|
unM : SortedDMap k $ const v
|
|
|
|
|
|
|
|
-- Helper function
|
|
|
|
unDPair : (x : a ** const b x) -> (a, b)
|
|
|
|
unDPair (k ** v) = (k, v)
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
empty : Ord k => SortedMap k v
|
2021-08-26 17:00:45 +03:00
|
|
|
empty = M empty
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
lookup : k -> SortedMap k v -> Maybe v
|
2021-08-26 17:00:45 +03:00
|
|
|
lookup k = map snd . lookup k . unM
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
insert : k -> v -> SortedMap k v -> SortedMap k v
|
2021-08-26 17:00:45 +03:00
|
|
|
insert k v = M . insert k v . unM
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2020-06-12 00:14:11 +03:00
|
|
|
export
|
|
|
|
singleton : Ord k => k -> v -> SortedMap k v
|
2021-08-26 17:00:45 +03:00
|
|
|
singleton = M .: singleton
|
2020-06-12 00:14:11 +03:00
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
insertFrom : Foldable f => f (k, v) -> SortedMap k v -> SortedMap k v
|
|
|
|
insertFrom = flip $ foldl $ flip $ uncurry insert
|
|
|
|
|
|
|
|
export
|
|
|
|
delete : k -> SortedMap k v -> SortedMap k v
|
2021-08-26 17:00:45 +03:00
|
|
|
delete k = M . delete k . unM
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
fromList : Ord k => List (k, v) -> SortedMap k v
|
2021-08-26 17:00:45 +03:00
|
|
|
fromList = flip insertFrom empty
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
toList : SortedMap k v -> List (k, v)
|
2021-08-26 17:00:45 +03:00
|
|
|
toList = map unDPair . toList . unM
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
||| Gets the keys of the map.
|
|
|
|
export
|
|
|
|
keys : SortedMap k v -> List k
|
|
|
|
keys = map fst . toList
|
|
|
|
|
|
|
|
||| Gets the values of the map. Could contain duplicates.
|
|
|
|
export
|
|
|
|
values : SortedMap k v -> List v
|
|
|
|
values = map snd . toList
|
|
|
|
|
|
|
|
export
|
|
|
|
implementation Functor (SortedMap k) where
|
2021-08-26 17:00:45 +03:00
|
|
|
map f = M . map f . unM
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
export
|
|
|
|
implementation Foldable (SortedMap k) where
|
|
|
|
foldr f z = foldr f z . values
|
2021-06-01 17:05:04 +03:00
|
|
|
foldl f z = foldl f z . values
|
2020-05-18 15:59:07 +03:00
|
|
|
|
2021-11-02 18:34:52 +03:00
|
|
|
null = null . unM
|
2020-12-10 21:04:23 +03:00
|
|
|
|
2021-06-01 17:05:04 +03:00
|
|
|
foldMap f = foldMap f . values
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
implementation Traversable (SortedMap k) where
|
2021-08-26 17:00:45 +03:00
|
|
|
traverse f = map M . traverse f . unM
|
2020-05-18 15:59:07 +03:00
|
|
|
|
|
|
|
||| Merge two maps. When encountering duplicate keys, using a function to combine the values.
|
|
|
|
||| Uses the ordering of the first map given.
|
|
|
|
export
|
|
|
|
mergeWith : (v -> v -> v) -> SortedMap k v -> SortedMap k v -> SortedMap k v
|
|
|
|
mergeWith f x y = insertFrom inserted x where
|
|
|
|
inserted : List (k, v)
|
|
|
|
inserted = do
|
|
|
|
(k, v) <- toList y
|
|
|
|
let v' = (maybe id f $ lookup k x) v
|
|
|
|
pure (k, v')
|
|
|
|
|
|
|
|
||| Merge two maps using the Semigroup (and by extension, Monoid) operation.
|
|
|
|
||| Uses mergeWith internally, so the ordering of the left map is kept.
|
|
|
|
export
|
|
|
|
merge : Semigroup v => SortedMap k v -> SortedMap k v -> SortedMap k v
|
|
|
|
merge = mergeWith (<+>)
|
|
|
|
|
|
|
|
||| Left-biased merge, also keeps the ordering specified by the left map.
|
|
|
|
export
|
|
|
|
mergeLeft : SortedMap k v -> SortedMap k v -> SortedMap k v
|
|
|
|
mergeLeft = mergeWith const
|
|
|
|
|
2021-08-10 10:13:36 +03:00
|
|
|
||| looks up a key in map, returning the left and right closest values, so that
|
|
|
|
||| k1 <= k < k2. If at the end of the beginning and/or end of the sorted map, returns
|
|
|
|
||| nothing appropriately
|
|
|
|
export
|
|
|
|
lookupBetween : key -> SortedMap key val -> (Maybe (key,val), Maybe (key,val))
|
2021-08-26 17:00:45 +03:00
|
|
|
lookupBetween k = bimap (map unDPair) (map unDPair) . lookupBetween k . unM
|
2021-08-10 10:13:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
||| Returns the leftmost (least) key and value
|
|
|
|
export
|
|
|
|
leftMost : SortedMap key val -> Maybe (key,val)
|
2021-08-26 17:00:45 +03:00
|
|
|
leftMost = map unDPair . leftMost . unM
|
2021-08-10 10:13:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
||| Returns the rightmost (greatest) key and value
|
|
|
|
export
|
|
|
|
rightMost : SortedMap key val -> Maybe (key,val)
|
2021-08-26 17:00:45 +03:00
|
|
|
rightMost = map unDPair . rightMost . unM
|
2021-08-10 10:13:36 +03:00
|
|
|
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
|
|
|
(Show k, Show v) => Show (SortedMap k v) where
|
|
|
|
show m = "fromList " ++ (show $ toList m)
|
|
|
|
|
2021-03-01 16:22:46 +03:00
|
|
|
export
|
|
|
|
(Eq k, Eq v) => Eq (SortedMap k v) where
|
|
|
|
(==) = (==) `on` toList
|
|
|
|
|
2020-05-18 15:59:07 +03:00
|
|
|
-- TODO: is this the right variant of merge to use for this? I think it is, but
|
|
|
|
-- I could also see the advantages of using `mergeLeft`. The current approach is
|
|
|
|
-- strictly more powerful I believe, because `mergeLeft` can be emulated with
|
|
|
|
-- the `First` monoid. However, this does require more code to do the same
|
|
|
|
-- thing.
|
|
|
|
export
|
|
|
|
Semigroup v => Semigroup (SortedMap k v) where
|
|
|
|
(<+>) = merge
|
|
|
|
|
|
|
|
||| For `neutral <+> y`, y is rebuilt in `Ord k`, so this is not a "strict" Monoid.
|
|
|
|
||| However, semantically, it should be equal.
|
|
|
|
export
|
|
|
|
(Ord k, Semigroup v) => Monoid (SortedMap k v) where
|
|
|
|
neutral = empty
|
2022-03-22 11:19:45 +03:00
|
|
|
|
|
|
|
export %inline
|
|
|
|
Cast (SortedDMap k (const v)) (SortedMap k v) where
|
|
|
|
cast = M
|
|
|
|
|
|
|
|
export %inline
|
|
|
|
Cast (SortedMap k v) (SortedDMap k (const v)) where
|
|
|
|
cast = unM
|