OrderedCollections (ordered_collections v0.3.3)
OrderedCollections provides sorted data structures for Elixir,
including a SortedMap
and a SortedSet
.
For SortedMap
, keys are maintained in a sorted order per Erlang's
:gb_trees
module.
For SortedSet
, elements are maintained in a sorted order per Erlang's
:gb_sets
module.
Core Modules
OrderedCollections.SortedMap
- A sorted key-value store implemented using Erlang's:gb_trees
.OrderedCollections.SortedSet
- A sorted set implemented using Erlang's:gb_sets
.
Core Module Examples
iex> sm = OrderedCollections.SortedMap.new(%{b: 2, a: 1})
iex> OrderedCollections.SortedMap.get(sm, :a)
1
iex> ss = OrderedCollections.SortedSet.new([3, 1, 2])
iex> OrderedCollections.SortedSet.to_list(ss)
[1, 2, 3]
Convenience Functions
This module also provides convenience functions that wrap the core module functions.
These functions are prefixed with map_
for SortedMap operations and set_
for SortedSet operations.
Map Convenience Functions
iex> sm = OrderedCollections.new_map(%{x: 10})
iex> sm = OrderedCollections.map_put(sm, :y, 20)
iex> OrderedCollections.map_get(sm, :y)
20
iex> sm = OrderedCollections.new_map()
iex> sm = OrderedCollections.map_put(sm, :a, 1)
iex> OrderedCollections.map_to_map(sm)
%{a: 1}
Set Convenience Functions
iex> ss = OrderedCollections.new_set([3, 1, 2])
iex> ss = OrderedCollections.set_add_value(ss, 4)
iex> OrderedCollections.set_member?(ss, 4)
true
iex> ss = OrderedCollections.new_set()
iex> ss = OrderedCollections.set_add_value(ss, 1)
iex> OrderedCollections.SortedSet.to_list(ss)
[1]
Map Operations via Convenience Functions
The following examples show how to use the convenience functions for map operations:
iex> sm = OrderedCollections.new_map(%{b: 2, a: 1, c: 3})
iex> OrderedCollections.map_keys(sm)
[:a, :b, :c]
iex> OrderedCollections.map_values(sm)
[1, 2, 3]
iex> sm2 = OrderedCollections.new_map(%{b: 4, d: 5})
iex> OrderedCollections.map_merge(sm, sm2) |> OrderedCollections.map_to_map()
%{a: 1, b: 4, c: 3, d: 5}
iex> OrderedCollections.map_range(sm, :a, :b)
[a: 1, b: 2]
iex> OrderedCollections.map_min_key(sm)
:a
iex> OrderedCollections.map_max_key(sm)
:c
Set Operations via Convenience Functions
The following examples show how to use the convenience functions for set operations:
iex> set1 = OrderedCollections.new_set([1, 2, 3])
iex> set2 = OrderedCollections.new_set([3, 4, 5])
iex> OrderedCollections.set_union(set1, set2) |> OrderedCollections.SortedSet.to_list()
[1, 2, 3, 4, 5]
iex> OrderedCollections.set_difference(set1, set2) |> OrderedCollections.SortedSet.to_list()
[1, 2]
iex> OrderedCollections.set_intersection(set1, set2) |> OrderedCollections.SortedSet.to_list()
[3]
Range Operations via Convenience Functions
You can get elements within a specific range using convenience functions:
iex> set = OrderedCollections.new_set([1, 2, 3, 4, 5])
iex> OrderedCollections.set_range(set, 2, 4) |> OrderedCollections.SortedSet.to_list()
[2, 3, 4]
Min/Max Operations via Convenience Functions
You can get the minimum and maximum elements using convenience functions:
iex> set = OrderedCollections.new_set([3, 1, 2])
iex> OrderedCollections.set_min(set)
1
iex> OrderedCollections.set_max(set)
3
Summary
Functions
Deletes a key.
Retrieves the value for the given key from a SortedMap, returning a default if the key is missing.
Checks if a key exists.
Returns the keys of the SortedMap in sorted order.
Returns the largest key. Returns :none
if the map is empty.
Merges two SortedMaps. In case of duplicate keys, values from the second map override those from the first.
Returns the smallest key. Returns :none
if the map is empty.
Inserts a key-value pair into a SortedMap.
Iterates over a range of keys, returning key-value pairs
whose keys are between min
and max
(inclusive).
Rebalances the tree.
Creates a map from a SortedMap.
Updates a key with a function. If the key doesn't exist, it is set to nil
.
Updates a key with a function. If the key doesn't exist, it sets it to default
.
Replaces the value for key
with new_value
if the key exists.
If key
does not exist, the map remains unchanged.
Returns the values of the SortedMap in the order corresponding to the sorted keys.
Creates a new empty SortedMap.
Creates a new SortedMap from a regular map.
Creates a new empty SortedSet.
Creates a new SortedSet from a list.
Adds a value to the SortedSet.
Deletes a value from the SortedSet.
Returns the difference between two SortedSets.
Returns the intersection of two SortedSets.
Returns the maximum element of the SortedSet.
Checks if a value is a member of the SortedSet.
Returns the minimum element of the SortedSet.
Returns elements within a given range (inclusive).
Returns the union of two SortedSets.
Functions
@spec map_delete(OrderedCollections.SortedMap.t(), any()) :: OrderedCollections.SortedMap.t()
Deletes a key.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1, b: 2})
iex> sm2 = OrderedCollections.map_delete(sm, :b)
iex> OrderedCollections.map_has_key?(sm2, :b)
false
@spec map_get(OrderedCollections.SortedMap.t(), any(), any()) :: any()
Retrieves the value for the given key from a SortedMap, returning a default if the key is missing.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1}) iex> OrderedCollections.map_get(sm, :a) 1 iex> OrderedCollections.map_get(sm, :b, 2) 2
@spec map_has_key?(OrderedCollections.SortedMap.t(), any()) :: boolean()
Checks if a key exists.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1, b: 2})
iex> OrderedCollections.map_has_key?(sm, :a)
true
iex> OrderedCollections.map_has_key?(sm, :c)
false
@spec map_keys(OrderedCollections.SortedMap.t()) :: list()
Returns the keys of the SortedMap in sorted order.
Examples
iex> sm = OrderedCollections.new_map(%{b: 2, a: 1, c: 3})
iex> OrderedCollections.map_keys(sm)
[:a, :b, :c]
@spec map_max_key(OrderedCollections.SortedMap.t()) :: any()
Returns the largest key. Returns :none
if the map is empty.
Examples
iex> sm = OrderedCollections.new_map(%{b: 2, a: 1, c: 3})
iex> OrderedCollections.map_max_key(sm)
:c
iex> sm_empty = OrderedCollections.new_map()
iex> OrderedCollections.map_max_key(sm_empty)
:none
@spec map_merge(OrderedCollections.SortedMap.t(), OrderedCollections.SortedMap.t()) :: OrderedCollections.SortedMap.t()
Merges two SortedMaps. In case of duplicate keys, values from the second map override those from the first.
Examples
iex> sm1 = OrderedCollections.new_map(%{a: 1, b: 2})
iex> sm2 = OrderedCollections.new_map(%{b: 3, c: 4})
iex> OrderedCollections.map_merge(sm1, sm2) |> OrderedCollections.map_to_map()
%{a: 1, b: 3, c: 4}
@spec map_min_key(OrderedCollections.SortedMap.t()) :: any()
Returns the smallest key. Returns :none
if the map is empty.
Examples
iex> sm = OrderedCollections.new_map(%{b: 2, a: 1, c: 3})
iex> OrderedCollections.map_min_key(sm)
:a
iex> sm_empty = OrderedCollections.new_map()
iex> OrderedCollections.map_min_key(sm_empty)
:none
@spec map_put(OrderedCollections.SortedMap.t(), any(), any()) :: OrderedCollections.SortedMap.t()
Inserts a key-value pair into a SortedMap.
Examples
iex> sm = OrderedCollections.new_map() iex> sm = OrderedCollections.map_put(sm, :a, 1) iex> OrderedCollections.map_get(sm, :a) 1
@spec map_range(OrderedCollections.SortedMap.t(), any(), any()) :: [{any(), any()}]
Iterates over a range of keys, returning key-value pairs
whose keys are between min
and max
(inclusive).
Examples
iex> sm = OrderedCollections.new_map(%{a: 1, b: 2, c: 3, d: 4})
iex> OrderedCollections.map_range(sm, :b, :c)
[b: 2, c: 3]
@spec map_rebalance(OrderedCollections.SortedMap.t()) :: OrderedCollections.SortedMap.t()
Rebalances the tree.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1, b: 2, c: 3})
iex> smi = OrderedCollections.new_map() |> OrderedCollections.map_put(:a, 1) |> OrderedCollections.map_put(:b, 2) |> OrderedCollections.map_put(:c, 3)
iex> sm == smi
false
iex> OrderedCollections.map_rebalance(sm) == OrderedCollections.map_rebalance(smi)
true
@spec map_to_map(OrderedCollections.SortedMap.t()) :: map()
Creates a map from a SortedMap.
Examples
iex> sm = OrderedCollections.new_map(%{b: 2, a: 1}) iex> OrderedCollections.map_to_map(sm) %{a: 1, b: 2}
@spec map_update(OrderedCollections.SortedMap.t(), any(), (any() -> any())) :: OrderedCollections.SortedMap.t()
Updates a key with a function. If the key doesn't exist, it is set to nil
.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1})
iex> sm = OrderedCollections.map_update(sm, :a, &(&1 + 1))
iex> OrderedCollections.map_get(sm, :a)
2
iex> OrderedCollections.new_map(%{a: 1}) |> OrderedCollections.map_update(:b, &(&1 + 1)) |> OrderedCollections.map_get(:b)
nil
@spec map_update(OrderedCollections.SortedMap.t(), any(), (any() -> any()), any()) :: OrderedCollections.SortedMap.t()
Updates a key with a function. If the key doesn't exist, it sets it to default
.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1})
iex> sm = OrderedCollections.map_update(sm, :a, &(&1 + 5), 0)
iex> OrderedCollections.map_get(sm, :a)
6
iex> OrderedCollections.new_map(%{a: 1}) |> OrderedCollections.map_update(:b, &(&1 + 1), 10) |> OrderedCollections.map_get(:b)
10
@spec map_update_with_value(OrderedCollections.SortedMap.t(), any(), any()) :: OrderedCollections.SortedMap.t()
Replaces the value for key
with new_value
if the key exists.
If key
does not exist, the map remains unchanged.
Examples
iex> sm = OrderedCollections.new_map(%{a: 1, b: 2})
iex> sm = OrderedCollections.map_update_with_value(sm, :a, 100)
iex> OrderedCollections.map_get(sm, :a)
100
iex> OrderedCollections.map_update_with_value(sm, :c, 300)
iex> OrderedCollections.map_get(sm, :c)
nil
@spec map_values(OrderedCollections.SortedMap.t()) :: list()
Returns the values of the SortedMap in the order corresponding to the sorted keys.
Examples
iex> sm = OrderedCollections.new_map(%{b: 2, a: 1, c: 3})
iex> OrderedCollections.map_values(sm)
[1, 2, 3]
@spec new_map() :: OrderedCollections.SortedMap.t()
Creates a new empty SortedMap.
Examples
iex> sm = OrderedCollections.new_map() iex> OrderedCollections.map_to_map(sm) %{}
@spec new_map(map()) :: OrderedCollections.SortedMap.t()
Creates a new SortedMap from a regular map.
Examples
iex> m = %{b: 2, a: 1} iex> sm = OrderedCollections.new_map(m) iex> OrderedCollections.map_get(sm, :a) 1
@spec new_set() :: OrderedCollections.SortedSet.t()
Creates a new empty SortedSet.
Examples
iex> ss = OrderedCollections.new_set()
iex> OrderedCollections.SortedSet.to_list(ss)
[]
@spec new_set(list()) :: OrderedCollections.SortedSet.t()
Creates a new SortedSet from a list.
Examples
iex> ss = OrderedCollections.new_set([3, 1, 2])
iex> OrderedCollections.SortedSet.to_list(ss)
[1, 2, 3]
@spec set_add_value(OrderedCollections.SortedSet.t(), any()) :: OrderedCollections.SortedSet.t()
Adds a value to the SortedSet.
Examples
iex> ss = OrderedCollections.new_set([2, 3])
iex> ss = OrderedCollections.set_add_value(ss, 1)
iex> ss = OrderedCollections.set_add_value(ss, 3)
iex> OrderedCollections.SortedSet.to_list(ss)
[1, 2, 3]
@spec set_delete_value(OrderedCollections.SortedSet.t(), any()) :: OrderedCollections.SortedSet.t()
Deletes a value from the SortedSet.
Examples
iex> ss = OrderedCollections.new_set([1, 2, 3])
iex> ss = OrderedCollections.set_delete_value(ss, 2)
iex> OrderedCollections.SortedSet.to_list(ss)
[1, 3]
@spec set_difference( OrderedCollections.SortedSet.t(), OrderedCollections.SortedSet.t() ) :: OrderedCollections.SortedSet.t()
Returns the difference between two SortedSets.
Examples
iex> set1 = OrderedCollections.new_set([1, 2, 3])
iex> set2 = OrderedCollections.new_set([3, 4, 5])
iex> OrderedCollections.set_difference(set1, set2) |> OrderedCollections.SortedSet.to_list()
[1, 2]
@spec set_intersection( OrderedCollections.SortedSet.t(), OrderedCollections.SortedSet.t() ) :: OrderedCollections.SortedSet.t()
Returns the intersection of two SortedSets.
Examples
iex> set1 = OrderedCollections.new_set([1, 2, 3])
iex> set2 = OrderedCollections.new_set([3, 4, 5])
iex> OrderedCollections.set_intersection(set1, set2) |> OrderedCollections.SortedSet.to_list()
[3]
@spec set_max(OrderedCollections.SortedSet.non_empty_t()) :: OrderedCollections.SortedSet.element()
Returns the maximum element of the SortedSet.
Examples
iex> ss = OrderedCollections.new_set([3, 1, 2])
iex> OrderedCollections.set_max(ss)
3
@spec set_member?(OrderedCollections.SortedSet.t(), any()) :: boolean()
Checks if a value is a member of the SortedSet.
Examples
iex> ss = OrderedCollections.new_set([1, 2, 3])
iex> OrderedCollections.set_member?(ss, 2)
true
@spec set_min(OrderedCollections.SortedSet.non_empty_t()) :: OrderedCollections.SortedSet.element()
Returns the minimum element of the SortedSet.
Examples
iex> ss = OrderedCollections.new_set([3, 1, 2])
iex> OrderedCollections.set_min(ss)
1
@spec set_range(OrderedCollections.SortedSet.t(), any(), any()) :: OrderedCollections.SortedSet.t()
Returns elements within a given range (inclusive).
Examples
iex> ss = OrderedCollections.new_set([1, 2, 3, 4, 5])
iex> OrderedCollections.set_range(ss, 2, 4) |> OrderedCollections.SortedSet.to_list()
[2, 3, 4]
@spec set_union(OrderedCollections.SortedSet.t(), OrderedCollections.SortedSet.t()) :: OrderedCollections.SortedSet.t()
Returns the union of two SortedSets.
Examples
iex> set1 = OrderedCollections.new_set([1, 2, 3])
iex> set2 = OrderedCollections.new_set([3, 4, 5])
iex> OrderedCollections.set_union(set1, set2) |> OrderedCollections.SortedSet.to_list()
[1, 2, 3, 4, 5]