Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement spanR & breakR #476

Merged
merged 3 commits into from
Jan 17, 2024
Merged
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
- { cabal: "3.10", os: ubuntu-latest, ghc: "9.2.8" }
- { cabal: "3.10", os: ubuntu-latest, ghc: "9.4.8" }
- { cabal: "3.10", os: ubuntu-latest, ghc: "9.6.3" }
- { cabal: "3.10", os: ubuntu-latest, ghc: "9.6.3",
flags: "-fUnsafeChecks -fInternalChecks" }
- { cabal: "3.10", os: ubuntu-latest, ghc: "9.8.1" }
# Win
- { cabal: "3.10", os: windows-latest, ghc: "8.4.4" }
Expand Down Expand Up @@ -89,7 +91,8 @@ jobs:
# ----------------
- name: Build
run: |
cabal configure --haddock-all --enable-tests --enable-benchmarks --benchmark-option=-l
set -x
cabal configure ${{ matrix.flags }} --haddock-all --enable-tests --enable-benchmarks --benchmark-option=-l
cabal build all --write-ghc-environment-files=always
# ----------------
- name: Test
Expand Down
50 changes: 48 additions & 2 deletions vector/src/Data/Vector.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ module Data.Vector (
takeWhile, dropWhile,

-- ** Partitioning
partition, unstablePartition, partitionWith, span, break, groupBy, group,
partition, unstablePartition, partitionWith, span, break, spanR, breakR, groupBy, group,

-- ** Searching
elem, notElem, find, findIndex, findIndexR, findIndices, elemIndex, elemIndices,
Expand Down Expand Up @@ -1400,16 +1400,62 @@ unstablePartition = G.unstablePartition

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.span (<4) $ V.generate 10 id
-- ([0,1,2,3],[4,5,6,7,8,9])
span :: (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE span #-}
span = G.span

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.break (>4) $ V.generate 10 id
-- ([0,1,2,3,4],[5,6,7,8,9])
break :: (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE break #-}
break = G.break

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.spanR (>4) $ V.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
spanR :: (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE spanR #-}
spanR = G.spanR

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- @since NEXT_VERSION
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.breakR (<5) $ V.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
breakR :: (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE breakR #-}
breakR = G.breakR

-- | /O(n)/ Split a vector into a list of slices, using a predicate function.
--
-- The concatenation of this list of slices is equal to the argument vector,
Expand Down Expand Up @@ -2252,4 +2298,4 @@ copy = G.copy

-- $setup
-- >>> :set -Wno-type-defaults
-- >>> import Prelude (Char, String, Bool(True, False), min, max, fst, even, undefined)
-- >>> import Prelude (Char, String, Bool(True, False), min, max, fst, even, undefined, Ord(..))
56 changes: 54 additions & 2 deletions vector/src/Data/Vector/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module Data.Vector.Generic (
takeWhile, dropWhile,

-- ** Partitioning
partition, partitionWith, unstablePartition, span, break, groupBy, group,
partition, partitionWith, unstablePartition, span, break, spanR, breakR, groupBy, group,

-- ** Searching
elem, notElem, find, findIndex, findIndexR, findIndices, elemIndex, elemIndices,
Expand Down Expand Up @@ -1536,18 +1536,70 @@ unstablePartition_new f (New.New p) = runST (

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.span (<4) $ V.generate 10 id
-- ([0,1,2,3],[4,5,6,7,8,9])
span :: Vector v a => (a -> Bool) -> v a -> (v a, v a)
{-# INLINE span #-}
span f = break (not . f)

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.break (>4) $ V.generate 10 id
-- ([0,1,2,3,4],[5,6,7,8,9])
break :: Vector v a => (a -> Bool) -> v a -> (v a, v a)
{-# INLINE break #-}
break f xs = case findIndex f xs of
Just i -> (unsafeSlice 0 i xs, unsafeSlice i (length xs - i) xs)
Nothing -> (xs, empty)

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.spanR (>4) $ V.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
spanR :: Vector v a => (a -> Bool) -> v a -> (v a, v a)
{-# INLINE spanR #-}
spanR f = breakR (not . f)

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- @since NEXT_VERSION
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector as V
-- >>> V.breakR (<5) $ V.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
breakR :: Vector v a => (a -> Bool) -> v a -> (v a, v a)
{-# INLINE breakR #-}
breakR f xs = case findIndexR f xs of
Just i -> ( unsafeSlice (i+1) (length xs - i - 1) xs
, unsafeSlice 0 (i+1) xs)
Nothing -> (xs, empty)




-- | /O(n)/ Split a vector into a list of slices.
--
-- The concatenation of this list of slices is equal to the argument vector,
Expand Down Expand Up @@ -2659,4 +2711,4 @@ dataCast f = gcast1 f
-- $setup
-- >>> :set -XFlexibleContexts
-- >>> :set -Wno-type-defaults
-- >>> import Prelude (Bool(True, False), even)
-- >>> import Prelude (Bool(True, False), even, Ord(..))
50 changes: 48 additions & 2 deletions vector/src/Data/Vector/Primitive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ module Data.Vector.Primitive (
takeWhile, dropWhile,

-- ** Partitioning
partition, unstablePartition, partitionWith, span, break, groupBy, group,
partition, unstablePartition, partitionWith, span, break, spanR, breakR, groupBy, group,

-- ** Searching
elem, notElem, find, findIndex, findIndexR, findIndices, elemIndex, elemIndices,
Expand Down Expand Up @@ -1156,16 +1156,62 @@ unstablePartition = G.unstablePartition

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Primitive as VP
-- >>> VP.span (<4) $ VP.generate 10 id
-- ([0,1,2,3],[4,5,6,7,8,9])
span :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE span #-}
span = G.span

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Primitive as VP
-- >>> VP.break (>4) $ VP.generate 10 id
-- ([0,1,2,3,4],[5,6,7,8,9])
break :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE break #-}
break = G.break

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Primitive as VP
-- >>> VP.spanR (>4) $ VP.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
spanR :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE spanR #-}
spanR = G.spanR

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- @since NEXT_VERSION
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Primitive as VP
-- >>> VP.breakR (<5) $ VP.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
breakR :: Prim a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE breakR #-}
breakR = G.breakR

-- | /O(n)/ Split a vector into a list of slices, using a predicate function.
--
-- The concatenation of this list of slices is equal to the argument vector,
Expand Down Expand Up @@ -1894,4 +1940,4 @@ copy :: (Prim a, PrimMonad m) => MVector (PrimState m) a -> Vector a -> m ()
copy = G.copy

-- $setup
-- >>> import Prelude (($), min, even, max, succ)
-- >>> import Prelude (($), min, even, max, succ, id, Ord(..))
50 changes: 48 additions & 2 deletions vector/src/Data/Vector/Storable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module Data.Vector.Storable (
takeWhile, dropWhile,

-- ** Partitioning
partition, unstablePartition, partitionWith, span, break, groupBy, group,
partition, unstablePartition, partitionWith, span, break, spanR, breakR, groupBy, group,

-- ** Searching
elem, notElem, find, findIndex, findIndexR, findIndices, elemIndex, elemIndices,
Expand Down Expand Up @@ -1178,16 +1178,62 @@ unstablePartition = G.unstablePartition

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Storable as VS
-- >>> VS.span (<4) $ VS.generate 10 id
-- ([0,1,2,3],[4,5,6,7,8,9])
span :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE span #-}
span = G.span

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Storable as VS
-- >>> VS.break (>4) $ VS.generate 10 id
-- ([0,1,2,3,4],[5,6,7,8,9])
break :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE break #-}
break = G.break

-- | /O(n)/ Split the vector into the longest prefix of elements that satisfy
-- the predicate and the rest without copying.
--
-- Does not fuse.
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Storable as VS
-- >>> VS.spanR (>4) $ VS.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
spanR :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE spanR #-}
spanR = G.spanR

-- | /O(n)/ Split the vector into the longest prefix of elements that do not
-- satisfy the predicate and the rest without copying.
--
-- Does not fuse.
--
-- @since NEXT_VERSION
--
-- ==== __Examples__
--
-- >>> import qualified Data.Vector.Storable as VS
-- >>> VS.breakR (<5) $ VS.generate 10 id
-- ([5,6,7,8,9],[0,1,2,3,4])
breakR :: Storable a => (a -> Bool) -> Vector a -> (Vector a, Vector a)
{-# INLINE breakR #-}
breakR = G.breakR

-- | /O(n)/ Split a vector into a list of slices, using a predicate function.
--
-- The concatenation of this list of slices is equal to the argument vector,
Expand Down Expand Up @@ -1998,4 +2044,4 @@ unsafeWith :: Storable a => Vector a -> (Ptr a -> IO b) -> IO b
unsafeWith (Vector _ fp) = withForeignPtr fp

-- $setup
-- >>> import Prelude (Bool(..), Double, ($), (+), (/), succ, even, min, max)
-- >>> import Prelude (Bool(..), Double, ($), (+), (/), succ, even, min, max, id, Ord(..))
Loading
Loading