priority-queue 0.1.2 → 0.2
raw patch · 2 files changed
+17/−20 lines, 2 filesdep ~basedep ~stateref
Dependency ranges changed: base, stateref
Files
- priority-queue.cabal +3/−3
- src/Data/PriorityQueue.hs +14/−17
priority-queue.cabal view
@@ -1,5 +1,5 @@ name: priority-queue-version: 0.1.2+version: 0.2 stability: provisional license: BSD3 license-file: LICENSE@@ -24,10 +24,10 @@ extensions: CPP if flag(splitBase)- build-depends: base >= 3, containers+ build-depends: base >= 3 && <5, containers else build-depends: base < 3 cpp-options: -DNoMinViewWithKey - build-depends: reord >= 0.0.0.2, stateref >= 0.2.1.0, + build-depends: reord >= 0.0.0.2, stateref >= 0.3 && < 0.4, queue >= 0.1.1.3
src/Data/PriorityQueue.hs view
@@ -74,21 +74,18 @@ -- |Build a priority queue using an instance of the default modifiable -- reference for the requested monad and value type-mkDefaultPriorityQueue :: - ( DefaultStateRef sr m (PQ a)- , ModifyRef sr m (PQ a)- ) => sr -> PriorityQueue m a+mkDefaultPriorityQueue :: Ref m (PQ a) -> PriorityQueue m a mkDefaultPriorityQueue = PriorityQueue -- |Construct a new priority queue using the specified indexing function newPriorityQueue :: - ( DefaultStateRef sr m1 (PQ a)- , ModifyRef sr m1 (PQ a)- , NewRef sr m (PQ a)+ ( Monad m+ , HasRef m1+ , NewRef (Ref m1 (PQ a)) m (PQ a) , Ord p ) => (a -> p) -> m (PriorityQueue m1 a) newPriorityQueue f = do- pq <- newRef (emptyPQ f)+ pq <- newReference (emptyPQ f) return (mkDefaultPriorityQueue pq) @@ -96,18 +93,18 @@ -- the user's responsibility to ensure that this function provides a -- sensible order. newPriorityQueueBy :: - ( DefaultStateRef sr m1 (PQ a)- , ModifyRef sr m1 (PQ a)- , NewRef sr m (PQ a)+ ( Monad m+ , HasRef m1+ , NewRef (Ref m1 (PQ a)) m (PQ a) ) => (a -> a -> Ordering) -> m (PriorityQueue m1 a) newPriorityQueueBy cmp = newPriorityQueue (ReOrd cmp) instance Monad m => Enqueue (PriorityQueue m a) m a where- enqueue (PriorityQueue pqRef) x = modifyRef pqRef ins+ enqueue (PriorityQueue pqRef) x = modifyReference pqRef ins where ins (PQ f pq) = PQ f (M.insertWith (flip (++)) (f x) [x] pq) instance Monad m => Dequeue (PriorityQueue m a) m a where- dequeue q@(PriorityQueue pqRef) = atomicModifyRef pqRef dq+ dequeue q@(PriorityQueue pqRef) = atomicModifyReference pqRef dq where dq orig@(PQ f pq) = case minViewWithKey pq of Nothing -> (orig, Nothing)@@ -117,7 +114,7 @@ Just ((k,[i]), pq') -> (PQ f pq', Just i) Just ((k,i:is), pq') -> (PQ f (M.insert k is pq'), Just i) - dequeueBatch q@(PriorityQueue pqRef) = atomicModifyRef pqRef dq+ dequeueBatch q@(PriorityQueue pqRef) = atomicModifyReference pqRef dq where dq orig@(PQ f pq) = case M.minView pq of Nothing -> (orig, [])@@ -128,7 +125,7 @@ -- quick hack; there's probably a more efficient (and/or less ugly) way to do this instance Monad m => DequeueWhere (PriorityQueue m a) m a where- dequeueWhere (PriorityQueue pqRef) p = atomicModifyRef pqRef dq+ dequeueWhere (PriorityQueue pqRef) p = atomicModifyReference pqRef dq where extractFirstWhere :: (a -> Bool) -> [a] -> (a, [a]) extractFirstWhere p (x:xs)@@ -145,12 +142,12 @@ instance Monad m => PeekQueue (PriorityQueue m a) m a where peekQueue (PriorityQueue pqRef) = do- PQ f pq <- readRef pqRef+ PQ f pq <- readReference pqRef return [v | (k, vs) <- M.toAscList pq, v <- vs] instance Monad m => QueueSize (PriorityQueue m a) m where queueSize (PriorityQueue pqRef) = do- PQ f pq <- readRef pqRef+ PQ f pq <- readReference pqRef return (M.size pq) -- |local version of minViewWithKey, because some versions of Data.Map