Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Data structure internals, helper operations and unsafe functions.
Implementation
The tree is structurally identical to the
Patricia
tree, holding Color
s as values.
A key \(k\) in the tree denotes a right-open interval \([k, k_R)\) within which every key has the same color as \(k\). \(k_R\) is the key immediately to the right of \(k\), or, if \(k\) is the rightmost key, \(+\infty\).
Two adjacent intervals must not have the same color. This both removes redundancies and allows to make assumptions about the color of the key immediately to the left.
The following is a visual example of a possible 4-bit tree under these rules:
Synopsis
- data Zebra where
- data Color
- type Prefix = Word
- type Key = Word
- beyond :: Prefix -> Key -> Bool
- upper :: Prefix -> Key
- lower :: Prefix -> Key
- type Mask = Word
- zeroBit :: Key -> Mask -> Bool
- mask :: Key -> Mask -> Word
- branchingBit :: Prefix -> Prefix -> Mask
- unsafeSizeL :: Color -> Word -> Zebra -> Word
- unsafeSizeR :: Color -> Word -> Zebra -> Word
- unsafeFillL :: Word -> Color -> Zebra -> Zebra
- data Range where
- unsafeMonoRange :: Word -> Word -> Zebra -> Maybe Color
- unsafeSizeRange :: Color -> Word -> Word -> Zebra -> Word
- unsafeFillRange :: Word -> Word -> Color -> Zebra -> Zebra
- unsafeFoldlRange :: Word -> Word -> (a -> Range -> Color -> a) -> a -> Zebra -> a
- unsafeFoldlRange' :: Word -> Word -> (a -> Range -> Color -> a) -> a -> Zebra -> a
- unsafeFoldrRange :: Word -> Word -> (Range -> Color -> a -> a) -> a -> Zebra -> a
- unsafeFoldrRange' :: Word -> Word -> (Range -> Color -> a -> a) -> a -> Zebra -> a
- unsafeSize :: Color -> Zebra -> Word
Documentation
Fully-strict one-dimensional space partitioning tree.
Bin | |
Bla | |
| |
Whi | |
| |
Nil !Color | Invariant: unreachable state. |
Space partition colors.
Bit operations
Compare
beyond :: Prefix -> Key -> Bool Source #
\(\mathcal{O}(1)\). Whether the key does not match the prefix.
Create
zeroBit :: Key -> Mask -> Bool Source #
\(\mathcal{O}(1)\).
Get the state of the masked bit from the Key
.
branchingBit :: Prefix -> Prefix -> Mask Source #
\(\mathcal{O}(1)\).
Find the bit two Prefix
es disagree on.
Note that using this function on two equal integers yields 1 << (-1)
,
which results in undefined behavior.
Directional
Size
unsafeSizeL :: Color -> Word -> Zebra -> Word Source #
\(\mathcal{O}(\min(n,W) + n_L)\). Calculate the number of keys of the given color that are smaller than the given key.
The given key must not be equal to
.maxBound
unsafeSizeR :: Color -> Word -> Zebra -> Word Source #
\(\mathcal{O}(\min(n,W) + n_R)\). Calculate the number of keys of the given color that are greater than or equal to the given key.
The given key must not be 0
.
Insert
unsafeFillL :: Word -> Color -> Zebra -> Zebra Source #
\(\mathcal{O}(\min(n,W))\). Set every key smaller than the given one to the given color.
The given key must not be 0
.
Range
A closed interval between two keys.
UnsafeRange | Invariant: \(k_L \le k_R\). |
pattern Range | Reorders endpoints to fit mathematical notation: \([12, 3]\) will be converted to \([3, 12]\). Pattern matching guarantees \(k_1 \le k_2\). |
Size
\(\mathcal{O}(\min(n,W))\). Check whether all keys in the range are of the same color.
\(k_R\) must be greater than \(k_L\).
\(\mathcal{O}(\min(n,W) + n_I)\). Calculate the number of keys of the given color in the \([k_L, k_R)\) interval.
\(k_R\) must be greater than \(k_L\).
Insert
\(\mathcal{O}(\min(n,W) + n_L)\). Set every key in the \([k_L, k_R)\) interval to the given color.
\(k_L\) must not be 0
. \(k_R\) must be greater than \(k_L\).
Fold
Left-to-right
\(\mathcal{O}(n)\). Fold left-to-right over the ranges of all the keys in the \([k_L, k_R)\) interval.
\(k_R\) must be greater than \(k_L\).
\(\mathcal{O}(n)\). Fold left-to-right over the ranges of all the keys in the \([k_L, k_R)\) interval.
\(k_R\) must be greater than \(k_L\).
Right-to-left
\(\mathcal{O}(\min(n,W) + n_{I_L})\). Fold right-to-left over the ranges of all the keys in the \([k_L, k_R)\) interval.
\(k_R\) must be greater than \(k_L\).
\(\mathcal{O}(\min(n,W) + n_I)\). Fold right-to-left with a strict accumulator over the ranges of all the keys in the \([k_L, k_R)\) interval.
\(k_R\) must be greater than \(k_L\).