Skip to content

Commit

Permalink
Add haddocks for mutable drop/take/init/tail and corresponding unsafe…
Browse files Browse the repository at this point in the history
… variants (#377)

* Add haddocks for mutable take, drop, init & tail

* Add documentation for unsafe variants
  • Loading branch information
Shimuuar authored May 1, 2021
1 parent e9d36b1 commit e7d09b8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions vector/src/Data/Vector/Generic/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,16 @@ slice :: MVector v a
slice i n v = BOUNDS_CHECK(checkSlice) "slice" i n (length v)
$ unsafeSlice i n v

-- | Take @n@ first elements of the mutable vector without making a
-- copy. For negative @n@ empty vector is returned. If @n@ is larger
-- than vector's length empty vector is returned,
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

-- | Drop @n@ first element of the mutable vector without making a
-- copy. For negative @n@ vector is returned unchanged and if @n@ is
-- larger than vector's length empty vector is returned.
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
Expand All @@ -549,10 +555,14 @@ splitAt n v = ( unsafeSlice 0 m v
n' = max n 0
len = length v

-- | Drop last element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
init :: MVector v a => v s a -> v s a
{-# INLINE init #-}
init v = slice 0 (length v - 1) v

-- | Drop first element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
tail :: MVector v a => v s a -> v s a
{-# INLINE tail #-}
tail v = slice 1 (length v - 1) v
Expand All @@ -567,18 +577,24 @@ unsafeSlice :: MVector v a => Int -- ^ starting index
unsafeSlice i n v = UNSAFE_CHECK(checkSlice) "unsafeSlice" i n (length v)
$ basicUnsafeSlice i n v

-- | Same as 'init' but doesn't do range checks.
unsafeInit :: MVector v a => v s a -> v s a
{-# INLINE unsafeInit #-}
unsafeInit v = unsafeSlice 0 (length v - 1) v

-- | Same as 'tail' but doesn't do range checks.
unsafeTail :: MVector v a => v s a -> v s a
{-# INLINE unsafeTail #-}
unsafeTail v = unsafeSlice 1 (length v - 1) v

-- | Unsafe variant of 'take'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeTake :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeTake #-}
unsafeTake n v = unsafeSlice 0 n v

-- | Unsafe variant of 'drop'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeDrop :: MVector v a => Int -> v s a -> v s a
{-# INLINE unsafeDrop #-}
unsafeDrop n v = unsafeSlice n (length v - n) v
Expand Down
16 changes: 16 additions & 0 deletions vector/src/Data/Vector/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,16 @@ slice :: Int -- ^ @i@ starting index
{-# INLINE slice #-}
slice = G.slice

-- | Take @n@ first elements of the mutable vector without making a
-- copy. For negative @n@ empty vector is returned. If @n@ is larger
-- than vector's length empty vector is returned,
take :: Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take

-- | Drop @n@ first element of the mutable vector without making a
-- copy. For negative @n@ vector is returned unchanged and if @n@ is
-- larger than vector's length empty vector is returned.
drop :: Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop
Expand All @@ -243,10 +249,14 @@ drop = G.drop
splitAt :: Int -> MVector s a -> (MVector s a, MVector s a)
splitAt = G.splitAt

-- | Drop last element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
init :: MVector s a -> MVector s a
{-# INLINE init #-}
init = G.init

-- | Drop first element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
tail :: MVector s a -> MVector s a
{-# INLINE tail #-}
tail = G.tail
Expand All @@ -260,18 +270,24 @@ unsafeSlice :: Int -- ^ starting index
{-# INLINE unsafeSlice #-}
unsafeSlice = G.unsafeSlice

-- | Unsafe variant of 'take'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeTake :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

-- | Unsafe variant of 'drop'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeDrop :: Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

-- | Same as 'init' but doesn't do range checks.
unsafeInit :: MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit

-- | Same as 'tail' but doesn't do range checks.
unsafeTail :: MVector s a -> MVector s a
{-# INLINE unsafeTail #-}
unsafeTail = G.unsafeTail
Expand Down
16 changes: 16 additions & 0 deletions vector/src/Data/Vector/Primitive/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,16 @@ slice :: Prim a
{-# INLINE slice #-}
slice = G.slice

-- | Take @n@ first elements of the mutable vector without making a
-- copy. For negative @n@ empty vector is returned. If @n@ is larger
-- than vector's length empty vector is returned,
take :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take

-- | Drop @n@ first element of the mutable vector without making a
-- copy. For negative @n@ vector is returned unchanged and if @n@ is
-- larger than vector's length empty vector is returned.
drop :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop
Expand All @@ -203,10 +209,14 @@ splitAt :: Prim a => Int -> MVector s a -> (MVector s a, MVector s a)
{-# INLINE splitAt #-}
splitAt = G.splitAt

-- | Drop last element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
init :: Prim a => MVector s a -> MVector s a
{-# INLINE init #-}
init = G.init

-- | Drop first element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
tail :: Prim a => MVector s a -> MVector s a
{-# INLINE tail #-}
tail = G.tail
Expand All @@ -221,18 +231,24 @@ unsafeSlice :: Prim a
{-# INLINE unsafeSlice #-}
unsafeSlice = G.unsafeSlice

-- | Unsafe variant of 'take'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeTake :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

-- | Unsafe variant of 'drop'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeDrop :: Prim a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

-- | Same as 'init' but doesn't do range checks.
unsafeInit :: Prim a => MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit

-- | Same as 'tail' but doesn't do range checks.
unsafeTail :: Prim a => MVector s a -> MVector s a
{-# INLINE unsafeTail #-}
unsafeTail = G.unsafeTail
Expand Down
16 changes: 16 additions & 0 deletions vector/src/Data/Vector/Storable/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,16 @@ slice :: Storable a
{-# INLINE slice #-}
slice = G.slice

-- | Take @n@ first elements of the mutable vector without making a
-- copy. For negative @n@ empty vector is returned. If @n@ is larger
-- than vector's length empty vector is returned,
take :: Storable a => Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take

-- | Drop @n@ first element of the mutable vector without making a
-- copy. For negative @n@ vector is returned unchanged and if @n@ is
-- larger than vector's length empty vector is returned.
drop :: Storable a => Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop
Expand All @@ -299,10 +305,14 @@ splitAt :: Storable a => Int -> MVector s a -> (MVector s a, MVector s a)
{-# INLINE splitAt #-}
splitAt = G.splitAt

-- | Drop last element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
init :: Storable a => MVector s a -> MVector s a
{-# INLINE init #-}
init = G.init

-- | Drop first element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
tail :: Storable a => MVector s a -> MVector s a
{-# INLINE tail #-}
tail = G.tail
Expand All @@ -317,18 +327,24 @@ unsafeSlice :: Storable a
{-# INLINE unsafeSlice #-}
unsafeSlice = G.unsafeSlice

-- | Unsafe variant of 'take'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeTake :: Storable a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

-- | Unsafe variant of 'drop'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeDrop :: Storable a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

-- | Same as 'init' but doesn't do range checks.
unsafeInit :: Storable a => MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit

-- | Same as 'tail' but doesn't do range checks.
unsafeTail :: Storable a => MVector s a -> MVector s a
{-# INLINE unsafeTail #-}
unsafeTail = G.unsafeTail
Expand Down
16 changes: 16 additions & 0 deletions vector/src/Data/Vector/Unboxed/Mutable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,16 @@ slice :: Unbox a
{-# INLINE slice #-}
slice = G.slice

-- | Take @n@ first elements of the mutable vector without making a
-- copy. For negative @n@ empty vector is returned. If @n@ is larger
-- than vector's length empty vector is returned,
take :: Unbox a => Int -> MVector s a -> MVector s a
{-# INLINE take #-}
take = G.take

-- | Drop @n@ first element of the mutable vector without making a
-- copy. For negative @n@ vector is returned unchanged and if @n@ is
-- larger than vector's length empty vector is returned.
drop :: Unbox a => Int -> MVector s a -> MVector s a
{-# INLINE drop #-}
drop = G.drop
Expand All @@ -114,10 +120,14 @@ splitAt :: Unbox a => Int -> MVector s a -> (MVector s a, MVector s a)
{-# INLINE splitAt #-}
splitAt = G.splitAt

-- | Drop last element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
init :: Unbox a => MVector s a -> MVector s a
{-# INLINE init #-}
init = G.init

-- | Drop first element of the mutable vector without making a copy. If
-- vector is empty exception is thrown.
tail :: Unbox a => MVector s a -> MVector s a
{-# INLINE tail #-}
tail = G.tail
Expand All @@ -132,18 +142,24 @@ unsafeSlice :: Unbox a
{-# INLINE unsafeSlice #-}
unsafeSlice = G.unsafeSlice

-- | Unsafe variant of 'take'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeTake :: Unbox a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeTake #-}
unsafeTake = G.unsafeTake

-- | Unsafe variant of 'drop'. If called with out of range @n@ it will
-- simply create invalid slice that likely violate memory safety
unsafeDrop :: Unbox a => Int -> MVector s a -> MVector s a
{-# INLINE unsafeDrop #-}
unsafeDrop = G.unsafeDrop

-- | Same as 'init' but doesn't do range checks.
unsafeInit :: Unbox a => MVector s a -> MVector s a
{-# INLINE unsafeInit #-}
unsafeInit = G.unsafeInit

-- | Same as 'tail' but doesn't do range checks.
unsafeTail :: Unbox a => MVector s a -> MVector s a
{-# INLINE unsafeTail #-}
unsafeTail = G.unsafeTail
Expand Down

0 comments on commit e7d09b8

Please sign in to comment.