stm-data-collection-0.1.0.0: Collection of STM-based Data Structures

Copyright(c) Alex Semin, 2015
LicenseBSD3
Maintaineralllex.semin@gmail.com
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell2010

Data.STM.PriorityQueue.Class

Description

Concurrent Priority Queue implemented over Haskell STM. This container allows to store items supplied with key and provides efficient way to retrive item with the smallest key.

import Control.Concurrent.STM
import qualified Data.STM.PriorityQueue as PQ
import Control.Monad ( forM_ )

main :: IO ()
main = do
    pq <- atomically $ (PQ.new :: STM (PQ.Impl Int Int))
    let kvs = [(2, 1), (5, 3), (1, 2), (4, 5)]
    forM_ kvs $ \(k, v) -> atomically $ PQ.insert pq k v
    x <- atomically $ PQ.deleteMin pq
    putStrLn $ "x = " ++ show x -- prints 2

Documentation

class PriorityQueue q where Source

Minimal complete definition

new, insert, peekMin, deleteMin

Methods

new :: Ord k => STM (q k v) Source

O(1). Creates empty priority queue.

insert :: Ord k => q k v -> k -> v -> STM () Source

O(log(n)). Performs insertion of an item with key. | Note: depending on the implementation time complexity may be better.

peekMin :: Ord k => q k v -> STM v Source

O(1). Returns an item with the smallest key without removing it.

deleteMin :: Ord k => q k v -> STM v Source

O(log(n)). Returns an item with the smallest key and removes it. | Note: depending on the implementation time complexity may be better.

tryDeleteMin :: Ord k => q k v -> STM (Maybe v) Source

O(log(n)). Tries to delete item and returns if transaction fails. | Note: depending on the implementation time complexity may be better.