bisect v0.4.0 Bisect View Source

Bisection algorithms.

Hex Version Docs Status Hex downloads GitHub MIT License


Installation:

If you have Hex, the package can be installed by adding bisect to your list of dependencies in mix.exs:

def deps do
    [
        {:bisect, "~> 0.2.0"},
    ]
end

Link to this section Summary

Functions

Returns the leftmost index where to insert term in list enumerable, assuming the list is sorted.

Returns the rightmost index where to insert term in list enumerable, assuming the list is sorted.

Inserts term into list enumerable, and keeps it sorted assuming the list is already sorted.

Inserts term into list enumerable, and keeps it sorte assuming the list is already sorted.

Executes binary search in list enumerable by passing list elements to the function for comparison, assuming the list is sorted.

Link to this section Functions

Link to this function

bisect_left(enumerable, term, opts \\ [])

View Source (since 0.1.0)

Specs

bisect_left(Enum.t(), term(), keyword()) :: non_neg_integer()

Returns the leftmost index where to insert term in list enumerable, assuming the list is sorted.

Examples

iex> Bisect.bisect_left([1, 2], 1)
0

iex> Bisect.bisect_left([1, 2], 2)
1

iex> Bisect.bisect_left([1, 2], 4)
2

Options

  • rhs_key: Path of the value of term to be compared.

See Kernel.get_in/2

See Bisect.search/3 for more options.

Link to this function

bisect_right(enumerable, term, opts \\ [])

View Source (since 0.1.0)

Specs

bisect_right(Enum.t(), term(), keyword()) :: non_neg_integer()

Returns the rightmost index where to insert term in list enumerable, assuming the list is sorted.

Examples

iex> Bisect.bisect_right([1, 2], 1)
1

iex> Bisect.bisect_right([1, 2, 2, 4], 4)
4

iex> Bisect.bisect_right([2, 4], 0)
0

Options

  • rhs_key: Path of the value of term to be compared.

See Kernel.get_in/2

See Bisect.search/3 for more options.

Link to this function

insort_left(enumerable, term, opts \\ [])

View Source (since 0.1.0)

Specs

insort_left(Enum.t(), term(), keyword()) :: Enum.t()

Inserts term into list enumerable, and keeps it sorted assuming the list is already sorted.

If term is already in enumerable, inserts it to the left of the leftmost term.

Examples

iex> Bisect.insort_left([1, 2], 1)
[1, 1, 2]

iex> Bisect.insort_left([1, 2, 2, 4], 4)
[1, 2, 2, 4, 4]

iex> Bisect.insort_left([2, 4], 0)
[0, 2, 4]

iex> Bisect.insort_left(
...>   [%{value: 2}, %{value: 4}],
...>   %{value: 0},
...>   key: [:value]
...> )
[%{value: 0}, %{value: 2}, %{value: 4}]

Options

See Bisect.bisect_left/3

Link to this function

insort_right(enumerable, term, opts \\ [])

View Source (since 0.1.0)

Specs

insort_right(Enum.t(), term(), keyword()) :: Enum.t()

Inserts term into list enumerable, and keeps it sorte assuming the list is already sorted.

If term is already in enumerable, inserts it to the right of the rightmost term.

Examples

iex> Bisect.insort_right([1, 2], 1)
[1, 1, 2]

iex> Bisect.insort_right([1, 2, 2, 4], 4)
[1, 2, 2, 4, 4]

iex> Bisect.insort_right([2, 4], 0)
[0, 2, 4]

iex> Bisect.insort_right(
...>   [%{value: 2}, %{value: 4}],
...>   %{value: 0},
...>   key: [:value]
...> )
[%{value: 0}, %{value: 2}, %{value: 4}]

Options

See Bisect.bisect_right/3

Link to this function

search(enumerable, function, opts \\ [])

View Source (since 0.4.0)

Specs

search(Enum.t(), (term() -> boolean()), keyword()) :: non_neg_integer()

Executes binary search in list enumerable by passing list elements to the function for comparison, assuming the list is sorted.

Options

  • key or lhs_key: Path of the value to be compared, by being passed to function while iteration.

See Kernel.get_in/2

Examples

iex> Bisect.search([1, 2, 4], fn x ->
...>   x == 4
...> end)
2

iex> Bisect.search([1, 2, 4, 8], fn x ->
...>   x == 7
...> end)
4

iex> Bisect.search([1, 2], fn x ->
...>   x >= 1
...> end)
0

iex> Bisect.search([1, 2], fn x ->
...>   x > 1
...> end)
1

iex> Bisect.search([2, 1], fn x ->
...>   x < 0
...> end)
2

iex> Bisect.search(
...>   [%{value: 1}, %{value: 2}],
...>   fn x ->
...>     x > 1
...>   end,
...>   lhs_key: [:value]
...> )
1