# Ticket #3909: containers-pqueue.3.patch

File containers-pqueue.3.patch, 37.6 KB (added by LouisWasserman, 6 years ago) |
---|

Line | |
---|---|

1 | Thu Mar 4 14:00:15 CST 2010 wasserman.louis@gmail.com |

2 | * Data.PQueue with binomial heaps |

3 | |

4 | New patches: |

5 | |

6 | [Data.PQueue with binomial heaps |

7 | wasserman.louis@gmail.com**20100304200015 |

8 | Ignore-this: e47837554730acb06ca90cd2f9a5a830 |

9 | ] { |

10 | adddir ./Data/PQueue |

11 | addfile ./Data/PQueue.hs |

12 | hunk ./Data/PQueue.hs 1 |

13 | + |

14 | +----------------------------------------------------------------------------- |

15 | +-- | |

16 | +-- Module : Data.MinQueue |

17 | +-- Copyright : (c) Louis Wasserman 2010 |

18 | +-- License : BSD-style |

19 | +-- Maintainer : libraries@haskell.org |

20 | +-- Stability : experimental |

21 | +-- Portability : portable |

22 | +-- |

23 | +-- General purpose priority queue, supporting extract-maximum operations. |

24 | +-- |

25 | +-- This module reexports "Data.PQueue.Min". If you need to use a max-queue, |

26 | +-- you should import "Data.PQueue.Max". |

27 | +--------------------------------------------------------------------------- |

28 | +module Data.PQueue ( |

29 | + PQueue, |

30 | + module Data.PQueue.Min) where |

31 | hunk ./Data/PQueue.hs 20 |

32 | +import Data.PQueue.Min |

33 | + |

34 | +type PQueue = MinQueue |

35 | addfile ./Data/PQueue/Max.hs |

36 | hunk ./Data/PQueue/Max.hs 1 |

37 | +{-# LANGUAGE CPP #-} |

38 | hunk ./Data/PQueue/Max.hs 3 |

39 | +----------------------------------------------------------------------------- |

40 | +-- | |

41 | +-- Module : Data.MinQueue.Max |

42 | +-- Copyright : (c) Louis Wasserman 2010 |

43 | +-- License : BSD-style |

44 | +-- Maintainer : libraries@haskell.org |

45 | +-- Stability : experimental |

46 | +-- Portability : portable |

47 | +-- |

48 | +-- General purpose priority queue, supporting extract-maximum operations. |

49 | +-- |

50 | +-- An amortized running time is given for each operation, with /n/ referring |

51 | +-- to the length of the sequence and /i/ being the integral index used by |

52 | +-- some operations. These bounds hold even in a persistent (shared) setting. |

53 | +-- |

54 | +-- This implementation is based on a binomial heap augmented with a global root. |

55 | +-- The spine of the heap is maintained strictly, ensuring that computations happen |

56 | +-- as they are performed. Note that this module is a small wrapper around |

57 | +-- "Data.PQueue.Min". |

58 | +-- |

59 | +-- /WARNING:/ 'toList' and 'toDescList' are /not/ equivalent, unlike for example |

60 | +-- "Data.Map". |

61 | +----------------------------------------------------------------------------- |

62 | +module Data.PQueue.Max( |

63 | + MaxQueue, |

64 | + -- * Basic operations |

65 | + empty, |

66 | + null, |

67 | + size, |

68 | + -- * Query operations |

69 | + ViewQ(..), |

70 | + top, |

71 | + delete, |

72 | + extract, |

73 | + -- * Construction operations |

74 | + singleton, |

75 | + insert, |

76 | + union, |

77 | + unions, |

78 | + intersection, |

79 | + difference, |

80 | + -- * Extracting elements |

81 | + (!!), |

82 | + take, |

83 | + drop, |

84 | + splitAt, |

85 | + takeWhile, |

86 | + dropWhile, |

87 | + span, |

88 | + break, |

89 | + -- * Fold\/Functor\/Traversable variations |

90 | + mapMonotonic, |

91 | + foldrDesc, |

92 | + foldlDesc, |

93 | + traverseMonotonic, |

94 | + -- * List operations |

95 | + toList, |

96 | + toDescList, |

97 | + fromList, |

98 | + fromDescList) where |

99 | + |

100 | +import Control.Applicative (Applicative(..), (<$>)) |

101 | + |

102 | +import Data.Monoid |

103 | +import Data.Foldable hiding (toList) |

104 | +import Data.Traversable |

105 | + |

106 | +import qualified Data.PQueue.Min as Min |

107 | + |

108 | +import Prelude hiding (null, foldr, foldl, take, drop, takeWhile, dropWhile, splitAt, span, break, (!!)) |

109 | + |

110 | +#ifdef __GLASGOW_HASKELL__ |

111 | +import GHC.Exts (build) |

112 | +import Text.Read (Lexeme(Ident), lexP, parens, prec, |

113 | + readPrec, readListPrec, readListPrecDefault) |

114 | +#endif |

115 | + |

116 | +-- | A priority queue implementation. Implemented as a wrapper around "Data.PQueue.Min". |

117 | +-- /Warning/: the 'Functor', 'Foldable', and 'Traversable' instances of this type /ignore ordering/. |

118 | +-- For 'Functor', it is guaranteed that if @f@ is a monotonic function, then @'fmap' f@ on a valid |

119 | +-- 'MaxQueue' will return a valid 'MaxQueue'. An analogous guarantee holds for 'traverse'. (Note: |

120 | +-- if passed constant-time operations, every function in 'Functor', 'Foldable', and 'Traversable' |

121 | +-- will run in /O(n)/.) |

122 | +-- |

123 | +-- If you wish to perform folds on a priority queue that respect order, use 'foldrDesc' or |

124 | +-- 'foldlDesc'. |

125 | +newtype MaxQueue a = MaxQ {unMaxQ :: Min.MinQueue (Down a)} |

126 | +newtype Down a = Down {unDown :: a} deriving (Eq) |

127 | + |

128 | +instance Ord a => Ord (Down a) where |

129 | + Down x `compare` Down y = compare y x |

130 | + Down x <= Down y = y <= x |

131 | + Down x < Down y = y < x |

132 | + Down x >= Down y = y >= x |

133 | + Down x > Down y = y > x |

134 | + |

135 | +instance Ord a => Eq (MaxQueue a) where |

136 | + q1 == q2 = toDescList q1 == toDescList q2 |

137 | + |

138 | +instance Ord a => Ord (MaxQueue a) where |

139 | + q1 `compare` q2 = toDescList q1 `compare` toDescList q2 |

140 | + |

141 | +instance (Ord a, Show a) => Show (MaxQueue a) where |

142 | + showsPrec p xs = showParen (p > 10) $ |

143 | + showString "fromDescList " . shows (toDescList xs) |

144 | + |

145 | +instance Read a => Read (MaxQueue a) where |

146 | +#ifdef __GLASGOW_HASKELL__ |

147 | + readPrec = parens $ prec 10 $ do |

148 | + Ident "fromDescList" <- lexP |

149 | + xs <- readPrec |

150 | + return (fromDescList xs) |

151 | + |

152 | + readListPrec = readListPrecDefault |

153 | +#else |

154 | + readsPrec p = readParen (p > 10) $ \ r -> do |

155 | + ("fromDescList",s) <- lex r |

156 | + (xs,t) <- reads s |

157 | + return (fromDescList xs,t) |

158 | +#endif |

159 | + |

160 | +instance Ord a => Monoid (MaxQueue a) where |

161 | + mempty = empty |

162 | + mappend = union |

163 | + |

164 | +-- | /O(1)/. The empty priority queue. |

165 | +empty :: MaxQueue a |

166 | +empty = MaxQ Min.empty |

167 | + |

168 | +-- | /O(1)/. Is this the empty priority queue? |

169 | +null :: MaxQueue a -> Bool |

170 | +null (MaxQ q) = Min.null q |

171 | + |

172 | +-- | /O(1)/. The number of elements in the queue. |

173 | +size :: MaxQueue a -> Int |

174 | +size (MaxQ q) = Min.size q |

175 | + |

176 | +data ViewQ a = EmptyQ -- ^ empty queue |

177 | + | a :^ MaxQueue a -- ^ the top (maximum) of the queue and the rest of the queue |

178 | + deriving (Eq, Ord, Read, Show) |

179 | + |

180 | +instance Functor ViewQ where |

181 | + fmap f (a :^ q) = f a :^ fmap f q |

182 | + fmap _ _ = EmptyQ |

183 | + |

184 | +instance Foldable ViewQ where |

185 | + foldr _ z EmptyQ = z |

186 | + foldr f z (a :^ q) = a `f` foldr f z q |

187 | + foldl _ z EmptyQ = z |

188 | + foldl f z (a :^ q) = foldl f (z `f` a) q |

189 | + |

190 | +instance Traversable ViewQ where |

191 | + traverse _ EmptyQ = pure EmptyQ |

192 | + traverse f (a :^ q) = (:^) <$> f a <*> traverse f q |

193 | + |

194 | +-- | /O(log n)/. The top (maximum) element of the queue, if there is one. |

195 | +top :: Ord a => MaxQueue a -> Maybe a |

196 | +top q = case extract q of |

197 | + EmptyQ -> Nothing |

198 | + x :^ _ -> Just x |

199 | + |

200 | +-- | /O(log n)/. Extract the top (maximum) element of the sequence, if there is one. |

201 | +extract :: Ord a => MaxQueue a -> ViewQ a |

202 | +extract (MaxQ q) = case Min.extract q of |

203 | + Min.EmptyQ -> EmptyQ |

204 | + (Min.:^) (Down a) q' |

205 | + -> a :^ MaxQ q' |

206 | + |

207 | +-- | /O(log n)/. Delete the top (maximum) element of the sequence, if there is one. |

208 | +delete :: Ord a => MaxQueue a -> Maybe (MaxQueue a) |

209 | +delete (MaxQ q) = MaxQ <$> Min.delete q |

210 | + |

211 | +-- | /O(1)/. Construct a priority queue with a single element. |

212 | +singleton :: a -> MaxQueue a |

213 | +singleton = MaxQ . Min.singleton . Down |

214 | + |

215 | +-- | /O(1)/. Insert an element into the priority queue. |

216 | +insert :: Ord a => a -> MaxQueue a -> MaxQueue a |

217 | +insert x (MaxQ q) = MaxQ (Min.insert (Down x) q) |

218 | + |

219 | +-- | /O(log (min(n,m)))/. Take the union of two priority queues. |

220 | +union :: Ord a => MaxQueue a -> MaxQueue a -> MaxQueue a |

221 | +MaxQ q1 `union` MaxQ q2 = MaxQ (Min.union q1 q2) |

222 | + |

223 | +-- | Takes the union of a list of priority queues. Equivalent to @'foldl' 'union' 'empty'@. |

224 | +unions :: Ord a => [MaxQueue a] -> MaxQueue a |

225 | +unions = foldl union empty |

226 | + |

227 | +-- | /O(n log n + m log m)/. Take the intersection of two priority queues. |

228 | +intersection :: Ord a => MaxQueue a -> MaxQueue a -> MaxQueue a |

229 | +MaxQ q1 `intersection` MaxQ q2 = MaxQ (Min.intersection q1 q2) |

230 | + |

231 | +-- | /O(n log n + m log m)/. Takes the difference of two priority queues. |

232 | +difference :: Ord a => MaxQueue a -> MaxQueue a -> MaxQueue a |

233 | +MaxQ q1 `difference` MaxQ q2 = MaxQ (Min.difference q1 q2) |

234 | + |

235 | +-- | /O(k log n)/. Returns the @(k+1)@th largest element of the queue. |

236 | +(!!) :: Ord a => MaxQueue a -> Int -> a |

237 | +MaxQ q !! n = unDown ((Min.!!) q n) |

238 | + |

239 | +{-# INLINE take #-} |

240 | +-- | /O(k log n)/. Returns the list of the @k@ largest elements of the queue, in descending order, or |

241 | +-- all elements of the queue, if @k >= n@. |

242 | +take :: Ord a => Int -> MaxQueue a -> [a] |

243 | +take k (MaxQ q) = [a | Down a <- Min.take k q] |

244 | + |

245 | +-- | /O(k log n)/. Returns the queue with the @k@ largest elements deleted, or the empty queue if @k >= n@. |

246 | +drop :: Ord a => Int -> MaxQueue a -> MaxQueue a |

247 | +drop k (MaxQ q) = MaxQ (Min.drop k q) |

248 | + |

249 | +-- | /O(k log n)/. Equivalent to @(take k queue, drop k queue)@. |

250 | +splitAt :: Ord a => Int -> MaxQueue a -> ([a], MaxQueue a) |

251 | +splitAt k (MaxQ q) = (map unDown xs, MaxQ q') where |

252 | + (xs, q') = Min.splitAt k q |

253 | + |

254 | +-- | 'takeWhile', applied to a predicate @p@ and a queue @queue@, returns the |

255 | +-- longest prefix (possibly empty) of @queue@ of elements that satisfy @p@. |

256 | +takeWhile :: Ord a => (a -> Bool) -> MaxQueue a -> [a] |

257 | +takeWhile p (MaxQ q) = map unDown (Min.takeWhile (p . unDown) q) |

258 | + |

259 | +-- | 'dropWhile' @p queue@ returns the queue remaining after 'takeWhile' @p queue@. |

260 | +-- |

261 | +dropWhile :: Ord a => (a -> Bool) -> MaxQueue a -> MaxQueue a |

262 | +dropWhile p (MaxQ q) = MaxQ (Min.dropWhile (p . unDown) q) |

263 | + |

264 | +-- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where |

265 | +-- first element is longest prefix (possibly empty) of @queue@ of elements that |

266 | +-- satisfy @p@ and second element is the remainder of the queue. |

267 | +-- |

268 | +span :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a) |

269 | +span p (MaxQ q) = (map unDown xs, MaxQ q') where |

270 | + (xs, q') = Min.span (p . unDown) q |

271 | + |

272 | +-- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where |

273 | +-- first element is longest prefix (possibly empty) of @queue@ of elements that |

274 | +-- /do not satisfy/ @p@ and second element is the remainder of the queue. |

275 | +break :: Ord a => (a -> Bool) -> MaxQueue a -> ([a], MaxQueue a) |

276 | +break p = span (not . p) |

277 | + |

278 | +-- | /O(n)/. Assumes that the function it is given is monotonic, and applies this function to every element of the priority queue, |

279 | +-- as in 'fmap'. If it is not, the result is undefined. |

280 | +mapMonotonic :: (a -> b) -> MaxQueue a -> MaxQueue b |

281 | +mapMonotonic = fmap |

282 | + |

283 | +-- | /O(n)/. Assumes that the function it is given is monotonic, in some sense, and performs the 'traverse' operation. |

284 | +-- If the function is not monotonic, the result is undefined. |

285 | +traverseMonotonic :: Applicative f => (a -> f b) -> MaxQueue a -> f (MaxQueue b) |

286 | +traverseMonotonic = traverse |

287 | + |

288 | +instance Functor Down where |

289 | + fmap f (Down a) = Down (f a) |

290 | + |

291 | +instance Foldable Down where |

292 | + foldr f z (Down a) = a `f` z |

293 | + foldl f z (Down a) = z `f` a |

294 | + |

295 | +instance Traversable Down where |

296 | + traverse f (Down a) = Down <$> f a |

297 | + |

298 | +instance Functor MaxQueue where |

299 | + fmap f (MaxQ q) = MaxQ (fmap (fmap f) q) |

300 | + |

301 | +instance Foldable MaxQueue where |

302 | + foldr f z (MaxQ q) = foldr (flip (foldr f)) z q |

303 | + foldl f z (MaxQ q) = foldl (foldl f) z q |

304 | + |

305 | +instance Traversable MaxQueue where |

306 | + traverse f (MaxQ q) = MaxQ <$> traverse (traverse f) q |

307 | + |

308 | +-- | /O(n log n)/. Performs a right-fold on the elements of a priority queue in descending order. |

309 | +foldrDesc :: Ord a => (a -> b -> b) -> b -> MaxQueue a -> b |

310 | +foldrDesc f z (MaxQ q) = Min.foldrAsc (flip (foldr f)) z q |

311 | + |

312 | +-- | /O(n log n)/. Performs a left-fold on the elements of a priority queue in descending order. |

313 | +foldlDesc :: Ord a => (b -> a -> b) -> b -> MaxQueue a -> b |

314 | +foldlDesc f z (MaxQ q) = Min.foldlAsc (foldl f) z q |

315 | + |

316 | +{-# INLINE toDescList #-} |

317 | +-- | /O(n log n)/. Extracts the elements of the priority queue in descending order. |

318 | +toDescList :: Ord a => MaxQueue a -> [a] |

319 | +#ifdef __GLASGOW_HASKELL__ |

320 | +toDescList q = build (\ c nil -> foldrDesc c nil q) |

321 | +#else |

322 | +toDescList = foldrDesc (:) [] |

323 | +#endif |

324 | + |

325 | +{-# INLINE toList #-} |

326 | +-- | /O(n)/. Returns the elements of the priority queue in no particular order. |

327 | +toList :: MaxQueue a -> [a] |

328 | +#ifdef __GLASGOW_HASKELL__ |

329 | +toList q = build (\ c nil -> foldr c nil q) |

330 | +#else |

331 | +toList = foldr (:) [] |

332 | +#endif |

333 | + |

334 | +{-# INLINE fromDescList #-} |

335 | +-- | /O(n)/. Constructs a priority queue from an descending list. /Warning/: Does not check the precondition. |

336 | +fromDescList :: [a] -> MaxQueue a |

337 | +fromDescList = MaxQ . Min.fromAscList . map Down |

338 | + |

339 | +{-# INLINE fromList #-} |

340 | +-- | /O(n log n)/. Constructs a priority queue from an unordered list. |

341 | +fromList :: Ord a => [a] -> MaxQueue a |

342 | +fromList = foldr insert empty |

343 | addfile ./Data/PQueue/Min.hs |

344 | hunk ./Data/PQueue/Min.hs 1 |

345 | +{-# LANGUAGE CPP #-} |

346 | hunk ./Data/PQueue/Min.hs 3 |

347 | +----------------------------------------------------------------------------- |

348 | +-- | |

349 | +-- Module : Data.MinQueue.Min |

350 | +-- Copyright : (c) Louis Wasserman 2010 |

351 | +-- License : BSD-style |

352 | +-- Maintainer : libraries@haskell.org |

353 | +-- Stability : experimental |

354 | +-- Portability : portable |

355 | +-- |

356 | +-- General purpose priority queue, supporting extract-minimum operations. |

357 | +-- |

358 | +-- An amortized running time is given for each operation, with /n/ referring |

359 | +-- to the length of the sequence and /i/ being the integral index used by |

360 | +-- some operations. These bounds hold even in a persistent (shared) setting. |

361 | +-- |

362 | +-- This implementation is based on a binomial heap augmented with a global root. |

363 | +-- The spine of the heap is maintained strictly, ensuring that computations happen |

364 | +-- as they are performed. |

365 | +-- |

366 | +-- /WARNING:/ 'toList' and 'toAscList' are /not/ equivalent, unlike for example |

367 | +-- "Data.Map". |

368 | +----------------------------------------------------------------------------- |

369 | +module Data.PQueue.Min ( |

370 | + MinQueue, |

371 | + -- * Basic operations |

372 | + empty, |

373 | + null, |

374 | + size, |

375 | + -- * Query operations |

376 | + ViewQ(..), |

377 | + top, |

378 | + delete, |

379 | + extract, |

380 | + -- * Construction operations |

381 | + singleton, |

382 | + insert, |

383 | + union, |

384 | + unions, |

385 | + intersection, |

386 | + difference, |

387 | + -- * Extracting elements |

388 | + (!!), |

389 | + take, |

390 | + drop, |

391 | + splitAt, |

392 | + takeWhile, |

393 | + dropWhile, |

394 | + span, |

395 | + break, |

396 | + -- * Fold\/Functor\/Traversable variations |

397 | + mapMonotonic, |

398 | + foldrAsc, |

399 | + foldlAsc, |

400 | + traverseMonotonic, |

401 | + -- * List operations |

402 | + toList, |

403 | + toAscList, |

404 | + fromList, |

405 | + fromAscList) where |

406 | + |

407 | +import Prelude hiding (null, foldr, foldl, take, drop, takeWhile, dropWhile, splitAt, span, break, (!!)) |

408 | + |

409 | +import Control.Applicative (Applicative(..), (<$>)) |

410 | + |

411 | +import Data.Monoid |

412 | +import Data.Foldable hiding (toList) |

413 | +import Data.Traversable |

414 | + |

415 | +import qualified Data.List as List |

416 | + |

417 | +#ifdef __GLASGOW_HASKELL__ |

418 | +import GHC.Exts (build) |

419 | +import Text.Read (Lexeme(Ident), lexP, parens, prec, |

420 | + readPrec, readListPrec, readListPrecDefault) |

421 | +#endif |

422 | + |

423 | +-- | A priority queue implementation. Implemented as a find-min wrapper around a binomial heap. |

424 | +-- /Warning/: the 'Functor', 'Foldable', and 'Traversable' instances of this type /ignore ordering/. |

425 | +-- For 'Functor', it is guaranteed that if @f@ is a monotonic function, then @'fmap' f@ on a valid |

426 | +-- 'MinQueue' will return a valid 'MinQueue'. An analogous guarantee holds for 'traverse'. (Note: |

427 | +-- if passed constant-time operations, every function in 'Functor', 'Foldable', and 'Traversable' |

428 | +-- will run in /O(n)/.) |

429 | +-- |

430 | +-- If you wish to perform folds on a priority queue that respect order, use 'foldrAsc' or |

431 | +-- 'foldlAsc'. |

432 | +data MinQueue a = Empty | MinQueue {-# UNPACK #-} !Int a !(BinomHeap a) |

433 | +type BinomHeap a = BinomForest a Zero |

434 | + |

435 | +instance Ord a => Eq (MinQueue a) where |

436 | + q1 == q2 = toAscList q1 == toAscList q2 |

437 | + |

438 | +instance Ord a => Ord (MinQueue a) where |

439 | + compare q1 q2 = compare (toAscList q1) (toAscList q2) |

440 | + |

441 | +instance (Ord a, Show a) => Show (MinQueue a) where |

442 | + showsPrec p xs = showParen (p > 10) $ |

443 | + showString "fromAscList " . shows (toAscList xs) |

444 | + |

445 | +instance Read a => Read (MinQueue a) where |

446 | +#ifdef __GLASGOW_HASKELL__ |

447 | + readPrec = parens $ prec 10 $ do |

448 | + Ident "fromAscList" <- lexP |

449 | + xs <- readPrec |

450 | + return (fromAscList xs) |

451 | + |

452 | + readListPrec = readListPrecDefault |

453 | +#else |

454 | + readsPrec p = readParen (p > 10) $ \ r -> do |

455 | + ("fromAscList",s) <- lex r |

456 | + (xs,t) <- reads s |

457 | + return (fromAscList xs,t) |

458 | +#endif |

459 | + |

460 | +instance Ord a => Monoid (MinQueue a) where |

461 | + mempty = Empty |

462 | + mappend = union |

463 | + |

464 | +-- We implement tree ranks in the type system with a nicely elegant approach, as follows. |

465 | +-- |

466 | +-- A binomial tree of rank @0@ with elements of type @e@ has type @'BinomTree' e 'Zero'@. |

467 | +-- If a binomial tree of rank @k@ has type @BinomTree e k@, then a binomial tree of rank |

468 | +-- @k+1@ has type @'BinomTree' e ('Succ' e k)@. Therefore, we may justifiably label the |

469 | +-- second type argument as the /rank/ of the node. |

470 | +-- |

471 | +-- Consider the set-theoretic definition of the natural numbers, in which a number is specified |

472 | +-- to be the set of all numbers less than it, and 0 is the empty set. The ranks of binomial |

473 | +-- trees are similar: the children of a binomial tree of rank @k@ are a collection of a binomial |

474 | +-- tree of every rank less than @k@. We can then /define/ the type representing rank @k@ |

475 | +-- to be a sequence of a binomial tree of every rank less than @k@. In particular, |

476 | +-- @'Succ' e k@ is equivalent to @(BinomTree e k, k)@, since @k@ is a type representing a |

477 | +-- sequence of binomial trees of rank less than @k@. We may reasonably define the type |

478 | +-- corresponding to @0@ to be @()@, since it should be an unambiguous ``unit.'' This is |

479 | +-- nicely analogous to the construction of the natural numbers, where @succ(a) = {a} `union` a@. |

480 | +-- |

481 | +-- Now that we've defined rank types, we note that a binomial tree with a given rank @rk@, |

482 | +-- written as @'BinomTree' e rk@, has a root of type @e@ and a set of children of type @rk@. |

483 | +-- We may justifiably say, then, |

484 | +-- |

485 | +-- > data BinomTree e rk = BinomTree e rk |

486 | +-- |

487 | +-- Cute! |

488 | +-- |

489 | +-- To implement binomial heaps, in which we may have at most one root of each rank, we define |

490 | +-- @'BinomForest' e rk@ to be a binomial forest of roots of rank at least @rk@. Since there is only |

491 | +-- one root of each rank, we may either have a binomial forest of rank @rk@ or not, and then |

492 | +-- a @BinomForest e (Succ e rk)@ contains the rest of the forest. We also have a 'Nil' constructor, |

493 | +-- for when we have no more roots. We maintain the invariant that @Nil@ always follows a @Cons@, but |

494 | +-- don't implement that in the type system. |

495 | +data BinomForest e rk = Nil | Skip !(BinomForest' e rk) | Cons {-# UNPACK #-} !(BinomTree e rk) !(BinomForest' e rk) |

496 | +type BinomForest' e rk = BinomForest e (Succ e rk) |

497 | + |

498 | +instance Ord e => Monoid (BinomForest e rk) where |

499 | + mempty = Nil |

500 | + mappend = merge (<=) |

501 | + |

502 | +data BinomTree e rk = BinomTree e rk |

503 | +type BinomTree' e rk = BinomTree e (Succ e rk) |

504 | +data Succ e rk = Succ {-# UNPACK #-} !(BinomTree e rk) rk |

505 | +type Zero = () |

506 | + |

507 | +-- basics |

508 | + |

509 | +-- | /O(1)/. The empty priority queue. |

510 | +empty :: MinQueue a |

511 | +empty = Empty |

512 | + |

513 | +-- | /O(1)/. Is this the empty priority queue? |

514 | +null :: MinQueue a -> Bool |

515 | +null Empty = True |

516 | +null _ = False |

517 | + |

518 | +-- | /O(1)/. The number of elements in the queue. |

519 | +size :: MinQueue a -> Int |

520 | +size Empty = 0 |

521 | +size (MinQueue n _ _) = n |

522 | + |

523 | +-- queries |

524 | + |

525 | +-- | View of the top of a sequence. Note: the 'Functor', 'Foldable', and 'Traversable' instances |

526 | +-- have the same caveats as the instances for 'MinQueue'. |

527 | +data ViewQ a = EmptyQ -- ^ empty queue |

528 | + | a :^ MinQueue a -- ^ the top (minimum) of the queue and the rest of the queue |

529 | + deriving (Eq, Ord, Read, Show) |

530 | + |

531 | +instance Functor ViewQ where |

532 | + fmap f (a :^ q) = f a :^ fmap f q |

533 | + fmap _ _ = EmptyQ |

534 | + |

535 | +instance Foldable ViewQ where |

536 | + foldr _ z EmptyQ = z |

537 | + foldr f z (a :^ q) = a `f` foldr f z q |

538 | + foldl _ z EmptyQ = z |

539 | + foldl f z (a :^ q) = foldl f (z `f` a) q |

540 | + |

541 | +instance Traversable ViewQ where |

542 | + traverse _ EmptyQ = pure EmptyQ |

543 | + traverse f (a :^ q) = (:^) <$> f a <*> traverse f q |

544 | + |

545 | +-- | /O(1)/. View the top (minimum) element of the queue, if there is one. |

546 | +top :: Ord a => MinQueue a -> Maybe a |

547 | +top q = case extract q of |

548 | + EmptyQ -> Nothing |

549 | + x :^ _ -> Just x |

550 | + |

551 | +-- | /O(log n)/. Extract the top (minimum) element of the sequence, if there is one. |

552 | +extract :: Ord a => MinQueue a -> ViewQ a |

553 | +extract Empty = EmptyQ |

554 | +extract (MinQueue n x f) = x :^ delete' n f |

555 | + |

556 | +-- | /O(log n)/. Delete the top element of the sequence, if there is one. |

557 | +delete :: Ord a => MinQueue a -> Maybe (MinQueue a) |

558 | +delete q = case extract q of |

559 | + EmptyQ -> Nothing |

560 | + _ :^ q' -> Just q' |

561 | + |

562 | +-- | Takes a size and a binomial forest and produces a priority queue with a distinguished global root. |

563 | +delete' :: Ord a => Int -> BinomHeap a -> MinQueue a |

564 | +delete' n f = n `seq` case extractBin (<=) f of |

565 | + NoExtract -> Empty |

566 | + YesExtract x' _ f' |

567 | + -> MinQueue (n-1) x' f' |

568 | + |

569 | +-- | A specialized type intended to organize the return of extract-min queries |

570 | +-- from a binomial forest. We walk all the way through the forest, and then |

571 | +-- walk backwards. @Extract e rk@ is the result type of an extract-min |

572 | +-- operation that has walked as far backwards of rank @rk@ -- that is, it |

573 | +-- has visited every root of rank @>= rk@. |

574 | +-- |

575 | +-- The interpretation of @YesExtract minKey children forest@ is |

576 | +-- |

577 | +-- * @minKey@ is the key of the minimum root visited so far. It may have |

578 | +-- any rank @>= rk@. We will denote the root corresponding to |

579 | +-- @minKey@ as @minRoot@. |

580 | +-- |

581 | +-- * @children@ is those children of @minRoot@ which have not yet been |

582 | +-- merged with the rest of the forest. Specifically, these are |

583 | +-- the children with rank @< rk@. |

584 | +-- |

585 | +-- * @forest@ is a partial reconstruction of the binomial forest without |

586 | +-- @minRoot@. It is the union of all old roots with rank @>= rk@ |

587 | +-- (except @minRoot@), with the set of all children of @minRoot@ |

588 | +-- with rank @>= rk@. Note that @forest@ is lazy, so if we discover |

589 | +-- a smaller key than @minKey@ later, we haven't wasted significant |

590 | +-- work. |

591 | +data Extract e rk = NoExtract | YesExtract e rk (BinomForest e rk) |

592 | + |

593 | +-- | Walks backward from the biggest key in the forest, as far as rank @rk@. |

594 | +-- Returns its progress. Each successive application of @extractBin@ takes |

595 | +-- amortized /O(1)/ time, so applying it from the beginning takes /O(log n)/ time. |

596 | +extractBin :: (e -> e -> Bool) -> BinomForest e rk -> Extract e rk |

597 | +extractBin _ Nil = NoExtract |

598 | +extractBin (<=) (Skip f) = case extractBin (<=) f of |

599 | + NoExtract -> NoExtract |

600 | + YesExtract minKey (Succ kChild kChildren) f' -> |

601 | + YesExtract minKey kChildren (Cons kChild f') |

602 | +extractBin (<=) (Cons t@(BinomTree x ts) f) = case extractBin (<=) f of |

603 | + YesExtract minKey (Succ kChild kChildren) f' |

604 | + | minKey <= x -> YesExtract minKey kChildren (Skip (carry1 (<=) (t `cat` kChild) f')) |

605 | + _ -> YesExtract x ts (Skip f) |

606 | + where cat = joinBin (<=) |

607 | + |

608 | +-- | /O(1)/. Construct a priority queue with a single element. |

609 | +singleton :: a -> MinQueue a |

610 | +singleton x = MinQueue 1 x Nil |

611 | + |

612 | +-- | /O(1)/. Insert an element into the priority queue. |

613 | +insert :: Ord a => a -> MinQueue a -> MinQueue a |

614 | +insert x' (MinQueue n x f) |

615 | + | x' <= x = MinQueue (n+1) x' (insertBin x f) |

616 | + | otherwise = MinQueue (n+1) x (insertBin x' f) |

617 | + where insertBin = carry1 (<=) . tip |

618 | +insert x Empty = singleton x |

619 | + |

620 | +-- | /O(log (min(n,m)))/. Take the union of two priority queues. |

621 | +union :: Ord a => MinQueue a -> MinQueue a -> MinQueue a |

622 | +Empty `union` q = q |

623 | +q `union` Empty = q |

624 | +MinQueue n1 x1 f1 `union` MinQueue n2 x2 f2 |

625 | + | x1 <= x2 = MinQueue (n1 + n2) x1 (carry (<=) (tip x2) f1 f2) |

626 | + | otherwise = MinQueue (n1 + n2) x2 (carry (<=) (tip x1) f1 f2) |

627 | + |

628 | +-- | Takes the union of a list of priority queues. Equivalent to @'foldl' 'union' 'empty'@. |

629 | +unions :: Ord a => [MinQueue a] -> MinQueue a |

630 | +unions = foldl union Empty |

631 | + |

632 | +-- | /O(n log n + m log m)/. Take the intersection of two priority queues. |

633 | +intersection :: Ord a => MinQueue a -> MinQueue a -> MinQueue a |

634 | +Empty `intersection` _ = Empty |

635 | +_ `intersection` Empty = Empty |

636 | +MinQueue _ x1 f1 `intersection` MinQueue _ x2 f2 = intersectBin (<=) compare x1 f1 x2 f2 |

637 | + |

638 | +-- Takes the intersection of two binomial heaps. Essentially, this is just the algorithm |

639 | +-- for intersecting two sorted lists, except unconsing is replaced by dequeueing. |

640 | +-- Not particularly necessary, but interesting to have just for grins. |

641 | +intersectBin :: (a -> a -> Bool) -> (a -> a -> Ordering) -> a -> BinomHeap a -> a -> BinomHeap a -> MinQueue a |

642 | +intersectBin (<=) cmp = intersect where |

643 | + intersect x1 f1 x2 f2 = case (cmp x1 x2, extractBin (<=) f1, extractBin (<=) f2) of |

644 | + (LT, YesExtract x1' _ f1', _) |

645 | + -> intersect x1' f1' x2 f2 |

646 | + (EQ, YesExtract x1' _ f1', YesExtract x2' _ f2') |

647 | + -> x1 `insertMinQ` intersect x1' f1' x2' f2' |

648 | + (EQ, _, _) |

649 | + -> singleton x1 |

650 | + (GT, _, YesExtract x2' _ f2') |

651 | + -> intersect x1 f1 x2' f2' |

652 | + _ -> Empty |

653 | + |

654 | +-- | /O(n log n + m log m)/. Takes the difference of two priority queues. |

655 | +difference :: Ord a => MinQueue a -> MinQueue a -> MinQueue a |

656 | +queue `difference` Empty |

657 | + = queue |

658 | +Empty `difference` _ |

659 | + = Empty |

660 | +MinQueue n1 x1 f1 `difference` MinQueue _ x2 f2 |

661 | + = differenceBin (<=) compare n1 x1 f1 x2 f2 |

662 | + |

663 | +-- Takes the difference of two binomial heaps. Essentially, this is just the algorithm |

664 | +-- for the difference of two sorted lists, except unconsing is replaced by dequeueing. |

665 | +-- Not particularly necessary, but interesting to have just for grins. |

666 | +differenceBin :: (a -> a -> Bool) -> (a -> a -> Ordering) -> Int -> a -> BinomHeap a -> a -> BinomHeap a -> MinQueue a |

667 | +differenceBin (<=) cmp = diffBin where |

668 | + diffBin n x1 f1 x2 f2 = n `seq` case (cmp x1 x2, extractBin (<=) f1, extractBin (<=) f2) of |

669 | + (LT, YesExtract x1' _ f1', _) |

670 | + -> x1 `insertMinQ` diffBin (n-1) x1' f1' x2 f2 |

671 | + (LT, _, _) |

672 | + -> singleton x1 |

673 | + (EQ, YesExtract x1' _ f1', YesExtract x2' _ f2') |

674 | + -> diffBin (n-1) x1' f1' x2' f2' |

675 | + (EQ, YesExtract x1' _ f1', _) |

676 | + -> MinQueue n x1' f1' |

677 | + (EQ, _, _) |

678 | + -> Empty |

679 | + (GT, _, YesExtract x2' _ f2') |

680 | + -> diffBin n x1 f1 x2' f2' |

681 | + (GT, _, _) |

682 | + -> MinQueue n x1 f1 |

683 | + |

684 | +{-# INLINE tip #-} |

685 | +-- | Constructs a binomial tree of rank 0. |

686 | +tip :: e -> BinomTree e () |

687 | +tip x = BinomTree x () |

688 | + |

689 | +-- | Given two binomial forests starting at rank @rk@, takes their union. |

690 | +-- Each successive application of this function costs /O(1)/, so applying it |

691 | +-- from the beginning costs /O(log n)/. |

692 | +merge :: (e -> e -> Bool) -> BinomForest e rk -> BinomForest e rk -> BinomForest e rk |

693 | +merge (<=) f1 f2 = case (f1, f2) of |

694 | + (Nil, _) -> f2 |

695 | + (_, Nil) -> f1 |

696 | + (Skip f1', Skip f2') |

697 | + -> Skip (merge (<=) f1' f2') |

698 | + (Skip f1', Cons t2 f2') |

699 | + -> Cons t2 (merge (<=) f1' f2') |

700 | + (Cons t1 f1', Skip f2') |

701 | + -> Cons t1 (merge (<=) f1' f2') |

702 | + (Cons t1 f1', Cons t2 f2') |

703 | + -> Skip (carry (<=) (t1 `cat` t2) f1' f2') |

704 | + where cat = joinBin (<=) |

705 | + |

706 | +-- | Merges two binomial forests with another tree. If we are thinking of the trees |

707 | +-- in the binomial forest as binary digits, this corresponds to a carry operation. |

708 | +-- Each call to this function takes /O(1)/ time, so in total, it costs /O(log n)/. |

709 | +carry :: (e -> e -> Bool) -> BinomTree e rk -> BinomForest e rk -> BinomForest e rk -> BinomForest e rk |

710 | +carry (<=) t0 f1 f2 = t0 `seq` case (f1, f2) of |

711 | + (Nil, Nil) -> Cons t0 Nil |

712 | + (Nil, Skip f2') -> Cons t0 f2' |

713 | + (Skip f1', Nil) -> Cons t0 f1' |

714 | + (Nil, Cons t2 f2') -> Skip (carry1 (<=) (t0 `cat` t2) f2') |

715 | + (Cons t1 f1', Nil) -> Skip (carry1 (<=) (t0 `cat` t1) f1') |

716 | + (Skip f1', Skip f2') -> Cons t0 (merge (<=) f1' f2') |

717 | + (Skip f1', Cons t2 f2') -> Skip (carry (<=) (t0 `cat` t2) f1' f2') |

718 | + (Cons t1 f1', Skip f2') -> Skip (carry (<=) (t0 `cat` t1) f1' f2') |

719 | + (Cons t1 f1', Cons t2 f2') |

720 | + -> Cons t0 (carry (<=) (t1 `cat` t2) f1' f2') |

721 | + where cat = joinBin (<=) |

722 | + |

723 | +-- | Merges a binomial tree into a binomial forest. If we are thinking |

724 | +-- of the trees in the binomial forest as binary digits, this corresponds |

725 | +-- to adding a power of 2. This costs amortized /O(1)/ time. |

726 | +carry1 :: (e -> e -> Bool) -> BinomTree e rk -> BinomForest e rk -> BinomForest e rk |

727 | +carry1 (<=) t f = t `seq` case f of |

728 | + Nil -> Cons t Nil |

729 | + Skip f -> Cons t f |

730 | + Cons t' f' -> Skip (carry1 (<=) (t `cat` t') f') |

731 | + where cat = joinBin (<=) |

732 | + |

733 | +-- | The carrying operation: takes two binomial heaps of the same rank @k@ |

734 | +-- and returns one of rank @k+1@. Takes /O(1)/ time. |

735 | +joinBin :: (e -> e -> Bool) -> BinomTree e rk -> BinomTree e rk -> BinomTree' e rk |

736 | +joinBin (<=) t1@(BinomTree x1 ts1) t2@(BinomTree x2 ts2) |

737 | + | x1 <= x2 = BinomTree x1 (Succ t2 ts1) |

738 | + | otherwise = BinomTree x2 (Succ t1 ts2) |

739 | + |

740 | +-- indexing operations |

741 | + |

742 | +-- | Index (subscript) operator, starting from 0. @queue !! k@ returns the @(k+1)@th smallest element in the queue. |

743 | +(!!) :: Ord a => MinQueue a -> Int -> a |

744 | +q !! n | n < 0 = error "Data.PQueue.Min.!!: negative index" |

745 | + | n >= size q |

746 | + = error "Data.PQueue.Min.!!: index too large" |

747 | +q !! n = let index q n = n `seq` case extract q of |

748 | + x :^ q' | n == 0 -> x |

749 | + | otherwise -> index q' (n-1) |

750 | + _ -> error "Data.PQueue.Min.!!: index too large" |

751 | + in index q n |

752 | + |

753 | + |

754 | +{-# INLINE takeWhile #-} |

755 | +-- | 'takeWhile', applied to a predicate @p@ and a queue @queue@, returns the |

756 | +-- longest prefix (possibly empty) of @queue@ of elements that satisfy @p@. |

757 | +takeWhile :: Ord a => (a -> Bool) -> MinQueue a -> [a] |

758 | +#ifdef __GLASGOW_HASKELL__ |

759 | +takeWhile p queue = build (\ c nil -> |

760 | + let takeW queue = case extract queue of |

761 | + x :^ queue' |

762 | + | p x -> x `c` takeW queue' |

763 | + _ -> nil |

764 | + in takeW queue) |

765 | +#else |

766 | +takeWhile p = List.takeWhile p . toAscList |

767 | +#endif |

768 | + |

769 | +-- | 'dropWhile' @p queue@ returns the queue remaining after 'takeWhile' @p queue@. |

770 | +dropWhile :: Ord a => (a -> Bool) -> MinQueue a -> MinQueue a |

771 | +dropWhile p queue = case extract queue of |

772 | + EmptyQ -> queue |

773 | + x :^ queue' |

774 | + | p x -> dropWhile p queue' |

775 | + | otherwise -> queue |

776 | + |

777 | +-- | 'span', applied to a predicate @p@ and a queue @queue@, returns a tuple where |

778 | +-- first element is longest prefix (possibly empty) of @queue@ of elements that |

779 | +-- satisfy @p@ and second element is the remainder of the queue. |

780 | +span :: Ord a => (a -> Bool) -> MinQueue a -> ([a], MinQueue a) |

781 | +span p queue = case extract queue of |

782 | + EmptyQ -> ([], queue) |

783 | + x :^ q' | p x -> let (ys, q'') = span p q' in (x:ys, q'') |

784 | + | otherwise -> ([], queue) |

785 | + |

786 | +-- | 'break', applied to a predicate @p@ and a queue @queue@, returns a tuple where |

787 | +-- first element is longest prefix (possibly empty) of @queue@ of elements that |

788 | +-- /do not satisfy/ @p@ and second element is the remainder of the queue. |

789 | +break :: Ord a => (a -> Bool) -> MinQueue a -> ([a], MinQueue a) |

790 | +break p = span (not . p) |

791 | + |

792 | +{-# INLINE take #-} |

793 | +-- | /O(k log n)/. 'take' @k@, applied to a queue @queue@, returns a list of the smallest @k@ elements of @queue@, |

794 | +-- or all elements of @queue@ itself if @k >= 'size' queue@. |

795 | +take :: Ord a => Int -> MinQueue a -> [a] |

796 | +take n = List.take n . toAscList |

797 | + |

798 | +-- | /O(k log n)/. 'drop' @k@, applied to a queue @queue@, returns @queue@ with the smallest @k@ elements deleted, |

799 | +-- or an empty queue if @k >= size 'queue'@. |

800 | +drop :: Ord a => Int -> MinQueue a -> MinQueue a |

801 | +drop n queue |

802 | + | n <= 0 = queue |

803 | + | otherwise = case delete queue of |

804 | + Nothing -> empty |

805 | + Just queue' -> drop (n-1) queue' |

806 | + |

807 | +-- | /O(k log n)/. Equivalent to @('take' k queue, 'drop' k queue)@. |

808 | +splitAt :: Ord a => Int -> MinQueue a -> ([a], MinQueue a) |

809 | +splitAt n queue |

810 | + | n <= 0 = ([], queue) |

811 | + | otherwise = case extract queue of |

812 | + EmptyQ -> ([], queue) |

813 | + x :^ queue' -> let (xs, queue'') = splitAt (n-1) queue' in (x:xs, queue'') |

814 | + |

815 | + |

816 | +-- folding |

817 | + |

818 | +-- | /O(n)/. Assumes that the function it is given is monotonic, and applies this function to every element of the priority queue, |

819 | +-- as in 'fmap'. If it is not, the result is undefined. |

820 | +mapMonotonic :: (a -> b) -> MinQueue a -> MinQueue b |

821 | +mapMonotonic = fmap |

822 | + |

823 | +-- | /O(n)/. Assumes that the function it is given is monotonic, in some sense, and performs the 'traverse' operation. |

824 | +-- If the function is not monotonic, the result is undefined. |

825 | +traverseMonotonic :: Applicative f => (a -> f b) -> MinQueue a -> f (MinQueue b) |

826 | +traverseMonotonic = traverse |

827 | + |

828 | +instance Functor MinQueue where |

829 | + fmap _ Empty = Empty |

830 | + fmap f (MinQueue n x forest) = MinQueue n (f x) (mapForest f (const ()) forest) |

831 | + |

832 | +mapForest :: (a -> b) -> (rk -> rk') -> BinomForest a rk -> BinomForest b rk' |

833 | +mapForest f fCh forest = case forest of |

834 | + Nil -> Nil |

835 | + Skip forest' |

836 | + -> Skip (fF' forest') |

837 | + Cons t forest' |

838 | + -> Cons (fT t) (fF' forest') |

839 | + where fT (BinomTree x ts) = BinomTree (f x) (fCh ts) |

840 | + fCh' (Succ t ts) = Succ (fT t) (fCh ts) |

841 | + fF' = mapForest f fCh' |

842 | + |

843 | +instance Foldable MinQueue where |

844 | + foldr _ n Empty = n |

845 | + foldr c n (MinQueue _ x f) = x `c` foldrUnord c n (const id) f |

846 | + foldMap _ Empty = mempty |

847 | + foldMap f (MinQueue _ x forest) = f x `mappend` foldMap0 mappend mempty f forest |

848 | + |

849 | +-- | The initial level of 'foldMap'. Avoids unnecessary @'mappend' 'mempty'@ computations. |

850 | +foldMap0 :: (m -> m -> m) -> m -> (a -> m) -> BinomHeap a -> m |

851 | +foldMap0 (><) zero f forest = case forest of |

852 | + Nil -> zero |

853 | + Skip forest' |

854 | + -> fF' forest' |

855 | + Cons (BinomTree x _) forest' |

856 | + -> f x >< fF' forest' |

857 | + where fF' = foldMapUnord (><) zero f (\ (Succ (BinomTree x _) _) -> f x) |

858 | + |

859 | +-- | A recursive implementation of 'foldMap' capable of working up to trees of arbitrary rank. |

860 | +-- Does not respect ordering of the elements. |

861 | +foldMapUnord :: (m -> m -> m) -> m -> (a -> m) -> (rk -> m) -> BinomForest a rk -> m |

862 | +foldMapUnord (><) zero f fCh forest = case forest of |

863 | + Nil -> zero |

864 | + Skip forest' -> fF' forest' |

865 | + Cons t forest' -> fT t >< fF' forest' |

866 | + where fT (BinomTree x ts) = f x >< fCh ts |

867 | + fCh' (Succ t tss) = fT t >< fCh tss |

868 | + fF' = foldMapUnord (><) zero f fCh' |

869 | + |

870 | +-- | 'foldr' implementation on the binomial forest. Does not respect ordering of the elements. |

871 | +foldrUnord :: (a -> b -> b) -> b -> (rk -> b -> b) -> BinomForest a rk -> b |

872 | +foldrUnord c n cCh forest = case forest of |

873 | + Nil -> n |

874 | + Skip f' -> cF' f' |

875 | + Cons t f' -> t `cT` cF' f' |

876 | + where cT (BinomTree x ts) = c x . cCh ts |

877 | + cCh' (Succ t tss) = cT t . cCh tss |

878 | + cF' = foldrUnord c n cCh' |

879 | + |

880 | +instance Traversable MinQueue where |

881 | + traverse _ Empty = pure Empty |

882 | + traverse f (MinQueue n x forest) |

883 | + = MinQueue n <$> f x <*> traverseBin f (const (pure ())) forest |

884 | + |

885 | +traverseBin :: Applicative f => (a -> f b) -> (rk -> f rk') -> BinomForest a rk -> f (BinomForest b rk') |

886 | +traverseBin f fCh forest = case forest of |

887 | + Nil -> pure Nil |

888 | + Skip forest' |

889 | + -> Skip <$> fF' forest' |

890 | + Cons t forest' |

891 | + -> Cons <$> fT t <*> fF' forest' |

892 | + where fF' = traverseBin f fCh' |

893 | + fT (BinomTree x ts) = BinomTree <$> f x <*> fCh ts |

894 | + fCh' (Succ t tss) = Succ <$> fT t <*> fCh tss |

895 | + |

896 | +{-# INLINE toAscList #-} |

897 | +-- | /O(n log n)/. Extracts the elements of the priority queue in ascending order. |

898 | +toAscList :: Ord a => MinQueue a -> [a] |

899 | +#ifdef __GLASGOW_HASKELL__ |

900 | +toAscList q = build (\ c nil -> foldrAsc c nil q) |

901 | +#else |

902 | +toAscList = foldrAsc (:) [] |

903 | +#endif |

904 | + |

905 | +{-# INLINE toList #-} |

906 | +-- | /O(n)/. Returns the elements of the priority queue in no particular order. |

907 | +toList :: MinQueue a -> [a] |

908 | +#ifdef __GLASGOW_HASKELL__ |

909 | +toList q = build (\ c nil -> foldr c nil q) |

910 | +#else |

911 | +toList = foldr (:) [] |

912 | +#endif |

913 | + |

914 | +-- | /O(n log n)/. Performs a right-fold on the elements of a priority queue in ascending order. |

915 | +foldrAsc :: Ord a => (a -> b -> b) -> b -> MinQueue a -> b |

916 | +foldrAsc c n (MinQueue _ x f) = x `c` foldrOrd (<=) c n f |

917 | +foldrAsc _ n _ = n |

918 | + |

919 | +-- | /O(n log n)/. Performs a left-fold on the elements of a priority queue in ascending order. |

920 | +foldlAsc :: Ord a => (b -> a -> b) -> b -> MinQueue a -> b |

921 | +foldlAsc f z (MinQueue _ x forest) = foldlOrd (<=) f (z `f` x) forest |

922 | +foldlAsc _ z _ = z |

923 | + |

924 | +-- | Right fold on a binomial forest. Respects order. |

925 | +foldrOrd :: (a -> a -> Bool) -> (a -> b -> b) -> b -> BinomHeap a -> b |

926 | +foldrOrd (<=) c n = foldQ0 where |

927 | + foldQ0 = foldQ1 . extractBin (<=) |

928 | + foldQ1 NoExtract = n |

929 | + foldQ1 (YesExtract x _ f) |

930 | + = x `c` foldQ0 f |

931 | + |

932 | +-- | Left fold on a binomial forest. Respects order. |

933 | +foldlOrd :: (a -> a -> Bool) -> (b -> a -> b) -> b -> BinomHeap a -> b |

934 | +foldlOrd (<=) f z = foldlQ0 z where |

935 | + foldlQ0 z = foldlQ1 z . extractBin (<=) |

936 | + foldlQ1 z NoExtract = z |

937 | + foldlQ1 z (YesExtract x _ f') |

938 | + = foldlQ0 (z `f` x) f' |

939 | + |

940 | + |

941 | +{-# INLINE fromAscList #-} |

942 | +-- | /O(n)/. Constructs a priority queue from an ascending list. /Warning/: Does not check the precondition. |

943 | +fromAscList :: [a] -> MinQueue a |

944 | +fromAscList = foldr insertMinQ Empty |

945 | + |

946 | +insertMinQ :: a -> MinQueue a -> MinQueue a |

947 | +insertMinQ x Empty = singleton x |

948 | +insertMinQ x (MinQueue n x' f) = MinQueue (n+1) x (insertMin (tip x') f) |

949 | + |

950 | +-- | @insertMin t f@ assumes that the root of @t@ compares as less than |

951 | +-- every other root in @f@, and merges accordingly. |

952 | +insertMin :: BinomTree e rk -> BinomForest e rk -> BinomForest e rk |

953 | +insertMin t Nil = Cons t Nil |

954 | +insertMin t (Skip f) = Cons t f |

955 | +insertMin (BinomTree x ts) (Cons t' f) = Skip (insertMin (BinomTree x (Succ t' ts)) f) |

956 | + |

957 | +{-# INLINE fromList #-} |

958 | +-- | /O(n log n)/. Constructs a priority queue from an unordered list. |

959 | +fromList :: Ord a => [a] -> MinQueue a |

960 | +fromList = foldr insert Empty |

961 | hunk ./containers.cabal 36 |

962 | Data.Graph |

963 | Data.Sequence |

964 | Data.Tree |

965 | + Data.PQueue |

966 | + Data.PQueue.Min |

967 | + Data.PQueue.Max |

968 | } |

969 | if impl(ghc) { |

970 | extensions: DeriveDataTypeable, MagicHash, Rank2Types |

971 | } |

972 | |

973 | Context: |

974 | |

975 | [Tweak layout to work with the alternative layout rule |

976 | Ian Lynagh <igloo@earth.li>**20091129154519] |

977 | [Disable building Data.Sequence (and dependents) for nhc98. |

978 | Malcolm.Wallace@cs.york.ac.uk**20091124025653 |

979 | There is some subtlety of polymorphically recursive datatypes and |

980 | type-class defaulting that nhc98's type system barfs over. |

981 | ] |

982 | [Fix another instance of non-ghc breakage. |

983 | Malcolm.Wallace@cs.york.ac.uk**20091123092637] |

984 | [Add #ifdef around ghc-only (<$) as member of Functor class. |

985 | Malcolm.Wallace@cs.york.ac.uk**20091123085155] |

986 | [Fix broken code in non-GHC branch of an ifdef. |

987 | Malcolm.Wallace@cs.york.ac.uk**20091123084824] |

988 | [doc bugfix: correct description of index argument |

989 | Ross Paterson <ross@soi.city.ac.uk>**20091028105532 |

990 | Ignore-this: 9790e7bf422c4cb528722c03cfa4fed9 |

991 | |

992 | As noted by iaefai on the libraries list. |

993 | |

994 | Please merge to STABLE. |

995 | ] |

996 | [Bump version to 0.3.0.0 |

997 | Ian Lynagh <igloo@earth.li>**20090920141847] |

998 | [update base dependency |

999 | Ross Paterson <ross@soi.city.ac.uk>**20090916073125 |

1000 | Ignore-this: ad382ffc6c6a18c15364e6c072f19edb |

1001 | |

1002 | The package uses mkNoRepType and Data.Functor, which were not in the |

1003 | stable branch of base-4. |

1004 | ] |

1005 | [add fast version of <$ for Seq |

1006 | Ross Paterson <ross@soi.city.ac.uk>**20090916072812 |

1007 | Ignore-this: 5a39a7d31d39760ed589790b1118d240 |

1008 | ] |

1009 | [new methods for Data.Sequence (proposal #3271) |

1010 | Ross Paterson <ross@soi.city.ac.uk>**20090915173324 |

1011 | Ignore-this: cf17bedd709a6ab3448fd718dcdf62e7 |

1012 | |

1013 | Adds a lot of new methods to Data.Sequence, mostly paralleling those |

1014 | in Data.List. Several of these are significantly faster than versions |

1015 | implemented with the previous public interface. In particular, replicate |

1016 | takes O(log n) time and space instead of O(n). |

1017 | (by Louis Wasserman) |

1018 | ] |

1019 | [Fix "Cabal check" warnings |

1020 | Ian Lynagh <igloo@earth.li>**20090811215900] |

1021 | [TAG 2009-06-25 |

1022 | Ian Lynagh <igloo@earth.li>**20090625160202] |

1023 | Patch bundle hash: |

1024 | 3ab40f81c6d139d348b36617f726a7e136f3d20f |