Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file iMap.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153# 1 "Camomile/internal/iMap.ml"(** mappings from integer to arbitrary types *)(* Copyright (C) 2003 Yamagata Yoriyuki. distributed with LGPL *)(* 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 *)letcompare_uintn1n2=letsgn1=(n1lsr24)-(n2lsr24)inifsgn1=0then(n1land0xffffff)-(n2land0xffffff)elsesgn1let(>)n1n2=compare_uintn1n2>0let(<)n1n2=compare_uintn1n2<0let(<=)n1n2=compare_uintn1n2<=0letmax_int=~-1letmin_int=0type'at=(int*int*'a)AvlTree.treetypekey=intincludeAvlTreeletmake?(eq=(=))l(n1,n2,v)r=letn1,l=ifis_emptyl||n1=min_intthenn1,emptyelselet(k1,k2,v0),l'=split_rightmostlinifk2+1=n1&&eqvv0thenk1,l'elsen1,linletn2,r=ifis_emptyr||n2=max_intthenn2,emptyelselet(k1,k2,v0),r'=split_leftmostrinifn2+1=k1&&eqvv0thenk2,r'elsen2,rinmake_treel(n1,n2,v)rletrecfromns=ifis_emptysthenemptyelselet(n1,n2,v)asx=rootsinlets0=left_branchsinlets1=right_branchsinifn<n1thenmake_tree(fromns0)xs1elseifn>n2thenfromns1elsemake_treeempty(n,n2,v)s1letafterns=ifn=max_intthenemptyelsefrom(n+1)sletrecuntilns=ifis_emptysthenemptyelselet(n1,n2,v)asx=rootsinlets0=left_branchsinlets1=right_branchsinifn>n2thenmake_trees0x(untilns1)elseifn<n1thenuntilns0elsemake_trees0(n1,n,v)emptyletbeforens=ifn=min_intthenemptyelseuntil(n-1)sletadd_range?eqn1n2vs=ifn1>n2theninvalid_arg"IMap.add_range"elsemake?eq(beforen1s)(n1,n2,v)(aftern2s)letadd?eqnvs=add_range?eqnnvsletrecfindnm=ifis_emptymthenraiseNot_foundelselet(n1,n2,v)=rootminifn<n1thenfindn(left_branchm)elseifn1<=n&&n<=n2thenvelsefindn(right_branchm)letremove_rangen1n2m=ifn1>n2theninvalid_arg"IMap.remove_range"elseconcat(beforen1m)(aftern2m)letremovenm=remove_rangennmletrecmemnm=ifis_emptymthenfalseelselet(n1,n2,_)=rootminifn<n1thenmemn(left_branchm)elseifn1<=n&&n<=n2thentrueelsememn(right_branchm)letiter_rangeprocm=AvlTree.iter(fun(n1,n2,v)->procn1n2v)mletfold_rangefma=AvlTree.fold(fun(n1,n2,v)a->fn1n2va)maletfoldfma=letrecloopn1n2va=leta=fn1vainifn1=n2thenaelseloop(n1+1)n2vainfold_rangeloopmaletiterprocm=fold(funnv()->procnv)m()letrecmap?eqfm=ifis_emptymthenemptyelseletn1,n2,v=rootminletl=mapf(left_branchm)inletr=mapf(right_branchm)inletv=fvinmake?eql(n1,n2,v)rletmapi?eqfm=fold(funnva->add?eqn(fnv)a)memptyletrecset_to_mapsv=ifis_emptysthenemptyelselet(n1,n2)=rootsinletl=left_branchsinletr=right_branchsinmake_tree(set_to_maplv)(n1,n2,v)(set_to_maprv)letdomainm=letfn1n2_s=ISet.add_rangen1n2sinfold_rangefmISet.emptyletmap_to_setpm=letfn1n2vs=ifpvthenISet.add_rangen1n2selsesinfold_rangefmISet.empty