From 0fecf81c3e1221dabb55f62d7417fc07056e09fc Mon Sep 17 00:00:00 2001 From: David Feuer Date: Fri, 17 Mar 2023 00:55:06 -0400 Subject: [PATCH] Use Data.Ord.Down We used to use our own `Down` newtype because the one in `Data.Ord` had a somewhat lousy `Ord` instance. That has since been corrected, so we can just do what everyone else does. --- CHANGELOG.md | 5 +++ src/BinomialQueue/Max.hs | 35 ++++++++-------- src/Data/PQueue/Internals/Down.hs | 40 ++++-------------- src/Data/PQueue/Max.hs | 29 ++++++------- src/Data/PQueue/Prio/Max/Internals.hs | 59 ++++++++++++++------------- 5 files changed, 75 insertions(+), 93 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae7ac82..8c034df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Revision history for pqueue +## 1.5.0 + + * Remove `Data.PQueue.Internals.Down.Down`. Use the usual `Data.Ord.Down` + instead. + ## 1.4.3.0 -- 2022-10-30 * Add instances for [indexed-traversable](https://hackage.haskell.org/package/indexed-traversable). diff --git a/src/BinomialQueue/Max.hs b/src/BinomialQueue/Max.hs index 7622f89..b878f56 100644 --- a/src/BinomialQueue/Max.hs +++ b/src/BinomialQueue/Max.hs @@ -98,7 +98,8 @@ import Data.Semigroup (Semigroup((<>))) import qualified Data.List as List import qualified BinomialQueue.Min as MinQ -import Data.PQueue.Internals.Down +import Data.PQueue.Internals.Down (getDown) +import Data.Ord (Down (..)) #ifdef __GLASGOW_HASKELL__ import GHC.Exts (build) @@ -115,7 +116,7 @@ findMax = fromMaybe (error "Error: findMax called on empty queue") . getMax -- | \(O(1)\). The top (maximum) element of the queue, if there is one. getMax :: Ord a => MaxQueue a -> Maybe a -getMax (MaxQueue q) = unDown <$> MinQ.getMin q +getMax (MaxQueue q) = getDown <$> MinQ.getMin q -- | \(O(\log n)\). Deletes the maximum element. If the queue is empty, does nothing. deleteMax :: Ord a => MaxQueue a -> MaxQueue a @@ -142,19 +143,19 @@ q !! n = (List.!!) (toDescList q) n -- | 'takeWhile', applied to a predicate @p@ and a queue @queue@, returns the -- longest prefix (possibly empty) of @queue@ of elements that satisfy @p@. takeWhile :: Ord a => (a -> Bool) -> MaxQueue a -> [a] -takeWhile p = fmap unDown . MinQ.takeWhile (p . unDown) . unMaxQueue +takeWhile p = fmap getDown . MinQ.takeWhile (p . getDown) . unMaxQueue -- | 'dropWhile' @p queue@ returns the queue remaining after 'takeWhile' @p queue@. dropWhile :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a -dropWhile p = MaxQueue . MinQ.dropWhile (p . unDown) . unMaxQueue +dropWhile p = MaxQueue . MinQ.dropWhile (p . getDown) . unMaxQueue -- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where -- first element is longest prefix (possibly empty) of @queue@ of elements that -- satisfy @p@ and second element is the remainder of the queue. span :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a) span p (MaxQueue queue) - | (front, rear) <- MinQ.span (p . unDown) queue - = (fmap unDown front, MaxQueue rear) + | (front, rear) <- MinQ.span (p . getDown) queue + = (fmap getDown front, MaxQueue rear) -- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where -- first element is longest prefix (possibly empty) of @queue@ of elements that @@ -177,11 +178,11 @@ drop n (MaxQueue queue) = MaxQueue (MinQ.drop n queue) splitAt :: Ord a => Int -> MaxQueue a -> ([a], MaxQueue a) splitAt n (MaxQueue queue) | (l, r) <- MinQ.splitAt n queue - = (fmap unDown l, MaxQueue r) + = (fmap getDown l, MaxQueue r) -- | \(O(n)\). Returns the queue with all elements not satisfying @p@ removed. filter :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a -filter p = MaxQueue . MinQ.filter (p . unDown) . unMaxQueue +filter p = MaxQueue . MinQ.filter (p . getDown) . unMaxQueue -- | \(O(n)\). Returns a pair where the first queue contains all elements satisfying @p@, and the second queue -- contains all elements not satisfying @p@. @@ -189,7 +190,7 @@ partition :: Ord a => (a -> Bool) -> MaxQueue a -> (MaxQueue a, MaxQueue a) partition p = go . unMaxQueue where go queue - | (l, r) <- MinQ.partition (p . unDown) queue + | (l, r) <- MinQ.partition (p . getDown) queue = (MaxQueue l, MaxQueue r) -- | \(O(n)\). Creates a new priority queue containing the images of the elements of this queue. @@ -202,13 +203,13 @@ map f = MaxQueue . MinQ.map (fmap f) . unMaxQueue -- -- If the order of the elements is irrelevant, consider using 'toListU'. toList :: Ord a => MaxQueue a -> [a] -toList = fmap unDown . MinQ.toAscList . unMaxQueue +toList = fmap getDown . MinQ.toAscList . unMaxQueue toAscList :: Ord a => MaxQueue a -> [a] -toAscList = fmap unDown . MinQ.toDescList . unMaxQueue +toAscList = fmap getDown . MinQ.toDescList . unMaxQueue toDescList :: Ord a => MaxQueue a -> [a] -toDescList = fmap unDown . MinQ.toAscList . unMaxQueue +toDescList = fmap getDown . MinQ.toAscList . unMaxQueue -- | \(O(n \log n)\). Performs a right fold on the elements of a priority queue in descending order. foldrDesc :: Ord a => (a -> b -> b) -> b -> MaxQueue a -> b @@ -245,7 +246,7 @@ elemsU = toListU -- | Convert to a list in an arbitrary order. toListU :: MaxQueue a -> [a] -toListU = fmap unDown . MinQ.toListU . unMaxQueue +toListU = fmap getDown . MinQ.toListU . unMaxQueue -- | Get the number of elements in a 'MaxQueue'. size :: MaxQueue a -> Int @@ -255,7 +256,7 @@ empty :: MaxQueue a empty = MaxQueue MinQ.empty foldMapU :: Monoid m => (a -> m) -> MaxQueue a -> m -foldMapU f = MinQ.foldMapU (f . unDown) . unMaxQueue +foldMapU f = MinQ.foldMapU (f . getDown) . unMaxQueue seqSpine :: MaxQueue a -> b -> b seqSpine = MinQ.seqSpine . unMaxQueue @@ -267,7 +268,7 @@ foldlU' :: (b -> a -> b) -> b -> MaxQueue a -> b foldlU' f b = MinQ.foldlU' (\acc (Down a) -> f acc a) b . unMaxQueue foldrU :: (a -> b -> b) -> b -> MaxQueue a -> b -foldrU c n = MinQ.foldrU (c . unDown) n . unMaxQueue +foldrU c n = MinQ.foldrU (c . getDown) n . unMaxQueue null :: MaxQueue a -> Bool null = MinQ.null . unMaxQueue @@ -276,13 +277,13 @@ singleton :: a -> MaxQueue a singleton = MaxQueue . MinQ.singleton . Down mapMaybe :: Ord b => (a -> Maybe b) -> MaxQueue a -> MaxQueue b -mapMaybe f = MaxQueue . MinQ.mapMaybe (fmap Down . f . unDown) . unMaxQueue +mapMaybe f = MaxQueue . MinQ.mapMaybe (fmap Down . f . getDown) . unMaxQueue insert :: Ord a => a -> MaxQueue a -> MaxQueue a insert a (MaxQueue q) = MaxQueue (MinQ.insert (Down a) q) mapEither :: (Ord b, Ord c) => (a -> Either b c) -> MaxQueue a -> (MaxQueue b, MaxQueue c) -mapEither f (MaxQueue q) = case MinQ.mapEither (bimap Down Down . f . unDown) q of +mapEither f (MaxQueue q) = case MinQ.mapEither (bimap Down Down . f . getDown) q of (l, r) -> (MaxQueue l, MaxQueue r) union :: Ord a => MaxQueue a -> MaxQueue a -> MaxQueue a diff --git a/src/Data/PQueue/Internals/Down.hs b/src/Data/PQueue/Internals/Down.hs index 66a2837..ed5e99a 100644 --- a/src/Data/PQueue/Internals/Down.hs +++ b/src/Data/PQueue/Internals/Down.hs @@ -1,37 +1,11 @@ {-# LANGUAGE CPP #-} -{-# LANGUAGE BangPatterns #-} -module Data.PQueue.Internals.Down where +module Data.PQueue.Internals.Down + ( getDown + ) where +import Data.Ord (Down (..)) -import Control.DeepSeq (NFData(rnf)) -import Data.Foldable (Foldable (..)) - -#if __GLASGOW_HASKELL__ -import Data.Data (Data) -#endif - -newtype Down a = Down { unDown :: a } -#if __GLASGOW_HASKELL__ - deriving (Eq, Data) -#else - deriving (Eq) +#if !MIN_VERSION_base(4,14,0) +getDown :: Down a -> a +getDown (Down a) = a #endif - -instance NFData a => NFData (Down a) where - rnf (Down a) = rnf a - -instance Ord a => Ord (Down a) where - Down a `compare` Down b = b `compare` a - Down a <= Down b = b <= a - Down a >= Down b = b >= a - Down a < Down b = b < a - Down a > Down b = b > a - -instance Functor Down where - fmap f (Down a) = Down (f a) - -instance Foldable Down where - foldr f z (Down a) = a `f` z - foldl f z (Down a) = z `f` a - foldr' f !z (Down a) = a `f` z - foldl' f !z (Down a) = z `f` a diff --git a/src/Data/PQueue/Max.hs b/src/Data/PQueue/Max.hs index 6a81739..32aa8de 100644 --- a/src/Data/PQueue/Max.hs +++ b/src/Data/PQueue/Max.hs @@ -94,7 +94,8 @@ import Data.Foldable (foldl') import qualified Data.PQueue.Min as Min import qualified Data.PQueue.Prio.Max.Internals as Prio -import Data.PQueue.Internals.Down (Down(..)) +import Data.PQueue.Internals.Down (getDown) +import Data.Ord (Down(..)) import Prelude hiding (null, map, take, drop, takeWhile, dropWhile, splitAt, span, break, (!!), filter) @@ -171,7 +172,7 @@ findMax = fromMaybe (error "Error: findMax called on empty queue") . getMax -- | \(O(1)\). The top (maximum) element of the queue, if there is one. getMax :: MaxQueue a -> Maybe a -getMax (MaxQ q) = unDown <$> Min.getMin q +getMax (MaxQ q) = getDown <$> Min.getMin q -- | \(O(\log n)\). Deletes the maximum element of the queue. Does nothing on an empty queue. deleteMax :: Ord a => MaxQueue a -> MaxQueue a @@ -210,7 +211,7 @@ unions qs = MaxQ (Min.unions [q | MaxQ q <- qs]) -- | \(O(k \log n)\)/. Returns the @(k+1)@th largest element of the queue. (!!) :: Ord a => MaxQueue a -> Int -> a -MaxQ q !! n = unDown ((Min.!!) q n) +MaxQ q !! n = getDown ((Min.!!) q n) {-# INLINE take #-} -- | \(O(k \log n)\)/. Returns the list of the @k@ largest elements of the queue, in descending order, or @@ -224,25 +225,25 @@ drop k (MaxQ q) = MaxQ (Min.drop k q) -- | \(O(k \log n)\)/. Equivalent to @(take k queue, drop k queue)@. splitAt :: Ord a => Int -> MaxQueue a -> ([a], MaxQueue a) -splitAt k (MaxQ q) = (fmap unDown xs, MaxQ q') where +splitAt k (MaxQ q) = (fmap getDown xs, MaxQ q') where (xs, q') = Min.splitAt k q -- | 'takeWhile', applied to a predicate @p@ and a queue @queue@, returns the -- longest prefix (possibly empty) of @queue@ of elements that satisfy @p@. takeWhile :: Ord a => (a -> Bool) -> MaxQueue a -> [a] -takeWhile p (MaxQ q) = fmap unDown (Min.takeWhile (p . unDown) q) +takeWhile p (MaxQ q) = fmap getDown (Min.takeWhile (p . getDown) q) -- | 'dropWhile' @p queue@ returns the queue remaining after 'takeWhile' @p queue@. dropWhile :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a -dropWhile p (MaxQ q) = MaxQ (Min.dropWhile (p . unDown) q) +dropWhile p (MaxQ q) = MaxQ (Min.dropWhile (p . getDown) q) -- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where -- first element is longest prefix (possibly empty) of @queue@ of elements that -- satisfy @p@ and second element is the remainder of the queue. -- span :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a) -span p (MaxQ q) = (fmap unDown xs, MaxQ q') where - (xs, q') = Min.span (p . unDown) q +span p (MaxQ q) = (fmap getDown xs, MaxQ q') where + (xs, q') = Min.span (p . getDown) q -- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where -- first element is longest prefix (possibly empty) of @queue@ of elements that @@ -252,13 +253,13 @@ break p = span (not . p) -- | \(O(n)\). Returns a queue of those elements which satisfy the predicate. filter :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a -filter p (MaxQ q) = MaxQ (Min.filter (p . unDown) q) +filter p (MaxQ q) = MaxQ (Min.filter (p . getDown) q) -- | \(O(n)\). Returns a pair of queues, where the left queue contains those elements that satisfy the predicate, -- and the right queue contains those that do not. partition :: Ord a => (a -> Bool) -> MaxQueue a -> (MaxQueue a, MaxQueue a) partition p (MaxQ q) = (MaxQ q0, MaxQ q1) - where (q0, q1) = Min.partition (p . unDown) q + where (q0, q1) = Min.partition (p . getDown) q -- | \(O(n)\). Maps a function over the elements of the queue, and collects the 'Just' values. mapMaybe :: Ord b => (a -> Maybe b) -> MaxQueue a -> MaxQueue b @@ -267,7 +268,7 @@ mapMaybe f (MaxQ q) = MaxQ (Min.mapMaybe (\(Down x) -> Down <$> f x) q) -- | \(O(n)\). Maps a function over the elements of the queue, and separates the 'Left' and 'Right' values. mapEither :: (Ord b, Ord c) => (a -> Either b c) -> MaxQueue a -> (MaxQueue b, MaxQueue c) mapEither f (MaxQ q) = (MaxQ q0, MaxQ q1) - where (q0, q1) = Min.mapEither (either (Left . Down) (Right . Down) . f . unDown) q + where (q0, q1) = Min.mapEither (either (Left . Down) (Right . Down) . f . getDown) q -- | \(O(n)\). Creates a new priority queue containing the images of the elements of this queue. -- Equivalent to @'fromList' . 'Data.List.map' f . toList@. @@ -287,7 +288,7 @@ foldrU f z (MaxQ q) = Min.foldrU (flip (foldr f)) z q -- -- @since 1.4.2 foldMapU :: Monoid m => (a -> m) -> MaxQueue a -> m -foldMapU f (MaxQ q) = Min.foldMapU (f . unDown) q +foldMapU f (MaxQ q) = Min.foldMapU (f . getDown) q -- | \(O(n)\). Unordered left fold on a priority queue. This is rarely -- what you want; 'foldrU' and 'foldlU'' are more likely to perform @@ -309,7 +310,7 @@ elemsU = toListU {-# INLINE toListU #-} -- | \(O(n)\). Returns a list of the elements of the priority queue, in no particular order. toListU :: MaxQueue a -> [a] -toListU (MaxQ q) = fmap unDown (Min.toListU q) +toListU (MaxQ q) = fmap getDown (Min.toListU q) -- | \(O(n \log n)\). Performs a right-fold on the elements of a priority queue in ascending order. -- @'foldrAsc' f z q == 'foldlDesc' (flip f) z q@. @@ -346,7 +347,7 @@ toDescList q = build (\c nil -> foldrDesc c nil q) -- -- If the order of the elements is irrelevant, consider using 'toListU'. toList :: Ord a => MaxQueue a -> [a] -toList (MaxQ q) = fmap unDown (Min.toList q) +toList (MaxQ q) = fmap getDown (Min.toList q) {-# INLINE fromAscList #-} -- | \(O(n)\). Constructs a priority queue from an ascending list. /Warning/: Does not check the precondition. diff --git a/src/Data/PQueue/Prio/Max/Internals.hs b/src/Data/PQueue/Prio/Max/Internals.hs index 701bb28..6290ac2 100644 --- a/src/Data/PQueue/Prio/Max/Internals.hs +++ b/src/Data/PQueue/Prio/Max/Internals.hs @@ -106,7 +106,8 @@ module Data.PQueue.Prio.Max.Internals ( where import Data.Maybe (fromMaybe) -import Data.PQueue.Internals.Down +import Data.PQueue.Internals.Down (getDown) +import Data.Ord (Down (..)) import Data.PQueue.Prio.Internals (MinPQueue) import qualified Data.PQueue.Prio.Internals as PrioInternals import Control.DeepSeq (NFData(rnf)) @@ -277,14 +278,14 @@ adjustMaxA = adjustMaxWithKeyA . const -- | \(O(1)\). Alter the value at the maximum key. If the queue is empty, does nothing. adjustMaxWithKey :: (k -> a -> a) -> MaxPQueue k a -> MaxPQueue k a -adjustMaxWithKey f (MaxPQ q) = MaxPQ (Q.adjustMinWithKey (f . unDown) q) +adjustMaxWithKey f (MaxPQ q) = MaxPQ (Q.adjustMinWithKey (f . getDown) q) -- | \(O(1)\) per operation. Alter the value at the maximum key in an -- 'Applicative' context. If the queue is empty, does nothing. -- -- @since 1.4.2 adjustMaxWithKeyA :: Applicative f => (k -> a -> f a) -> MaxPQueue k a -> f (MaxPQueue k a) -adjustMaxWithKeyA f (MaxPQ q) = PrioInternals.adjustMinWithKeyA' MaxPQ (f . unDown) q +adjustMaxWithKeyA f (MaxPQ q) = PrioInternals.adjustMinWithKeyA' MaxPQ (f . getDown) q -- | \(O(\log n)\). (Actually \(O(1)\) if there's no deletion.) Update the value at the maximum key. -- If the queue is empty, does nothing. @@ -302,7 +303,7 @@ updateMaxA = updateMaxWithKeyA . const -- | \(O(\log n)\). (Actually \(O(1)\) if there's no deletion.) Update the value at the maximum key. -- If the queue is empty, does nothing. updateMaxWithKey :: Ord k => (k -> a -> Maybe a) -> MaxPQueue k a -> MaxPQueue k a -updateMaxWithKey f (MaxPQ q) = MaxPQ (Q.updateMinWithKey (f . unDown) q) +updateMaxWithKey f (MaxPQ q) = MaxPQ (Q.updateMinWithKey (f . getDown) q) -- | \(O(\log n)\) per operation. (Actually \(O(1)\) if there's no deletion.) Update -- the value at the maximum key in an 'Applicative' context. If the queue is @@ -310,7 +311,7 @@ updateMaxWithKey f (MaxPQ q) = MaxPQ (Q.updateMinWithKey (f . unDown) q) -- -- @since 1.4.2 updateMaxWithKeyA :: (Applicative f, Ord k) => (k -> a -> f (Maybe a)) -> MaxPQueue k a -> f (MaxPQueue k a) -updateMaxWithKeyA f (MaxPQ q) = PrioInternals.updateMinWithKeyA' MaxPQ (f . unDown) q +updateMaxWithKeyA f (MaxPQ q) = PrioInternals.updateMinWithKeyA' MaxPQ (f . getDown) q -- | \(O(\log n)\). Retrieves the value associated with the maximum key of the queue, and the queue -- stripped of that element, or 'Nothing' if passed an empty queue. @@ -332,7 +333,7 @@ map = mapWithKey . const -- | \(O(n)\). Map a function over all values in the queue. mapWithKey :: (k -> a -> b) -> MaxPQueue k a -> MaxPQueue k b -mapWithKey f (MaxPQ q) = MaxPQ (Q.mapWithKey (f . unDown) q) +mapWithKey f (MaxPQ q) = MaxPQ (Q.mapWithKey (f . getDown) q) -- | \(O(n)\). Map a function over all values in the queue. mapKeys :: Ord k' => (k -> k') -> MaxPQueue k a -> MaxPQueue k' a @@ -349,14 +350,14 @@ mapKeysMonotonic f (MaxPQ q) = MaxPQ (Q.mapKeysMonotonic (fmap f) q) -- -- If you do not care about the traversal order, consider using 'foldrWithKeyU'. foldrWithKey :: Ord k => (k -> a -> b -> b) -> b -> MaxPQueue k a -> b -foldrWithKey f z (MaxPQ q) = Q.foldrWithKey (f . unDown) z q +foldrWithKey f z (MaxPQ q) = Q.foldrWithKey (f . getDown) z q -- | \(O(n \log n)\). Fold the keys and values in the map, such that -- @'foldlWithKey' f z q == 'List.foldl' ('uncurry' . f) z ('toDescList' q)@. -- -- If you do not care about the traversal order, consider using 'foldlWithKeyU'. foldlWithKey :: Ord k => (b -> k -> a -> b) -> b -> MaxPQueue k a -> b -foldlWithKey f z0 (MaxPQ q) = Q.foldlWithKey (\z -> f z . unDown) z0 q +foldlWithKey f z0 (MaxPQ q) = Q.foldlWithKey (\z -> f z . getDown) z0 q -- | \(O(n \log n)\). Traverses the elements of the queue in descending order by key. -- (@'traverseWithKey' f q == 'fromDescList' <$> 'traverse' ('uncurry' f) ('toDescList' q)@) @@ -365,7 +366,7 @@ foldlWithKey f z0 (MaxPQ q) = Q.foldlWithKey (\z -> f z . unDown) z0 q -- -- If you are working in a strict monad, consider using 'mapMWithKey'. traverseWithKey :: (Ord k, Applicative f) => (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b) -traverseWithKey f (MaxPQ q) = MaxPQ <$> Q.traverseWithKey (f . unDown) q +traverseWithKey f (MaxPQ q) = MaxPQ <$> Q.traverseWithKey (f . getDown) q -- | A strictly accumulating version of 'traverseWithKey'. This works well in -- 'IO' and strict @State@, and is likely what you want for other "strict" monads, @@ -387,7 +388,7 @@ insertMin' k a (MaxPQ q) = MaxPQ (PrioInternals.insertMax' (Down k) a q) -- | \(O(k \log n)\)/. Takes the first @k@ (key, value) pairs in the queue, or the first @n@ if @k >= n@. -- (@'take' k q == 'List.take' k ('toDescList' q)@) take :: Ord k => Int -> MaxPQueue k a -> [(k, a)] -take k (MaxPQ q) = fmap (first' unDown) (Q.take k q) +take k (MaxPQ q) = fmap (first' getDown) (Q.take k q) -- | \(O(k \log n)\)/. Deletes the first @k@ (key, value) pairs in the queue, or returns an empty queue if @k >= n@. drop :: Ord k => Int -> MaxPQueue k a -> MaxPQueue k a @@ -396,7 +397,7 @@ drop k (MaxPQ q) = MaxPQ (Q.drop k q) -- | \(O(k \log n)\)/. Equivalent to @('take' k q, 'drop' k q)@. splitAt :: Ord k => Int -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a) splitAt k (MaxPQ q) = case Q.splitAt k q of - (xs, q') -> (fmap (first' unDown) xs, MaxPQ q') + (xs, q') -> (fmap (first' getDown) xs, MaxPQ q') -- | Takes the longest possible prefix of elements satisfying the predicate. -- (@'takeWhile' p q == 'List.takeWhile' (p . 'snd') ('toDescList' q)@) @@ -406,7 +407,7 @@ takeWhile = takeWhileWithKey . const -- | Takes the longest possible prefix of elements satisfying the predicate. -- (@'takeWhile' p q == 'List.takeWhile' (uncurry p) ('toDescList' q)@) takeWhileWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> [(k, a)] -takeWhileWithKey p (MaxPQ q) = fmap (first' unDown) (Q.takeWhileWithKey (p . unDown) q) +takeWhileWithKey p (MaxPQ q) = fmap (first' getDown) (Q.takeWhileWithKey (p . getDown) q) -- | Removes the longest possible prefix of elements satisfying the predicate. dropWhile :: Ord k => (a -> Bool) -> MaxPQueue k a -> MaxPQueue k a @@ -414,7 +415,7 @@ dropWhile = dropWhileWithKey . const -- | Removes the longest possible prefix of elements satisfying the predicate. dropWhileWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a -dropWhileWithKey p (MaxPQ q) = MaxPQ (Q.dropWhileWithKey (p . unDown) q) +dropWhileWithKey p (MaxPQ q) = MaxPQ (Q.dropWhileWithKey (p . getDown) q) -- | Equivalent to @('takeWhile' p q, 'dropWhile' p q)@. span :: Ord k => (a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a) @@ -426,13 +427,13 @@ break = breakWithKey . const -- | Equivalent to @'spanWithKey' (\k a -> 'not' (p k a)) q@. spanWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a) -spanWithKey p (MaxPQ q) = case Q.spanWithKey (p . unDown) q of - (xs, q') -> (fmap (first' unDown) xs, MaxPQ q') +spanWithKey p (MaxPQ q) = case Q.spanWithKey (p . getDown) q of + (xs, q') -> (fmap (first' getDown) xs, MaxPQ q') -- | Equivalent to @'spanWithKey' (\k a -> 'not' (p k a)) q@. breakWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> ([(k, a)], MaxPQueue k a) -breakWithKey p (MaxPQ q) = case Q.breakWithKey (p . unDown) q of - (xs, q') -> (fmap (first' unDown) xs, MaxPQ q') +breakWithKey p (MaxPQ q) = case Q.breakWithKey (p . getDown) q of + (xs, q') -> (fmap (first' getDown) xs, MaxPQ q') -- | \(O(n)\). Filter all values that satisfy the predicate. filter :: Ord k => (a -> Bool) -> MaxPQueue k a -> MaxPQueue k a @@ -440,7 +441,7 @@ filter = filterWithKey . const -- | \(O(n)\). Filter all values that satisfy the predicate. filterWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> MaxPQueue k a -filterWithKey p (MaxPQ q) = MaxPQ (Q.filterWithKey (p . unDown) q) +filterWithKey p (MaxPQ q) = MaxPQ (Q.filterWithKey (p . getDown) q) -- | \(O(n)\). Partition the queue according to a predicate. The first queue contains all elements -- which satisfy the predicate, the second all elements that fail the predicate. @@ -450,7 +451,7 @@ partition = partitionWithKey . const -- | \(O(n)\). Partition the queue according to a predicate. The first queue contains all elements -- which satisfy the predicate, the second all elements that fail the predicate. partitionWithKey :: Ord k => (k -> a -> Bool) -> MaxPQueue k a -> (MaxPQueue k a, MaxPQueue k a) -partitionWithKey p (MaxPQ q) = case Q.partitionWithKey (p . unDown) q of +partitionWithKey p (MaxPQ q) = case Q.partitionWithKey (p . getDown) q of (q1, q0) -> (MaxPQ q1, MaxPQ q0) -- | \(O(n)\). Map values and collect the 'Just' results. @@ -459,7 +460,7 @@ mapMaybe = mapMaybeWithKey . const -- | \(O(n)\). Map values and collect the 'Just' results. mapMaybeWithKey :: Ord k => (k -> a -> Maybe b) -> MaxPQueue k a -> MaxPQueue k b -mapMaybeWithKey f (MaxPQ q) = MaxPQ (Q.mapMaybeWithKey (f . unDown) q) +mapMaybeWithKey f (MaxPQ q) = MaxPQ (Q.mapMaybeWithKey (f . getDown) q) -- | \(O(n)\). Map values and separate the 'Left' and 'Right' results. mapEither :: Ord k => (a -> Either b c) -> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c) @@ -467,7 +468,7 @@ mapEither = mapEitherWithKey . const -- | \(O(n)\). Map values and separate the 'Left' and 'Right' results. mapEitherWithKey :: Ord k => (k -> a -> Either b c) -> MaxPQueue k a -> (MaxPQueue k b, MaxPQueue k c) -mapEitherWithKey f (MaxPQ q) = case Q.mapEitherWithKey (f . unDown) q of +mapEitherWithKey f (MaxPQ q) = case Q.mapEitherWithKey (f . getDown) q of (qL, qR) -> (MaxPQ qL, MaxPQ qR) -- | \(O(n)\). Build a priority queue from the list of (key, value) pairs. @@ -496,11 +497,11 @@ assocs = toDescList -- | \(O(n \log n)\). Return all (key, value) pairs in ascending order by key. toAscList :: Ord k => MaxPQueue k a -> [(k, a)] -toAscList (MaxPQ q) = fmap (first' unDown) (Q.toDescList q) +toAscList (MaxPQ q) = fmap (first' getDown) (Q.toDescList q) -- | \(O(n \log n)\). Return all (key, value) pairs in descending order by key. toDescList :: Ord k => MaxPQueue k a -> [(k, a)] -toDescList (MaxPQ q) = fmap (first' unDown) (Q.toAscList q) +toDescList (MaxPQ q) = fmap (first' getDown) (Q.toAscList q) -- | \(O(n \log n)\). Equivalent to 'toDescList'. -- @@ -514,13 +515,13 @@ foldrU = foldrWithKeyU . const -- | \(O(n)\). An unordered right fold over the elements of the queue, in no particular order. foldrWithKeyU :: (k -> a -> b -> b) -> b -> MaxPQueue k a -> b -foldrWithKeyU f z (MaxPQ q) = Q.foldrWithKeyU (f . unDown) z q +foldrWithKeyU f z (MaxPQ q) = Q.foldrWithKeyU (f . getDown) z q -- | \(O(n)\). An unordered monoidal fold over the elements of the queue, in no particular order. -- -- @since 1.4.2 foldMapWithKeyU :: Monoid m => (k -> a -> m) -> MaxPQueue k a -> m -foldMapWithKeyU f (MaxPQ q) = Q.foldMapWithKeyU (f . unDown) q +foldMapWithKeyU f (MaxPQ q) = Q.foldMapWithKeyU (f . getDown) q -- | \(O(n)\). An unordered left fold over the elements of the queue, in no -- particular order. This is rarely what you want; 'foldrU' and 'foldlU'' are @@ -539,13 +540,13 @@ foldlU' f = foldlWithKeyU' (const . f) -- particular order. This is rarely what you want; 'foldrWithKeyU' and -- 'foldlWithKeyU'' are more likely to perform well. foldlWithKeyU :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b -foldlWithKeyU f z0 (MaxPQ q) = Q.foldlWithKeyU (\z -> f z . unDown) z0 q +foldlWithKeyU f z0 (MaxPQ q) = Q.foldlWithKeyU (\z -> f z . getDown) z0 q -- | \(O(n)\). An unordered left fold over the elements of the queue, in no particular order. -- -- @since 1.4.2 foldlWithKeyU' :: (b -> k -> a -> b) -> b -> MaxPQueue k a -> b -foldlWithKeyU' f z0 (MaxPQ q) = Q.foldlWithKeyU' (\z -> f z . unDown) z0 q +foldlWithKeyU' f z0 (MaxPQ q) = Q.foldlWithKeyU' (\z -> f z . getDown) z0 q -- | \(O(n)\). An unordered traversal over a priority queue, in no particular order. -- While there is no guarantee in which order the elements are traversed, the resulting @@ -557,7 +558,7 @@ traverseU = traverseWithKeyU . const -- While there is no guarantee in which order the elements are traversed, the resulting -- priority queue will be perfectly valid. traverseWithKeyU :: (Applicative f) => (k -> a -> f b) -> MaxPQueue k a -> f (MaxPQueue k b) -traverseWithKeyU f (MaxPQ q) = MaxPQ <$> Q.traverseWithKeyU (f . unDown) q +traverseWithKeyU f (MaxPQ q) = MaxPQ <$> Q.traverseWithKeyU (f . getDown) q -- | \(O(n)\). Return all keys of the queue in no particular order. keysU :: MaxPQueue k a -> [k] @@ -573,7 +574,7 @@ assocsU = toListU -- | \(O(n)\). Returns all (key, value) pairs in the queue in no particular order. toListU :: MaxPQueue k a -> [(k, a)] -toListU (MaxPQ q) = fmap (first' unDown) (Q.toListU q) +toListU (MaxPQ q) = fmap (first' getDown) (Q.toListU q) -- | \(O(\log n)\). @seqSpine q r@ forces the spine of @q@ and returns @r@. --