tbox-0.1.0: Transactional variables and data structures with IO hooks

Portabilitynon-portable (requires STM)
Stabilityexperimental
MaintainerPeter Robinson <robinson@ecs.tuwien.ac.at>

Control.Concurrent.TBox.TSkipList

Contents

Description

Provides an implementation of a skip list in the AdvSTM monad. A skip list is a probabilistic data structure with map-like operations. In contrast to a balanced tree, a skip list does not need any rebalancing, which makes it suitable for concurrent programming. See: William Pugh. Skip Lists: A Probabilistic Alternative to Balanced Trees.

The elements of the skip list are stored in a TBox. When an element of the skip list is modified, the operation is relegated to the corresponding TBox.

For a concrete instance see module Control.Concurrent.TFile.TSkipList

Synopsis

Data type

data TSkipList t k a Source

newIOSource

Arguments

:: TBox t k a 
=> Float

Probability for choosing a new level

-> Int

Maximum number of levels

-> IO (TSkipList t k a) 

An empty skiplist.

Operations

insert :: (Ord k, TBox t k a) => k -> a -> TSkipList t k a -> AdvSTM ()Source

lookup :: (Ord k, TBox t k a) => k -> TSkipList t k a -> AdvSTM (Maybe a)Source

update :: (Ord k, TBox t k a) => k -> a -> TSkipList t k a -> AdvSTM ()Source

Updates an element. Throws AssertionFailed if the element is not in the list.

delete :: (Ord k, TBox t k a) => k -> TSkipList t k a -> AdvSTM ()Source

geq :: (Ord k, TBox t k a) => k -> TSkipList t k a -> AdvSTM (Map k a)Source

Returns all elements that are greater than the key. TODO: currently in O(n), can be made more efficient (like leq)

leq :: (Ord k, TBox t k a) => k -> TSkipList t k a -> AdvSTM (Map k a)Source

Returns all elements that are smaller than the key.

min :: (Ord k, TBox t k a) => TSkipList t k a -> AdvSTM (Maybe a)Source

Returns the element with the least key, if it exists. O(1).

filter :: (Ord k, TBox t k a) => (k -> a -> Bool) -> TSkipList t k a -> AdvSTM (Map k a)Source

Returns all elements that satisfy the predicate. O(n).

Low-level Operations

insertNode :: (Ord k, TBox t k a) => k -> Node t k a -> TSkipList t k a -> AdvSTM ()Source

lookupNode :: (Ord k, TBox t k a) => k -> TSkipList t k a -> AdvSTM (Maybe (Node t k a))Source

readAndValidate :: (Ord k, TBox t k a) => TSkipList t k a -> Node t k a -> AdvSTM (Maybe a)Source

Reads the TBox of the node. If the TBox is empty, the node is removed from the skip list. This is necessary when TBoxs are shared between different data structures.

newNode :: TBox t k a => k -> t k a -> Int -> AdvSTM (Node t k a)Source

contentTBox :: Node t k a -> t k aSource

key :: Node t k a -> kSource

Utilities

chooseLevel :: TSkipList t k a -> AdvSTM IntSource

Returns a randomly chosen level. Is used for inserting new elements. Note that this function uses unsafeIOToAdvSTM to access the random number generator.

toString :: (Ord k, Show k, TBox t k a) => k -> TSkipList t k a -> AdvSTM StringSource

Debug helper. Returns the skip list as a string. All elements smaller than the given key are written to the string.