imbib-1.2.5: Minimalistic .bib reference manager.
Copyright(c) Bryan O'Sullivan 2007 JP Bernardy 2010
LicenseBSD-style
Maintainernobody
Stabilityexperimental
Portabilityportable
Safe HaskellSafe-Inferred
LanguageHaskell2010

SuffixTreeCluster

Description

A suffix tree cluster implementation.

Since many function names (but not the type name) clash with Prelude names, this module is usually imported qualified, e.g.

 import Data.SuffixTreeCluster (STree)
 import qualified Data.SuffixTreeCluster as T

This implementation constructs the suffix tree lazily, so subtrees are not created until they are traversed.

Estimates are given for performance. The value k is a constant; n is the length of a query string; and t is the number of elements (nodes and leaves) in a suffix tree.

Synopsis

Types

type Alphabet a = [a] Source #

The list of symbols that constructWith can possibly see in its input.

type Edge a b = (Prefix a, STree a b) Source #

An edge in the suffix tree.

data Prefix a Source #

The prefix string associated with an Edge. Use mkPrefix to create a value of this type, and prefix to deconstruct one.

Instances

Instances details
Functor Prefix Source # 
Instance details

Defined in SuffixTreeCluster

Methods

fmap :: (a -> b) -> Prefix a -> Prefix b #

(<$) :: a -> Prefix b -> Prefix a #

Show a => Show (Prefix a) Source # 
Instance details

Defined in SuffixTreeCluster

Methods

showsPrec :: Int -> Prefix a -> ShowS #

show :: Prefix a -> String #

showList :: [Prefix a] -> ShowS #

Eq a => Eq (Prefix a) Source # 
Instance details

Defined in SuffixTreeCluster

Methods

(==) :: Prefix a -> Prefix a -> Bool #

(/=) :: Prefix a -> Prefix a -> Bool #

Ord a => Ord (Prefix a) Source # 
Instance details

Defined in SuffixTreeCluster

Methods

compare :: Prefix a -> Prefix a -> Ordering #

(<) :: Prefix a -> Prefix a -> Bool #

(<=) :: Prefix a -> Prefix a -> Bool #

(>) :: Prefix a -> Prefix a -> Bool #

(>=) :: Prefix a -> Prefix a -> Bool #

max :: Prefix a -> Prefix a -> Prefix a #

min :: Prefix a -> Prefix a -> Prefix a #

data STree a b Source #

The suffix tree type. The implementation is exposed to ease the development of custom traversal functions. Note that (Prefix a, STree a b) pairs are not stored in any order.

Constructors

Node b [Edge a b] 

Instances

Instances details
Functor (STree a) Source # 
Instance details

Defined in SuffixTreeCluster

Methods

fmap :: (a0 -> b) -> STree a a0 -> STree a b #

(<$) :: a0 -> STree a b -> STree a a0 #

(Show b, Show a) => Show (STree a b) Source # 
Instance details

Defined in SuffixTreeCluster

Methods

showsPrec :: Int -> STree a b -> ShowS #

show :: STree a b -> String #

showList :: [STree a b] -> ShowS #

Construction

construct :: (Eq b, Monoid b, Ord a) => [([a], b)] -> STree a b Source #

Querying

elem :: Eq a => [a] -> STree a b -> Bool Source #

O(n). Indicates whether the suffix tree contains the given sublist. Performance is linear in the length n of the sublist.

findEdge :: Eq a => [a] -> STree a b -> Maybe (Edge a b, Int) Source #

O(n). Finds the given subsequence in the suffix tree. On failure, returns Nothing. On success, returns the Edge in the suffix tree at which the subsequence ends, along with the number of elements to drop from the prefix of the Edge to get the "real" remaining prefix.

Here is an example:

> find "ssip" (construct "mississippi")
Just ((mkPrefix "ppi",Leaf),1)

This indicates that the edge (mkPrefix "ppi",Leaf) matches, and that we must strip 1 character from the string "ppi" to get the remaining prefix string "pi".

Performance is linear in the length n of the query list.

findTree :: Eq a => [a] -> STree a b -> Maybe (STree a b) Source #

O(n). Finds the subtree rooted at the end of the given query sequence. On failure, returns Nothing.

Performance is linear in the length n of the query list.

findPath :: Eq a => [a] -> STree a b -> [Edge a b] Source #

O(n). Returns the path from the Edge in the suffix tree at which the given subsequence ends, up to the root of the tree. If the subsequence is not found, returns the empty list.

Performance is linear in the length of the query list.

Other useful functions

mkPrefix :: [a] -> Prefix a Source #

O(1). Construct a Prefix value.

prefix :: Prefix a -> [a] Source #

O(n). Obtain the list stored in a Prefix.

suffixes :: [a] -> [[a]] Source #

O(n). Returns all non-empty suffixes of the argument, longest first. Behaves as follows:

suffixes xs == init (tails xs)

select :: Eq b => ([a], STree a [b]) -> Tree ([a], [b]) Source #

commonStrings :: (Eq b, Ord a, Ord b) => [([a], [b])] -> Map [b] [[a]] Source #

Debug

printTree :: Show a => Tree a -> IO () Source #