{-# OPTIONS -XBangPatterns #-}-- ----------------------------------------------------------------------------{- |
Module : Holumbus.Data.PrefixTree.PrefixSet
Copyright : Copyright (C) 2010 Uwe Schmidt
License : MIT
Maintainer : Uwe Schmidt (uwe@fh-wedel.de)
Stability : experimental
Portability: not portable
A simplified version of PrefixTree for implementing sets.
There is one important difference to the PrefixTree implementation:
The fields are not marked to be strict. This enables building the
set on the fly.
This feature is used in fuzzy search, when an index tree is restricted
to a set of keys, e.g. the set of all none case significant keys
-}-- ----------------------------------------------------------------------------moduleHolumbus.Data.PrefixTree.PrefixSetwhereimportData.List(sort,nub)importHolumbus.Data.PrefixTree.Types-- ------------------------------------------ | Set of strings implemented as lazy prefix tree.-- @type PrefixSet = PrefixTree ()@ is not feasable because of the strict fields in the PrefixTree definitiondataPrefixSet=PSempty|PSelemPrefixSet|PSnextSymPrefixSetPrefixSetderiving(Show)emptyPS::PrefixSetemptyPS=PSemptyelemPS::PrefixSet->PrefixSetelemPSs@(PSelem_)=selemPSs=PSelemselem0PS::PrefixSetelem0PS=elemPSemptyPSnextPS::Sym->PrefixSet->PrefixSet->PrefixSetnextPS_PSemptyn=nnextPSscn=PSnextscnlastPS::Sym->PrefixSet->PrefixSetlastPSsc=nextPSscemptyPSnullPS::PrefixSet->BoolnullPSPSempty=TruenullPS_=FalsesinglePS::Key->PrefixSetsinglePS=foldr(\cr->lastPScr)elem0PS-- ------------------------------------------------------------prefixPS::Key->PrefixSetprefixPS=foldr(\cr->elemPS(lastPScr))elem0PS-- ------------------------------------------------------------unionPS::PrefixSet->PrefixSet->PrefixSetunionPSPSemptyps2=ps2unionPSps1PSempty=ps1unionPS(PSelemps1)(PSelemps2)=PSelem(unionPSps1ps2)unionPS(PSelemps1)ps2=PSelem(unionPSps1ps2)unionPSps1(PSelemps2)=PSelem(unionPSps1ps2)unionPSps1@(PSnextc1s1n1)ps2@(PSnextc2s2n2)|c1<c2=nextPSc1s1(unionPSn1ps2)|c1>c2=nextPSc2s2(unionPSps1n2)|otherwise=nextPSc1(unionPSs1s2)(unionPSn1n2)-- ------------------------------------------------------------foldPS::(Key->b->b)->b->(Key->Key)->PrefixSet->bfoldPS_r_PSempty=rfoldPSfrkf(PSelemps1)=letr'=foldPSfrkfps1inf(kf[])r'foldPSfrkf(PSnextc1s1n1)=letr'=foldPSfrkfn1infoldPSfr'(kf.(c1:))s1foldWithKeyPS::(Key->b->b)->b->PrefixSet->bfoldWithKeyPSfe=foldPSfeid-- ------------------------------------------------------------elemsPS::PrefixSet->[Key]elemsPS=foldWithKeyPS(:)[]-- ------------------------------------------------------------fuzzyCharPS::(Sym->[Sym])->PrefixSet->PrefixSetfuzzyCharPS_PSempty=PSemptyfuzzyCharPSf(PSelemps)=PSelem$fuzzyCharPSfpsfuzzyCharPSf(PSnextcsn)=unionPSps1(fuzzyCharPSfn)wheres'=fuzzyCharPSfscs=sort.nub.f$cps1=foldr(\c'r'->nextPSc's'r')emptyPS$cs-- ------------------------------------------------------------fuzzyCharsPS::(Sym->[Key])->PrefixSet->PrefixSetfuzzyCharsPS_PSempty=PSemptyfuzzyCharsPSf(PSelemps)=PSelem$fuzzyCharsPSfpsfuzzyCharsPSf(PSnextcsn)=unionPSps1(fuzzyCharsPSfn)wheres'=fuzzyCharsPSfscs=sort.nub.f$cps1=foldr(\w'r'->nextPSww's'r')emptyPS$csnextPSw[]_r'=r'nextPSw(x:xs)s''r'=nextPSx(foldrlastPSs''xs)r'-- ------------------------------------------------------------