module Data.Array.Comfort.Shape.Set where

import qualified Data.Set as Set
import Data.Set (Set)
import Data.Maybe.HT (toMaybe)


offset :: Ord a => Set a -> a -> Maybe Int
offset :: forall a. Ord a => Set a -> a -> Maybe Int
offset = (a -> Set a -> Maybe Int) -> Set a -> a -> Maybe Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Set a -> Maybe Int
forall a. Ord a => a -> Set a -> Maybe Int
Set.lookupIndex

uncheckedOffset :: Ord a => Set a -> a -> Int
uncheckedOffset :: forall a. Ord a => Set a -> a -> Int
uncheckedOffset = (a -> Set a -> Int) -> Set a -> a -> Int
forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> Set a -> Int
forall a. Ord a => a -> Set a -> Int
Set.findIndex


indexFromOffset :: Set a -> Int -> Maybe a
indexFromOffset :: forall a. Set a -> Int -> Maybe a
indexFromOffset Set a
set Int
k =
   Bool -> a -> Maybe a
forall a. Bool -> a -> Maybe a
toMaybe (Int
0Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<=Int
k Bool -> Bool -> Bool
&& Int
kInt -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<Set a -> Int
forall a. Set a -> Int
Set.size Set a
set) (Int -> Set a -> a
forall a. Int -> Set a -> a
Set.elemAt Int
k Set a
set)

uncheckedIndexFromOffset :: Set a -> Int -> a
uncheckedIndexFromOffset :: forall a. Set a -> Int -> a
uncheckedIndexFromOffset = (Int -> Set a -> a) -> Set a -> Int -> a
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> Set a -> a
forall a. Int -> Set a -> a
Set.elemAt