Skip to content

Commit be5ecf6

Browse files
committed
Add Eq1 and Ord1 Vector
1 parent 71c1a69 commit be5ecf6

File tree

4 files changed

+55
-21
lines changed

4 files changed

+55
-21
lines changed

Data/Vector.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ import Prelude hiding ( length, null,
191191
enumFromTo, enumFromThenTo,
192192
mapM, mapM_, sequence, sequence_ )
193193

194+
#if MIN_VERSION_base(4,9,0)
195+
import Data.Functor.Classes (Eq1 (..), Ord1 (..))
196+
#endif
197+
194198
import Data.Typeable ( Typeable )
195199
import Data.Data ( Data(..) )
196200
import Text.Read ( Read(..), readListPrecDefault )
@@ -293,6 +297,14 @@ instance Ord a => Ord (Vector a) where
293297
{-# INLINE (>=) #-}
294298
xs >= ys = Bundle.cmp (G.stream xs) (G.stream ys) /= LT
295299

300+
#if MIN_VERSION_base(4,9,0)
301+
instance Eq1 Vector where
302+
liftEq eq xs ys = Bundle.eqBy eq (G.stream xs) (G.stream ys)
303+
304+
instance Ord1 Vector where
305+
liftCompare cmp xs ys = Bundle.cmpBy cmp (G.stream xs) (G.stream ys)
306+
#endif
307+
296308
instance Semigroup (Vector a) where
297309
{-# INLINE (<>) #-}
298310
(<>) = (++)

Data/Vector/Fusion/Bundle.hs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ module Data.Vector.Fusion.Bundle (
7373
-- * Monadic combinators
7474
mapM, mapM_, zipWithM, zipWithM_, filterM, foldM, fold1M, foldM', fold1M',
7575

76-
eq, cmp
76+
eq, cmp, eqBy, cmpBy
7777
) where
7878

7979
import Data.Vector.Generic.Base ( Vector )
@@ -98,6 +98,10 @@ import Prelude hiding ( length, null,
9898
enumFromTo, enumFromThenTo,
9999
mapM, mapM_ )
100100

101+
#if MIN_VERSION_base(4,9,0)
102+
import Data.Functor.Classes (Eq1 (..), Ord1 (..))
103+
#endif
104+
101105
import GHC.Base ( build )
102106

103107
-- Data.Vector.Internal.Check is unused
@@ -486,14 +490,22 @@ scanl1' = M.scanl1'
486490
-- -----------
487491

488492
-- | Check if two 'Bundle's are equal
489-
eq :: Eq a => Bundle v a -> Bundle v a -> Bool
493+
eq :: (Eq a) => Bundle v a -> Bundle v a -> Bool
490494
{-# INLINE eq #-}
491-
eq x y = unId (M.eq x y)
495+
eq = eqBy (==)
496+
497+
eqBy :: (a -> b -> Bool) -> Bundle v a -> Bundle v b -> Bool
498+
{-# INLINE eqBy #-}
499+
eqBy e x y = unId (M.eqBy e x y)
492500

493501
-- | Lexicographically compare two 'Bundle's
494-
cmp :: Ord a => Bundle v a -> Bundle v a -> Ordering
502+
cmp :: (Ord a) => Bundle v a -> Bundle v a -> Ordering
495503
{-# INLINE cmp #-}
496-
cmp x y = unId (M.cmp x y)
504+
cmp = cmpBy compare
505+
506+
cmpBy :: (a -> b -> Ordering) -> Bundle v a -> Bundle v b -> Ordering
507+
{-# INLINE cmpBy #-}
508+
cmpBy c x y = unId (M.cmpBy c x y)
497509

498510
instance Eq a => Eq (M.Bundle Id v a) where
499511
{-# INLINE (==) #-}
@@ -503,6 +515,16 @@ instance Ord a => Ord (M.Bundle Id v a) where
503515
{-# INLINE compare #-}
504516
compare = cmp
505517

518+
#if MIN_VERSION_base(4,9,0)
519+
instance Eq1 (M.Bundle Id v) where
520+
{-# INLINE liftEq #-}
521+
liftEq = eqBy
522+
523+
instance Ord1 (M.Bundle Id v) where
524+
{-# INLINE liftCompare #-}
525+
liftCompare = cmpBy
526+
#endif
527+
506528
-- Monadic combinators
507529
-- -------------------
508530

Data/Vector/Fusion/Bundle/Monadic.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module Data.Vector.Fusion.Bundle.Monadic (
4040
zip, zip3, zip4, zip5, zip6,
4141

4242
-- * Comparisons
43-
eq, cmp,
43+
eqBy, cmpBy,
4444

4545
-- * Filtering
4646
filter, filterM, takeWhile, takeWhileM, dropWhile, dropWhileM,
@@ -424,14 +424,14 @@ zip6 = zipWith6 (,,,,,)
424424
-- -----------
425425

426426
-- | Check if two 'Bundle's are equal
427-
eq :: (Monad m, Eq a) => Bundle m v a -> Bundle m v a -> m Bool
428-
{-# INLINE_FUSED eq #-}
429-
eq x y = sElems x `S.eq` sElems y
427+
eqBy :: (Monad m) => (a -> b -> Bool) -> Bundle m v a -> Bundle m v b -> m Bool
428+
{-# INLINE_FUSED eqBy #-}
429+
eqBy eq x y = S.eqBy eq (sElems x) (sElems y)
430430

431431
-- | Lexicographically compare two 'Bundle's
432-
cmp :: (Monad m, Ord a) => Bundle m v a -> Bundle m v a -> m Ordering
433-
{-# INLINE_FUSED cmp #-}
434-
cmp x y = sElems x `S.cmp` sElems y
432+
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Bundle m v a -> Bundle m v b -> m Ordering
433+
{-# INLINE_FUSED cmpBy #-}
434+
cmpBy cmp x y = S.cmpBy cmp (sElems x) (sElems y)
435435

436436
-- Filtering
437437
-- ---------

Data/Vector/Fusion/Stream/Monadic.hs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module Data.Vector.Fusion.Stream.Monadic (
3737
zip, zip3, zip4, zip5, zip6,
3838

3939
-- * Comparisons
40-
eq, cmp,
40+
eqBy, cmpBy,
4141

4242
-- * Filtering
4343
filter, filterM, uniq, mapMaybe, takeWhile, takeWhileM, dropWhile, dropWhileM,
@@ -625,9 +625,9 @@ zip6 = zipWith6 (,,,,,)
625625
-- -----------
626626

627627
-- | Check if two 'Stream's are equal
628-
eq :: (Monad m, Eq a) => Stream m a -> Stream m a -> m Bool
629-
{-# INLINE_FUSED eq #-}
630-
eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
628+
eqBy :: (Monad m) => (a -> b -> Bool) -> Stream m a -> Stream m b -> m Bool
629+
{-# INLINE_FUSED eqBy #-}
630+
eqBy eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
631631
where
632632
eq_loop0 !_ s1 s2 = do
633633
r <- step1 s1
@@ -640,7 +640,7 @@ eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
640640
r <- step2 s2
641641
case r of
642642
Yield y s2'
643-
| x == y -> eq_loop0 SPEC s1 s2'
643+
| eq x y -> eq_loop0 SPEC s1 s2'
644644
| otherwise -> return False
645645
Skip s2' -> eq_loop1 SPEC x s1 s2'
646646
Done -> return False
@@ -653,9 +653,9 @@ eq (Stream step1 t1) (Stream step2 t2) = eq_loop0 SPEC t1 t2
653653
Done -> return True
654654

655655
-- | Lexicographically compare two 'Stream's
656-
cmp :: (Monad m, Ord a) => Stream m a -> Stream m a -> m Ordering
657-
{-# INLINE_FUSED cmp #-}
658-
cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
656+
cmpBy :: (Monad m) => (a -> b -> Ordering) -> Stream m a -> Stream m b -> m Ordering
657+
{-# INLINE_FUSED cmpBy #-}
658+
cmpBy cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
659659
where
660660
cmp_loop0 !_ s1 s2 = do
661661
r <- step1 s1
@@ -667,7 +667,7 @@ cmp (Stream step1 t1) (Stream step2 t2) = cmp_loop0 SPEC t1 t2
667667
cmp_loop1 !_ x s1 s2 = do
668668
r <- step2 s2
669669
case r of
670-
Yield y s2' -> case x `compare` y of
670+
Yield y s2' -> case x `cmp` y of
671671
EQ -> cmp_loop0 SPEC s1 s2'
672672
c -> return c
673673
Skip s2' -> cmp_loop1 SPEC x s1 s2'

0 commit comments

Comments
 (0)