123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141(*
* RefList - List reference
* Copyright (C) 2003 Nicolas Cannasse
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library 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. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)exceptionEmpty_listtype'at='alistrefletempty()=ref[]letis_emptyx=match!xwith|[]->true|_->falseletof_listl=reflletto_listrl=!rlletcopy~dst~src=dst:=!srcletcopy_list~dst~src=dst:=srcletaddrlitem=rl:=List.append!rl[item]letpushrlitem=rl:=item::!rlletclearrl=rl:=[]letlengthrl=List.length!rllethdrl=tryList.hd!rlwith_->raiseEmpty_listlettlrl=tryref(List.tl!rl)with_->raiseEmpty_listletiterfrl=List.iterf!rlletfor_allfrl=List.for_allf!rlletmapfrl=ref(BatList.mapf!rl)lettransformfrl=rl:=BatList.mapf!rlletmap_listfrl=BatList.mapf!rlletfindfrl=List.findf!rlletrevrl=rl:=List.rev!rlletfind_exnfexnrl=tryList.findf!rlwith_->raiseexnletfind_exc=find_exnletexistsfrl=List.existsf!rlletsort~cmprl=rl:=List.sortcmp!rlletrfindfrl=BatList.rfindf!rlletfirst=hdletlastrl=letrecloop=function|x::[]->x|_::l->loopl|[]->assertfalseinmatch!rlwith|[]->raiseEmpty_list|l->looplletremoverlitem=rl:=BatList.remove!rlitemletremove_ifpredrl=rl:=BatList.remove_ifpred!rlletremove_allrlitem=rl:=BatList.remove_all!rlitemletfilterpredrl=rl:=List.filterpred!rlletadd_sort~cmprlitem=letrecadd_aux=function|x::lnextasl->letr=cmpxiteminifr<0thenitem::lelsex::(add_auxlnext)|[]->[item]inrl:=add_aux!rlletpoprl=match!rlwith|[]->raiseEmpty_list|e::l->rl:=l;eletnpoprln=letrecpop_auxln=ifn=0thenbeginrl:=l;[]endelsematchlwith|[]->raiseEmpty_list|x::l->x::(pop_auxl(n-1))inpop_aux!rlnletcopy_enum~dst~src=dst:=BatList.of_enumsrcletenumrl=BatList.enum!rlletof_enume=ref(BatList.of_enume)letbackwardsrl=BatList.backwards!rlletof_backwardse=ref(BatList.of_backwardse)letfold_leftfal=List.fold_leftfa!lletfold_rightfla=BatList.fold_rightf!lamoduleIndex=structletremove_atrlpos=letp=ref(-1)inletrecdel_aux=function|x::l->incrp;if!p=posthenlelsex::(del_auxl)|[]->invalid_arg"RefList.Index.remove_at: index not found"inrl:=del_aux!rlletindexpredrl=letindex=ref(-1)inignore(List.find(funit->incrindex;predit;)!rl);!indexletindex_ofrlitem=letindex=ref(-1)inignore(List.find(funit->incrindex;it=item;)!rl);!indexletat_indexrlpos=List.nth!rlposletsetrlposnewitem=letp=ref(-1)inrl:=List.map(funitem->incrp;if!p=posthennewitemelseitem)!rl;if!p<pos||pos<0theninvalid_arg"RefList.Index.set: Index out of range"end