Skip to content

Mark nub and sort functions as INLINE #51

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

Merged
merged 2 commits into from
Feb 5, 2025
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
3 changes: 3 additions & 0 deletions src/Data/Vector/Algorithms.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import qualified Data.Vector.Algorithms.Search as S
-- | The `nub` function which removes duplicate elements from a vector.
nub :: forall v e . (V.Vector v e, Ord e) => v e -> v e
nub = nubBy compare
{-# INLINE nub #-}

-- | A version of `nub` with a custom comparison predicate.
--
Expand All @@ -31,6 +32,7 @@ nubBy cmp vec = runST $ do
destMV <- nubByMut sortUniqBy cmp mv
v <- V.unsafeFreeze destMV
pure (V.force v)
{-# INLINE nubBy #-}

-- | The `nubByMut` function takes in an in-place sort algorithm
-- and uses it to do a de-deduplicated sort. It then uses this to
Expand Down Expand Up @@ -72,3 +74,4 @@ nubByMut alg cmp inp = do
go (srcInd + 1) (destInd + 1)
go 0 0
pure dest
{-# INLINABLE nubByMut #-}
4 changes: 2 additions & 2 deletions src/Data/Vector/Algorithms/AmericanFlag.hs
Original file line number Diff line number Diff line change
Expand Up @@ -244,15 +244,15 @@ sort :: forall e m v. (PrimMonad m, MVector v e, Lexicographic e, Ord e)
sort v = sortBy compare terminate (size p) index v
where p :: Proxy e
p = Proxy
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | A variant on `sort` that returns a vector of unique elements.
sortUniq :: forall e m v. (PrimMonad m, MVector v e, Lexicographic e, Ord e)
=> v (PrimState m) e -> m (v (PrimState m) e)
sortUniq v = sortUniqBy compare terminate (size p) index v
where p :: Proxy e
p = Proxy
{-# INLINABLE sortUniq #-}
{-# INLINE sortUniq #-}

-- | A fully parameterized version of the sorting algorithm. Again, this
-- function takes both radix information and a comparison, because the
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Vector/Algorithms/Heap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ import qualified Data.Vector.Algorithms.Optimal as O
-- | Sorts an entire array using the default ordering.
sort :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m ()
sort = sortBy compare
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | A variant on `sort` that returns a vector of unique elements.
sortUniq :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m (v (PrimState m) e)
sortUniq = sortUniqBy compare
{-# INLINABLE sortUniq #-}
{-# INLINE sortUniq #-}

-- | Sorts an entire array using a custom ordering.
sortBy :: (PrimMonad m, MVector v e) => Comparison e -> v (PrimState m) e -> m ()
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Vector/Algorithms/Insertion.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import qualified Data.Vector.Algorithms.Optimal as O
-- | Sorts an entire array using the default comparison for the type
sort :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m ()
sort = sortBy compare
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | A variant on `sort` that returns a vector of unique elements.
sortUniq :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m (v (PrimState m) e)
sortUniq = sortUniqBy compare
{-# INLINABLE sortUniq #-}
{-# INLINE sortUniq #-}

-- | Sorts an entire array using a given comparison
sortBy :: (PrimMonad m, MVector v e) => Comparison e -> v (PrimState m) e -> m ()
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Vector/Algorithms/Intro.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ import qualified Data.Vector.Algorithms.Heap as H
-- | Sorts an entire array using the default ordering.
sort :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m ()
sort = sortBy compare
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | A variant on `sort` that returns a vector of unique elements.
sortUniq :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m (v (PrimState m) e)
sortUniq = sortUniqBy compare
{-# INLINABLE sortUniq #-}
{-# INLINE sortUniq #-}

-- | A variant on `sortBy` which returns a vector of unique elements.
sortBy :: (PrimMonad m, MVector v e) => Comparison e -> v (PrimState m) e -> m ()
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Vector/Algorithms/Merge.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ import qualified Data.Vector.Algorithms.Insertion as I
-- | Sorts an array using the default comparison.
sort :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m ()
sort = sortBy compare
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | A variant on `sort` that returns a vector of unique elements.
sortUniq :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m (v (PrimState m) e)
sortUniq = sortUniqBy compare
{-# INLINABLE sortUniq #-}
{-# INLINE sortUniq #-}

-- | Sorts an array using a custom comparison.
sortBy :: (PrimMonad m, MVector v e) => Comparison e -> v (PrimState m) e -> m ()
Expand Down
2 changes: 1 addition & 1 deletion src/Data/Vector/Algorithms/Radix.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ sort arr = sortBy (passes e) (size e) radix arr
where
e :: e
e = undefined
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | Radix sorts an array using custom radix information
-- requires the number of passes to fully sort the array,
Expand Down
4 changes: 2 additions & 2 deletions src/Data/Vector/Algorithms/Tim.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ import Data.Vector.Algorithms.Common (uniqueMutableBy)
-- | Sorts an array using the default comparison.
sort :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m ()
sort = sortBy compare
{-# INLINABLE sort #-}
{-# INLINE sort #-}

-- | A variant on `sort` that returns a vector of unique elements.
sortUniq :: (PrimMonad m, MVector v e, Ord e) => v (PrimState m) e -> m (v (PrimState m) e)
sortUniq = sortUniqBy compare
{-# INLINABLE sortUniq #-}
{-# INLINE sortUniq #-}

-- | Sorts an array using a custom comparison.
sortBy :: (PrimMonad m, MVector v e)
Expand Down
Loading