12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061(* Copyright (C) 2014--2016 Petter A. Urkedal <paurkedal@gmail.com>
*
* 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 3 of the License, or (at your
* option) any later version, with the LGPL-3.0 Linking Exception.
*
* 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
* and the LGPL-3.0 Linking Exception along with this library. If not, see
* <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.
*)moduletypeS=sigtypeelttypetvalempty:tvalis_empty:t->boolvalcard:t->intvalpush:elt->t->tvalmerge:t->t->tvalpop_e:t->elt*tendmoduleMake(Elt:Set.OrderedType)=structtypeelt=Elt.ttypet=O|Yofint*elt*t*tletempty=Oletis_emptyh=h=Oletcard=function|O->0|Y(n,_,_,_)->nletrecpushe'=function|O->Y(1,e',O,O)|Y(n,e,hL,hR)->lete_min,e_max=ifElt.comparee'e<0thene',eelsee,e'inifcardhL<cardhRthenY(n+1,e_min,pushe_maxhL,hR)elseY(n+1,e_min,hL,pushe_maxhR)letrecmergehLhR=matchhL,hRwith|O,h|h,O->h|Y(nL,eL,hA,hB),Y(nR,eR,hC,hD)->ifElt.compareeLeR<0thenY(nL+nR,eL,mergehAhB,hR)elseY(nL+nR,eR,hL,mergehChD)letpop_e=function|O->invalid_arg"Caqti_heap.pop_e: Empty heap."|Y(_,e,hL,hR)->e,mergehLhRend