module Numeric.Search.Range (searchFromTo) where
searchFromTo :: Integral a => (a -> Bool) -> a -> a -> Maybe a
searchFromTo :: (a -> Bool) -> a -> a -> Maybe a
searchFromTo a -> Bool
p a
l a
h
| a
l a -> a -> Bool
forall a. Ord a => a -> a -> Bool
> a
h = Maybe a
forall a. Maybe a
Nothing
| a -> Bool
p a
h = a -> Maybe a
forall a. a -> Maybe a
Just ((a -> Bool) -> a -> a -> a
forall a. Integral a => (a -> Bool) -> a -> a -> a
searchSafeRange a -> Bool
p a
l a
h)
| Bool
otherwise = Maybe a
forall a. Maybe a
Nothing
searchSafeRange :: Integral a => (a -> Bool) -> a -> a -> a
searchSafeRange :: (a -> Bool) -> a -> a -> a
searchSafeRange a -> Bool
p a
l a
h
| a
l a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
h = a
l
| a -> Bool
p a
m = (a -> Bool) -> a -> a -> a
forall a. Integral a => (a -> Bool) -> a -> a -> a
searchSafeRange a -> Bool
p a
l a
m
| Bool
otherwise = (a -> Bool) -> a -> a -> a
forall a. Integral a => (a -> Bool) -> a -> a -> a
searchSafeRange a -> Bool
p (a
ma -> a -> a
forall a. Num a => a -> a -> a
+a
1) a
h
where m :: a
m = a
l a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
2 a -> a -> a
forall a. Num a => a -> a -> a
+ a
h a -> a -> a
forall a. Integral a => a -> a -> a
`div` a
2