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

non_empty_set()

@type non_empty_set() :: :gb_sets.set()

non_empty_t()

@type non_empty_t() :: %OrderedCollections.SortedSet{set: non_empty_set()}

t()

@opaque t()

Functions

add(sorted_set, value)

@spec add(t(), any()) :: t()

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]

delete(sorted_set, value)

@spec delete(t(), any()) :: t()

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]

difference(sorted_set1, sorted_set2)

@spec difference(t(), t()) :: t()

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]

max(sorted_set)

@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

member?(sorted_set, value)

@spec member?(t(), any()) :: boolean()

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

min(sorted_set)

@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

new()

@spec new() :: t()

Creates a new empty SortedSet.

Examples

iex> set = OrderedCollections.SortedSet.new()
iex> OrderedCollections.SortedSet.to_list(set)
[]

new(list)

@spec new(list()) :: t()

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]

range(sorted_set, min, max)

@spec range(t(), any(), any()) :: list()

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]

to_list(sorted_set)

@spec to_list(t()) :: list()

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]

union(sorted_set1, sorted_set2)

@spec union(t(), t()) :: t()

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]