Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file tbl31.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524# 1 "Camomile/internal/tbl31.ml"(** Tbl31 : fast table keyed by integers *)(* 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 *)(* CRC-hash, algorithm comes from addnode.c/pathalias *)(* 31-bits CRC-polynomial, by Andrew Appel*)letpoly=0x48000000letcrc_tbl=Array.init128(funi->letrecloopjsum=ifj<0thensumelseifiland(1lslj)<>0thenloop(j-1)(sumlxor(polylsrj))elseloop(j-1)suminloop(7-1)0)letbyte3n=nlsr24land127letbyte2n=nlsr16land255letbyte1n=nlsr8land255letbyte0n=nland255let(lsl)xn=ifn>=Sys.word_sizethen0elseifn<=~-Sys.word_sizethen0elseifn<0thenxlsr(~-n)elsexlslntype'atbl='aarrayarrayarrayarraytype'at='atbltype'atagged=Tagof'a*intletuntag(Tag(a,_))=aletid(Tag(_,n))=nletgettbln=letlev=Array.unsafe_gettbl(byte3n)inletlev=Array.unsafe_getlev(byte2n)inletlev=Array.unsafe_getlev(byte1n)inArray.unsafe_getlev(byte0n)(* let get tbl n =
Printf.printf "level 3 %d" (Array.length tbl); print_newline ();
let lev = tbl.(byte3 n) in
Printf.printf "level 2 %d" (Array.length tbl); print_newline ();
let lev = lev.(byte2 n) in
Printf.printf "level 1 %d" (Array.length tbl); print_newline ();
let lev = lev.(byte1 n) in
Printf.printf "level 0 %d" (Array.length tbl); print_newline ();
lev.(byte0 n) *)moduletypeNodeType=sigtypeelttypetvallevel:intvalmake:elt->ttaggedvalof_map:int->elt->eltIMap.t->ttaggedvalof_set:int->elt->ISet.t->elt->ttaggedendmoduleMakeNode(Sub:NodeType)=structtypeelt=Sub.elttypenode=Sub.tarraytypet=nodeletlevel=Sub.level+1moduleNodeHash=structtypet=nodetaggedletequalxy=leta=untagxinletb=untagyinletrecloopi=ifi<0thentrueelseifa.(i)==b.(i)thenloop(i-1)elsefalseinloop(iflevel=3then127else255)lethash=idendmoduleNodePool=Weak.Make(NodeHash)letpool=NodePool.create256letcrc_hashv=letrecloopisum=ifi<0thensumelseleta=idv.(i)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte3a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte2a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte1a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte0a)land0x7f)inloop(i-1)suminloop(iflevel=3then127else255)0lethashconsa=letn=crc_hashainletb=Array.mapuntagain(* prerr_int (Array.length b); prerr_newline(); *)letx=Tag(b,n)intryNodePool.findpoolxwithNot_found->NodePool.addpoolx;xletmake_rawdef=Array.make(iflevel=3then128else256)(Sub.makedef)letmakedef=hashcons(make_rawdef)letof_mapn0defm=leta=make_rawdefinbeginifIMap.is_emptymthen()elseletl=AvlTree.left_branchminletr=AvlTree.right_branchminifIMap.is_emptyl&&IMap.is_emptyrthenletk1,k2,v=AvlTree.rootminleti1=(k1-n0)lsr(8*level)inletn1=n0lor(i1lsl(8*level))inletn2=n1lor(1lsl(8*level)-1)ina.(i1)<-Sub.of_mapn1def(IMap.untiln2(IMap.fromn1m));leti2=(k2-n0)lsr(8*level)inifi1<>i2thenletn1=n0lor(i2lsl(8*level))inletn2=n1lor(1lsl(8*level)-1)ina.(i2)<-Sub.of_mapn1def(IMap.untiln2(IMap.fromn1m));letb=Sub.makevinfori=i1+1toi2-1doa.(i)<-bdone;else()elsefori=0toiflevel=3then127else255doletn1=n0lor(ilsl(8*level))inletn2=n1lor(1lsl(8*level)-1)inletm'=IMap.untiln2(IMap.fromn1m)inifIMap.is_emptym'then()elsea.(i)<-Sub.of_mapn1defm'doneend;hashconsaletof_setn0defsv=leta=make_rawdefinfori=0toiflevel=3then127else255doletn1=n0lor(ilsl(8*level))inletn2=n1lor(1lsl(8*level)-1)inlets'=ISet.untiln2(ISet.fromn1s)inifISet.is_emptys'then()elsea.(i)<-Sub.of_setn1defs'vdone;hashconsaendmoduleMakeTbl(Lev0:NodeType)=structmoduleLev1=MakeNode(Lev0)moduleLev2=MakeNode(Lev1)moduleLev3=MakeNode(Lev2)includeLev3letget=getletof_mapdefm=untag(Lev3.of_map0defm)endmoduleArrayLeaf(H:Hashtbl.HashedType)=structtypeelt=H.ttypet=eltarraytypenode=tletlevel=0moduleNodeHash=structtypet=nodetaggedletequalxy=leta=untagxinletb=untagyinletrecloopi=ifi>=255thentrueelseifH.equala.(i)b.(i)thenloop(i+1)elsefalseinloop0lethash=idendmodulePool=Weak.Make(NodeHash)letpool=Pool.create256letcrc_hashv=letrecloopisum=ifi<0thensumelseleta=H.hashv.(i)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte3a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte2a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte1a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte0a)land0x7f)inloop(i-1)suminloop2550lethashconsa=letn=crc_hashainletx=Tag(a,n)intryPool.findpoolxwithNot_found->Pool.addpoolx;xletmake_rawdef=Array.make256defletmakedef=hashcons(make_rawdef)letof_mapn0defm=leta=make_rawdefinIMap.iter_range(funn1n2v->(* Printf.eprintf "Tl31.ArrayLeaf.of_map : %x %x - %x: %s\n" n0 n1 n2 *)(* (String.escaped (Obj.magic v)); *)fori=n1-n0ton2-n0doa.(i)<-vdone)m;hashconsaletof_setn0defsv=leta=make_rawdefinISet.iter_range(funn1n2->fori=n1-n0ton2-n0doa.(i)<-vdone)s;hashconsaendmoduletypeType=sigtypeelttypet=elttblvalget:elttbl->int->eltvalof_map:elt->eltIMap.t->elttblendmoduleMake(H:Hashtbl.HashedType)=MakeTbl(ArrayLeaf(H))moduleStringContentsHash=structtypet=Bytes.ttaggedletequalx1x2=lets1=untagx1inlets2=untagx2inifBytes.lengths1<>Bytes.lengths2thenfalseelseletrecloopi=ifi<0thentrueelseifBytes.gets1i<>Bytes.gets2ithenfalseelseloop(i-1)inloop(Bytes.lengths1-1)lethash=idendletbytes_hashv=letrecloopisum=ifi<0thensumelseleta=Char.code(Bytes.getvi)inletsum=sumlsr7lxorcrc_tbl.(sumlxoraland0x7f)inloop(i-1)suminloop(Bytes.lengthv-5)0moduleBoolLeaf=structtypeelt=booltypet=Bytes.tletlevel=0modulePool=Weak.Make(StringContentsHash)letpool=Pool.create256lethashconss=letn=bytes_hashsinletx=Tag(s,n)intryPool.findpoolxwithNot_found->Pool.addpoolx;xletmake_rawdef=Bytes.make32(ifdefthen'\255'else'\000')letmakedef=hashcons(make_rawdef)letboolsetskb=letj=Char.code(Bytes.gets(k/8))inletj'=ifbthenjlor(1lsl(kmod8))elsejinBytes.sets(k/8)(Char.chrj')letof_mapn0defm=leta=make_rawdefinIMap.iter_range(funn1n2v->fori=n1-n0ton2-n0doboolsetaivdone)m;hashconsaletof_setn0defsv=leta=make_rawdefinISet.iter_range(funn1n2->fori=n1-n0ton2-n0doboolsetaivdone)s;hashconsaendmoduleBool=structmoduleBoolTbl=MakeTbl(BoolLeaf)includeBoolTblletof_sets=untag(BoolTbl.of_set0falsestrue)letgettbln=letlev=Array.unsafe_gettbl(byte3n)inletlev=Array.unsafe_getlev(byte2n)inletlev=Array.unsafe_getlev(byte1n)inletk=byte0ninleti=Char.code(Bytes.unsafe_getlev(k/8))inilsr(kmod8)land1<>0endmoduleCharLeaf=structtypeelt=chartypet=Bytes.tletlevel=0modulePool=Weak.Make(StringContentsHash)letpool=Pool.create256lethashconss=letn=bytes_hashsinletx=Tag(s,n)intryPool.findpoolxwithNot_found->Pool.addpoolx;xletmake_rawc=Bytes.make256cletmakec=hashcons(make_rawc)letof_mapn0defm=leta=make_rawdefinIMap.iter_range(funn1n2v->fori=n1-n0ton2-n0doBytes.setaivdone)m;hashconsaletof_setn0defsv=leta=make_rawdefinISet.iter_range(funn1n2->fori=n1-n0ton2-n0doBytes.setaivdone)s;hashconsaendmoduleChar=structmoduleCharTbl=MakeTbl(CharLeaf)includeCharTblletgettbln=letlev=Array.unsafe_gettbl(byte3n)inletlev=Array.unsafe_getlev(byte2n)inletlev=Array.unsafe_getlev(byte1n)inBytes.unsafe_getlev(byte0n)endmoduleBitsContentsHash=structtypet=Bitsvect.ttaggedletequalx1x2=leta1=untagx1inleta2=untagx2inletrecloopi=ifi<0thentrueelseifBitsvect.geta1i=Bitsvect.geta2ithenloop(i-1)elsefalseinloop255lethash=idendmoduleBitsLeaf=structtypeelt=inttypet=Bitsvect.tletlevel=0modulePool=Weak.Make(BitsContentsHash)letpool=Pool.create256lethashv=letrecloopisum=ifi<0thensumelseleta=Bitsvect.getviinletsum=sumlsr7lxorcrc_tbl.(sumlxoraland0x7f)inloop(i-1)suminloop(Bitsvect.lengthv-5)0lethashconsa=letn=hashainletx=Tag(a,n)intryPool.findpoolxwithNot_found->Pool.addpoolx;xletmake_raw=Bitsvect.make256letmakedef=hashcons(make_rawdef)letof_mapn0defm=leta=make_rawdefinIMap.iter_range(funn1n2v->fori=n1-n0ton2-n0doBitsvect.setaivdone)m;hashconsaletof_setn0defsv=leta=make_rawdefinISet.iter_range(funn1n2->fori=n1-n0ton2-n0doBitsvect.setaivdone)s;hashconsaendmoduleBits=structincludeMakeTbl(BitsLeaf)letgettbln=letlev=Array.unsafe_gettbl(byte3n)inletlev=Array.unsafe_getlev(byte2n)inletlev=Array.unsafe_getlev(byte1n)inBitsvect.unsafe_getlev(byte0n)endmoduleBytesContentsHash=structtypet=Bytesvect.ttaggedletequalx1x2=leta1=untagx1inleta2=untagx2inletrecloopi=ifi<0thentrueelseifBytesvect.geta1i=Bytesvect.geta2ithenloop(i-1)elsefalseinloop255lethash=idendmoduleBytesLeaf=structtypeelt=inttypet=Bytesvect.tletlevel=0modulePool=Weak.Make(BytesContentsHash)letpool=Pool.create256lethashv=letrecloopisum=ifi<0thensumelseleta=Bytesvect.getviinletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte3a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte2a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte1a)land0x7f)inletsum=sumlsr7lxorcrc_tbl.(sumlxor(byte0a)land0x7f)inloop(i-1)suminloop2550lethashconsa=letn=hashainletx=Tag(a,n)intryPool.findpoolxwithNot_found->Pool.addpoolx;xletmake_raw=Bytesvect.make256letmakedef=hashcons(make_rawdef)letof_mapn0defm=leta=make_rawdefinIMap.iter_range(funn1n2v->fori=n1-n0ton2-n0doBytesvect.setaivdone)m;hashconsaletof_setn0defsv=leta=make_rawdefinISet.iter_range(funn1n2->fori=n1-n0ton2-n0doBytesvect.setaivdone)s;hashconsaendmoduleBytes=structincludeMakeTbl(BytesLeaf)letgettbln=letlev=Array.unsafe_gettbl(byte3n)inletlev=Array.unsafe_getlev(byte2n)inletlev=Array.unsafe_getlev(byte1n)inBytesvect.unsafe_getlev(byte0n)end