transients-0: Transients

Copyright(c) Edward Kmett 2015
LicenseBSD-style
MaintainerEdward Kmett <ekmett@gmail.com>
Portabilitynon-portable
Safe HaskellUnsafe
LanguageHaskell2010

Data.Transient.WordMap.Internal

Contents

Description

This module suppose a Word64-based array-mapped PATRICIA Trie.

The most significant nybble is isolated by using techniques based on https://www.fpcomplete.com/user/edwardk/revisiting-matrix-multiplication/part-4 but modified to work nybble-by-nybble rather than bit-by-bit.

This structure secretly maintains a finger to the previous mutation to speed access and repeated operations.

Synopsis

Documentation

Utilities

ptrEq :: a -> a -> Bool Source

ptrNeq :: a -> a -> Bool Source

level :: Word64 -> Int Source

Note: level 0 will return a negative shift, so don't use it

apogeeBit :: Word64 -> Word64 -> Int Source

Given a pair of keys, they agree down to this height in the display, don't use this when they are the same

apogeeBit k ok = unsafeShiftR (level (xor k ok)) 2
level (xor k ok) = unsafeShiftL (apogeeBit k ok) 2

WordMaps

data Node a Source

Constructors

Full !Word64 !Offset !(SmallArray (Node a)) 
Node !Word64 !Offset !Mask !(SmallArray (Node a)) 
Tip !Word64 a 

Instances

Functor Node Source 
Foldable Node Source 
FunctorWithIndex Word64 Node Source 
FoldableWithIndex Word64 Node Source 
Show a => Show (Node a) Source 
NFData a => NFData (Node a) Source 

data WordMap a Source

Constructors

WordMap 

Instances

Functor WordMap Source 
Foldable WordMap Source 
FunctorWithIndex Word64 WordMap Source 
FoldableWithIndex Word64 WordMap Source 
IsList (WordMap a) Source 
Eq v => Eq (WordMap v) Source 
Ord v => Ord (WordMap v) Source 
Show a => Show (WordMap a) Source 
NFData a => NFData (WordMap a) Source 
AsEmpty (WordMap a) Source 
Ixed (WordMap a) Source 
At (WordMap a) Source 
type Item (WordMap a) = (Word64, a) Source 
type IxValue (WordMap a) = a Source 
type Index (WordMap a) = Word64 Source 

data TWordMap s a Source

This is a transient WordMap with a clojure-like API

Instances

AsEmpty (TWordMap s a) Source 

persisted :: (forall s. TWordMap s a) -> WordMap a Source

newtype MWordMap s a Source

This is a mutable WordMap with a classic Haskell mutable container-style API

On the plus side, this API means you don't need to carefully avoid reusing a transient On the minus side, you have an extra reference to track.

Constructors

MWordMap 

Fields

runMWordMap :: MutVar s (TWordMap s a)
 

Instances

thaw :: PrimMonad m => WordMap a -> m (MWordMap (PrimState m) a) Source

freeze :: PrimMonad m => MWordMap (PrimState m) a -> m (WordMap a) Source

Transient WordMaps

transient :: WordMap a -> TWordMap s a Source

O(1) worst-case conversion from an immutable structure to a mutable one

persistent :: PrimMonad m => TWordMap (PrimState m) a -> m (WordMap a) Source

O(1) amortized conversion from a mutable structure to an immutable one

emptyM :: PrimMonad m => m (MWordMap (PrimState m) a) Source

singleton :: Word64 -> a -> WordMap a Source

Build a singleton WordMap

singletonM :: PrimMonad m => Word64 -> a -> m (MWordMap (PrimState m) a) Source

lookupTNode :: Word64 -> TNode s a -> ST s (Maybe a) Source

lookupT :: PrimMonad m => Word64 -> TWordMap (PrimState m) a -> m (Maybe a) Source

lookupM :: PrimMonad m => Word64 -> MWordMap (PrimState m) a -> m (Maybe a) Source

modify :: (forall s. TWordMap s a -> ST s (TWordMap s b)) -> WordMap a -> WordMap b Source

Modify an immutable structure with mutable operations.

modify f wm passed f a "persisted" transient that may be reused.

modifyM :: PrimMonad m => (TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)) -> MWordMap (PrimState m) a -> m () Source

Modify a mutable wordmap with a transient operation.

query :: PrimMonad m => (WordMap a -> r) -> TWordMap (PrimState m) a -> m r Source

Query a transient structure with queries designed for an immutable structure.

This does _not_ destroy the transient, although, it does mean subsequent actions need to copy-on-write from scratch.

After query f wm, wm is considered persisted and may be reused.

queryM :: PrimMonad m => (WordMap a -> r) -> MWordMap (PrimState m) a -> m r Source

Query a mutable structure with queries designed for an immutable structure.

Construction

type Hint s = forall x. SmallMutableArray# s x -> State# s -> State# s Source

apply :: PrimMonad m => Hint (PrimState m) -> SmallMutableArray (PrimState m) a -> m () Source

forkT :: Hint s -> Word64 -> TNode s a -> Word64 -> TNode s a -> ST s (TNode s a) Source

fork :: Word64 -> Node a -> Word64 -> Node a -> Node a Source

unplugT :: Hint s -> Word64 -> TNode s a -> ST s (TNode s a) Source

plug :: Word64 -> Node a -> Node a -> Node a Source

plugT :: Hint s -> Word64 -> TNode s a -> TNode s a -> ST s (TNode s a) Source

plugPathT :: Hint s -> Word64 -> Int -> Int -> TNode s a -> SmallMutableArray s (TNode s a) -> ST s (TNode s a) Source

Given k located under acc, plugPathT k i t acc ns plugs acc recursively into each of the nodes of ns from [i..t-1] from the bottom up

plugPath :: Word64 -> Int -> Int -> Node a -> SmallArray (Node a) -> Node a Source

Given k located under acc, plugPathT k i t acc ns plugs acc recursively into each of the nodes of ns from [i..t-1] from the bottom up

unplugPathT :: Hint s -> Word64 -> Int -> Int -> SmallMutableArray s (TNode s a) -> ST s (TNode s a) Source

replugPathT :: PrimMonad m => Hint (PrimState m) -> Word64 -> Int -> Int -> Maybe v -> SmallMutableArray (PrimState m) (TNode (PrimState m) v) -> m (TNode (PrimState m) v) Source

replugPath :: Word64 -> Int -> Int -> Maybe v -> SmallArray (Node v) -> Node v Source

trimWithHint :: PrimMonad m => Hint (PrimState m) -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

O(1) This function enables us to GC the items that lie on the path to the finger.

Normally we only do this lazily as the finger moves out of a given area, but if you have particularly burdensome items for the garbage collector it may be worth paying this price to explicitly allow them to go free.

trimT :: PrimMonad m => TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

trimM :: PrimMonad m => MWordMap (PrimState m) a -> m () Source

O(1) This function enables us to GC the items that lie on the path to the finger.

Normally we only do this lazily as the finger moves out of a given area, but if you have particularly burdensome items for the garbage collector it may be worth paying this price. to explicitly allow them to go free.

trim :: WordMap a -> WordMap a Source

O(1) This function enables us to GC the items that lie on the path to the finger.

Normally we only do this lazily as the finger moves out of a given area, but if you have particularly burdensome items for the garbage collector it may be worth paying this price. to explicitly allow them to go free.

focusWithHint :: PrimMonad m => Hint (PrimState m) -> Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

focusT :: PrimMonad m => Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

This changes the location of the focus in a transient map. Operations near the focus are considerably cheaper.

focusT k wm invalidates any unpersisted transient wm it is passed

focusM :: PrimMonad m => Word64 -> MWordMap (PrimState m) a -> m () Source

This changes the location of the focus in a mutable map. Operations near the focus are considerably cheaper.

focus0 :: Word64 -> WordMap a -> WordMap a Source

This changes the location of the focus in an immutable map. Operations near the focus are considerably cheaper.

insertWithHint :: PrimMonad m => Hint (PrimState m) -> Word64 -> a -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

insertT :: PrimMonad m => Word64 -> a -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

Transient insert.

insertT k v wm invalidates any unpersisted transient wm it is passed

insertM :: PrimMonad m => Word64 -> a -> MWordMap (PrimState m) a -> m () Source

Mutable insert.

insert0 :: Word64 -> a -> WordMap a -> WordMap a Source

Immutable insert.

insert :: Word64 -> a -> WordMap a -> WordMap a Source

Immutable insert.

deleteWithHint :: PrimMonad m => Hint (PrimState m) -> Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

deleteT :: PrimMonad m => Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a) Source

Transient delete. deleteT k v wm invalidates any unpersisted transient it is passed.

deleteM :: PrimMonad m => Word64 -> MWordMap (PrimState m) a -> m () Source

Mutable delete.

delete0 :: Word64 -> WordMap a -> WordMap a Source

Immutable delete.

delete :: Word64 -> WordMap a -> WordMap a Source

Immutable delete.

Instances

stToPrim :: PrimMonad m => ST (PrimState m) a -> m a Source

fromListT :: PrimMonad m => [(Word64, a)] -> m (TWordMap (PrimState m) a) Source

toListT :: PrimMonad m => TWordMap (PrimState m) a -> m [(Word64, a)] Source

fromListM :: PrimMonad m => [(Word64, a)] -> m (MWordMap (PrimState m) a) Source

toListM :: PrimMonad m => MWordMap (PrimState m) a -> m [(Word64, a)] Source