module Data.Interval.Map.Strict where import qualified Data.Foldable as Foldable import Data.Function (($), id) import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Maybe (Maybe(..), maybe) import Data.Ord (Ord(..)) import Data.Tuple (fst) import Data.Interval (Interval) import qualified Data.Interval as Interval import qualified Data.Interval.Sieve as Interval.Sieve -- | Return an 'Interval' spanning over all the keys of the given 'Map'. interval :: Ord k => Map k x -> Maybe (Interval k) interval m | Map.null m = Nothing interval m = (Interval.<=..<=) (fst $ Map.findMin m) (fst $ Map.findMax m) -- | Return non-'Map.null' sub-'Map's of the given 'Map' -- sliced according to the given 'Interval.Sieve.Sieve'. slice :: Ord k => Interval.Sieve.Sieve k -> Map k x -> [Map k x] slice (Interval.Sieve.Sieve is) m = Foldable.foldr (\i -> let l = Interval.low i in let h = Interval.high i in let (_lt_l, eq_l, gt_l) = Map.splitLookup (Interval.limit l) m in let (lt_h, eq_h, _gt_h) = Map.splitLookup (Interval.limit h) gt_l in case (case Interval.adherence l of Interval.In -> maybe id (Map.insert (Interval.limit l)) eq_l Interval.Out -> id) $ (case Interval.adherence h of Interval.In -> maybe id (Map.insert (Interval.limit h)) eq_h Interval.Out -> id) lt_h of s | Map.null s -> id s -> (:) s ) [] is