123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139(**************************************************************************)(* *)(* Copyright (C) Jean-Christophe Filliatre *)(* *)(* This software is free software; you can redistribute it and/or *)(* modify it under the terms of the GNU Library General Public *)(* License version 2.1, with the special exception on linking *)(* described in file LICENSE. *)(* *)(* This software is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *)(* *)(**************************************************************************)(** A flexible array is a Braun tree (a cute data structure that can
be used for other purposes, e.g. to implement priority queues).
*)type'atree=Empty|Nodeof'atree*'a*'atreetype'at={size:int;tree:'atree;}letempty={size=0;tree=Empty}letlengtha=a.sizeletrecmake_treenv=ifn=0thenEmptyelseifnmod2=1thenlett=make_tree(n/2)vinNode(t,v,t)elseNode(make_tree(n/2)v,v,make_tree(n/2-1)v)letmakenv=ifn<0theninvalid_arg"make";{size=n;tree=make_treenv}letrecget_treeti=matchtwith|Empty->assertfalse|Node(l,x,r)->ifi=0thenxelseifimod2=1thenget_treel(i/2)elseget_treer(i/2-1)letgetai=ifi<0||i>=a.sizetheninvalid_arg"get";get_treea.treeiletrecset_treetiv=matchtwith|Empty->assertfalse|Node(l,x,r)->ifi=0thenNode(l,v,r)elseifimod2=1thenNode(set_treel(i/2)v,x,r)elseNode(l,x,set_treer(i/2-1)v)letsetaiv=ifi<0||i>=a.sizetheninvalid_arg"set";{awithtree=set_treea.treeiv}(* low extension *)letreccons_auxv=function|Empty->Node(Empty,v,Empty)|Node(l,x,r)->Node(cons_auxxr,v,l)letconsva={size=a.size+1;tree=cons_auxva.tree}(* low removal *)letrectail_aux=function|Empty->assertfalse|Node(Empty,_,Empty)->Empty|Node(l,_,r)->Node(r,get_treel0,tail_auxl)lettaila=ifa.size=0theninvalid_arg"tail";{size=a.size-1;tree=tail_auxa.tree}(* high extension *)letrecsnoc_auxstv=matchtwith|Empty->Node(Empty,v,Empty)|Node(l,x,r)->ifsmod2=1thenNode(snoc_aux(s/2)lv,x,r)elseNode(l,x,snoc_aux(s/2-1)rv)letsnocav={size=a.size+1;tree=snoc_auxa.sizea.treev}(* high removal *)letrecliat_auxs=function|Empty->assertfalse|Node(Empty,_,Empty)->Empty|Node(l,x,r)->ifsmod2=0thenNode(liat_aux(s/2)l,x,r)elseNode(l,x,liat_aux(s/2)r)letliata=ifa.size=0theninvalid_arg"liat";{size=a.size-1;tree=liat_auxa.sizea.tree}letfoldifacca=letaddtq=ift<>EmptythenQueue.addtqinletrecloopaccicurrent(left,rightasnext)=ifi=a.sizethenbeginassert(Queue.is_emptycurrent&&Queue.is_emptyleft&&Queue.is_emptyright);accendelseifQueue.is_emptycurrentthenbeginQueue.transferrightleft;loopaccileft(current,right)endelsebeginmatchQueue.popcurrentwith|Empty->assertfalse|Node(l,x,r)->letacc=faccixinaddlleft;addrright;loopacc(i+1)currentnextendinifa.size>0thenbeginletstart=Queue.create()inQueue.adda.treestart;loopacc0start(Queue.create(),Queue.create())endelseaccletiterifa=foldi(fun()ix->fix)()aletiterfa=foldi(fun()_x->fx)()aletfoldfacca=foldi(funacc_x->faccx)acca