Add operators for indexing

This commit is contained in:
Kiana Sheibani 2022-05-24 09:44:13 -04:00
parent 861f1e29f2
commit 97d1bdb538
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
2 changed files with 33 additions and 5 deletions

View file

@ -201,12 +201,31 @@ array v = MkArray COrder (calcStrides COrder s) s (fromList $ collapse v)
-- Indexing
--------------------------------------------------------------------------------
infix 2 !!
infix 2 !?
infixl 3 !!..
infix 3 !?..
||| Index the array using the given `Coords` object.
export
index : Coords s -> Array s a -> a
index is arr = index (getLocation (strides arr) is) (getPrim arr)
export
(!!) : Array s a -> Coords s -> a
(!!) = flip index
export
indexMaybe : Vect rk Nat -> Array {rk} s a -> Maybe a
indexMaybe is arr = safeIndex (getLocation' (strides arr) is) (getPrim arr)
export
(!?) : Array {rk} s a -> Vect rk Nat -> Maybe a
(!?) = flip indexMaybe
||| Index the array using the given `CoordsRange` object.
export
indexRange : (rs : CoordsRange s) -> Array s a -> Array (newShape rs) a
@ -218,6 +237,10 @@ indexRange rs arr = let ord = getOrder arr
map (\(is,is') => (getLocation' sts is', index (getLocation' (strides arr) is) (getPrim arr))) $
getCoordsList {s = shape arr} $ rewrite sym $ shapeEq arr in rs)
export
(!!..) : Array s a -> (rs : CoordsRange s) -> Array (newShape rs) a
arr !!.. rs = indexRange rs arr
--------------------------------------------------------------------------------
-- Operations on arrays