module Language.Parser.Ptera.Data.IntMap.GreaterRestriction (
    restrictGreater,
) where

import           Language.Parser.Ptera.Prelude

import           Data.IntMap.Internal


restrictGreater :: Key -> IntMap a -> IntMap a
restrictGreater :: forall a. Key -> IntMap a -> IntMap a
restrictGreater Key
k IntMap a
t = case IntMap a
t of
    Bin Key
_ Key
m IntMap a
l IntMap a
r | Key
m forall a. Ord a => a -> a -> Bool
< Key
0 ->
        if Key
k forall a. Ord a => a -> a -> Bool
>= Key
0 -- handle negative numbers.
            then forall a. Key -> IntMap a -> IntMap a
go Key
k IntMap a
l
            else IntMap a
l forall a. IntMap a -> IntMap a -> IntMap a
`union` forall a. Key -> IntMap a -> IntMap a
go Key
k IntMap a
r
    IntMap a
_ ->
        forall a. Key -> IntMap a -> IntMap a
go Key
k IntMap a
t
    where
        go :: Key -> IntMap a -> IntMap a
go Key
k' IntMap a
t' = case IntMap a
t' of
            Bin Key
p Key
m IntMap a
l IntMap a
r
                | Key -> Key -> Key -> Bool
nomatch Key
k' Key
p Key
m ->
                    if Key
k' forall a. Ord a => a -> a -> Bool
> Key
p
                        then forall a. IntMap a
Nil
                        else IntMap a
t'
                | Key -> Key -> Bool
zero Key
k' Key
m ->
                    Key -> IntMap a -> IntMap a
go Key
k' IntMap a
l forall a. IntMap a -> IntMap a -> IntMap a
`union` IntMap a
r
                | Bool
otherwise ->
                    Key -> IntMap a -> IntMap a
go Key
k' IntMap a
r
            Tip Key
ky a
_
                | Key
k' forall a. Ord a => a -> a -> Bool
> Key
ky ->
                    forall a. IntMap a
Nil
                | Key
k' forall a. Ord a => a -> a -> Bool
< Key
ky ->
                    IntMap a
t'
                | Bool
otherwise ->
                    forall a. IntMap a
Nil
            IntMap a
Nil ->
                forall a. IntMap a
Nil