Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file conduit_trie.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106(*
* Copyright (c) 2007-2014 Dave Scott <dave.scott@citrix.com>
* Copyright (c) 2014 Anil Madhavapeddy <anil@recoil.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*)openSexplib.Stdtype'at=|Nodeofstring*'aoption*'atlist[@@derivingsexp](* Invariant: the only node with an empty string is the root *)letempty=Node("",None,[])letis_prefixab=String.lengthb>=(String.lengtha)&&String.subb0(String.lengtha)=aletcommon_prefixab=letj=ref0in(* length of common prefix *)letskip=reffalseinfori=0tomin(String.lengtha)(String.lengthb)-1doifnot!skipthenifa.[i]=b.[i]thenincrjelseskip:=truedone;String.suba0!jletsubba=letlength=String.lengthb-(String.lengtha)inString.subb(String.lengthb-length)lengthletstring=function|Node(s,_,_)->s(* Relying on the invariant that only the root node has an empty string, it is
safe to examine the first characters of the child strings. Moreover since
common prefixes are always represented as shared nodes, there can be at most
one child with the same initial character as the key we're looking up. *)letchooseremainingns=matchList.partition(funx->(stringx).[0]=remaining.[0])nswith|[n],rest->Some(n,rest)|[],_->None|_::_,_->assertfalseletrecinsertkv=function(* k could be equal to s *)|Node(s,None,ns)whenk=s->Node(s,Somev,ns)(* k could be a prefix of s *)|Node(s,v',ns)whenis_prefixks->assert(subsk<>"");Node(k,Somev,[Node(subsk,v',ns)])(* s could be a prefix of k *)|Node(s,v',ns)whenis_prefixsk->letremaining=subksinassert(remaining<>"");beginmatchchooseremainingnswith|Some(n,rest)->Node(s,v',insertremainingvn::rest)|None->Node(s,v',Node(remaining,Somev,[])::ns)end(* s and k could share a non-empty common prefix *)|Node(s,v',ns)->letp=common_prefixskinlets'=subspandk'=subkpinassert(s'<>"");assert(k'<>"");Node(p,None,[Node(s',v',ns);Node(k',Somev,[])])letrecfold_over_pathfstracc=function|Node(p,v,_)whenp=str->faccv|Node(p,v,ns)whenis_prefixpstr->letremaining=substrpinbeginmatchchooseremainingnswith|Some(n,_)->fold_over_pathfremaining(faccv)n|None->faccvend|_->accletlongest_prefixstrt=fold_over_path(funaccb->ifb=Nonethenaccelseb)strNonetletfoldfacct=letrecinnerpacc=function|Node(p',v,ns)->letpp=p^p'inletacc=matchvwith|Somev->fppvacc|None->accinList.fold_left(funaccn->innerppaccn)accnsininner""acct