LinkedMapSet (LinkedMapSet v0.1.0) View Source

A LinkedMapSet is an extension to MapSet that keeps pointers to previous and next elements based on add order.

Link to this section Summary

Functions

Adds an item to the linked map, or moves an existing one to tail.

Adds a new item to the linked map, unless it already exists.

Adds a new item to the linked map, or raises if value already exists.

Returns whether the given value exists in the given linked_map_set.

Create a new LinkedMapSet

Remove an item from the linked map if it exists.

Removes an item from the linked map, or raises if it doesn't exist.

Returns the number of items in the linked_map_set.

Returns the values as a List in order.

Link to this section Types

Specs

t() :: %LinkedMapSet{head: any(), items: map(), tail: any()}

Link to this section Functions

Link to this function

add(linked_map_set, value)

View Source

Specs

add(t(), any()) :: t()

Adds an item to the linked map, or moves an existing one to tail.

Returns the updated LinkedMapSet.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("foo")
iex> LinkedMapSet.new |> LinkedMapSet.add("foo") |> LinkedMapSet.add("bar")
%LinkedMapSet{
  head: "foo",
  items: %{
    "bar" => %Node{next: nil, previous: "foo", value: "bar"},
    "foo" => %Node{next: "bar", previous: nil, value: "foo"}
  },
  tail: "bar"
}
Link to this function

add_new(linked_map_set, value)

View Source

Specs

add_new(t(), any()) :: t()

Adds a new item to the linked map, unless it already exists.

Returns the updated LinkedMapSet.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.add_new("a")
%LinkedMapSet{
  head: "a",
  items: %{
    "a" => %Node{next: nil, previous: nil, value: "a"}
  },
  tail: "a"
}
Link to this function

add_new!(linked_map_set, value)

View Source

Specs

add_new!(t(), any()) :: t()

Adds a new item to the linked map, or raises if value already exists.

Returns the updated LinkedMapSet or raises if value already exists.

Behaves the same as add_new/2 but raises if value already exists.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.add_new!("a")
** (LinkedMapSet.DuplicateValueError) value "a" is already present
Link to this function

member?(linked_map_set, value)

View Source

Specs

member?(t(), any()) :: boolean()

Returns whether the given value exists in the given linked_map_set.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.member?("a")
true

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.member?("b")
false

Specs

new() :: t()

Create a new LinkedMapSet

Returns a new empty LinkedMapSet.

Examples

iex> LinkedMapSet.new()
%LinkedMapSet{head: nil, items: %{}, tail: nil}
Link to this function

remove(linked_map_set, value)

View Source

Specs

remove(t(), any()) :: t()

Remove an item from the linked map if it exists.

Returns the updated LinkedMapSet.

Examples

iex> linked_map_set = LinkedMapSet.new |> LinkedMapSet.add("a") |> LinkedMapSet.add("b") |> LinkedMapSet.add("c")
%LinkedMapSet{
  head: "a",
  items: %{
    "a" => %Node{next: "b", previous: nil, value: "a"},
    "b" => %Node{next: "c", previous: "a", value: "b"},
    "c" => %Node{next: nil, previous: "b", value: "c"}
  },
  tail: "c"
}
iex> LinkedMapSet.remove(linked_map_set, "b")
%LinkedMapSet{
  head: "a",
  items: %{
    "a" => %Node{next: "c", previous: nil, value: "a"},
    "c" => %Node{next: nil, previous: "a", value: "c"}
  },
  tail: "c"
}
Link to this function

remove!(linked_map_set, value)

View Source

Specs

remove!(t(), any()) :: t()

Removes an item from the linked map, or raises if it doesn't exist.

Returns the updated LinkedMapSet, or raises if value doesn't exist.

Behavies the same as remove/2, but raises if value doesn't exist.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.remove!("b")
** (LinkedMapSet.MissingValueError) value "b" is not present

Specs

size(t()) :: non_neg_integer()

Returns the number of items in the linked_map_set.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.size()
1

Specs

to_list(t()) :: [any()]

Returns the values as a List in order.

Examples

iex> LinkedMapSet.new() |> LinkedMapSet.add("a") |> LinkedMapSet.add("b") |> LinkedMapSet.to_list()