unordered_fold i ~init ~add ~remove
constructs a more incremental version of:
let%map m = i in
Map.fold m ~init ~f:add
assuming that remove
is the inverse of add
, and that the operations for different keys can be performed in any order. Note that data_equal
defaults to phys_equal
, but a more precise equality can be provided instead.
When the data for a key updates, by default remove
is called on the old data and then add
is called on the new data. update
provides an alternative single function to call each time a key's data updates, and can be used to improve efficiency.
For the initial computation, by default add
is called on all the elements in the map. As this can be inefficient, specialized_initial
can be provided to perform the computation in a more effective way.
If revert_to_init_when_empty
is true, then if the input map transitions from being full to empty, then instead of calling remove
on every kv-pair, it will instead just set the output to whatever you've passed as init
. The default value of revert_to_init_when_empty
is false
, so this optimization does not apply automatically.