bisect v0.4.0 Bisect View Source
Bisection algorithms.
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
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)
2Options
rhs_key: Path of the value oftermto be compared.
See Kernel.get_in/2
See Bisect.search/3 for more options.
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)
0Options
rhs_key: Path of the value oftermto be compared.
See Kernel.get_in/2
See Bisect.search/3 for more options.
Specs
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
Specs
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
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
keyorlhs_key: Path of the value to be compared, by being passed tofunctionwhile 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