Module Base__Set
type ('elt, 'cmp) tThe 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.
type ('k, 'cmp) comparator= (module Base.Comparator.S with type comparator_witness = 'cmp and type t = 'k)
val invariants : (_, _) t -> boolTests internal invariants of the set data structure. Returns true on success.
val comparator_s : ('a, 'cmp) t -> ('a, 'cmp) comparatorReturns a first-class module that can be used to build other map/set/etc with the same notion of comparison.
val comparator : ('a, 'cmp) t -> ('a, 'cmp) Base.Comparator.tval empty : ('a, 'cmp) comparator -> ('a, 'cmp) tCreates an empty set based on the provided comparator.
val singleton : ('a, 'cmp) comparator -> 'a -> ('a, 'cmp) tCreates a set based on the provided comparator that contains only the provided element.
val length : (_, _) t -> intReturns the cardinality of the set.
O(1).
val is_empty : (_, _) t -> boolis_empty tistrueifftis empty.O(1).
val mem : ('a, _) t -> 'a -> boolmem t areturnstrueiffais int.O(log n).
val add : ('a, 'cmp) t -> 'a -> ('a, 'cmp) tadd t areturns a new set withaadded tot, or returnstifmem t a.O(log n).
val remove : ('a, 'cmp) t -> 'a -> ('a, 'cmp) tremove t areturns a new set witharemoved fromtifmem t a, or returnstotherwise.O(log n).
val union : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'cmp) tunion t1 t2returns the union of the two sets.O(length t1 + length t2).
val union_list : ('a, 'cmp) comparator -> ('a, 'cmp) t list -> ('a, 'cmp) tunion c listreturns the union of all the sets inlist. Thecomparatorargument is required for the case wherelistis empty.O(max(List.length list, n log n)), wherenis the sum of sizes of the input sets.
val inter : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'cmp) tinter t1 t2computes the intersection of setst1andt2.O(length t1 + length t2).
val diff : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'cmp) tdiff t1 t2computes the set differencet1 - t2, i.e., the set containing all elements int1that are not int2.O(length t1 + length t2).
val symmetric_diff : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'a) Base.Either.t Base.Sequence.tsymmetric_diff t1 t2returns a sequence of changes betweent1andt2. It is intended to be efficient in the case wheret1andt2share a large amount of structure.
val compare_direct : ('a, 'cmp) t -> ('a, 'cmp) t -> intcompare_direct t1 t2compares the setst1andt2. It returns the same result ascompare, but unlike compare, doesn't require arguments to be passed in for the type parameters of the set.O(length t1 + length t2).
val hash_fold_direct : 'a Base.Hash.folder -> ('a, 'cmp) t Base.Hash.folderHash function: a building block to use when hashing data structures containing sets in them.
hash_fold_direct hash_fold_keyis compatible withcompare_directiffhash_fold_keyis compatible with(comparator s).compareof the setsbeing hashed.
val equal : ('a, 'cmp) t -> ('a, 'cmp) t -> boolequal t1 t2returnstrueiff the two sets have the same elements.O(length t1 + length t2)
val exists : ('a, _) t -> f:('a -> bool) -> boolexists t ~freturnstrueiff there exists anaintfor whichf a.O(n), but returns as soon as it finds anafor whichf a.
val for_all : ('a, _) t -> f:('a -> bool) -> boolfor_all t ~freturnstrueiff for allaint,f a.O(n), but returns as soon as it finds anafor whichnot (f a).
val count : ('a, _) t -> f:('a -> bool) -> intcount treturns the number of elements oftfor whichfreturnstrue.O(n).
val sum : (module Base.Container.Summable with type t = 'sum) -> ('a, _) t -> f:('a -> 'sum) -> 'sumsum treturns the sum off tfor eachtin the set.O(n).
val find : ('a, _) t -> f:('a -> bool) -> 'a optionfind t freturns an element oftfor whichfreturns true, with no guarantee as to which element is returned.O(n), but returns as soon as a suitable element is found.
val find_map : ('a, _) t -> f:('a -> 'b option) -> 'b optionfind_map t freturnsbfor someaintfor whichf a = Some b. If no suchaexists, thenfindreturnsNone.O(n), but returns as soon as a suitable element is found.
val find_exn : ('a, _) t -> f:('a -> bool) -> 'aLike
find, but throws an exception on failure.
val nth : ('a, _) t -> int -> 'a optionnth t ireturns theith smallest element oft, inO(log n)time. The smallest element hasi = 0. ReturnsNoneifi < 0ori >= length t.
val remove_index : ('a, 'cmp) t -> int -> ('a, 'cmp) tremove_index t ireturns a version oftwith theith smallest element removed, inO(log n)time. The smallest element hasi = 0. Returnstifi < 0ori >= length t.
val is_subset : ('a, 'cmp) t -> of_:('a, 'cmp) t -> boolis_subset t1 ~of_:t2returns true ifft1is a subset oft2.
module Named : sig ... endNamedallows the validation of subset and equality relationships between sets. ANamed.tis a record of a set and a name, where the name is used in error messages, andNamed.is_subsetandNamed.equalvalidate subset and equality relationships respectively.
val of_list : ('a, 'cmp) comparator -> 'a list -> ('a, 'cmp) tThe list or array given to
of_listandof_arrayneed not be sorted.
val of_array : ('a, 'cmp) comparator -> 'a array -> ('a, 'cmp) tval to_list : ('a, _) t -> 'a listto_listandto_arrayproduce sequences sorted in ascending order according to the comparator.
val to_array : ('a, _) t -> 'a arrayval of_sorted_array : ('a, 'cmp) comparator -> 'a array -> ('a, 'cmp) t Base.Or_error.tCreate 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).
val of_sorted_array_unchecked : ('a, 'cmp) comparator -> 'a array -> ('a, 'cmp) tSimilar to
of_sorted_array, but without checking the input array.
val of_increasing_iterator_unchecked : ('a, 'cmp) comparator -> len:int -> f:(int -> 'a) -> ('a, 'cmp) tof_increasing_iterator_unchecked c ~len ~fbehaves likeof_sorted_array_unchecked c (Array.init len ~f), with the additional restriction that a decreasing order is not supported. The advantage is not requiring you to allocate an intermediate array.fwill be called with 0, 1, ...len - 1, in order.
val stable_dedup_list : ('a, _) comparator -> 'a list -> 'a liststable_dedup_listis here rather than in theListmodule 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 ofComparatorand using the resultingstable_dedup_list.
val map : ('b, 'cmp) comparator -> ('a, _) t -> f:('a -> 'b) -> ('b, 'cmp) tmap c t ~freturns a new set created by applyingfto every element int. The returned set is based on the providedcomparator.O(n log n).
val filter_map : ('b, 'cmp) comparator -> ('a, _) t -> f:('a -> 'b option) -> ('b, 'cmp) tLike
map, except elements for whichfreturnsNonewill be dropped.
val filter : ('a, 'cmp) t -> f:('a -> bool) -> ('a, 'cmp) tfilter t ~freturns the subset oftfor whichfevaluates to true.O(n log n).
val fold : ('a, _) t -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accumfold t ~init ~ffolds over the elements of the set from smallest to largest.
val fold_result : ('a, _) t -> init:'accum -> f:('accum -> 'a -> ('accum, 'e) Base.Result.t) -> ('accum, 'e) Base.Result.tfold_result ~init ~ffolds over the elements of the set from smallest to largest, short circuiting the fold iff accum xis anError _
val fold_until : ('a, _) t -> init:'accum -> f:('accum -> 'a -> ('accum, 'final) Base__.Set_intf.Continue_or_stop.t) -> finish:('accum -> 'final) -> 'finalfold_until t ~init ~fis a short-circuiting version offold. IffreturnsStop _the computation ceases and results in that value. IffreturnsContinue _, the fold will proceed.
val fold_right : ('a, _) t -> init:'accum -> f:('a -> 'accum -> 'accum) -> 'accumLike
fold, except that it goes from the largest to the smallest element.
val iter : ('a, _) t -> f:('a -> unit) -> unititer t ~fcallsfon every element oft, going in order from the smallest to largest.
val iter2 : ('a, 'cmp) t -> ('a, 'cmp) t -> f:([ `Left of 'a | `Right of 'a | `Both of 'a * 'a ] -> unit) -> unitIterate two sets side by side. Complexity is
O(m+n)wheremandnare the sizes of the two input sets. As an example, with the inputs0; 1and1; 2,fwill be called with`Left 0;`Both (1, 1); and`Right 2.
val partition_tf : ('a, 'cmp) t -> f:('a -> bool) -> ('a, 'cmp) t * ('a, 'cmp) tif
a, b = partition_tf set ~fthenais the elements on whichfproducedtrue, andbis the elements on whichfproducesfalse.
val min_elt : ('a, _) t -> 'a optionReturns the smallest element of the set.
O(log n).
val max_elt : ('a, _) t -> 'a optionReturns the largest element of the set.
O(log n).
val choose : ('a, _) t -> 'a optionreturns an arbitrary element, or
Noneif the set is empty.
val split : ('a, 'cmp) t -> 'a -> ('a, 'cmp) t * 'a option * ('a, 'cmp) tsplit t xproduces a triple(t1, maybe_x, t2)wheret1is the set of elements strictly less thanx,maybe_xis the member (if any) oftwhich compares equal tox, andt2is the set of elements strictly larger thanx.
val group_by : ('a, 'cmp) t -> equiv:('a -> 'a -> bool) -> ('a, 'cmp) t listif
equivis an equivalence predicate, thengroup_by set ~equivproduces 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 ~equivproduces:
[Set.of_list ['A';'a']; Set.singleton 'b'; Set.singleton 'c']group_byruns in O(n^2) time, so if you have a comparison function, it's usually much faster to useSet.of_list.
val to_sequence : ?order:[ `Increasing | `Decreasing ] -> ?greater_or_equal_to:'a -> ?less_or_equal_to:'a -> ('a, 'cmp) t -> 'a Base.Sequence.tto_sequence tconverts the settto a sequence of the elements betweengreater_or_equal_toandless_or_equal_toinclusive in the order indicated byorder. Ifgreater_or_equal_to > less_or_equal_tothe sequence is empty. Cost is O(log n) up front and amortized O(1) for each element produced.
val binary_search : ('a, 'cmp) t -> compare:('a -> 'key -> int) -> [ `Last_strictly_less_than | `Last_less_than_or_equal_to | `Last_equal_to | `First_equal_to | `First_greater_than_or_equal_to | `First_strictly_greater_than ] -> 'key -> 'a optionbinary_search t ~compare which eltreturns the element intspecified bycompareandwhich, if one exists.tmust be sorted in increasing order according tocompare, wherecompareandeltdividetinto three (possibly empty) segments:| < elt | = elt | > elt |
binary_searchreturns an element on the boundary of segments as specified bywhich. See the diagram below next to thewhichvariants.binary_searchdoes not check thatcompareorderst, and behavior is unspecified ifcomparedoesn't ordert. Behavior is also unspecified ifcomparemutatest.
val binary_search_segmented : ('a, 'cmp) t -> segment_of:('a -> [ `Left | `Right ]) -> [ `Last_on_left | `First_on_right ] -> 'a optionbinary_search_segmented t ~segment_of whichtakes asegment_offunction that dividestinto two (possibly empty) segments:| segment_of elt = `Left | segment_of elt = `Right |
binary_search_segmentedreturns the element on the boundary of the segments as specified bywhich:`Last_on_leftyields the last element of the left segment, while`First_on_rightyields the first element of the right segment. It returnsNoneif the segment is empty.binary_search_segmenteddoes not check thatsegment_ofsegmentstas in the diagram, and behavior is unspecified ifsegment_ofdoesn't segmentt. Behavior is also unspecified ifsegment_ofmutatest.
module Merge_to_sequence_element : sig ... endProduces the elements of the two sets between
greater_or_equal_toandless_or_equal_toinorder, noting whether each element appears in the left set, the right set, or both. In the both case, both elements are returned, in case the caller can distinguish between elements that are equal to the sets' comparator. Runs in O(length t + length t').
val merge_to_sequence : ?order:[ `Increasing | `Decreasing ] -> ?greater_or_equal_to:'a -> ?less_or_equal_to:'a -> ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'a) Merge_to_sequence_element.t Base.Sequence.t
module M : functor (Elt : sig ... end) -> sig ... endMis meant to be used in combination with OCaml applicative functor types:
include Base__.Set_intf.For_deriving with type ('a, 'b) t := ('a, 'b) t
module type Sexp_of_m = sig ... endmodule type M_of_sexp = sig ... endmodule type Compare_m = sig ... endmodule type Equal_m = sig ... endmodule type Hash_fold_m = Base.Hasher.Sval sexp_of_m__t : (module Sexp_of_m with type t = 'elt) -> ('elt, 'cmp) t -> Base.Sexp.tval m__t_of_sexp : (module M_of_sexp with type comparator_witness = 'cmp and type t = 'elt) -> Base.Sexp.t -> ('elt, 'cmp) tval compare_m__t : (module Compare_m) -> ('elt, 'cmp) t -> ('elt, 'cmp) t -> intval equal_m__t : (module Equal_m) -> ('elt, 'cmp) t -> ('elt, 'cmp) t -> boolval hash_fold_m__t : (module Hash_fold_m with type t = 'elt) -> Base.Hash.state -> ('elt, _) t -> Base.Hash.stateval hash_m__t : (module Hash_fold_m with type t = 'elt) -> ('elt, _) t -> int
module Poly : Base__.Set_intf.S_poly with type 'elt t = ('elt, Base.Comparator.Poly.comparator_witness) tA polymorphic Set.
module Using_comparator : sig ... endUsing comparator is a similar interface as the toplevel of
Set, except the functions take a~comparator:('elt, 'cmp) Comparator.twhere the functions at the toplevel ofSettakes a('elt, 'cmp) comparator.
Modules and module types for extending Set
For use in extensions of Base, like Core_kernel.
module With_comparator = Base__.Set_intf.With_comparatormodule With_first_class_module = Base__.Set_intf.With_first_class_modulemodule Without_comparator = Base__.Set_intf.Without_comparatormodule type For_deriving = Base__.Set_intf.For_derivingmodule type S_poly = Base__.Set_intf.S_polymodule type Accessors0 = Base__.Set_intf.Accessors0module type Accessors1 = Base__.Set_intf.Accessors1module type Accessors2 = Base__.Set_intf.Accessors2module type Accessors_generic = Base__.Set_intf.Accessors_genericmodule type Creators0 = Base__.Set_intf.Creators0module type Creators1 = Base__.Set_intf.Creators1module type Creators2 = Base__.Set_intf.Creators2module type Creators_and_accessors2_with_comparator = Base__.Set_intf.Creators_and_accessors2_with_comparatormodule type Creators_generic = Base__.Set_intf.Creators_genericmodule type Elt_plain = Base__.Set_intf.Elt_plain