Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ module Data.Vector (
unsafeIndexM, unsafeHeadM, unsafeLastM,

-- ** Extracting subvectors (slicing)
slice, init, tail, take, drop, splitAt,
slice, init, tail, take, takeEnd, drop, dropEnd, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
unsafeTakeEnd, unsafeDropEnd,

-- * Construction

Expand Down Expand Up @@ -570,12 +571,24 @@ take :: Int -> Vector a -> Vector a
{-# INLINE take #-}
take = G.take

-- | /O(1)/ Yield the last @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case it is returned unchanged.
takeEnd :: Int -> Vector a -> Vector a
{-# INLINE takeEnd #-}
takeEnd = G.takeEnd

-- | /O(1)/ Yield all but the first @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
drop :: Int -> Vector a -> Vector a
{-# INLINE drop #-}
drop = G.drop

-- | /O(1)/ Yield all but the last @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
dropEnd :: Int -> Vector a -> Vector a
{-# INLINE dropEnd #-}
dropEnd = G.dropEnd

-- | /O(1)/ Yield the first @n@ elements paired with the remainder without copying.
--
-- Note that @'splitAt' n v@ is equivalent to @('take' n v, 'drop' n v)@
Expand Down Expand Up @@ -611,12 +624,24 @@ unsafeTake :: Int -> Vector a -> Vector a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

-- | /O(1)/ Yield the last @n@ elements without copying. The vector must
-- contain at least @n@ elements but this is not checked.
unsafeTakeEnd :: Int -> Vector a -> Vector a
{-# INLINE unsafeTakeEnd #-}
unsafeTakeEnd = G.unsafeTakeEnd

-- | /O(1)/ Yield all but the first @n@ elements without copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDrop :: Int -> Vector a -> Vector a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

-- | /O(1)/ Yield all but the last @n@ elements without copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDropEnd :: Int -> Vector a -> Vector a
{-# INLINE unsafeDropEnd #-}
unsafeDropEnd = G.unsafeDropEnd

-- Initialisation
-- --------------

Expand Down
35 changes: 34 additions & 1 deletion Data/Vector/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ module Data.Vector.Generic (
unsafeIndexM, unsafeHeadM, unsafeLastM,

-- ** Extracting subvectors (slicing)
slice, init, tail, take, drop, splitAt,
slice, init, tail, take, takeEnd, drop, dropEnd, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
unsafeTakeEnd, unsafeDropEnd,

-- * Construction

Expand Down Expand Up @@ -415,6 +416,16 @@ take :: Vector v a => Int -> v a -> v a
take n v = unsafeSlice 0 (delay_inline min n' (length v)) v
where n' = max n 0

-- | /O(1)/ Yield the last @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case it is returned unchanged.
takeEnd :: Vector v a => Int -> v a -> v a
{-# INLINE_FUSED takeEnd #-}
takeEnd n v = unsafeSlice i (delay_inline min n' m) v
where
n' = max n 0
m = length v
i = delay_inline max 0 (m - n')

-- | /O(1)/ Yield all but the first @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
drop :: Vector v a => Int -> v a -> v a
Expand All @@ -424,6 +435,15 @@ drop n v = unsafeSlice (delay_inline min n' len)
where n' = max n 0
len = length v

-- | /O(1)/ Yield all but the last @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
dropEnd :: Vector v a => Int -> v a -> v a
{-# INLINE_FUSED dropEnd #-}
dropEnd n v = unsafeSlice 0 (delay_inline max 0 (m - n')) v
where
n' = max n 0
m = length v

-- | /O(1)/ Yield the first @n@ elements paired with the remainder without copying.
--
-- Note that @'splitAt' n v@ is equivalent to @('take' n v, 'drop' n v)@
Expand Down Expand Up @@ -466,12 +486,25 @@ unsafeTake :: Vector v a => Int -> v a -> v a
{-# INLINE unsafeTake #-}
unsafeTake n v = unsafeSlice 0 n v

-- | /O(1)/ Yield the last @n@ elements without copying. The vector must
-- contain at least @n@ elements but this is not checked.
unsafeTakeEnd :: Vector v a => Int -> v a -> v a
{-# INLINE unsafeTakeEnd #-}
unsafeTakeEnd n v = unsafeSlice (m - n) n v
where m = length v

-- | /O(1)/ Yield all but the first @n@ elements without copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDrop :: Vector v a => Int -> v a -> v a
{-# INLINE unsafeDrop #-}
unsafeDrop n v = unsafeSlice n (length v - n) v

-- | /O(1)/ Yield all but the last @n@ elements without copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDropEnd :: Vector v a => Int -> v a -> v a
{-# INLINE unsafeDropEnd #-}
unsafeDropEnd n v = unsafeSlice 0 (length v - n) v

{-# RULES

"slice/new [Vector]" forall i n p.
Expand Down
27 changes: 26 additions & 1 deletion Data/Vector/Generic/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ module Data.Vector.Generic.Mutable (
length, null,

-- ** Extracting subvectors
slice, init, tail, take, drop, splitAt,
slice, init, tail, take, takeEnd, drop, dropEnd, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
unsafeTakeEnd, unsafeDropEnd,

-- ** Overlapping
overlaps,
Expand Down Expand Up @@ -518,13 +519,28 @@ take :: MVector v a => Int -> v s a -> v s a
{-# INLINE take #-}
take n v = unsafeSlice 0 (min (max n 0) (length v)) v

takeEnd :: MVector v a => Int -> v s a -> v s a
{-# INLINE takeEnd #-}
takeEnd n v = unsafeSlice i (min n' m) v
where
n' = max n 0
m = length v
i = max 0 (m - n')

drop :: MVector v a => Int -> v s a -> v s a
{-# INLINE drop #-}
drop n v = unsafeSlice (min m n') (max 0 (m - n')) v
where
n' = max n 0
m = length v

dropEnd :: MVector v a => Int -> v s a -> v s a
{-# INLINE dropEnd #-}
dropEnd n v = unsafeSlice 0 (max 0 (m - n')) v
where
n' = max n 0
m = length v

{-# INLINE splitAt #-}
splitAt :: MVector v a => Int -> v s a -> (v s a, v s a)
splitAt n v = ( unsafeSlice 0 m v
Expand Down Expand Up @@ -565,10 +581,19 @@ unsafeTake :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeTake #-}
unsafeTake n v = unsafeSlice 0 n v

unsafeTakeEnd :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeTakeEnd #-}
unsafeTakeEnd n v = unsafeSlice (m - n) n v
where m = length v

unsafeDrop :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeDrop #-}
unsafeDrop n v = unsafeSlice n (length v - n) v

unsafeDropEnd :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeDropEnd #-}
unsafeDropEnd n v = unsafeSlice 0 (length v - n) v

-- Overlapping
-- -----------

Expand Down
10 changes: 9 additions & 1 deletion Data/Vector/Generic/New.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
module Data.Vector.Generic.New (
New(..), create, run, runPrim, apply, modify, modifyWithBundle,
unstream, transform, unstreamR, transformR,
slice, init, tail, take, drop,
slice, init, tail, take, takeEnd, drop, dropEnd,
unsafeSlice, unsafeInit, unsafeTail
) where

Expand Down Expand Up @@ -132,10 +132,18 @@ take :: Vector v a => Int -> New v a -> New v a
{-# INLINE_FUSED take #-}
take n m = apply (MVector.take n) m

takeEnd :: Vector v a => Int -> New v a -> New v a
{-# INLINE_FUSED takeEnd #-}
takeEnd n m = apply (MVector.takeEnd n) m

drop :: Vector v a => Int -> New v a -> New v a
{-# INLINE_FUSED drop #-}
drop n m = apply (MVector.drop n) m

dropEnd :: Vector v a => Int -> New v a -> New v a
{-# INLINE_FUSED dropEnd #-}
dropEnd n m = apply (MVector.dropEnd n) m

unsafeSlice :: Vector v a => Int -> Int -> New v a -> New v a
{-# INLINE_FUSED unsafeSlice #-}
unsafeSlice i n m = apply (MVector.unsafeSlice i n) m
Expand Down
19 changes: 18 additions & 1 deletion Data/Vector/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ module Data.Vector.Mutable (
length, null,

-- ** Extracting subvectors
slice, init, tail, take, drop, splitAt,
slice, init, tail, take, takeEnd, drop, dropEnd, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
unsafeTakeEnd, unsafeDropEnd,

-- ** Overlapping
overlaps,
Expand Down Expand Up @@ -212,10 +213,18 @@ take :: Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take

takeEnd :: Int -> MVector s a -> MVector s a
{-# INLINE takeEnd #-}
takeEnd = G.takeEnd

drop :: Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop

dropEnd :: Int -> MVector s a -> MVector s a
{-# INLINE dropEnd #-}
dropEnd = G.dropEnd

{-# INLINE splitAt #-}
splitAt :: Int -> MVector s a -> (MVector s a, MVector s a)
splitAt = G.splitAt
Expand All @@ -241,10 +250,18 @@ unsafeTake :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

unsafeTakeEnd :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeTakeEnd #-}
unsafeTakeEnd = G.unsafeTakeEnd

unsafeDrop :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

unsafeDropEnd :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeDropEnd #-}
unsafeDropEnd = G.unsafeDropEnd

unsafeInit :: MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit
Expand Down
29 changes: 27 additions & 2 deletions Data/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ module Data.Vector.Primitive (
unsafeIndexM, unsafeHeadM, unsafeLastM,

-- ** Extracting subvectors (slicing)
slice, init, tail, take, drop, splitAt,
slice, init, tail, take, takeEnd, drop, dropEnd, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
unsafeTakeEnd, unsafeDropEnd,

-- * Construction

Expand Down Expand Up @@ -413,18 +414,30 @@ tail :: Prim a => Vector a -> Vector a
{-# INLINE tail #-}
tail = G.tail

-- | /O(1)/ Yield at the first @n@ elements without copying. The vector may
-- | /O(1)/ Yield the first @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case it is returned unchanged.
take :: Prim a => Int -> Vector a -> Vector a
{-# INLINE take #-}
take = G.take

-- | /O(1)/ Yield the last @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case it is returned unchanged.
takeEnd :: Prim a => Int -> Vector a -> Vector a
{-# INLINE takeEnd #-}
takeEnd = G.takeEnd

-- | /O(1)/ Yield all but the first @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
drop :: Prim a => Int -> Vector a -> Vector a
{-# INLINE drop #-}
drop = G.drop

-- | /O(1)/ Yield all but the last @n@ elements without copying. The vector may
-- contain less than @n@ elements in which case an empty vector is returned.
dropEnd :: Prim a => Int -> Vector a -> Vector a
{-# INLINE dropEnd #-}
dropEnd = G.dropEnd

-- | /O(1)/ Yield the first @n@ elements paired with the remainder without copying.
--
-- Note that @'splitAt' n v@ is equivalent to @('take' n v, 'drop' n v)@
Expand Down Expand Up @@ -460,12 +473,24 @@ unsafeTake :: Prim a => Int -> Vector a -> Vector a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

-- | /O(1)/ Yield the last @n@ elements without copying. The vector must
-- contain at least @n@ elements but this is not checked.
unsafeTakeEnd :: Prim a => Int -> Vector a -> Vector a
{-# INLINE unsafeTakeEnd #-}
unsafeTakeEnd = G.unsafeTakeEnd

-- | /O(1)/ Yield all but the first @n@ elements without copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDrop :: Prim a => Int -> Vector a -> Vector a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

-- | /O(1)/ Yield all but the last @n@ elements without copying. The vector
-- must contain at least @n@ elements but this is not checked.
unsafeDropEnd :: Prim a => Int -> Vector a -> Vector a
{-# INLINE unsafeDropEnd #-}
unsafeDropEnd = G.unsafeDropEnd

-- Initialisation
-- --------------

Expand Down
19 changes: 18 additions & 1 deletion Data/Vector/Primitive/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ module Data.Vector.Primitive.Mutable (
length, null,

-- ** Extracting subvectors
slice, init, tail, take, drop, splitAt,
slice, init, tail, take, takeEnd, drop, dropEnd, splitAt,
unsafeSlice, unsafeInit, unsafeTail, unsafeTake, unsafeDrop,
unsafeTakeEnd, unsafeDropEnd,

-- ** Overlapping
overlaps,
Expand Down Expand Up @@ -154,10 +155,18 @@ take :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take

takeEnd :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE takeEnd #-}
takeEnd = G.takeEnd

drop :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop

dropEnd :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE dropEnd #-}
dropEnd = G.dropEnd

splitAt :: Prim a => Int -> MVector s a -> (MVector s a, MVector s a)
{-# INLINE splitAt #-}
splitAt = G.splitAt
Expand All @@ -184,10 +193,18 @@ unsafeTake :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

unsafeTakeEnd :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeTakeEnd #-}
unsafeTakeEnd = G.unsafeTakeEnd

unsafeDrop :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

unsafeDropEnd :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeDropEnd #-}
unsafeDropEnd = G.unsafeDropEnd

unsafeInit :: Prim a => MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit
Expand Down
Loading