Module Utils.IntSetSource

Compressed sparse bit set represented as a linked list of bitmasks, such that elements from the same group of Sys.word_size - 1 are stored in a single cell.

Sourcemodule type S = SetSig.S0

intSet is a set of integers implemented as a sparse bit set.

include S with type element = int
Sourcetype element = int
Sourcetype t
include SetSig.S1 with type 'a element := element and type 'a t := t
Sourceval empty : t
Sourceval is_empty : t -> bool
Sourceval is_not_empty : t -> bool
Sourceval singleton : element -> t
Sourceval is_singleton : t -> bool
Sourceval cardinal : t -> int
Sourceval choose : t -> element
Sourceval minimum : t -> element option

minimum s returns Some x where x is the smallest element of s, or None if s is empty.

Sourceval maximum : t -> element option

maximum s returns Some x where x is the largest element of s, or None if s is empty.

Sourceval mem : element -> t -> bool
Sourceval add : element -> t -> t
Sourceval remove : element -> t -> t
Sourceval union : t -> t -> t
Sourceval inter : t -> t -> t
Sourceval disjoint : t -> t -> bool
Sourceval iter : (element -> unit) -> t -> unit
Sourceval iteri : (int -> element -> unit) -> t -> unit
Sourceval rev_iter : (element -> unit) -> t -> unit
Sourceval fold : (element -> 'b -> 'b) -> t -> 'b -> 'b
Sourceval fold_right : ('a -> element -> 'a) -> 'a -> t -> 'a

fold_right f acc x computes f acc x for each element x of s, in descending order. The initial value of acc is seed; the final value is the returned value.

Sourceval map : (element -> element) -> t -> t

map f s returns the set whose elements are f x for each x in s.

Sourceval exists : (element -> bool) -> t -> bool

exists f s returns true if f x is true for at least one element x in s.

Sourceval for_all : (element -> bool) -> t -> bool

for_all f s returns true if f x is true for all elements x in s.

Sourceval elements : t -> element list
Sourceval compare : t -> t -> int
Sourceval equal : t -> t -> bool
Sourceval subset : t -> t -> bool
Sourceval quick_subset : t -> t -> bool
Sourceval diff : t -> t -> t

diff s1 s2 returns the set of elements that are in s1 but not in s2.

Decomposing sets

These functions implement the Refine.DECOMPOSABLE interface. We cannot reference it here as Refine is implemented using bitsets, that would create a reference cycle.

Sourceval compare_minimum : t -> t -> int

compare_minimum s1 s2 compares two sets by their least elements. Returns a negative number if the least element of s1 is smaller, a positive number if the least element of s2 is smaller, or zero if both sets are empty or have equal least elements.

Sourceval extract_unique_prefix : t -> t -> t * t

extract_unique_prefix s1 s2 splits s1 into two sets:

  • The first set contains elements strictly smaller than all elements of s2
  • The second set contains the remaining elements of s1

Raises if s2 is empty.

Sourceval extract_shared_prefix : t -> t -> t * (t * t)

extract_shared_prefix s1 s2 decomposes s1 and s2 into:

  • A common prefix (elements present in both s1 and s2)
  • The remaining parts of s1 and s2

Returns (common, (rest1, rest2)) where:

  • common contains elements that are part of both s1 and s2
  • s1 = common U rest1 and s2 = common U rest2
Sourceval sorted_union : t list -> t

sorted_union s computes the union of an ordered list of sets. This is an optimized special case of union that assumes the list is already sorted by the set order.

Sourceval of_list : element list -> t

of_list xs creates a set from a list of elements.

Sourceval init_interval : element -> element -> t

init_interval i j creates a set containing all integers from i to j. The behavior is undefined if i > j.

Sourceval init_subset : element -> element -> (element -> bool) -> t

init_subset i j f creates a set containing all elements x such that i ≤ x ≤ j and f x returns true.

Sourceval filter : (element -> bool) -> t -> t

filter f s returns the set of elements x in s such that f x returns true.

Sourceval filter_map : (element -> element option) -> t -> t

filter_map f s returns the set of elements y such that f x = Some y for some element x in s.

Sourceval split : element -> t -> t * bool * t

split x s returns (s1, found, s2) where:

  • s1 contains all elements strictly smaller than x
  • s2 contains all elements strictly larger than x
  • found is true if x is in s
  • s = s1 U {x} U s2 if found is true
Sourceval find : (element -> bool) -> t -> element

find f s returns the first element x in s such that f x is true. Raises Not_found if no such element exists.

Sourceval find_map : (element -> 'b option) -> t -> 'b option

find_map f s returns Some y where y is the first element such that f x = Some y for some x in s, or None if no such element exists.

Sourceval to_seq : t -> element Stdlib.Seq.t

to_seq s returns a sequence of all elements in s in ascending order.

Sourceval bind : t -> (element -> t) -> t

bind m f returns the union of f x for all elements x in m.

Sourceval split_by_run : (element -> element) -> t -> (element * t) list

Split a set into consecutive "runs" of elements that share the same class.

Parameters

  • cls : 'a element → 'b element that assigns a class to each element.
  • xs : 'a t – the input set to be split.

Returns A list of pairs. Each pair is made of a class (the result of cls for the run) and the subset of the original elements that belong to that run (preserving the original order).

Sourceval fused_inter_union : t -> t -> acc:t -> t

fused_inter_union s1 s2 ~acc returns union (inter s1 s2) acc. This is an optimized version that computes the intersection and union in a single pass.

Sourceval rev_map_elements : t -> (element -> 'b) -> 'b list

rev_map_elements s f returns a list of f x for all elements x in s, built from the highest to the lowest element.

Sourceval map_to_array : t -> (element -> 'b) -> 'b array

map_to_array s f returns an array of f x for all elements x in s.

Sourceval rank : element -> t -> int

rank x s returns the number of elements strictly smaller than x in the set s.

Sourceval allocate : t Stdlib.ref -> int

allocate s finds the first integer not in s and returns it. The set is modified to mark the returned integer as allocated.

  • parameter s

    A reference to the set.

  • returns

    The allocated integer.