View Source Bsearch (Bsearch v0.2.0)

Module that contains the functions to use the Binary search using tuples.

Summary

Functions

Returns the index of the position of a value inside the tuple, if the value is not present, it will return an tuple with error status.

Returns true of false of whether a tuple contains a certain value.

Binary search only works on ordered tuples, so this function normalizes a tuple or a list

Functions

Link to this function

find_index(tuple, value)

View Source
@spec find_index(tuple(), any()) :: {:ok, non_neg_integer()} | {:error, :not_found}

Returns the index of the position of a value inside the tuple, if the value is not present, it will return an tuple with error status.

Examples

iex> Bsearch.find_index({1, 2, 3}, 1)
{:ok, 0}

iex> Bsearch.find_index({1, 2, 3}, 2)
{:ok, 1}

iex> Bsearch.find_index({1, 2, 3}, 3)
{:ok, 2}

iex> Bsearch.find_index({1, 2, 3}, 0)
{:error, :not_found}

iex> Bsearch.find_index({1, 2, 3}, 4)
{:error, :not_found}
@spec member?(tuple(), any()) :: boolean()

Returns true of false of whether a tuple contains a certain value.

Examples

iex> Bsearch.member?({1, 2, 3}, 1)
true

iex> Bsearch.member?({1, 2, 3}, 2)
true

iex> Bsearch.member?({1, 2, 3}, 3)
true

iex> Bsearch.member?({1, 2, 3}, 0)
false

iex> Bsearch.member?({1, 2, 3}, 4)
false
@spec normalize(list() | tuple()) :: tuple()

Binary search only works on ordered tuples, so this function normalizes a tuple or a list

Examples

iex> Bsearch.normalize([3, 2, 1])
{1, 2, 3}

iex> Bsearch.normalize([2, 3, 2])
{2, 3}

iex> Bsearch.normalize({3, 2, 1})
{1, 2, 3}

iex> Bsearch.normalize({3, 3, 1})
{1, 3}