1 module Hcompta.Lib.Map.Strict where
3 import qualified Data.Foldable as Foldable
4 import Data.Map.Strict (Map)
5 import qualified Data.Map.Strict as Data.Map
7 import Hcompta.Lib.Interval (Interval)
8 import qualified Hcompta.Lib.Interval as Interval
9 import qualified Hcompta.Lib.Interval.Sieve as Interval.Sieve
13 -- | Return an 'Interval' spanning over all the keys of the given 'Map'.
14 interval :: Ord k => Map k x -> Maybe (Interval k)
15 interval m | Data.Map.null m = Nothing
18 (fst $ Data.Map.findMin m)
19 (fst $ Data.Map.findMax m)
21 -- | Return non-'Data.Map.null' sub-'Map's of the given 'Map'
22 -- sliced according to the given 'Interval.Sieve.Sieve'.
25 => Interval.Sieve.Sieve k
26 -> Map k x -> [Map k x]
27 slice (Interval.Sieve.Sieve is) m =
30 let l = Interval.low i in
31 let h = Interval.high i in
32 let (_lt_l, eq_l, gt_l) = Data.Map.splitLookup (Interval.limit l) m in
33 let (lt_h, eq_h, _gt_h) = Data.Map.splitLookup (Interval.limit h) gt_l in
35 (case Interval.adherence l of
36 Interval.In -> maybe id (Data.Map.insert (Interval.limit l)) eq_l
38 (case Interval.adherence h of
39 Interval.In -> maybe id (Data.Map.insert (Interval.limit h)) eq_h
42 s | Data.Map.null s -> id