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 :: k -> Map k a -> Map k a
below k
k Map k a
m =
  let
    (Map k a
x, Map k a
_) = k -> Map k a -> (Map k a, 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 :: k -> Map k a -> Map k a
above k
k Map k a
m =
  let
    (Map k a
_, Map k a
x) = k -> Map k a -> (Map k a, 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

{- |

@'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 :: 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
_) = k -> Map k a -> (Map k a, Maybe a, 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 -> k -> a -> Map k a -> Map k a
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 :: 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) = k -> Map k a -> (Map k a, Maybe a, 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 -> k -> a -> Map k a -> Map k a
forall k a. Ord k => k -> a -> Map k a -> Map k a
insert k
k a
v Map k a
x