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.
Summary↑
delete(sortedset, element) | Returns a |
difference(set1, set2) | Returns a |
disjoint?(set1, set2) | Returns |
equal?(sortedset, sortedset) | Returns |
intersection(set1, set2) | Returns a |
member?(sortedset, element) | Returns |
new(members \\ [], options \\ []) | Returns a new |
put(sortedset, element) | Returns a |
size(sortedset) | Returns the number of elements in a |
subset?(sortedset, sortedset) | Returns |
to_list(sortedset) | Returns a |
union(set1, set2) | Returns a |
Types ↑
Functions
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)
[]
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]
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
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
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]
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
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]>
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]
Returns the number of elements in a SortedSet
.
Examples
iex> SortedSet.size SortedSet.new([1,3,5])
3
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
Returns a List
with all of the members of set
.
Examples
iex> SortedSet.to_list SortedSet.new([1,3,5])
[1,3,5]
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]