Skip to content

Commit 0fecf81

Browse files
committed
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.
1 parent ceb0081 commit 0fecf81

File tree

5 files changed

+75
-93
lines changed

5 files changed

+75
-93
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Revision history for pqueue
22

3+
## 1.5.0
4+
5+
* Remove `Data.PQueue.Internals.Down.Down`. Use the usual `Data.Ord.Down`
6+
instead.
7+
38
## 1.4.3.0 -- 2022-10-30
49

510
* Add instances for [indexed-traversable](https://hackage.haskell.org/package/indexed-traversable).

src/BinomialQueue/Max.hs

+18-17
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ import Data.Semigroup (Semigroup((<>)))
9898
import qualified Data.List as List
9999

100100
import qualified BinomialQueue.Min as MinQ
101-
import Data.PQueue.Internals.Down
101+
import Data.PQueue.Internals.Down (getDown)
102+
import Data.Ord (Down (..))
102103

103104
#ifdef __GLASGOW_HASKELL__
104105
import GHC.Exts (build)
@@ -115,7 +116,7 @@ findMax = fromMaybe (error "Error: findMax called on empty queue") . getMax
115116

116117
-- | \(O(1)\). The top (maximum) element of the queue, if there is one.
117118
getMax :: Ord a => MaxQueue a -> Maybe a
118-
getMax (MaxQueue q) = unDown <$> MinQ.getMin q
119+
getMax (MaxQueue q) = getDown <$> MinQ.getMin q
119120

120121
-- | \(O(\log n)\). Deletes the maximum element. If the queue is empty, does nothing.
121122
deleteMax :: Ord a => MaxQueue a -> MaxQueue a
@@ -142,19 +143,19 @@ q !! n = (List.!!) (toDescList q) n
142143
-- | 'takeWhile', applied to a predicate @p@ and a queue @queue@, returns the
143144
-- longest prefix (possibly empty) of @queue@ of elements that satisfy @p@.
144145
takeWhile :: Ord a => (a -> Bool) -> MaxQueue a -> [a]
145-
takeWhile p = fmap unDown . MinQ.takeWhile (p . unDown) . unMaxQueue
146+
takeWhile p = fmap getDown . MinQ.takeWhile (p . getDown) . unMaxQueue
146147

147148
-- | 'dropWhile' @p queue@ returns the queue remaining after 'takeWhile' @p queue@.
148149
dropWhile :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a
149-
dropWhile p = MaxQueue . MinQ.dropWhile (p . unDown) . unMaxQueue
150+
dropWhile p = MaxQueue . MinQ.dropWhile (p . getDown) . unMaxQueue
150151

151152
-- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where
152153
-- first element is longest prefix (possibly empty) of @queue@ of elements that
153154
-- satisfy @p@ and second element is the remainder of the queue.
154155
span :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a)
155156
span p (MaxQueue queue)
156-
| (front, rear) <- MinQ.span (p . unDown) queue
157-
= (fmap unDown front, MaxQueue rear)
157+
| (front, rear) <- MinQ.span (p . getDown) queue
158+
= (fmap getDown front, MaxQueue rear)
158159

159160
-- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where
160161
-- first element is longest prefix (possibly empty) of @queue@ of elements that
@@ -177,19 +178,19 @@ drop n (MaxQueue queue) = MaxQueue (MinQ.drop n queue)
177178
splitAt :: Ord a => Int -> MaxQueue a -> ([a], MaxQueue a)
178179
splitAt n (MaxQueue queue)
179180
| (l, r) <- MinQ.splitAt n queue
180-
= (fmap unDown l, MaxQueue r)
181+
= (fmap getDown l, MaxQueue r)
181182

182183
-- | \(O(n)\). Returns the queue with all elements not satisfying @p@ removed.
183184
filter :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a
184-
filter p = MaxQueue . MinQ.filter (p . unDown) . unMaxQueue
185+
filter p = MaxQueue . MinQ.filter (p . getDown) . unMaxQueue
185186

186187
-- | \(O(n)\). Returns a pair where the first queue contains all elements satisfying @p@, and the second queue
187188
-- contains all elements not satisfying @p@.
188189
partition :: Ord a => (a -> Bool) -> MaxQueue a -> (MaxQueue a, MaxQueue a)
189190
partition p = go . unMaxQueue
190191
where
191192
go queue
192-
| (l, r) <- MinQ.partition (p . unDown) queue
193+
| (l, r) <- MinQ.partition (p . getDown) queue
193194
= (MaxQueue l, MaxQueue r)
194195

195196
-- | \(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
202203
--
203204
-- If the order of the elements is irrelevant, consider using 'toListU'.
204205
toList :: Ord a => MaxQueue a -> [a]
205-
toList = fmap unDown . MinQ.toAscList . unMaxQueue
206+
toList = fmap getDown . MinQ.toAscList . unMaxQueue
206207

207208
toAscList :: Ord a => MaxQueue a -> [a]
208-
toAscList = fmap unDown . MinQ.toDescList . unMaxQueue
209+
toAscList = fmap getDown . MinQ.toDescList . unMaxQueue
209210

210211
toDescList :: Ord a => MaxQueue a -> [a]
211-
toDescList = fmap unDown . MinQ.toAscList . unMaxQueue
212+
toDescList = fmap getDown . MinQ.toAscList . unMaxQueue
212213

213214
-- | \(O(n \log n)\). Performs a right fold on the elements of a priority queue in descending order.
214215
foldrDesc :: Ord a => (a -> b -> b) -> b -> MaxQueue a -> b
@@ -245,7 +246,7 @@ elemsU = toListU
245246

246247
-- | Convert to a list in an arbitrary order.
247248
toListU :: MaxQueue a -> [a]
248-
toListU = fmap unDown . MinQ.toListU . unMaxQueue
249+
toListU = fmap getDown . MinQ.toListU . unMaxQueue
249250

250251
-- | Get the number of elements in a 'MaxQueue'.
251252
size :: MaxQueue a -> Int
@@ -255,7 +256,7 @@ empty :: MaxQueue a
255256
empty = MaxQueue MinQ.empty
256257

257258
foldMapU :: Monoid m => (a -> m) -> MaxQueue a -> m
258-
foldMapU f = MinQ.foldMapU (f . unDown) . unMaxQueue
259+
foldMapU f = MinQ.foldMapU (f . getDown) . unMaxQueue
259260

260261
seqSpine :: MaxQueue a -> b -> b
261262
seqSpine = MinQ.seqSpine . unMaxQueue
@@ -267,7 +268,7 @@ foldlU' :: (b -> a -> b) -> b -> MaxQueue a -> b
267268
foldlU' f b = MinQ.foldlU' (\acc (Down a) -> f acc a) b . unMaxQueue
268269

269270
foldrU :: (a -> b -> b) -> b -> MaxQueue a -> b
270-
foldrU c n = MinQ.foldrU (c . unDown) n . unMaxQueue
271+
foldrU c n = MinQ.foldrU (c . getDown) n . unMaxQueue
271272

272273
null :: MaxQueue a -> Bool
273274
null = MinQ.null . unMaxQueue
@@ -276,13 +277,13 @@ singleton :: a -> MaxQueue a
276277
singleton = MaxQueue . MinQ.singleton . Down
277278

278279
mapMaybe :: Ord b => (a -> Maybe b) -> MaxQueue a -> MaxQueue b
279-
mapMaybe f = MaxQueue . MinQ.mapMaybe (fmap Down . f . unDown) . unMaxQueue
280+
mapMaybe f = MaxQueue . MinQ.mapMaybe (fmap Down . f . getDown) . unMaxQueue
280281

281282
insert :: Ord a => a -> MaxQueue a -> MaxQueue a
282283
insert a (MaxQueue q) = MaxQueue (MinQ.insert (Down a) q)
283284

284285
mapEither :: (Ord b, Ord c) => (a -> Either b c) -> MaxQueue a -> (MaxQueue b, MaxQueue c)
285-
mapEither f (MaxQueue q) = case MinQ.mapEither (bimap Down Down . f . unDown) q of
286+
mapEither f (MaxQueue q) = case MinQ.mapEither (bimap Down Down . f . getDown) q of
286287
(l, r) -> (MaxQueue l, MaxQueue r)
287288

288289
union :: Ord a => MaxQueue a -> MaxQueue a -> MaxQueue a

src/Data/PQueue/Internals/Down.hs

+7-33
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
{-# LANGUAGE CPP #-}
2-
{-# LANGUAGE BangPatterns #-}
32

4-
module Data.PQueue.Internals.Down where
3+
module Data.PQueue.Internals.Down
4+
( getDown
5+
) where
6+
import Data.Ord (Down (..))
57

6-
import Control.DeepSeq (NFData(rnf))
7-
import Data.Foldable (Foldable (..))
8-
9-
#if __GLASGOW_HASKELL__
10-
import Data.Data (Data)
11-
#endif
12-
13-
newtype Down a = Down { unDown :: a }
14-
#if __GLASGOW_HASKELL__
15-
deriving (Eq, Data)
16-
#else
17-
deriving (Eq)
8+
#if !MIN_VERSION_base(4,14,0)
9+
getDown :: Down a -> a
10+
getDown (Down a) = a
1811
#endif
19-
20-
instance NFData a => NFData (Down a) where
21-
rnf (Down a) = rnf a
22-
23-
instance Ord a => Ord (Down a) where
24-
Down a `compare` Down b = b `compare` a
25-
Down a <= Down b = b <= a
26-
Down a >= Down b = b >= a
27-
Down a < Down b = b < a
28-
Down a > Down b = b > a
29-
30-
instance Functor Down where
31-
fmap f (Down a) = Down (f a)
32-
33-
instance Foldable Down where
34-
foldr f z (Down a) = a `f` z
35-
foldl f z (Down a) = z `f` a
36-
foldr' f !z (Down a) = a `f` z
37-
foldl' f !z (Down a) = z `f` a

src/Data/PQueue/Max.hs

+15-14
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ import Data.Foldable (foldl')
9494

9595
import qualified Data.PQueue.Min as Min
9696
import qualified Data.PQueue.Prio.Max.Internals as Prio
97-
import Data.PQueue.Internals.Down (Down(..))
97+
import Data.PQueue.Internals.Down (getDown)
98+
import Data.Ord (Down(..))
9899

99100
import Prelude hiding (null, map, take, drop, takeWhile, dropWhile, splitAt, span, break, (!!), filter)
100101

@@ -171,7 +172,7 @@ findMax = fromMaybe (error "Error: findMax called on empty queue") . getMax
171172

172173
-- | \(O(1)\). The top (maximum) element of the queue, if there is one.
173174
getMax :: MaxQueue a -> Maybe a
174-
getMax (MaxQ q) = unDown <$> Min.getMin q
175+
getMax (MaxQ q) = getDown <$> Min.getMin q
175176

176177
-- | \(O(\log n)\). Deletes the maximum element of the queue. Does nothing on an empty queue.
177178
deleteMax :: Ord a => MaxQueue a -> MaxQueue a
@@ -210,7 +211,7 @@ unions qs = MaxQ (Min.unions [q | MaxQ q <- qs])
210211

211212
-- | \(O(k \log n)\)/. Returns the @(k+1)@th largest element of the queue.
212213
(!!) :: Ord a => MaxQueue a -> Int -> a
213-
MaxQ q !! n = unDown ((Min.!!) q n)
214+
MaxQ q !! n = getDown ((Min.!!) q n)
214215

215216
{-# INLINE take #-}
216217
-- | \(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)
224225

225226
-- | \(O(k \log n)\)/. Equivalent to @(take k queue, drop k queue)@.
226227
splitAt :: Ord a => Int -> MaxQueue a -> ([a], MaxQueue a)
227-
splitAt k (MaxQ q) = (fmap unDown xs, MaxQ q') where
228+
splitAt k (MaxQ q) = (fmap getDown xs, MaxQ q') where
228229
(xs, q') = Min.splitAt k q
229230

230231
-- | 'takeWhile', applied to a predicate @p@ and a queue @queue@, returns the
231232
-- longest prefix (possibly empty) of @queue@ of elements that satisfy @p@.
232233
takeWhile :: Ord a => (a -> Bool) -> MaxQueue a -> [a]
233-
takeWhile p (MaxQ q) = fmap unDown (Min.takeWhile (p . unDown) q)
234+
takeWhile p (MaxQ q) = fmap getDown (Min.takeWhile (p . getDown) q)
234235

235236
-- | 'dropWhile' @p queue@ returns the queue remaining after 'takeWhile' @p queue@.
236237
dropWhile :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a
237-
dropWhile p (MaxQ q) = MaxQ (Min.dropWhile (p . unDown) q)
238+
dropWhile p (MaxQ q) = MaxQ (Min.dropWhile (p . getDown) q)
238239

239240
-- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where
240241
-- first element is longest prefix (possibly empty) of @queue@ of elements that
241242
-- satisfy @p@ and second element is the remainder of the queue.
242243
--
243244
span :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a)
244-
span p (MaxQ q) = (fmap unDown xs, MaxQ q') where
245-
(xs, q') = Min.span (p . unDown) q
245+
span p (MaxQ q) = (fmap getDown xs, MaxQ q') where
246+
(xs, q') = Min.span (p . getDown) q
246247

247248
-- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where
248249
-- first element is longest prefix (possibly empty) of @queue@ of elements that
@@ -252,13 +253,13 @@ break p = span (not . p)
252253

253254
-- | \(O(n)\). Returns a queue of those elements which satisfy the predicate.
254255
filter :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a
255-
filter p (MaxQ q) = MaxQ (Min.filter (p . unDown) q)
256+
filter p (MaxQ q) = MaxQ (Min.filter (p . getDown) q)
256257

257258
-- | \(O(n)\). Returns a pair of queues, where the left queue contains those elements that satisfy the predicate,
258259
-- and the right queue contains those that do not.
259260
partition :: Ord a => (a -> Bool) -> MaxQueue a -> (MaxQueue a, MaxQueue a)
260261
partition p (MaxQ q) = (MaxQ q0, MaxQ q1)
261-
where (q0, q1) = Min.partition (p . unDown) q
262+
where (q0, q1) = Min.partition (p . getDown) q
262263

263264
-- | \(O(n)\). Maps a function over the elements of the queue, and collects the 'Just' values.
264265
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)
267268
-- | \(O(n)\). Maps a function over the elements of the queue, and separates the 'Left' and 'Right' values.
268269
mapEither :: (Ord b, Ord c) => (a -> Either b c) -> MaxQueue a -> (MaxQueue b, MaxQueue c)
269270
mapEither f (MaxQ q) = (MaxQ q0, MaxQ q1)
270-
where (q0, q1) = Min.mapEither (either (Left . Down) (Right . Down) . f . unDown) q
271+
where (q0, q1) = Min.mapEither (either (Left . Down) (Right . Down) . f . getDown) q
271272

272273
-- | \(O(n)\). Creates a new priority queue containing the images of the elements of this queue.
273274
-- Equivalent to @'fromList' . 'Data.List.map' f . toList@.
@@ -287,7 +288,7 @@ foldrU f z (MaxQ q) = Min.foldrU (flip (foldr f)) z q
287288
--
288289
-- @since 1.4.2
289290
foldMapU :: Monoid m => (a -> m) -> MaxQueue a -> m
290-
foldMapU f (MaxQ q) = Min.foldMapU (f . unDown) q
291+
foldMapU f (MaxQ q) = Min.foldMapU (f . getDown) q
291292

292293
-- | \(O(n)\). Unordered left fold on a priority queue. This is rarely
293294
-- what you want; 'foldrU' and 'foldlU'' are more likely to perform
@@ -309,7 +310,7 @@ elemsU = toListU
309310
{-# INLINE toListU #-}
310311
-- | \(O(n)\). Returns a list of the elements of the priority queue, in no particular order.
311312
toListU :: MaxQueue a -> [a]
312-
toListU (MaxQ q) = fmap unDown (Min.toListU q)
313+
toListU (MaxQ q) = fmap getDown (Min.toListU q)
313314

314315
-- | \(O(n \log n)\). Performs a right-fold on the elements of a priority queue in ascending order.
315316
-- @'foldrAsc' f z q == 'foldlDesc' (flip f) z q@.
@@ -346,7 +347,7 @@ toDescList q = build (\c nil -> foldrDesc c nil q)
346347
--
347348
-- If the order of the elements is irrelevant, consider using 'toListU'.
348349
toList :: Ord a => MaxQueue a -> [a]
349-
toList (MaxQ q) = fmap unDown (Min.toList q)
350+
toList (MaxQ q) = fmap getDown (Min.toList q)
350351

351352
{-# INLINE fromAscList #-}
352353
-- | \(O(n)\). Constructs a priority queue from an ascending list. /Warning/: Does not check the precondition.

0 commit comments

Comments
 (0)