Safe Haskell | None |
---|---|
Language | Haskell2010 |
Pure efficient implementation of the implicit treap data structure with the segment tree interface.
NOTE: Letter \( d \) in the documentation below means depth of the tree. Real
depth depends on the strategy for creating Priority
. If the strategy is poor,
the depth can be linear. However, if priorities are generated randomly, expected
depth is \( O(\log \ n) \).
Synopsis
- newtype Size = Size {}
- newtype Priority = Priority {
- unPriority :: Word64
- data Treap m a
- empty :: Treap m a
- one :: Measured m a => Priority -> a -> Treap m a
- size :: Treap m a -> Size
- sizeInt :: Treap m a -> Int
- monoid :: Monoid m => Treap m a -> m
- at :: forall m a. Int -> Treap m a -> Maybe a
- query :: forall m a. Measured m a => Int -> Int -> Treap m a -> m
- splitAt :: forall m a. Measured m a => Int -> Treap m a -> (Treap m a, Treap m a)
- merge :: Measured m a => Treap m a -> Treap m a -> Treap m a
- take :: forall m a. Measured m a => Int -> Treap m a -> Treap m a
- drop :: forall m a. Measured m a => Int -> Treap m a -> Treap m a
- rotate :: forall m a. Measured m a => Int -> Treap m a -> Treap m a
- insert :: forall m a. Measured m a => Int -> Priority -> a -> Treap m a -> Treap m a
- delete :: forall m a. Measured m a => Int -> Treap m a -> Treap m a
- recalculate :: Measured m a => Treap m a -> Treap m a
Data structure
Size of the Treap
data structure. Guaranteed to be always non-negative.
Priority in the Treap
data structure.
Treap
data structure.
Instances
Monoid m => Measured m (Treap m a) Source # | \( O(1) \). Takes cached value from the root. |
Defined in Treap.Pure | |
Foldable (Treap m) Source # | |
Defined in Treap.Pure fold :: Monoid m0 => Treap m m0 -> m0 # foldMap :: Monoid m0 => (a -> m0) -> Treap m a -> m0 # foldr :: (a -> b -> b) -> b -> Treap m a -> b # foldr' :: (a -> b -> b) -> b -> Treap m a -> b # foldl :: (b -> a -> b) -> b -> Treap m a -> b # foldl' :: (b -> a -> b) -> b -> Treap m a -> b # foldr1 :: (a -> a -> a) -> Treap m a -> a # foldl1 :: (a -> a -> a) -> Treap m a -> a # elem :: Eq a => a -> Treap m a -> Bool # maximum :: Ord a => Treap m a -> a # minimum :: Ord a => Treap m a -> a # | |
Measured m a => IsList (Treap m a) Source # | This instance allows to create TODO: It's possible to implement \( O(n) \) algorithm however. See issue #15: https://github.com/chshersh/treap/issues/15 |
(Eq m, Eq a) => Eq (Treap m a) Source # | |
(Read m, Read a) => Read (Treap m a) Source # | |
(Show m, Show a) => Show (Treap m a) Source # | |
Generic (Treap m a) Source # | |
(NFData m, NFData a) => NFData (Treap m a) Source # | |
Defined in Treap.Pure | |
type Rep (Treap m a) Source # | |
Defined in Treap.Pure type Rep (Treap m a) = D1 (MetaData "Treap" "Treap.Pure" "treap-0.0.0.0-5jWw5ZFdWFjIs5c8K2QNdd" False) (C1 (MetaCons "Node" PrefixI False) ((S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Size) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 Priority) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 m))) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 a) :*: (S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 (Treap m a)) :*: S1 (MetaSel (Nothing :: Maybe Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 (Treap m a))))) :+: C1 (MetaCons "Empty" PrefixI False) (U1 :: Type -> Type)) | |
type Item (Treap m a) Source # | |
Defined in Treap.Pure |
Smart constructors
Query functions
size :: Treap m a -> Size Source #
\( O(1) \). Returns the number of the elements in the Treap
. Always
non-negative.
Properties:
- \( \forall (t\ ::\ \mathrm{Treap}\ m\ a)\ .\ \mathrm{size}\ t \geqslant 0 \)
monoid :: Monoid m => Treap m a -> m Source #
\( O(1) \). Returns accumulated value in the root of the tree.
at :: forall m a. Int -> Treap m a -> Maybe a Source #
\( O(d) \). Lookup a value inside Treap
by a given index.
query :: forall m a. Measured m a => Int -> Int -> Treap m a -> m Source #
\( O(d) \). Return value of monoidal accumulator on a segment [l, r)
.
Cuts and joins
splitAt :: forall m a. Measured m a => Int -> Treap m a -> (Treap m a, Treap m a) Source #
\( O(d) \). splitAt i t
splits Treap
by the given index into two treaps
(t1, t2)
such that the following properties hold:
- \( \mathrm{size}\ t_1 = i \)
- \( \mathrm{size}\ t_2 = n - i \)
- \( \mathrm{merge}\ t_1\ t_2 \equiv t \)
Special cases:
merge :: Measured m a => Treap m a -> Treap m a -> Treap m a Source #
\( O(\max\ d_1\ d_2) \). Merge two Treap
s into single one.
>>>
pone p a = one (Priority p) a :: Treap (Sum Int) Int
>>>
prettyPrint $ merge (merge (pone 1 3) (pone 4 5)) (merge (pone 3 0) (pone 5 9))
4,17:9 ╱ 3,8:5 ╱╲ ╱ ╲ ╱ ╲ 1,3:3 1,0:0
Modification functions
insert :: forall m a. Measured m a => Int -> Priority -> a -> Treap m a -> Treap m a Source #
\( O(d) \). Insert a value into Treap
with given key and priority.
Updates monoidal accumulator accordingly.
Core internal functions
recalculate :: Measured m a => Treap m a -> Treap m a Source #
\( O(1) \). Calculate size and the value of the monoidal accumulator in the given root node. This function doesn't perform any recursive calls and it assumes that the values in the children are already correct. So use this function only in bottom-up manner.