Copyright | (c) Ross Paterson Ralf Hinze 2006 |
---|---|
License | BSD-style |
Maintainer | R.Paterson@city.ac.uk |
Stability | experimental |
Portability | non-portable (MPTCs and functional dependencies) |
Safe Haskell | None |
Language | Haskell2010 |
A general sequence representation with arbitrary annotations, for use as a base for implementations of various collection types, as described in section 4 of
- Ralf Hinze and Ross Paterson, "Finger trees: a simple general-purpose data structure", Journal of Functional Programming 16:2 (2006) pp 197-217. http://staff.city.ac.uk/~ross/papers/FingerTree.html
For a directly usable sequence type, see Data.Sequence
, which is
a specialization of this structure.
An amortized running time is given for each operation, with n referring to the length of the sequence. These bounds hold even in a persistent (shared) setting.
Note: Many of these operations have the same names as similar
operations on lists in the Prelude. The ambiguity may be resolved
using either qualification or the hiding
clause.
Synopsis
- data FingerTree v a
- data Digit a
- data Node v a
- deep :: Measured v a => Digit a -> FingerTree v (Node v a) -> Digit a -> FingerTree v a
- node2 :: Measured v a => a -> a -> Node v a
- node3 :: Measured v a => a -> a -> a -> Node v a
- class Monoid v => Measured v a | a -> v where
- measure :: a -> v
- empty :: Measured v a => FingerTree v a
- singleton :: Measured v a => a -> FingerTree v a
- append :: Measured v a => FingerTree v a -> FingerTree v a -> FingerTree v a
- fromList :: Measured v a => [a] -> FingerTree v a
- null :: Measured v a => FingerTree v a -> Bool
- data ViewL s a
- data ViewR s a
- viewl :: Measured v a => FingerTree v a -> ViewL (FingerTree v) a
- viewr :: Measured v a => FingerTree v a -> ViewR (FingerTree v) a
- split :: Measured v a => (v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a)
- takeUntil :: Measured v a => (v -> Bool) -> FingerTree v a -> FingerTree v a
- dropUntil :: Measured v a => (v -> Bool) -> FingerTree v a -> FingerTree v a
- reverse :: Measured v a => FingerTree v a -> FingerTree v a
- fmap' :: (Measured v1 a1, Measured v2 a2) => (a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2
- fmapWithPos :: (Measured v1 a1, Measured v2 a2) => (v1 -> a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2
- unsafeFmap :: (a -> b) -> FingerTree v a -> FingerTree v b
- traverse' :: (Measured v1 a1, Measured v2 a2, Applicative f) => (a1 -> f a2) -> FingerTree v1 a1 -> f (FingerTree v2 a2)
- traverseWithPos :: (Measured v1 a1, Measured v2 a2, Applicative f) => (v1 -> a1 -> f a2) -> FingerTree v1 a1 -> f (FingerTree v2 a2)
- unsafeTraverse :: Applicative f => (a -> f b) -> FingerTree v a -> f (FingerTree v b)
- (><) :: (Semigroup v, Container v) => v -> v -> v
- (<|) :: Cons v => Elem v -> v -> v
- (|>) :: Snoc v => v -> Elem v -> v
Documentation
data FingerTree v a Source #
A representation of a sequence of values of type a
, allowing
access to the ends in constant time, and append and split in time
logarithmic in the size of the smaller piece.
The collection is also parameterized by a measure type v
, which
is used to specify a position in the sequence for the split
operation.
The types of the operations enforce the constraint
,
which also implies that the type Measured
v av
is determined by a
.
A variety of abstract data types can be implemented by using different element types and measurements.
Instances
Instances
Instances
deep :: Measured v a => Digit a -> FingerTree v (Node v a) -> Digit a -> FingerTree v a Source #
class Monoid v => Measured v a | a -> v where Source #
Things that can be measured.
Instances
Measured v a => Measured v (Digit a) Source # | |
Defined in HaskellWorks.Data.FingerTree | |
Measured v a => Measured v (FingerTree v a) Source # | O(1). The cached measure of a tree. |
Defined in HaskellWorks.Data.FingerTree measure :: FingerTree v a -> v Source # | |
Monoid v => Measured v (Node v a) Source # | |
Defined in HaskellWorks.Data.FingerTree |
Construction
empty :: Measured v a => FingerTree v a Source #
O(1). The empty sequence.
singleton :: Measured v a => a -> FingerTree v a Source #
O(1). A singleton sequence.
append :: Measured v a => FingerTree v a -> FingerTree v a -> FingerTree v a Source #
O(log(min(n1,n2))). Concatenate two sequences.
fromList :: Measured v a => [a] -> FingerTree v a Source #
O(n). Create a sequence from a finite list of elements.
Deconstruction
View of the left end of a sequence.
Instances
Functor s => Functor (ViewL s) Source # | |
(Eq a, Eq (s a)) => Eq (ViewL s a) Source # | |
(Ord a, Ord (s a)) => Ord (ViewL s a) Source # | |
Defined in HaskellWorks.Data.FingerTree | |
(Read a, Read (s a)) => Read (ViewL s a) Source # | |
(Show a, Show (s a)) => Show (ViewL s a) Source # | |
Generic (ViewL s a) Source # | |
(NFData a, NFData (s a)) => NFData (ViewL s a) Source # | |
Defined in HaskellWorks.Data.FingerTree | |
type Rep (ViewL s a) Source # | |
Defined in HaskellWorks.Data.FingerTree type Rep (ViewL s a) = D1 ('MetaData "ViewL" "HaskellWorks.Data.FingerTree" "hw-fingertree-0.1.2.1-7DVjKwK8BqoETwS3vQ6f1i" 'False) (C1 ('MetaCons "EmptyL" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":<" ('InfixI 'RightAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (s a)))) |
View of the right end of a sequence.
EmptyR | empty sequence |
(s a) :> a infixl 5 | the sequence minus the rightmost element, -- and the rightmost element |
Instances
Functor s => Functor (ViewR s) Source # | |
(Eq a, Eq (s a)) => Eq (ViewR s a) Source # | |
(Ord a, Ord (s a)) => Ord (ViewR s a) Source # | |
Defined in HaskellWorks.Data.FingerTree | |
(Read a, Read (s a)) => Read (ViewR s a) Source # | |
(Show a, Show (s a)) => Show (ViewR s a) Source # | |
Generic (ViewR s a) Source # | |
(NFData a, NFData (s a)) => NFData (ViewR s a) Source # | |
Defined in HaskellWorks.Data.FingerTree | |
type Rep (ViewR s a) Source # | |
Defined in HaskellWorks.Data.FingerTree type Rep (ViewR s a) = D1 ('MetaData "ViewR" "HaskellWorks.Data.FingerTree" "hw-fingertree-0.1.2.1-7DVjKwK8BqoETwS3vQ6f1i" 'False) (C1 ('MetaCons "EmptyR" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons ":>" ('InfixI 'LeftAssociative 5) 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 (s a)) :*: S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 a))) |
viewl :: Measured v a => FingerTree v a -> ViewL (FingerTree v) a Source #
O(1). Analyse the left end of a sequence.
viewr :: Measured v a => FingerTree v a -> ViewR (FingerTree v) a Source #
O(1). Analyse the right end of a sequence.
split :: Measured v a => (v -> Bool) -> FingerTree v a -> (FingerTree v a, FingerTree v a) Source #
takeUntil :: Measured v a => (v -> Bool) -> FingerTree v a -> FingerTree v a Source #
dropUntil :: Measured v a => (v -> Bool) -> FingerTree v a -> FingerTree v a Source #
Transformation
reverse :: Measured v a => FingerTree v a -> FingerTree v a Source #
O(n). The reverse of a sequence.
fmap' :: (Measured v1 a1, Measured v2 a2) => (a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2 Source #
Like fmap
, but with a more constrained type.
fmapWithPos :: (Measured v1 a1, Measured v2 a2) => (v1 -> a1 -> a2) -> FingerTree v1 a1 -> FingerTree v2 a2 Source #
Map all elements of the tree with a function that also takes the measure of the prefix of the tree to the left of the element.
unsafeFmap :: (a -> b) -> FingerTree v a -> FingerTree v b Source #
Like fmap
, but safe only if the function preserves the measure.
traverse' :: (Measured v1 a1, Measured v2 a2, Applicative f) => (a1 -> f a2) -> FingerTree v1 a1 -> f (FingerTree v2 a2) Source #
Like traverse
, but with a more constrained type.
traverseWithPos :: (Measured v1 a1, Measured v2 a2, Applicative f) => (v1 -> a1 -> f a2) -> FingerTree v1 a1 -> f (FingerTree v2 a2) Source #
Traverse the tree with a function that also takes the measure of the prefix of the tree to the left of the element.
unsafeTraverse :: Applicative f => (a -> f b) -> FingerTree v a -> f (FingerTree v b) Source #
Like traverse
, but safe only if the function preserves the measure.
Example
Particular abstract data types may be implemented by defining
element types with suitable Measured
instances.
(from section 4.5 of the paper)
Simple sequences can be implemented using a Sum
monoid as a measure:
newtype Elem a = Elem { getElem :: a } instance Measured (Sum Int) (Elem a) where measure (Elem _) = Sum 1 newtype Seq a = Seq (FingerTree (Sum Int) (Elem a))
Then the measure of a subsequence is simply its length. This representation supports log-time extraction of subsequences:
take :: Int -> Seq a -> Seq a take k (Seq xs) = Seq (takeUntil (> Sum k) xs) drop :: Int -> Seq a -> Seq a drop k (Seq xs) = Seq (dropUntil (> Sum k) xs)
The module Data.Sequence
is an optimized instantiation of this type.
For further examples, see Data.IntervalMap.FingerTree and Data.PriorityQueue.FingerTree.