package urn
Install
Dune Dependency
Authors
Maintainers
Sources
md5=51102864138dbf2859a15c680a18e566
sha512=64d80d5a772d930c20bf6e2eae4127c858e59cb192dba36b9022a64746bda594631f7c631a2fcdd53c077ed930f92990791783d4d60128a5ccf2be110444fa83
doc/urn/Urn/module-type-U/index.html
Module type Urn.U
Source
Output signature of the functor Make
.
The type of weights.
The type of urns.
Creating Urns
singleton w x
returns the one-element urn containing x
with weight w
. Time complexity O(1).
of_list was
creates an urn from a list of pairs of weights and values. Time complexity O(n).
Adding Elements to Urns
add w a ur
returns an urn containing the same weights and elements as ur
but additionally containing a
with weight w
. Time complexity O(log n).
Add the weight-value pairs in the sequence to the urn. Time complexity O(m log n), where m is the length of the sequence.
Add the weight-value pairs in the list to the urn. Time complexity O(m log n), where m is the length of the list.
Sampling Urns
sample ur
samples an element of the urn ur
and returns it. Time complexity O(log n).
remove ur
samples an element of the urn ur
and returns it along with a new urn with that element removed, or None
if the new urn would be empty. Time complexity O(log n).
replace w a ur
samples the urn ur
, and returns the sampled element and its weight along with a new urn with the sampled elements removed and a
with weight w
added. Time complexity O(log n).
update f ur
samples an element of the urn ur
, then takes the chosen element a
and its weight w
, and replaces it with f w a
, returning a triple of (w, a), f w a, ur'
where ur'
is the urn containing all the elements and weights of ur
but with the chosen w, a
replaced by f w a
. Time complexity O(log n).
val update_opt :
(weight -> 'a -> (weight * 'a) option) ->
'a t ->
(weight * 'a) * (weight * 'a) option * 'a t option
update f ur
samples an element of the urn ur
, then takes the chosen element a
and its weight w
, and applies f
to them. If f w a
returns None
then the element is removed from the urn. If f w a
returns Some (w', a')
then the chosen elements weight and value is replaced by w'
and a'
respectively. The elements of the returned triple are as in update
. Time complexity O(log n).
Misc
size ur
returns the total number of elements in the urn ur
. Time complexity O(1).