OrderedCollections.SortedMap (ordered_collections v0.2.2)
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.
Converts the SortedMap
into a standard Map
, losing the sorted property.
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.
Types
@type t() :: %OrderedCollections.SortedMap{tree: :gb_trees.tree()}
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 with a function. If the key doesn’t exist,
it is set to nil
.
Examples
iex> sm = SortedMap.new(%{a: 1})
iex> sm = SortedMap.update(sm, :a, &(&1 + 1))
iex> SortedMap.get(sm, :a)
2
iex> SortedMap.new(%{a: 1}) |> SortedMap.update(:b, &(&1 + 1)) |> SortedMap.get(:b)
nil
Updates a key with a function. If the key doesn’t exist,
it sets it to default
.
Examples
iex> sm = SortedMap.new(%{a: 1})
iex> sm = SortedMap.update(sm, :a, &(&1 + 5), 0)
iex> SortedMap.get(sm, :a)
6
iex> SortedMap.new(%{a: 1}) |> SortedMap.update(:b, &(&1 + 1), 10) |> SortedMap.get(:b)
10
Replaces the value for key
with new_value
if the key exists.
If key
does not exist, the map remains unchanged.
Examples
iex> sm = SortedMap.new(%{a: 1, b: 2})
iex> sm = SortedMap.update_with_value(sm, :a, 100)
iex> SortedMap.get(sm, :a)
100
iex> SortedMap.update_with_value(sm, :c, 300)
iex> SortedMap.get(sm, :c)
nil