123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164(**************************************************************************)(* *)(* OCaml *)(* *)(* Francois Pottier, projet Cristal, INRIA Rocquencourt *)(* Jeremie Dimino, Jane Street Europe *)(* *)(* Copyright 2002 Institut National de Recherche en Informatique et *)(* en Automatique. *)(* *)(* All rights reserved. This file is distributed under the terms of *)(* the GNU Lesser General Public License version 2.1, with the *)(* special exception on linking described in the file LICENSE. *)(* *)(**************************************************************************)exceptionEmptytype'acell=|Nil|Consof{content:'a;mutablenext:'acell}type'at={mutablelength:int;mutablefirst:'acell;mutablelast:'acell}letcreate()={length=0;first=Nil;last=Nil}letclearq=q.length<-0;q.first<-Nil;q.last<-Nilletaddxq=letcell=Cons{content=x;next=Nil}inmatchq.lastwith|Nil->q.length<-1;q.first<-cell;q.last<-cell|Conslast->q.length<-q.length+1;last.next<-cell;q.last<-cellletpush=addletpeekq=matchq.firstwith|Nil->raiseEmpty|Cons{content}->contentletpeek_optq=matchq.firstwith|Nil->None|Cons{content}->Somecontentlettop=peeklettakeq=matchq.firstwith|Nil->raiseEmpty|Cons{content;next=Nil}->clearq;content|Cons{content;next}->q.length<-q.length-1;q.first<-next;contentlettake_optq=matchq.firstwith|Nil->None|Cons{content;next=Nil}->clearq;Somecontent|Cons{content;next}->q.length<-q.length-1;q.first<-next;Somecontentletpop=takeletcopy=letreccopyq_resprevcell=matchcellwith|Nil->q_res.last<-prev;q_res|Cons{content;next}->letres=Cons{content;next=Nil}inbeginmatchprevwith|Nil->q_res.first<-res|Consp->p.next<-resend;copyq_resresnextinfunq->copy{length=q.length;first=Nil;last=Nil}Nilq.firstletis_emptyq=q.length=0letlengthq=q.lengthletiter=letreciterfcell=matchcellwith|Nil->()|Cons{content;next}->fcontent;iterfnextinfunfq->iterfq.firstletfold=letrecfoldfaccucell=matchcellwith|Nil->accu|Cons{content;next}->letaccu=faccucontentinfoldfaccunextinfunfaccuq->foldfaccuq.firstlettransferq1q2=ifq1.length>0thenmatchq2.lastwith|Nil->q2.length<-q1.length;q2.first<-q1.first;q2.last<-q1.last;clearq1|Conslast->q2.length<-q2.length+q1.length;last.next<-q1.first;q2.last<-q1.last;clearq1(** {1 Iterators} *)letto_seqq=letrecauxc()=matchcwith|Nil->Seq.Nil|Cons{content=x;next;}->Seq.Cons(x,auxnext)inauxq.firstletadd_seqqi=Seq.iter(funx->pushxq)iletof_seqg=letq=create()inadd_seqqg;q