WeightedRandom.SearchTable (better_weighted_random v0.1.0)

Search table maps value ranges to some result (ex. score). Example of such table: [{:neg_inf, 0}, {-30, 4}, {-20, 8}, {-10, 10}, {10, 7}, {20, 0}] It will give such results with 'search' function: -50 -> 0 -30 -> 0 -29 -> 4 -15 -> 8 -10 -> 8 0 -> 10 10 -> 7 15 -> 7 30 -> 0

Summary

Functions

Compile search table into structure optimised for quick search.

Search for value in ranges.

Types

@type t() ::
  [{:neg_inf, any()} | [{number(), any()}]]
  | %WeightedRandom.SearchTable{search_tree: term()}

Functions

@spec compile([{:neg_inf, any()} | [{number(), any()}]]) ::
  %WeightedRandom.SearchTable{
    search_tree: term()
  }

Compile search table into structure optimised for quick search.

Link to this function

decompile(struct)

@spec decompile(%WeightedRandom.SearchTable{search_tree: term()}) :: [
  {:neg_inf, any()} | [{number(), any()}]
]
Link to this function

search(table_or_compiled, search_value)

@spec search(t(), number()) :: any()

Search for value in ranges.

Examples

iex> SearchTable.search([{:neg_inf, 1}, {0, 5}], 0)
5

iex> SearchTable.search([{:neg_inf, 1}, {0, 5}], -9)
1

iex> SearchTable.search([{:neg_inf, 1}, {0, 5}], 3)
5

iex> SearchTable.search([{:neg_inf, 1}, {-7, 5}, {7, 3}], -7)
1

iex> SearchTable.search([{:neg_inf, 1}, {-7, 5}, {7, 3}], 0)
5

iex> SearchTable.search([{:neg_inf, 1}, {-7, 5}, {7, 3}], 7)
3

iex> SearchTable.search([{:neg_inf, 1}, {-7, 5}, {7, 3}], 10)
3