123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133# 1 "Camomile/internal/xArray.ml"(** XArray : extensible arrays *)(* Copyright (C) 2002, 2003 Yamagata Yoriyuki. *)(* 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 of *)(* the License, or (at your option) any later version. *)(* As a special exception to the GNU Library General Public License, you *)(* may link, statically or dynamically, a "work that uses this library" *)(* with a publicly distributed version of this library to produce an *)(* executable file containing portions of this library, and distribute *)(* that executable file under terms of your choice, without any of the *)(* additional requirements listed in clause 6 of the GNU Library General *)(* Public License. By "a publicly distributed version of this library", *)(* we mean either the unmodified Library as distributed by the authors, *)(* or a modified version of this library that is distributed under the *)(* conditions defined in clause 3 of the GNU Library General Public *)(* License. This exception does not however invalidate any other reasons *)(* why the executable file might be covered by the GNU Library General *)(* Public 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 *)(* You can contact the authour by sending email to *)(* yoriyuki.y@gmail.com *)type'axarray={mutablelen:int;mutablebuf:'aarray;default:'a}type'at='axarrayletexpandxlen=ifArray.lengthx.buf>=lenthen()elseletbuf'=Array.make(2*len)x.defaultinArray.blitx.buf0buf'0x.len;x.buf<-buf'letgetxi=if0<=i&&i<x.lenthenx.buf.(i)elseinvalid_arg"XArray.get"letsetxie=ifi<x.lenthenx.buf.(i)<-eelsebeginexpandx(i+1);ifx.len<ithenArray.fillx.bufx.len(i-x.len)x.default;x.buf.(i)<-e;x.len<-i+1endletlengthx=x.lenletinit?bufsizelendeff=letbuf=Array.make(matchbufsizewithNone->len|Somen->n)definfori=0tolen-1dobuf.(i)<-fidone;{len=len;buf=buf;default=def}typeindex=intletnth_i=iletfirst_=0letlastx=lengthx-1letlook=getletout_of_rangexi=i<0||x.len<=iletnext_i=i+1letprev_i=i-1letmove_in=i+nletcompare_index_ij=i-jletmake?bufsizelendefault=letbuf=Array.make(matchbufsizewithNone->len|Somen->n)defaultin{len=len;buf=buf;default=default}letclearx=x.len<-0letresetx=x.len<-0;x.buf<-Array.make0x.defaultletcopyx={len=x.len;buf=Array.copyx.buf;default=x.default}letsubxposlen={len=len;buf=Array.subx.bufposlen;default=x.default}letadd_elementxe=expandx(x.len+1);x.buf.(x.len)<-e;x.len<-x.len+1letadd_arrayxa=expandx(x.len+Array.lengtha);Array.blita0x.bufx.len(Array.lengtha);x.len<-x.len+Array.lengthaletadd_xarrayx1x2=expandx1(x1.len+x2.len);Array.blitx2.buf0x1.bufx1.lenx2.len;x1.len<-x1.len+x2.lenletshrinkxlen=x.len<-minx.len(max0len)letappendx1x2=letbuf=Array.make(x1.len+x2.len)x1.defaultinArray.blitx1.buf0buf0x1.len;Array.blitx2.buf0bufx1.lenx2.len;{len=x1.len+x2.len;buf=buf;default=x1.default}letarray_ofx=Array.subx.buf0x.lenletiterprocx=fori=0tox.len-1doprocx.buf.(i)done