SortedMap (ordered_collections v0.1.0)
A sorted key-value store implemented using Erlang's :gb_trees
(Red-Black Trees).
This module stores items in sorted order by their keys.
Summary
Functions
Deletes a key.
Retrieves a value by key, returning a default if missing.
Checks if a key exists.
Returns the largest key. Returns :none
if the map is empty.
Returns the smallest key. Returns :none
if the map is empty.
Creates a new empty sorted map.
Creates a new sorted map from a standard map.
Inserts a key-value pair, maintaining order.
Iterates over a range of keys, returning key-value pairs
whose keys are between min
and max
(inclusive).
Returns all key-value pairs in sorted order.
Updates a key if it exists, otherwise sets a default value.
Types
Functions
Deletes a key.
Examples
iex> sm = SortedMap.new(%{a: 1, b: 2})
iex> sm2 = SortedMap.delete(sm, :b)
iex> SortedMap.has_key?(sm2, :b)
false
Retrieves a value by key, returning a default if missing.
Examples
iex> sm = SortedMap.new(%{a: 1})
iex> SortedMap.get(sm, :a)
1
iex> SortedMap.get(sm, :b, :default)
:default
Checks if a key exists.
Examples
iex> sm = SortedMap.new(%{a: 1, b: 2})
iex> SortedMap.has_key?(sm, :a)
true
iex> SortedMap.has_key?(sm, :c)
false
Returns the largest key. Returns :none
if the map is empty.
Examples
iex> sm = SortedMap.new(%{b: 2, a: 1, c: 3})
iex> SortedMap.max_key(sm)
:c
iex> sm_empty = SortedMap.new()
iex> SortedMap.max_key(sm_empty)
:none
Returns the smallest key. Returns :none
if the map is empty.
Examples
iex> sm = SortedMap.new(%{b: 2, a: 1, c: 3})
iex> SortedMap.min_key(sm)
:a
iex> sm_empty = SortedMap.new()
iex> SortedMap.min_key(sm_empty)
:none
@spec new() :: t()
Creates a new empty sorted map.
Examples
iex> sm = SortedMap.new()
iex> SortedMap.to_map(sm)
%{}
Creates a new sorted map from a standard map.
Examples
iex> sm = SortedMap.new(%{a: 1, b: 2})
iex> SortedMap.get(sm, :a)
1
iex> SortedMap.get(sm, :b)
2
Inserts a key-value pair, maintaining order.
Examples
iex> sm = SortedMap.new(%{a: 1})
iex> sm = SortedMap.put(sm, :b, 2)
iex> SortedMap.get(sm, :b)
2
Iterates over a range of keys, returning key-value pairs
whose keys are between min
and max
(inclusive).
Examples
iex> sm = SortedMap.new(%{a: 1, b: 2, c: 3, d: 4})
iex> SortedMap.range(sm, :b, :c)
[{:b, 2}, {:c, 3}]
Returns all key-value pairs in sorted order.
Examples
iex> sm = SortedMap.new(%{b: 2, a: 1})
iex> SortedMap.to_list(sm)
[{:a, 1}, {:b, 2}]
Converts the SortedMap
into a standard Map
, losing the sorted property.
Examples
iex> sm = SortedMap.new(%{b: 2, a: 1})
iex> SortedMap.to_map(sm)
%{a: 1, b: 2}
Updates a key if it exists, otherwise sets a default value.
Examples
iex> sm = SortedMap.new(%{a: 1})
iex> SortedMap.update(sm, :a, fn val -> val + 1 end) |> SortedMap.get(:a)
2
iex> SortedMap.new(%{a: 1}) |> SortedMap.update(:b, fn val -> val + 1 end, 10) |> SortedMap.get(:b)
10