array-utils 0.1 → 0.2
raw patch · 2 files changed
+56/−56 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Data.Array.Util: updateElemsIxOn :: (MArray a e m, Ix i) => (i -> e -> e) -> [i] -> a i e -> m ()
- Data.Array.Util: updateElemsIxOnM :: (MArray a e m, Ix i) => (i -> e -> m e) -> [i] -> a i e -> m ()
- Data.Array.Util: updateElemsOn :: (MArray a e m, Ix i) => (e -> e) -> [i] -> a i e -> m ()
- Data.Array.Util: updateElemsOnM :: (MArray a e m, Ix i) => (e -> m e) -> [i] -> a i e -> m ()
- Data.Array.Util: updateElemsSlice :: (MArray a e m, Ix i) => (e -> e) -> (i, i) -> a i e -> m ()
- Data.Array.Util: updateElemsSliceM :: (MArray a e m, Ix i) => (e -> m e) -> (i, i) -> a i e -> m ()
- Data.Array.Util: updateElemsWithin :: (MArray a e m, Ix i) => (e -> e) -> (i, i) -> a i e -> m ()
- Data.Array.Util: updateElemsWithinIx :: (MArray a e m, Ix i, Show i) => (i -> e -> e) -> (i, i) -> a i e -> m ()
- Data.Array.Util: updateElemsWithinIxM :: (MArray a e m, Ix i, Show i) => (i -> e -> m e) -> (i, i) -> a i e -> m ()
- Data.Array.Util: updateElemsWithinM :: (MArray a e m, Ix i) => (e -> m e) -> (i, i) -> a i e -> m ()
+ Data.Array.Util: updateOn :: (MArray a e m, Ix i) => (e -> e) -> [i] -> a i e -> m ()
+ Data.Array.Util: updateOnIx :: (MArray a e m, Ix i) => (i -> e -> e) -> [i] -> a i e -> m ()
+ Data.Array.Util: updateOnIxM :: (MArray a e m, Ix i) => (i -> e -> m e) -> [i] -> a i e -> m ()
+ Data.Array.Util: updateOnM :: (MArray a e m, Ix i) => (e -> m e) -> [i] -> a i e -> m ()
+ Data.Array.Util: updateSlice :: (MArray a e m, Ix i) => (e -> e) -> (i, i) -> a i e -> m ()
+ Data.Array.Util: updateSliceM :: (MArray a e m, Ix i) => (e -> m e) -> (i, i) -> a i e -> m ()
+ Data.Array.Util: updateWithin :: (MArray a e m, Ix i) => (e -> e) -> (i, i) -> a i e -> m ()
+ Data.Array.Util: updateWithinIx :: (MArray a e m, Ix i, Show i) => (i -> e -> e) -> (i, i) -> a i e -> m ()
+ Data.Array.Util: updateWithinIxM :: (MArray a e m, Ix i, Show i) => (i -> e -> m e) -> (i, i) -> a i e -> m ()
+ Data.Array.Util: updateWithinM :: (MArray a e m, Ix i) => (e -> m e) -> (i, i) -> a i e -> m ()
Files
- Data/Array/Util.hs +55/−55
- array-utils.cabal +1/−1
Data/Array/Util.hs view
@@ -10,22 +10,22 @@ , updateElemsM , updateElemsIxM -- * Updating certain elements- , updateElemsOn- , updateElemsIxOn+ , updateOn+ , updateOnIx -- ** Monadic versions- , updateElemsOnM- , updateElemsIxOnM+ , updateOnM+ , updateOnIxM -- * Updating within a bounded area- , updateElemsWithin- , updateElemsWithinIx+ , updateWithin+ , updateWithinIx -- ** Monadic versions- , updateElemsWithinM- , updateElemsWithinIxM+ , updateWithinM+ , updateWithinIxM -- * Updating slices -- $Slice- , updateElemsSlice+ , updateSlice -- ** Monadic versions- , updateElemsSliceM+ , updateSliceM ) where import GHC.Arr@@ -115,7 +115,7 @@ go 0 (range bnds) -- ======================--- = updateElemsWithin* =+-- = updateWithin* = -- ====================== {-|@@ -127,32 +127,32 @@ Throws an 'IndexOutOfBounds' exception if either of the indicies are out of bounds. -}-INLINE(updateElemsWithin)-updateElemsWithin :: (MArray a e m, Ix i)+INLINE(updateWithin)+updateWithin :: (MArray a e m, Ix i) => (e -> e) -- ^ Update function -> (i,i) -- ^ The bounds within which to apply f. -> a i e -- ^ The array -> m ()-updateElemsWithin f (start, finish) arr = do+updateWithin f (start, finish) arr = do bnds <- getBounds arr let !ok = inRange bnds start && inRange bnds finish indicies = map (unsafeIndex bnds) $ range (start, finish) - when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateElemsWithin"+ when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateWithin" forM_ indicies (update arr f) --- | The same as 'updateElemsWithin' but taking a monadic function.+-- | The same as 'updateWithin' but taking a monadic function. -- -- Throws an 'IndexOutOfBounds' exception if either of the indicies are out of bounds.-INLINE(updateElemsWithinM)-updateElemsWithinM :: (MArray a e m, Ix i) => (e -> m e) -> (i,i) -> a i e -> m ()-updateElemsWithinM f (start, finish) arr = do+INLINE(updateWithinM)+updateWithinM :: (MArray a e m, Ix i) => (e -> m e) -> (i,i) -> a i e -> m ()+updateWithinM f (start, finish) arr = do bnds <- getBounds arr let !ok = inRange bnds start && inRange bnds finish indicies = map (unsafeIndex bnds) $ range (start, finish) - when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateElemsWithin"+ when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateWithin" forM_ indicies (updateM arr f) @@ -160,9 +160,9 @@ -- , and applies the function to all elements returned by range (start, finish). -- -- Throws an 'IndexOutOfBounds' exception if either of the indicies are out of bounds.-INLINE(updateElemsWithinIx)-updateElemsWithinIx :: (MArray a e m, Ix i, Show i) => (i -> e -> e) -> (i,i) -> a i e -> m ()-updateElemsWithinIx f (start, finish) arr = do+INLINE(updateWithinIx)+updateWithinIx :: (MArray a e m, Ix i, Show i) => (i -> e -> e) -> (i,i) -> a i e -> m ()+updateWithinIx f (start, finish) arr = do bnds <- getBounds arr let !ok = inRange bnds start && inRange bnds finish rnge = range (start, finish)@@ -171,16 +171,16 @@ go (!i:is) (x:xs) = updateIx arr f i x >> go is xs go _ _ = return () - when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateElemsWithinIx"+ when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateWithinIx" go indicies rnge --- | The same as 'updateElemsWithinIx' but taking a monadic function.+-- | The same as 'updateWithinIx' but taking a monadic function. -- -- Throws an 'IndexOutOfBounds' exception if either of the indicies are out of bounds.-INLINE(updateElemsWithinIxM)-updateElemsWithinIxM :: (MArray a e m, Ix i, Show i) => (i -> e -> m e) -> (i,i) -> a i e -> m ()-updateElemsWithinIxM f (start, finish) arr = do+INLINE(updateWithinIxM)+updateWithinIxM :: (MArray a e m, Ix i, Show i) => (i -> e -> m e) -> (i,i) -> a i e -> m ()+updateWithinIxM f (start, finish) arr = do bnds <- getBounds arr let !ok = inRange bnds start && inRange bnds finish rnge = range (start, finish)@@ -189,11 +189,11 @@ go (!i:is) (x:xs) = updateIxM arr f i x >> go is xs go _ _ = return () - when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateElemsWithinIx"+ when (not ok) $ throw $ IndexOutOfBounds $ "Data.Array.Util updateWithinIx" go indicies rnge -- ==================--- = updateElemsOn* =+-- = updateOn* = -- ================== {-|@@ -203,13 +203,13 @@ out of bounds. In this case the array will be left unmutated. /O(length xs)/ -}-INLINE(updateElemsOn)-updateElemsOn :: (MArray a e m, Ix i)+INLINE(updateOn)+updateOn :: (MArray a e m, Ix i) => (e -> e) -- ^ Update function -> [i] -- ^ A list of indicies to update -> a i e -- ^ The array -> m ()-updateElemsOn f xs arr = do+updateOn f xs arr = do bnds <- getBounds arr let !ok = all (inRange bnds) xs toOffset = unsafeIndex bnds@@ -222,9 +222,9 @@ -- Throws an 'IndexOutOfBounds' exception if any of the indicies are -- out of bounds. In this case the array will be left unmutated. -- /O(length xs)/-INLINE(updateElemsOnM)-updateElemsOnM :: (MArray a e m, Ix i) => (e -> m e) -> [i] -> a i e -> m ()-updateElemsOnM f xs arr = do+INLINE(updateOnM)+updateOnM :: (MArray a e m, Ix i) => (e -> m e) -> [i] -> a i e -> m ()+updateOnM f xs arr = do bnds <- getBounds arr let !ok = all (inRange bnds) xs toOffset = unsafeIndex bnds@@ -236,9 +236,9 @@ -- | Takes a mapping function which takes an index, and a list of indicies -- to mutate. Throws 'IndexOutOfBounds' exception as 'updateElems'' does. -- /O(length xs)/-INLINE(updateElemsIxOn)-updateElemsIxOn :: (MArray a e m, Ix i) => (i -> e -> e) -> [i] -> a i e -> m ()-updateElemsIxOn f indexes arr = do+INLINE(updateOnIx)+updateOnIx :: (MArray a e m, Ix i) => (i -> e -> e) -> [i] -> a i e -> m ()+updateOnIx f indexes arr = do bnds@(_, hi) <- getBounds arr let toOffset = unsafeIndex bnds ixs = map toOffset indexes@@ -256,9 +256,9 @@ -- to mutate. /O(length xs)/ -- -- Throws 'IndexOutOfBounds' exception as 'updateElems'' does.-INLINE(updateElemsIxOnM)-updateElemsIxOnM :: (MArray a e m, Ix i) => (i -> e -> m e) -> [i] -> a i e -> m ()-updateElemsIxOnM f indexes arr = do+INLINE(updateOnIxM)+updateOnIxM :: (MArray a e m, Ix i) => (i -> e -> m e) -> [i] -> a i e -> m ()+updateOnIxM f indexes arr = do bnds@(_ , hi) <- getBounds arr let toOffset = unsafeIndex bnds !end = toOffset hi@@ -273,11 +273,11 @@ -- ======================--- = updateElemsSlice* =+-- = updateSlice* = -- ====================== {- $Slice-/Note the difference between these functions and updateElemsWithin./+/Note the difference between these functions and updateWithin./ These functions will update every element whose index holds this property: > f x = index (start,end) start <= ix && ix <= index (start,end) end@@ -287,7 +287,7 @@ >>> arr <- newArray ((1,1),(5,5)) 0 :: IO (IOArray Int Int) -- Produces aa 2D array with 25 elements all set to 0.->>> updateElemsSlice arr ((2,4),(3,5)) (+ 10)+>>> updateSlice arr ((2,4),(3,5)) (+ 10) -- Updates elements at indexes [(2,4),(2,5),(3,1),(3,2),(3,3),(3,4),(3,5)] to 10 \*Ix versions are not included, because there's no easy way to map from@@ -297,45 +297,45 @@ -} {-|-updateElemsSlice mutates every element in an array between a start+updateSlice mutates every element in an array between a start index and an end index. /O(size of arr)/ >>> arr <- newArray (1,10) 0 :: IO (IOArray Int Int) -- Produces a 1 based array with 10 elements all set to 0.->>> updateElemsSlice arr (2,4) (+ 10)+>>> updateSlice arr (2,4) (+ 10) -- Updates elements at indexes 2, 3 and 4 to 10 -}-INLINE(updateElemsSlice)-updateElemsSlice :: (MArray a e m, Ix i)+INLINE(updateSlice)+updateSlice :: (MArray a e m, Ix i) => (e -> e) -- ^ Update function -> (i,i) -- ^ The start and end of the region to update -> a i e -- ^ The array -> m ()-updateElemsSlice f (start', finish') arr = do+updateSlice f (start', finish') arr = do bnds@(_ , end') <- getBounds arr let !end = unsafeIndex bnds end' !start = unsafeIndex bnds start' !finish = unsafeIndex bnds finish' !ok = start >= 0 && start <= end && finish >= 0 && finish <= end - when (not ok) $ throw (IndexOutOfBounds $ "Data.Array.Util updateElemsSlice")+ when (not ok) $ throw (IndexOutOfBounds $ "Data.Array.Util updateSlice") forM_ [start..finish] (update arr f) -- | The same as updateElems but taking a monadic function. /O(size of arr)/-INLINE(updateElemsSliceM)-updateElemsSliceM :: (MArray a e m, Ix i) => (e -> m e) -> (i,i) -> a i e -> m ()-updateElemsSliceM f (start', finish') arr = do+INLINE(updateSliceM)+updateSliceM :: (MArray a e m, Ix i) => (e -> m e) -> (i,i) -> a i e -> m ()+updateSliceM f (start', finish') arr = do bnds@(_ , end') <- getBounds arr let !end = unsafeIndex bnds end' !start = unsafeIndex bnds start' !finish = unsafeIndex bnds finish' !ok = start >= 0 && start <= end && finish >= 0 && finish <= end - when (not ok) $ throw (IndexOutOfBounds $ "Data.Array.Util updateElemsSlice")+ when (not ok) $ throw (IndexOutOfBounds $ "Data.Array.Util updateSlice") forM_ [start..finish] (updateM arr f)
array-utils.cabal view
@@ -1,5 +1,5 @@ Name: array-utils-Version: 0.1+Version: 0.2 category: Data, Data Structures Synopsis: Primitive functions for updating many elements in mutable arrays at once Description: An collection of functions for working with multiple elements in