SortedSet

A Set implementation that always remains sorted.

SortedSet guarantees that no element appears more than once and that enumerating over members happens in their sorted order.

Source

Summary

delete(sortedset, element)

Returns a SortedSet with all of the members of sortedset except for element

difference(set1, set2)

Returns a SortedSet containing the items in set1 that are not in set2

disjoint?(set1, set2)

Returns true if no member of set1 is in set2. Otherwise returns false

equal?(sortedset, sortedset)

Returns true if all elements in set1 are in set2 and all elements in set2 are in set1

intersection(set1, set2)

Returns a SortedSet containing the items contained in both set1 and set2

member?(sortedset, element)

Returns true if set contains element

new(members \\ [], options \\ [])

Returns a new SortedSet, initialized with the unique, sorted values of members

put(sortedset, element)

Returns a SortedSet with all of the members of set plus element

size(sortedset)

Returns the number of elements in a SortedSet

subset?(sortedset, sortedset)

Returns true if all elements in set1 are in set2

to_list(sortedset)

Returns a List with all of the members of set

union(set1, set2)

Returns a SortedSet containing the items of both set1 and set2

Types

t

Functions

delete(sortedset, element)

Returns a SortedSet with all of the members of sortedset except for element.

Examples

iex> set = SortedSet.new([1,3,5])
iex> SortedSet.to_list SortedSet.delete(set, 1)
[3,5]

iex> set = SortedSet.new([1,3,5])
iex> SortedSet.to_list SortedSet.delete(set, 2)
[1,3,5]

iex> set = SortedSet.new([])
iex> SortedSet.to_list SortedSet.delete(set, 2)
[]
Source
difference(set1, set2)

Returns a SortedSet containing the items in set1 that are not in set2.

Examples

iex> set1 = SortedSet.new([1,2,3,4])
iex> set2 = SortedSet.new([2,4,6,8])
iex> SortedSet.to_list SortedSet.difference(set1, set2)
[1,3]
Source
disjoint?(set1, set2)

Returns true if no member of set1 is in set2. Otherwise returns false.

Examples

iex> set1 = SortedSet.new([1,2,3,4])
iex> set2 = SortedSet.new([5,6,7,8])
iex> SortedSet.disjoint?(set1, set2)
true

iex> set1 = SortedSet.new([1,2,3,4])
iex> set2 = SortedSet.new([4,5,6,7])
iex> SortedSet.disjoint?(set1, set2)
false
Source
equal?(sortedset, sortedset)

Returns true if all elements in set1 are in set2 and all elements in set2 are in set1

Examples

iex> set1 = SortedSet.new([1,3,5])
iex> set2 = SortedSet.new([1,3,5])
iex> SortedSet.equal?(set1, set2)
true

iex> set1 = SortedSet.new([1,3,5])
iex> set2 = SortedSet.new([1,2,3,4,5])
iex> SortedSet.equal?(set1, set2)
false
Source
intersection(set1, set2)

Returns a SortedSet containing the items contained in both set1 and set2.

Examples

iex> set1 = SortedSet.new([1,3,5,7])
iex> set2 = SortedSet.new([0,2,3,4,5])
iex> SortedSet.to_list SortedSet.intersection(set1, set2)
[3,5]
Source
member?(sortedset, element)

Returns true if set contains element

Examples

iex> set = SortedSet.new([1,3,5])
iex> SortedSet.member?(set, 1)
true

iex> set = SortedSet.new([1,3,5])
iex> SortedSet.member?(set, 0)
false
Source
new(members \\ [], options \\ [])

Returns a new SortedSet, initialized with the unique, sorted values of members.

Options

  • :comparator function taking two terms and deciding their order. Passed on to the underlying data structure, in this case a Red-Black tree. The default is to compare based on standard Erlang term comparison. To learn more about this option, see the examples given for RedBlackTree

Examples

iex> SortedSet.new()
#SortedSet<[]>

iex> SortedSet.new([1,3,5])
#SortedSet<[1, 3, 5]>

iex> SortedSet.new([:a, :b, :c], comparator: fn (term1, term2) ->
...>   RedBlackTree.compare_terms(term1, term2) * -1
...> end)
#SortedSet<[:c, :b, :a]>
Source
put(sortedset, element)

Returns a SortedSet with all of the members of set plus element.

Examples

iex> set = SortedSet.new([1,3,5])
iex> SortedSet.to_list SortedSet.put(set, 1)
[1,3,5]

iex> set = SortedSet.new([1,3,5])
iex> SortedSet.to_list SortedSet.put(set, 2)
[1,2,3,5]
Source
size(sortedset)

Returns the number of elements in a SortedSet.

Examples

iex> SortedSet.size SortedSet.new([1,3,5])
3
Source
subset?(sortedset, sortedset)

Returns true if all elements in set1 are in set2

Examples

iex> set1 = SortedSet.new([1,3,5])
iex> set2 = SortedSet.new([1,2,3,4,5])
iex> SortedSet.subset?(set1, set2)
true

iex> set1 = SortedSet.new([1,2,3,4,5])
iex> set2 = SortedSet.new([1,3,5])
iex> SortedSet.subset?(set1, set2)
false
Source
to_list(sortedset)

Returns a List with all of the members of set.

Examples

iex> SortedSet.to_list SortedSet.new([1,3,5])
[1,3,5]
Source
union(set1, set2)

Returns a SortedSet containing the items of both set1 and set2.

Examples

iex> set1 = SortedSet.new([1,3,5,7])
iex> set2 = SortedSet.new([0,2,3,4,5])
iex> SortedSet.to_list SortedSet.union(set1, set2)
[0,1,2,3,4,5,7]
Source