123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158(* The MIT License
Copyright (c) 2019 Craig Ferguson <craig@tarides.com>
Thomas Gazagnaire <thomas@tarides.com>
Ioana Cristescu <ioana@tarides.com>
Clément Pascutto <clement@tarides.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software. *)moduletypeARRAY=sigtypettypeeltvalget:t->int64->eltvallength:t->int64valpre_fetch:t->low:int64->high:int64->unitendmoduletypeENTRY=sigtypetmoduleKey:sigtypetvalequal:t->t->boolendmoduleValue:sigtypetendvalto_key:t->Key.tvalto_value:t->Value.tend(* Metrics must be
- totally ordered
- computable from entries and (potentially redundantly) from keys
- linearly interpolate-able on the int64 type *)moduletypeMETRIC=sigtypetmoduleEntry:ENTRYvalcompare:t->t->intvalof_entry:Entry.t->tvalof_key:Entry.Key.t->tvallinear_interpolate:low:int64*t->high:int64*t->t->int64endmoduletypeS=sigmoduleEntry:ENTRYmoduleArray:ARRAYwithtypeelt=Entry.tvalinterpolation_search:Array.t->Entry.Key.t->low:int64->high:int64->Entry.Value.tendmoduleMake(Entry:ENTRY)(Array:ARRAYwithtypeelt=Entry.t)(Metric:METRICwithmoduleEntry:=Entry):SwithmoduleEntry:=EntryandmoduleArray:=Array=structmoduleEntry=EntrymoduleArray=ArraymoduleValue=Entry.ValuemoduleKey=structincludeEntry.Keylet(=)ab=compareab=0endmoduleMetric=structincludeMetriclet(<)ab=compareab<0let(=)ab=compareab=0let(>)ab=compareab>0endletlook_aroundarraykeykey_metricindex=letrecsearch(op:int64->int64)curr=leti=opcurrinifi<0L||i>=Array.lengtharraythenraiseNot_foundelselete=array.(i)inlete_metric=Metric.of_entryeinifnotMetric.(key_metric=e_metric)thenraiseNot_foundelseifKey.equal(Entry.to_keye)keythenEntry.to_valueeelse(search[@tailcall])opiintrysearchInt64.predindexwithNot_found->(search[@tailcall])Int64.succindex(** Improves over binary search in cases where the values in some array are
uniformly distributed according to some metric (such as a hash). *)letinterpolation_searcharraykey~low~high=letkey_metric=Metric.of_keykeyin(* The core of the search *)letrecsearchlowhighlowest_entryhighest_entry=ifhigh<lowthenraiseNot_foundelse(Array.pre_fetcharray~low~high;letlowest_entry=Lazy.forcelowest_entryinifhigh=lowthenifKey.(key=Entry.to_keylowest_entry)thenEntry.to_valuelowest_entryelseraiseNot_foundelseletlowest_metric=Metric.of_entrylowest_entryinifMetric.(lowest_metric>key_metric)thenraiseNot_foundelselethighest_entry=Lazy.forcehighest_entryinlethighest_metric=Metric.of_entryhighest_entryinifMetric.(highest_metric<key_metric)thenraiseNot_foundelseletnext_index=Metric.linear_interpolate~low:(low,lowest_metric)~high:(high,highest_metric)key_metricinlete=array.(next_index)inlete_metric=Metric.of_entryeinifMetric.(key_metric=e_metric)thenifKey.(key=Entry.to_keye)thenEntry.to_valueeelselook_aroundarraykeykey_metricnext_indexelseifMetric.(key_metric>e_metric)then(search[@tailcall])Int64.(succnext_index)high(lazyarray.(Int64.(succnext_index)))(Lazy.from_valhighest_entry)else(search[@tailcall])low(Int64.prednext_index)(Lazy.from_vallowest_entry)(lazyarray.(Int64.(prednext_index))))inifhigh<0LthenraiseNot_foundelse(search[@tailcall])lowhigh(lazyarray.(low))(lazyarray.(high))end