module Data.Loc.Internal.Map
  ( module Data.Map,
    below,
    above,
    belowInclusive,
    aboveInclusive,
  )
where

import Data.Loc.Internal.Prelude
import Data.Map

-- | @'below' k m@ is the subset of 'Map' @m@ whose keys are less than @k@
below :: Ord k => k -> Map k a -> Map k a
below :: forall k a. Ord k => k -> Map k a -> Map k a
below k
k Map k a
m =
  let (Map k a
x, Map k a
_) = forall k a. Ord k => k -> Map k a -> (Map k a, Map k a)
split k
k Map k a
m
   in Map k a
x

-- | @'below' k m@ is the subset of 'Map' @m@ whose keys are greater than @k@
above :: Ord k => k -> Map k a -> Map k a
above :: forall k a. Ord k => k -> Map k a -> Map k a
above k
k Map k a
m =
  let (Map k a
_, Map k a
x) = forall k a. Ord k => k -> Map k a -> (Map k a, Map k a)
split k
k Map k a
m
   in Map k a
x

-- | @'belowInclusive' k m@ is the subset of 'Map' @m@ whose keys are less
-- than or equal to @k@
belowInclusive :: Ord k => k -> Map k a -> Map k a
belowInclusive :: forall k a. Ord k => k -> Map k a -> Map k a
belowInclusive k
k Map k a
m =
  let (Map k a
x, Maybe a
at, Map k a
_) = forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
k Map k a
m
   in case Maybe a
at of
        Maybe a
Nothing -> Map k a
x
        Just a
v -> forall k a. Ord k => k -> a -> Map k a -> Map k a
insert k
k a
v Map k a
x

-- | @'aboveInclusive' k m@ is the subset of 'Map' @m@ whose keys are
-- greater than or equal to @k@
aboveInclusive :: Ord k => k -> Map k a -> Map k a
aboveInclusive :: forall k a. Ord k => k -> Map k a -> Map k a
aboveInclusive k
k Map k a
m =
  let (Map k a
_, Maybe a
at, Map k a
x) = forall k a. Ord k => k -> Map k a -> (Map k a, Maybe a, Map k a)
splitLookup k
k Map k a
m
   in case Maybe a
at of
        Maybe a
Nothing -> Map k a
x
        Just a
v -> forall k a. Ord k => k -> a -> Map k a -> Map k a
insert k
k a
v Map k a
x