This module defines the Set
module for Core.Std
. We use "core_set" as the file
name rather than "set" to avoid conflicts with OCaml's standard set module.
This module uses the same organizational approach as Core_map
. See the
documentation in core_map.mli for a description of the approach.
Functions that construct a set take as an argument the comparator for the element type.
The type of a set. The first type parameter identifies the type of the element, and the second identifies the comparator, which determines the comparison function that is used for ordering elements in this set. Many operations (e.g., union), require that they be passed sets with the same element type and the same comparator type.
A Tree.t
contains just the tree data structure that a set is based on, without
including the comparator. Accordingly, any operation on a Tree.t
must also take
as an argument the corresponding comparator.
Tests internal invariants of the set data structure. Returns true on success.
Creates an empty set based on the provided comparator.
Creates a set based on the provided comparator that contains only the provided element.
union ~comparator list
returns the union of all the sets in list
. The
comparator
argument is required for the case where list
is empty.
O(max(List.length list, n log n))
, where n
is the sum of sizes of the input sets.
exists t ~f
returns true
iff there exists an a
in t
for which f a
. O(n)
,
but returns as soon as it finds an a
for which f a
.
for_all t ~f
returns true
iff for all a
in t
, f a
. O(n)
, but returns as
soon as it finds an a
for which not (f a)
.
count t
returns the number of elements of t
for which f
returns true
.
O(n)
.
sum t
returns the sum of f t
for each t
in the set.
O(n)
.
find t f
returns an element of t
for which f
returns true, with no guarantee as
to which element is returned. O(n)
, but returns as soon as a suitable element is
found.
find_map t f
returns b
for some a
in t
for which f a = Some b
. If no such
a
exists, then find
returns None
. O(n)
, but returns as soon as a suitable
element is found.
find_index t i
returns the i
th smallest element of t
, in O(log n)
time. The
smallest element has i = 0
. Returns None
if i < 0
or i >= length t
.
The list or array given to of_list
and of_array
need not be sorted.
to_list
and to_array
produce sequences sorted in ascending order according to the
comparator.
Create set from sorted array. The input must be sorted (either in ascending or
descending order as given by the comparator) and contain no duplicates, otherwise the
result is an error. The complexity of this function is O(n)
.
Similar to of_sorted_array
, but without checking the input array.
stable_dedup_list
is here rather than in the List
module because the
implementation relies crucially on sets, and because doing so allows one to avoid uses
of polymorphic comparison by instantiating the functor at a different implementation
of Comparator
and using the resulting stable_dedup_list
.
map ~comparator t ~f
returns a new set created by applying f
to every element in
t
. The returned set is based on the provided comparator
. O(n log n)
.
Like map, except elements for which f
returns None
will be dropped.
fold t ~init ~f
folds over the elements of the set from smallest to largest.
iter t ~f
calls f
on every element of t
, going in order from the smallest to
largest.
Iterate two sets side by side. Complexity is O(m+n)
where m
and n
are the sizes
of the two input sets. As an example, with the inputs 0; 1
and 1; 2
, f
will be
called with `Left 0
; `Both (1, 1)
; and `Right 2
.
if equiv
is an equivalence predicate, then group_by set ~equiv
produces a list
of equivalence classes (i.e., a set-theoretic quotient). E.g.,
let chars = Set.of_list ['A'; 'a'; 'b'; 'c'] in
let equiv c c' = Char.equal (Char.uppercase c) (Char.uppercase c') in
group_by chars ~equiv
produces:
Set.of_list['A';'a']; Set.singleton 'b'; Set.singleton 'c']
group_by
runs in O(n^2) time, so if you have a comparison function, it's usually
much faster to use Set.of_list
.
Convert a set to or from a map. to_map
takes a function to produce data for each
key. Both functions run in O(n) time (assuming the function passed to to_map
runs
in constant time).
Module Poly deals with sets that use OCaml's polymorphic comparison to compare elements.
Set
modulesThe signature that something needs to match in order to be used as a set element.
The signature that something needs to match in order to be used as a set element if
the resulting set is going to support bin_io
.
Make_using_comparator
builds a set from an element type that has a comparator.
Make_binable_using_comparator
is similar, except the element and set types support
bin_io
.