Skip to content

Commit f1eb8e9

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 f1eb8e9

File tree

6 files changed

+64
-98
lines changed

6 files changed

+64
-98
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Revision history for pqueue
22

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

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

pqueue.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ library
4545
BinomialQueue.Internals
4646
BinomialQueue.Min
4747
BinomialQueue.Max
48-
Data.PQueue.Internals.Down
4948
Data.PQueue.Internals.Foldable
5049
Data.PQueue.Prio.Max.Internals
5150
if impl(ghc) {

src/BinomialQueue/Max.hs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ 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.Ord (Down (..))
102102

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

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

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

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

151151
-- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where
152152
-- first element is longest prefix (possibly empty) of @queue@ of elements that
153153
-- satisfy @p@ and second element is the remainder of the queue.
154154
span :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a)
155155
span p (MaxQueue queue)
156-
| (front, rear) <- MinQ.span (p . unDown) queue
157-
= (fmap unDown front, MaxQueue rear)
156+
| (front, rear) <- MinQ.span (p . getDown) queue
157+
= (fmap getDown front, MaxQueue rear)
158158

159159
-- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where
160160
-- first element is longest prefix (possibly empty) of @queue@ of elements that
@@ -177,19 +177,19 @@ drop n (MaxQueue queue) = MaxQueue (MinQ.drop n queue)
177177
splitAt :: Ord a => Int -> MaxQueue a -> ([a], MaxQueue a)
178178
splitAt n (MaxQueue queue)
179179
| (l, r) <- MinQ.splitAt n queue
180-
= (fmap unDown l, MaxQueue r)
180+
= (fmap getDown l, MaxQueue r)
181181

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

186186
-- | \(O(n)\). Returns a pair where the first queue contains all elements satisfying @p@, and the second queue
187187
-- contains all elements not satisfying @p@.
188188
partition :: Ord a => (a -> Bool) -> MaxQueue a -> (MaxQueue a, MaxQueue a)
189189
partition p = go . unMaxQueue
190190
where
191191
go queue
192-
| (l, r) <- MinQ.partition (p . unDown) queue
192+
| (l, r) <- MinQ.partition (p . getDown) queue
193193
= (MaxQueue l, MaxQueue r)
194194

195195
-- | \(O(n)\). Creates a new priority queue containing the images of the elements of this queue.
@@ -202,13 +202,13 @@ map f = MaxQueue . MinQ.map (fmap f) . unMaxQueue
202202
--
203203
-- If the order of the elements is irrelevant, consider using 'toListU'.
204204
toList :: Ord a => MaxQueue a -> [a]
205-
toList = fmap unDown . MinQ.toAscList . unMaxQueue
205+
toList = fmap getDown . MinQ.toAscList . unMaxQueue
206206

207207
toAscList :: Ord a => MaxQueue a -> [a]
208-
toAscList = fmap unDown . MinQ.toDescList . unMaxQueue
208+
toAscList = fmap getDown . MinQ.toDescList . unMaxQueue
209209

210210
toDescList :: Ord a => MaxQueue a -> [a]
211-
toDescList = fmap unDown . MinQ.toAscList . unMaxQueue
211+
toDescList = fmap getDown . MinQ.toAscList . unMaxQueue
212212

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

246246
-- | Convert to a list in an arbitrary order.
247247
toListU :: MaxQueue a -> [a]
248-
toListU = fmap unDown . MinQ.toListU . unMaxQueue
248+
toListU = fmap getDown . MinQ.toListU . unMaxQueue
249249

250250
-- | Get the number of elements in a 'MaxQueue'.
251251
size :: MaxQueue a -> Int
@@ -255,7 +255,7 @@ empty :: MaxQueue a
255255
empty = MaxQueue MinQ.empty
256256

257257
foldMapU :: Monoid m => (a -> m) -> MaxQueue a -> m
258-
foldMapU f = MinQ.foldMapU (f . unDown) . unMaxQueue
258+
foldMapU f = MinQ.foldMapU (f . getDown) . unMaxQueue
259259

260260
seqSpine :: MaxQueue a -> b -> b
261261
seqSpine = MinQ.seqSpine . unMaxQueue
@@ -267,7 +267,7 @@ foldlU' :: (b -> a -> b) -> b -> MaxQueue a -> b
267267
foldlU' f b = MinQ.foldlU' (\acc (Down a) -> f acc a) b . unMaxQueue
268268

269269
foldrU :: (a -> b -> b) -> b -> MaxQueue a -> b
270-
foldrU c n = MinQ.foldrU (c . unDown) n . unMaxQueue
270+
foldrU c n = MinQ.foldrU (c . getDown) n . unMaxQueue
271271

272272
null :: MaxQueue a -> Bool
273273
null = MinQ.null . unMaxQueue
@@ -276,13 +276,13 @@ singleton :: a -> MaxQueue a
276276
singleton = MaxQueue . MinQ.singleton . Down
277277

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

281281
insert :: Ord a => a -> MaxQueue a -> MaxQueue a
282282
insert a (MaxQueue q) = MaxQueue (MinQ.insert (Down a) q)
283283

284284
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
285+
mapEither f (MaxQueue q) = case MinQ.mapEither (bimap Down Down . f . getDown) q of
286286
(l, r) -> (MaxQueue l, MaxQueue r)
287287

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

src/Data/PQueue/Internals/Down.hs

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/Data/PQueue/Max.hs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ 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.Ord (Down(..))
9898

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

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

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

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

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

215215
{-# INLINE take #-}
216216
-- | \(O(k \log n)\)/. Returns the list of the @k@ largest elements of the queue, in descending order, or
@@ -224,25 +224,25 @@ drop k (MaxQ q) = MaxQ (Min.drop k q)
224224

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

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

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

239239
-- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where
240240
-- first element is longest prefix (possibly empty) of @queue@ of elements that
241241
-- satisfy @p@ and second element is the remainder of the queue.
242242
--
243243
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
244+
span p (MaxQ q) = (fmap getDown xs, MaxQ q') where
245+
(xs, q') = Min.span (p . getDown) q
246246

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

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

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

263263
-- | \(O(n)\). Maps a function over the elements of the queue, and collects the 'Just' values.
264264
mapMaybe :: Ord b => (a -> Maybe b) -> MaxQueue a -> MaxQueue b
@@ -267,7 +267,7 @@ mapMaybe f (MaxQ q) = MaxQ (Min.mapMaybe (\(Down x) -> Down <$> f x) q)
267267
-- | \(O(n)\). Maps a function over the elements of the queue, and separates the 'Left' and 'Right' values.
268268
mapEither :: (Ord b, Ord c) => (a -> Either b c) -> MaxQueue a -> (MaxQueue b, MaxQueue c)
269269
mapEither f (MaxQ q) = (MaxQ q0, MaxQ q1)
270-
where (q0, q1) = Min.mapEither (either (Left . Down) (Right . Down) . f . unDown) q
270+
where (q0, q1) = Min.mapEither (either (Left . Down) (Right . Down) . f . getDown) q
271271

272272
-- | \(O(n)\). Creates a new priority queue containing the images of the elements of this queue.
273273
-- Equivalent to @'fromList' . 'Data.List.map' f . toList@.
@@ -287,7 +287,7 @@ foldrU f z (MaxQ q) = Min.foldrU (flip (foldr f)) z q
287287
--
288288
-- @since 1.4.2
289289
foldMapU :: Monoid m => (a -> m) -> MaxQueue a -> m
290-
foldMapU f (MaxQ q) = Min.foldMapU (f . unDown) q
290+
foldMapU f (MaxQ q) = Min.foldMapU (f . getDown) q
291291

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

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

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

0 commit comments

Comments
 (0)