{-# LANGUAGE CPP #-}

{-# OPTIONS_GHC -Wno-dodgy-exports #-} -- don't complain about empty exports for Agda.Utils.IArray

-- | Array utilities.

module Agda.Utils.IArray (module Agda.Utils.IArray, module Data.Array.IArray) where

import Data.Array.IArray
import Data.Array.Base   ( IArray(..) )
import Data.Ix           ( inRange )

#if MIN_VERSION_base(4,14,0)
import GHC.Ix            ( unsafeIndex )
#endif

-- Backported from array-0.5.6:

#if !MIN_VERSION_array(0,5,6)

{-# INLINE (!?) #-}
-- | Returns 'Just' the element of an immutable array at the specified index,
-- or 'Nothing' if the index is out of bounds.
--
(!?) :: (IArray a e, Ix i) => a i e -> i -> Maybe e
!? :: forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> i -> Maybe e
(!?) a i e
arr i
i
  | (i, i) -> i -> Bool
forall a. Ix a => (a, a) -> a -> Bool
inRange (i, i)
b i
i = e -> Maybe e
forall a. a -> Maybe a
Just (e -> Maybe e) -> e -> Maybe e
forall a b. (a -> b) -> a -> b
$ a i e -> Int -> e
forall i. Ix i => a i e -> Int -> e
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> Int -> e
unsafeAt a i e
arr (Int -> e) -> Int -> e
forall a b. (a -> b) -> a -> b
$
#if MIN_VERSION_base(4,14,0)
    (i, i) -> i -> Int
forall a. Ix a => (a, a) -> a -> Int
unsafeIndex (i, i)
b i
i
#else
    index b i
#endif
  | Bool
otherwise   = Maybe e
forall a. Maybe a
Nothing
  where b :: (i, i)
b = a i e -> (i, i)
forall i. Ix i => a i e -> (i, i)
forall (a :: * -> * -> *) e i.
(IArray a e, Ix i) =>
a i e -> (i, i)
bounds a i e
arr

#endif