123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191open!ImportincludeHash_set_intflethashable_s=Hashtbl.hashable_slethashable=Hashtbl.Private.hashableletpoly_hashable=Hashtbl.Poly.hashableletwith_return=With_return.with_returntype'at=('a,unit)Hashtbl.ttype'ahash_set='attype'aelt='amoduleAccessors=structlethashable=hashableletclear=Hashtbl.clearletlength=Hashtbl.lengthletmem=Hashtbl.memletis_emptyt=Hashtbl.is_emptytletfind_mapt~f=with_return(funr->Hashtbl.iter_keyst~f:(funelt->matchfeltwith|None->()|Some_aso->r.returno);None);;letfindt~f=find_mapt~f:(funa->iffathenSomeaelseNone)letaddtk=Hashtbl.sett~key:k~data:()letstrict_addtk=ifmemtkthenOr_error.error_string"element already exists"else(Hashtbl.sett~key:k~data:();Result.Ok());;letstrict_add_exntk=Or_error.ok_exn(strict_addtk)letremove=Hashtbl.removeletstrict_removetk=ifmemtkthen(removetk;Result.Ok())elseOr_error.error"element not in set"k(Hashtbl.sexp_of_keyt);;letstrict_remove_exntk=Or_error.ok_exn(strict_removetk)letfoldt~init~f=Hashtbl.foldt~init~f:(fun~key~data:()acc->facckey)letitert~f=Hashtbl.iter_keyst~fletcountt~f=Container.count~foldt~fletsummt~f=Container.sum~foldmt~fletmin_eltt~compare=Container.min_elt~foldt~compareletmax_eltt~compare=Container.max_elt~foldt~compareletfold_resultt~init~f=Container.fold_result~fold~init~ftletfold_untilt~init~f=Container.fold_until~fold~init~ftletto_list=Hashtbl.keysletsexp_of_tsexp_of_et=sexp_of_listsexp_of_e(to_listt|>List.sort~compare:(hashablet).compare);;letto_arrayt=letlen=lengthtinletindex=ref(len-1)infoldt~init:[||]~f:(funacckey->ifArray.lengthacc=0thenArray.create~lenkeyelse(index:=!index-1;acc.(!index)<-key;acc));;letexistst~f=Hashtbl.existsit~f:(fun~key~data:()->fkey)letfor_allt~f=not(Hashtbl.existsit~f:(fun~key~data:()->not(fkey)))letequalt1t2=Hashtbl.equal(fun()()->true)t1t2letcopyt=Hashtbl.copytletfiltert~f=Hashtbl.filterit~f:(fun~key~data:()->fkey)letuniont1t2=Hashtbl.merget1t2~f:(fun~key:__->Some())letdifft1t2=filtert1~f:(funkey->not(Hashtbl.memt2key))letintert1t2=letsmaller,larger=iflengtht1>lengtht2thent2,t1elset1,t2inHashtbl.filterismaller~f:(fun~key~data:()->Hashtbl.memlargerkey);;letfilter_inplacet~f=letto_remove=foldt~init:[]~f:(funacx->iffxthenacelsex::ac)inList.iterto_remove~f:(funx->removetx);;letof_hashtbl_keyshashtbl=Hashtbl.maphashtbl~f:ignoreletto_hashtblt~f=Hashtbl.mapit~f:(fun~key~data:()->fkey)endincludeAccessorsletcreate?growth_allowed?sizem=Hashtbl.create?growth_allowed?sizemletof_list?growth_allowed?sizeml=letsize=matchsizewith|Somex->x|None->List.lengthlinlett=Hashtbl.create?growth_allowed~sizeminList.iterl~f:(funk->addtk);t;;lett_of_sexpme_of_sexpsexp=matchsexpwith|Sexp.Atom_->of_sexp_error"Hash_set.t_of_sexp requires a list"sexp|Sexp.Listlist->lett=createm~size:(List.lengthlist)inList.iterlist~f:(funsexp->lete=e_of_sexpsexpinmatchstrict_addtewith|Ok()->()|Error_->of_sexp_error"Hash_set.t_of_sexp got a duplicate element"sexp);t;;moduleCreators(Elt:sigtype'atvalhashable:'atHashable.tend):sigvalt_of_sexp:(Sexp.t->'aElt.t)->Sexp.t->'aElt.ttincludeCreators_genericwithtype'at:='aElt.ttwithtype'aelt:='aElt.twithtype('elt,'z)create_options:=('elt,'z)create_options_without_first_class_moduleend=structletcreate?growth_allowed?size()=create?growth_allowed?size(Hashable.to_keyElt.hashable);;letof_list?growth_allowed?sizel=of_list?growth_allowed?size(Hashable.to_keyElt.hashable)l;;lett_of_sexpe_of_sexpsexp=t_of_sexp(Hashable.to_keyElt.hashable)e_of_sexpsexpendmodulePoly=structtype'at='ahash_settype'aelt='alethashable=poly_hashableincludeCreators(structtype'at='alethashable=hashableend)includeAccessorsletsexp_of_t=sexp_of_tlett_sexp_grammargrammar=Sexplib0.Sexp_grammar.coerce(List.t_sexp_grammargrammar)endmoduleM(Elt:T.T)=structtypenonrect=Elt.ttendletsexp_of_m__t(typeelt)(moduleElt:Sexp_of_mwithtypet=elt)t=sexp_of_tElt.sexp_of_tt;;letm__t_of_sexp(typeelt)(moduleElt:M_of_sexpwithtypet=elt)sexp=t_of_sexp(moduleElt)Elt.t_of_sexpsexp;;letm__t_sexp_grammar(typeelt)(moduleElt:M_sexp_grammarwithtypet=elt)=Sexplib0.Sexp_grammar.coerce(list_sexp_grammarElt.t_sexp_grammar);;letequal_m__t(module_:Equal_m)t1t2=equalt1t2modulePrivate=structlethashable=Hashtbl.Private.hashableend