OrderedCollections.SortedSet (ordered_collections v0.2.2)
A sorted set implemented using Erlang's :gb_sets
.
This module provides a sorted set data structure with efficient insertions,
deletions, and membership checks. Internally it wraps Erlang's :gb_sets
to maintain elements in sorted order.
Examples
iex> set = OrderedCollections.SortedSet.new()
iex> OrderedCollections.SortedSet.to_list(set)
[]
iex> set = OrderedCollections.SortedSet.new([3, 1, 2])
iex> OrderedCollections.SortedSet.to_list(set)
[1, 2, 3]
Summary
Functions
Adds a value to the SortedSet.
Deletes a value from the SortedSet.
Returns a new SortedSet that contains elements in the first set that are not present in the second.
Returns the maximum element of the SortedSet.
Checks if a value is a member of the SortedSet.
Returns the minimum element of the SortedSet.
Creates a new empty SortedSet.
Creates a new SortedSet from a list.
Returns a list of elements in the SortedSet that fall within the given range (inclusive).
Converts the SortedSet into a list of elements in sorted order.
Returns a new SortedSet that is the union of two SortedSets.
Types
@type non_empty_set() :: :gb_sets.set()
@type non_empty_t() :: %OrderedCollections.SortedSet{set: non_empty_set()}
@opaque t()
Functions
Adds a value to the SortedSet.
Examples
iex> set = OrderedCollections.SortedSet.new([2, 3])
iex> set = OrderedCollections.SortedSet.add(set, 1)
iex> OrderedCollections.SortedSet.to_list(set)
[1, 2, 3]
Deletes a value from the SortedSet.
Examples
iex> set = OrderedCollections.SortedSet.new([1, 2, 3])
iex> set = OrderedCollections.SortedSet.delete(set, 2)
iex> OrderedCollections.SortedSet.to_list(set)
[1, 3]
Returns a new SortedSet that contains elements in the first set that are not present in the second.
Examples
iex> set1 = OrderedCollections.SortedSet.new([1, 2, 3, 4, 5])
iex> set2 = OrderedCollections.SortedSet.new([2, 4])
iex> OrderedCollections.SortedSet.difference(set1, set2) |> OrderedCollections.SortedSet.to_list()
[1, 3, 5]
@spec max(non_empty_t()) :: any()
Returns the maximum element of the SortedSet.
Examples
iex> set = OrderedCollections.SortedSet.new([3, 1, 2])
iex> OrderedCollections.SortedSet.max(set)
3
Checks if a value is a member of the SortedSet.
Examples
iex> set = OrderedCollections.SortedSet.new([1, 2, 3])
iex> OrderedCollections.SortedSet.member?(set, 2)
true
iex> OrderedCollections.SortedSet.member?(set, 4)
false
@spec min(non_empty_t()) :: any()
Returns the minimum element of the SortedSet.
Examples
iex> set = OrderedCollections.SortedSet.new([3, 1, 2])
iex> OrderedCollections.SortedSet.min(set)
1
@spec new() :: t()
Creates a new empty SortedSet.
Examples
iex> set = OrderedCollections.SortedSet.new()
iex> OrderedCollections.SortedSet.to_list(set)
[]
Creates a new SortedSet from a list.
Examples
iex> set = OrderedCollections.SortedSet.new([3, 1, 2])
iex> OrderedCollections.SortedSet.to_list(set)
[1, 2, 3]
Returns a list of elements in the SortedSet that fall within the given range (inclusive).
Examples
iex> set = OrderedCollections.SortedSet.new([1, 2, 3, 4, 5])
iex> OrderedCollections.SortedSet.range(set, 2, 4)
[2, 3, 4]
Converts the SortedSet into a list of elements in sorted order.
Examples
iex> set = OrderedCollections.SortedSet.new([3, 1, 2])
iex> OrderedCollections.SortedSet.to_list(set)
[1, 2, 3]
Returns a new SortedSet that is the union of two SortedSets.
Examples
iex> set1 = OrderedCollections.SortedSet.new([1, 3, 5])
iex> set2 = OrderedCollections.SortedSet.new([2, 3, 4])
iex> OrderedCollections.SortedSet.union(set1, set2) |> OrderedCollections.SortedSet.to_list()
[1, 2, 3, 4, 5]