Copyright | (c) Edward Kmett 2015 |
---|---|
License | BSD-style |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Portability | non-portable |
Safe Haskell | Unsafe |
Language | Haskell2010 |
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.
- type Mask = Word16
- type Offset = Int
- ptrEq :: a -> a -> Bool
- ptrNeq :: a -> a -> Bool
- index :: Word16 -> Word16 -> Int
- level :: Word64 -> Int
- maskBit :: Word64 -> Offset -> Int
- mask :: Word64 -> Offset -> Word16
- apogeeBit :: Word64 -> Word64 -> Int
- apogee :: Word64 -> Word64 -> Mask
- data Node a
- data WordMap a = WordMap {
- fingerKey :: !Word64
- fingerMask :: !Mask
- fingerValue :: !(Maybe a)
- fingerPath :: !(SmallArray (Node a))
- data TNode s a
- data TWordMap s a = TWordMap {
- transientFingerKey :: !Word64
- transientFingerMask :: !Mask
- transientFingerValue :: !(Maybe a)
- transientFingerPath :: !(SmallMutableArray s (TNode s a))
- persisted :: (forall s. TWordMap s a) -> WordMap a
- unsafePersistentTNode :: TNode s a -> Node a
- unsafePersistent :: TWordMap s a -> WordMap a
- newtype MWordMap s a = MWordMap {
- runMWordMap :: MutVar s (TWordMap s a)
- thaw :: PrimMonad m => WordMap a -> m (MWordMap (PrimState m) a)
- freeze :: PrimMonad m => MWordMap (PrimState m) a -> m (WordMap a)
- transient :: WordMap a -> TWordMap s a
- persistent :: PrimMonad m => TWordMap (PrimState m) a -> m (WordMap a)
- empty :: WordMap a
- emptySmallMutableArray :: SmallMutableArray s a
- emptyT :: TWordMap s a
- emptyM :: PrimMonad m => m (MWordMap (PrimState m) a)
- singleton :: Word64 -> a -> WordMap a
- singletonT :: Word64 -> a -> TWordMap s a
- singletonM :: PrimMonad m => Word64 -> a -> m (MWordMap (PrimState m) a)
- lookupTNode :: Word64 -> TNode s a -> ST s (Maybe a)
- lookupT :: PrimMonad m => Word64 -> TWordMap (PrimState m) a -> m (Maybe a)
- lookupM :: PrimMonad m => Word64 -> MWordMap (PrimState m) a -> m (Maybe a)
- lookup0 :: Word64 -> WordMap a -> Maybe a
- lookupNode :: Word64 -> Node a -> Maybe a
- lookup :: Word64 -> WordMap a -> Maybe a
- modify :: (forall s. TWordMap s a -> ST s (TWordMap s b)) -> WordMap a -> WordMap b
- modifyM :: PrimMonad m => (TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)) -> MWordMap (PrimState m) a -> m ()
- query :: PrimMonad m => (WordMap a -> r) -> TWordMap (PrimState m) a -> m r
- queryM :: PrimMonad m => (WordMap a -> r) -> MWordMap (PrimState m) a -> m r
- type Hint s = forall x. SmallMutableArray# s x -> State# s -> State# s
- warm :: Hint s
- cold :: Hint s
- apply :: PrimMonad m => Hint (PrimState m) -> SmallMutableArray (PrimState m) a -> m ()
- insertSmallMutableArray :: Hint s -> SmallMutableArray s a -> Int -> a -> ST s (SmallMutableArray s a)
- insertSmallArray :: SmallArray a -> Int -> a -> SmallArray a
- deleteSmallMutableArray :: Hint s -> SmallMutableArray s a -> Int -> ST s (SmallMutableArray s a)
- deleteSmallArray :: SmallArray a -> Int -> SmallArray a
- node :: Word64 -> Offset -> Mask -> SmallArray (Node a) -> Node a
- nodeT :: Word64 -> Offset -> Mask -> SmallMutableArray s (TNode s a) -> TNode s a
- forkT :: Hint s -> Word64 -> TNode s a -> Word64 -> TNode s a -> ST s (TNode s a)
- fork :: Word64 -> Node a -> Word64 -> Node a -> Node a
- unplug :: Word64 -> Node a -> Node a
- unplugT :: Hint s -> Word64 -> TNode s a -> ST s (TNode s a)
- canonical :: WordMap a -> Maybe (Node a)
- plug :: Word64 -> Node a -> Node a -> Node a
- plugT :: Hint s -> Word64 -> TNode s a -> TNode s a -> ST s (TNode s a)
- plugPathT :: Hint s -> Word64 -> Int -> Int -> TNode s a -> SmallMutableArray s (TNode s a) -> ST s (TNode s a)
- plugPath :: Word64 -> Int -> Int -> Node a -> SmallArray (Node a) -> Node a
- unplugPathT :: Hint s -> Word64 -> Int -> Int -> SmallMutableArray s (TNode s a) -> ST s (TNode s a)
- unplugPath :: Word64 -> Int -> Int -> SmallArray (Node a) -> Node a
- replugPathT :: PrimMonad m => Hint (PrimState m) -> Word64 -> Int -> Int -> Maybe v -> SmallMutableArray (PrimState m) (TNode (PrimState m) v) -> m (TNode (PrimState m) v)
- replugPath :: Word64 -> Int -> Int -> Maybe v -> SmallArray (Node v) -> Node v
- unI# :: Int -> Int#
- trimWithHint :: PrimMonad m => Hint (PrimState m) -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- trimT :: PrimMonad m => TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- trimM :: PrimMonad m => MWordMap (PrimState m) a -> m ()
- trim :: WordMap a -> WordMap a
- focusWithHint :: PrimMonad m => Hint (PrimState m) -> Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- focusT :: PrimMonad m => Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- focusM :: PrimMonad m => Word64 -> MWordMap (PrimState m) a -> m ()
- focus0 :: Word64 -> WordMap a -> WordMap a
- focus :: Word64 -> WordMap a -> WordMap a
- insertWithHint :: PrimMonad m => Hint (PrimState m) -> Word64 -> a -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- insertT :: PrimMonad m => Word64 -> a -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- insertM :: PrimMonad m => Word64 -> a -> MWordMap (PrimState m) a -> m ()
- insert0 :: Word64 -> a -> WordMap a -> WordMap a
- insert :: Word64 -> a -> WordMap a -> WordMap a
- deleteWithHint :: PrimMonad m => Hint (PrimState m) -> Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- deleteT :: PrimMonad m => Word64 -> TWordMap (PrimState m) a -> m (TWordMap (PrimState m) a)
- deleteM :: PrimMonad m => Word64 -> MWordMap (PrimState m) a -> m ()
- delete0 :: Word64 -> WordMap a -> WordMap a
- delete :: Word64 -> WordMap a -> WordMap a
- stToPrim :: PrimMonad m => ST (PrimState m) a -> m a
- fromListT :: PrimMonad m => [(Word64, a)] -> m (TWordMap (PrimState m) a)
- toListT :: PrimMonad m => TWordMap (PrimState m) a -> m [(Word64, a)]
- fromListM :: PrimMonad m => [(Word64, a)] -> m (MWordMap (PrimState m) a)
- toListM :: PrimMonad m => MWordMap (PrimState m) a -> m [(Word64, a)]
Documentation
Utilities
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
WordMap | |
|
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 |
This is a transient WordMap with a clojure-like API
TWordMap | |
|
unsafePersistentTNode :: TNode s a -> Node a Source
unsafePersistent :: TWordMap s a -> WordMap 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.
MWordMap | |
|
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
singletonT :: Word64 -> a -> TWordMap s a Source
singletonM :: PrimMonad m => Word64 -> a -> m (MWordMap (PrimState m) a) Source
lookupNode :: Word64 -> Node a -> 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
insertSmallMutableArray :: Hint s -> SmallMutableArray s a -> Int -> a -> ST s (SmallMutableArray s a) Source
insertSmallArray :: SmallArray a -> Int -> a -> SmallArray a Source
deleteSmallMutableArray :: Hint s -> SmallMutableArray s a -> Int -> ST s (SmallMutableArray s a) Source
deleteSmallArray :: SmallArray a -> Int -> SmallArray 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
unplugPath :: Word64 -> Int -> Int -> SmallArray (Node a) -> Node 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.
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
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.