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 :: 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 Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< Key
0 ->
        if Key
k Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
>= Key
0 -- handle negative numbers.
            then Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> IntMap a
go Key
k IntMap a
l
            else IntMap a
l IntMap a -> IntMap a -> IntMap a
forall a. IntMap a -> IntMap a -> IntMap a
`union` Key -> IntMap a -> IntMap a
forall a. Key -> IntMap a -> IntMap a
go Key
k IntMap a
r
    IntMap a
_ ->
        Key -> IntMap a -> 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' Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
> Key
p
                        then IntMap a
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 IntMap a -> IntMap a -> IntMap a
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' Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
> Key
ky ->
                    IntMap a
forall a. IntMap a
Nil
                | Key
k' Key -> Key -> Bool
forall a. Ord a => a -> a -> Bool
< Key
ky ->
                    IntMap a
t'
                | Bool
otherwise ->
                    IntMap a
forall a. IntMap a
Nil
            IntMap a
Nil ->
                IntMap a
forall a. IntMap a
Nil