Module Core_kernel.Set
This module defines the Set module for Core. Functions that construct a set take as an argument the comparator for the element type.
This module uses the same organizational approach as Map.
- type ('elt, 'cmp) t- = ('elt, 'cmp) Base.Set.t
- 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.
- val compare : ('elt -> 'elt -> Core_kernel__.Import.int) -> ('cmp -> 'cmp -> Core_kernel__.Import.int) -> ('elt, 'cmp) t -> ('elt, 'cmp) t -> Core_kernel__.Import.int
- type ('k, 'cmp) comparator- = (module Comparator.S with type comparator_witness = 'cmp and type t = 'k)
module Tree : sig ... endmodule Using_comparator : sig ... end- val invariants : (_, _) t -> Core_kernel__.Import.bool
- Tests internal invariants of the set data structure. Returns true on success. 
- val comparator : ('a, 'cmp) t -> ('a, 'cmp) Comparator.t
- val empty : ('a, 'cmp) comparator -> ('a, 'cmp) t
- Creates an empty set based on the provided comparator. 
- val singleton : ('a, 'cmp) comparator -> 'a -> ('a, 'cmp) t
- Creates a set based on the provided comparator that contains only the provided element. 
- val length : (_, _) t -> Core_kernel__.Import.int
- Returns the cardinality of the set. - O(1).
- val is_empty : (_, _) t -> Core_kernel__.Import.bool
- is_empty tis- trueiff- tis empty.- O(1).
- val mem : ('a, _) t -> 'a -> Core_kernel__.Import.bool
- mem t areturns- trueiff- ais in- t.- O(log n).
- val add : ('a, 'cmp) t -> 'a -> ('a, 'cmp) t
- add t areturns a new set with- aadded to- t, or returns- tif- mem t a.- O(log n).
- val remove : ('a, 'cmp) t -> 'a -> ('a, 'cmp) t
- remove t areturns a new set with- aremoved from- tif- mem t a, or returns- totherwise.- O(log n).
- val union : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'cmp) t
- union t1 t2returns the union of the two sets.- O(length t1 + length t2).
- val union_list : ('a, 'cmp) comparator -> ('a, 'cmp) t Core_kernel__.Import.list -> ('a, 'cmp) t
- union c listreturns the union of all the sets in- list. The- cargument is required for the case where- listis empty.- O(max(List.length list, n log n)), where- nis the sum of sizes of the input sets.
- val inter : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'cmp) t
- inter t1 t2computes the intersection of sets- t1and- t2.- O(length t1 + length t2).
- val diff : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'cmp) t
- diff t1 t2computes the set difference- t1 - t2, i.e., the set containing all elements in- t1that are not in- t2.- O(length t1 + length t2).
- val symmetric_diff : ('a, 'cmp) t -> ('a, 'cmp) t -> ('a, 'a) Either.t Sequence.t
- symmetric_diff t1 t2returns a sequence of changes between- t1and- t2. It is intended to be efficient in the case where- t1and- t2share a large amount of structure.
- val compare_direct : ('a, 'cmp) t -> ('a, 'cmp) t -> Core_kernel__.Import.int
- compare_direct t1 t2compares the sets- t1and- t2. It returns the same result as- compare, 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 Core_kernel__.Import.Hash.folder -> ('a, 'cmp) t Core_kernel__.Import.Hash.folder
- Hash function: a building block to use when hashing data structures containing sets in them. - hash_fold_direct hash_fold_keyis compatible with- compare_directiff- hash_fold_keyis compatible with- (comparator s).compareof the set- sbeing hashed.
- val equal : ('a, 'cmp) t -> ('a, 'cmp) t -> Core_kernel__.Import.bool
- equal t1 t2returns- trueiff the two sets have the same elements.- O(length t1 + length t2)
- val exists : ('a, _) t -> f:('a -> Core_kernel__.Import.bool) -> Core_kernel__.Import.bool
- exists t ~freturns- trueiff there exists an- ain- tfor which- f a.- O(n), but returns as soon as it finds an- afor which- f a.
- val for_all : ('a, _) t -> f:('a -> Core_kernel__.Import.bool) -> Core_kernel__.Import.bool
- for_all t ~freturns- trueiff for all- ain- t,- f a.- O(n), but returns as soon as it finds an- afor which- not (f a).
- val count : ('a, _) t -> f:('a -> Core_kernel__.Import.bool) -> Core_kernel__.Import.int
- count treturns the number of elements of- tfor which- freturns- true.- O(n).
- val sum : (module Set_intf.Container.Summable with type t = 'sum) -> ('a, _) t -> f:('a -> 'sum) -> 'sum
- sum treturns the sum of- f tfor each- tin the set.- O(n).
- val find : ('a, _) t -> f:('a -> Core_kernel__.Import.bool) -> 'a Core_kernel__.Import.option
- find t freturns an element of- tfor which- freturns 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 Core_kernel__.Import.option) -> 'b Core_kernel__.Import.option
- find_map t freturns- bfor some- ain- tfor which- f a = Some b. If no such- aexists, then- findreturns- None.- O(n), but returns as soon as a suitable element is found.
- val find_exn : ('a, _) t -> f:('a -> Core_kernel__.Import.bool) -> 'a
- Like - find, but throws an exception on failure.
- val nth : ('a, _) t -> Core_kernel__.Import.int -> 'a Core_kernel__.Import.option
- nth t ireturns the- ith smallest element of- t, in- O(log n)time. The smallest element has- i = 0. Returns- Noneif- i < 0or- i >= length t.
- val remove_index : ('a, 'cmp) t -> Core_kernel__.Import.int -> ('a, 'cmp) t
- remove_index t ireturns a version of- twith the- ith smallest element removed, in- O(log n)time. The smallest element has- i = 0. Returns- tif- i < 0or- i >= length t.
- val is_subset : ('a, 'cmp) t -> of_:('a, 'cmp) t -> Core_kernel__.Import.bool
- is_subset t1 ~of_:t2returns true iff- t1is a subset of- t2.
- module Named : sig ... end
- Namedallows the validation of subset and equality relationships between sets. A- Named.tis a record of a set and a name, where the name is used in error messages, and- Named.is_subsetand- Named.equalvalidate subset and equality relationships respectively.
- val of_list : ('a, 'cmp) comparator -> 'a Core_kernel__.Import.list -> ('a, 'cmp) t
- The list or array given to - of_listand- of_arrayneed not be sorted.
- val of_array : ('a, 'cmp) comparator -> 'a Core_kernel__.Import.array -> ('a, 'cmp) t
- val of_hash_set : ('a, 'cmp) comparator -> 'a Hash_set.t -> ('a, 'cmp) t
- val of_hashtbl_keys : ('a, 'cmp) comparator -> ('a, _) Hashtbl.t -> ('a, 'cmp) t
- val to_list : ('a, _) t -> 'a Core_kernel__.Import.list
- to_listand- to_arrayproduce sequences sorted in ascending order according to the comparator.
- val to_array : ('a, _) t -> 'a Core_kernel__.Import.array
- val to_tree : ('a, 'cmp) t -> ('a, 'cmp) Tree.t
- val of_tree : ('a, 'cmp) comparator -> ('a, 'cmp) Tree.t -> ('a, 'cmp) t
- val of_sorted_array : ('a, 'cmp) comparator -> 'a Core_kernel__.Import.array -> ('a, 'cmp) t Or_error.t
- 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).
- val of_sorted_array_unchecked : ('a, 'cmp) comparator -> 'a Core_kernel__.Import.array -> ('a, 'cmp) t
- Similar to - of_sorted_array, but without checking the input array.
- val of_increasing_iterator_unchecked : ('a, 'cmp) comparator -> len:Core_kernel__.Import.int -> f:(Core_kernel__.Import.int -> 'a) -> ('a, 'cmp) t
- of_increasing_iterator_unchecked c ~len ~fbehaves like- of_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 Core_kernel__.Import.list -> 'a Core_kernel__.Import.list
- stable_dedup_listis here rather than in the- Listmodule 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- Comparatorand using the resulting- stable_dedup_list.
- val map : ('b, 'cmp) comparator -> ('a, _) t -> f:('a -> 'b) -> ('b, 'cmp) t
- map c t ~freturns a new set created by applying- fto every element in- t. The returned set is based on the provided- c.- O(n log n).
- val filter_map : ('b, 'cmp) comparator -> ('a, _) t -> f:('a -> 'b Core_kernel__.Import.option) -> ('b, 'cmp) t
- Like - map, except elements for which- freturns- Nonewill be dropped.
- val filter : ('a, 'cmp) t -> f:('a -> Core_kernel__.Import.bool) -> ('a, 'cmp) t
- filter t ~freturns the subset of- tfor which- fevaluates to true.- O(n log n).
- val fold : ('a, _) t -> init:'accum -> f:('accum -> 'a -> 'accum) -> 'accum
- fold 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) Result.t) -> ('accum, 'e) Result.t
- fold_result ~init ~ffolds over the elements of the set from smallest to largest, short circuiting the fold if- f accum xis an- Error _
- val fold_until : ('a, _) t -> init:'accum -> f:('accum -> 'a -> ('accum, 'final) Set_intf.Continue_or_stop.t) -> finish:('accum -> 'final) -> 'final
- fold_until t ~init ~fis a short-circuiting version of- fold. If- freturns- Stop _the computation ceases and results in that value. If- freturns- Continue _, the fold will proceed.
- val fold_right : ('a, _) t -> init:'accum -> f:('a -> 'accum -> 'accum) -> 'accum
- Like - fold, except that it goes from the largest to the smallest element.
- val iter : ('a, _) t -> f:('a -> Core_kernel__.Import.unit) -> Core_kernel__.Import.unit
- iter t ~fcalls- fon every element of- t, 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 ] -> Core_kernel__.Import.unit) -> Core_kernel__.Import.unit
- Iterate two sets side by side. Complexity is - O(m+n)where- mand- nare the sizes of the two input sets. As an example, with the inputs- 0; 1and- 1; 2,- fwill be called with- `Left 0;- `Both (1, 1); and- `Right 2.
- val partition_tf : ('a, 'cmp) t -> f:('a -> Core_kernel__.Import.bool) -> ('a, 'cmp) t * ('a, 'cmp) t
- If - a, b = partition_tf set ~fthen- ais the elements on which- fproduced- true, and- bis the elements on which- fproduces- false.
- val elements : ('a, _) t -> 'a Core_kernel__.Import.list
- Same as - to_list.
- val min_elt : ('a, _) t -> 'a Core_kernel__.Import.option
- Returns the smallest element of the set. - O(log n).
- val max_elt : ('a, _) t -> 'a Core_kernel__.Import.option
- Returns the largest element of the set. - O(log n).
- val choose : ('a, _) t -> 'a Core_kernel__.Import.option
- returns an arbitrary element, or - Noneif the set is empty.
- val split : ('a, 'cmp) t -> 'a -> ('a, 'cmp) t * 'a Core_kernel__.Import.option * ('a, 'cmp) t
- split t xproduces a triple- (t1, maybe_x, t2)where- t1is the set of elements strictly less than- x,- maybe_xis the member (if any) of- twhich compares equal to- x, and- t2is the set of elements strictly larger than- x.
- val group_by : ('a, 'cmp) t -> equiv:('a -> 'a -> Core_kernel__.Import.bool) -> ('a, 'cmp) t Core_kernel__.Import.list
- If - equivis an equivalence predicate, then- group_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 ~equiv- produces: - [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 use- Set.of_list.
- val to_sequence : ?order:[ `Increasing | `Decreasing ] -> ?greater_or_equal_to:'a -> ?less_or_equal_to:'a -> ('a, 'cmp) t -> 'a Sequence.t
- to_sequence tconverts the set- tto a sequence of the elements between- greater_or_equal_toand- less_or_equal_toinclusive in the order indicated by- order. If- greater_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.
- module Merge_to_sequence_element : sig ... end
- Produces the elements of the two sets between - greater_or_equal_toand- less_or_equal_toin- order, 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 Sequence.t
- val to_map : ('key, 'cmp) t -> f:('key -> 'data) -> ('key, 'data, 'cmp) Map.t
- Convert a set to or from a map. - to_maptakes a function to produce data for each key. Both functions run in O(n) time (assuming the function passed to- to_mapruns in constant time).
- val of_map_keys : ('key, _, 'cmp) Map.t -> ('key, 'cmp) t
- val quickcheck_generator : ('key, 'cmp) comparator -> 'key Quickcheck.Generator.t -> ('key, 'cmp) t Quickcheck.Generator.t
- val quickcheck_observer : 'key Quickcheck.Observer.t -> ('key, 'cmp) t Quickcheck.Observer.t
- val quickcheck_shrinker : 'key Quickcheck.Shrinker.t -> ('key, 'cmp) t Quickcheck.Shrinker.t
Polymorphic sets
Module Poly deals with sets that use OCaml's polymorphic comparison to compare elements.
Signatures and functors for building Set modules
module type Elt_plain = Set_intf.Elt_plain- module type Elt = Set_intf.Elt
- The signature that something needs to match in order to be used as a set element. 
- module type Elt_binable = Set_intf.Elt_binable
- 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.
- module type S_plain = Set_intf.S_plain
- Module signature for a Set that doesn't support - of_sexp.
- module type S = Set_intf.S
- Module signature for a Set. 
- module type S_binable = Set_intf.S_binable
- Module signature for a Set that supports - bin_io.
- module Make_plain : functor (Elt : Elt_plain) -> S_plain with type Elt.t = Elt.t
- Makebuilds a set from an element type that has a- comparefunction but doesn't have a comparator. This generates a new comparator.
module Make_binable : functor (Elt : Elt_binable) -> S_binable with type Elt.t = Elt.tmodule Make_plain_using_comparator : functor (Elt : sig ... end) -> S_plain with type Elt.t = Elt.t with type Elt.comparator_witness = Elt.comparator_witness- module Make_using_comparator : functor (Elt : sig ... end) -> S with type Elt.t = Elt.t with type Elt.comparator_witness = Elt.comparator_witness
- Make_using_comparatorbuilds a set from an element type that has a comparator.
module Make_binable_using_comparator : functor (Elt : sig ... end) -> S_binable with type Elt.t = Elt.t with type Elt.comparator_witness = Elt.comparator_witnessinclude Base.Set.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 Hash_fold_m = Base.Hasher.S- val sexp_of_m__t : (module Sexp_of_m with type t = 'elt) -> ('elt, 'cmp) t -> Base.Sexp.t
- val m__t_of_sexp : (module M_of_sexp with type comparator_witness = 'cmp and type t = 'elt) -> Base.Sexp.t -> ('elt, 'cmp) t
- val compare_m__t : (module Compare_m) -> ('elt, 'cmp) t -> ('elt, 'cmp) t -> int
- val hash_fold_m__t : (module Hash_fold_m with type t = 'elt) -> Base.Hash.state -> ('elt, 'a) t -> Base.Hash.state
- val hash_m__t : (module Hash_fold_m with type t = 'elt) -> ('elt, 'a) t -> int
- module Stable : sig ... end
- The following types and functors may be used to define stable modules.