abit v0.2.3 Abit.Matrix View Source

Use :atomics as an M x N matrix.

Link to this section Summary

Functions

Atomic addition and return of the result.

Applies the given fun function to all elements of matrix.

Returns column count of matrix.

Atomically compares the value at position with expected , and if those are equal, sets value at position to desired.

Atomically replaces value at position with value and returns the value it had before.

Returns value at position from the given matrix.

Returns a position tuple for the given atomics index.

Returns largest integer in matrix.

Returns smallest integer in matrix.

Returns a new %Abit.Matrix{} struct.

Returns atomics index corresponding to the position tuple in the given %Abit.Matrix{} struct.

Puts value into matrix at position.

Returns row count of matrix.

Returns size (rows * columns) of matrix.

Returns sum of integers in matrix.

Converts %Abit.Matrix{} struct to list of lists.

Link to this section Types

Link to this type

position()

View Source
position() :: {row :: non_neg_integer(), col :: non_neg_integer()}
Link to this type

t()

View Source
t() :: %Abit.Matrix{
  atomics_ref: reference(),
  m: pos_integer(),
  n: pos_integer()
}

Link to this section Functions

Link to this function

add(matrix, position, incr)

View Source (since 0.2.3)
add(t(), position(), integer()) :: integer()

Atomic addition and return of the result.

Adds incr to value at position and return result.

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.add({0, 0}, 2)
2
iex> matrix |> Abit.Matrix.add({0, 0}, 2)
4
iex> matrix |> Abit.Matrix.add({0, 0}, -8)
-4
Link to this function

apply(matrix, fun)

View Source (since 0.2.3)
apply(t(), (integer() -> integer()) | (integer(), position() -> integer())) ::
  :ok

Applies the given fun function to all elements of matrix.

If arity of fun is 1 it receives the integer as single argument. If arity of fun is 2 it receives the integer as first and position tuple as the second argument.

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.apply(fn int -> int + 2 end)
iex> matrix |> Abit.Matrix.get({0, 0})
2
iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.apply(fn _int, {row, col} -> row * col end)
iex> matrix |> Abit.Matrix.get({9, 9})
81
Link to this function

columns(matrix)

View Source (since 0.2.3)
columns(t()) :: pos_integer()

Returns column count of matrix.

Examples

iex> matrix = Abit.Matrix.new(4, 8)
iex> matrix |> Abit.Matrix.columns()
8
Link to this function

compare_exchange(matrix, position, expected, desired)

View Source (since 0.2.3)
compare_exchange(t(), position(), integer(), integer()) :: :ok | integer()

Atomically compares the value at position with expected , and if those are equal, sets value at position to desired.

Returns :ok if desired was written. Returns the actual value at position if it does not equal to desired.

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.compare_exchange({0, 0}, 0, -10)
:ok
iex> matrix |> Abit.Matrix.compare_exchange({0, 0}, 3, 10)
-10
Link to this function

exchange(matrix, position, value)

View Source
exchange(t(), position(), integer()) :: integer()

Atomically replaces value at position with value and returns the value it had before.

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.exchange({0, 0}, -10)
0
iex> matrix |> Abit.Matrix.exchange({0, 0}, -15)
-10
Link to this function

get(matrix, position)

View Source
get(t(), position()) :: integer()

Returns value at position from the given matrix.

Examples

iex> matrix = Abit.Matrix.new(10, 10, seed_fun: fn _ -> 3 end)
iex> matrix |> Abit.Matrix.get({0, 5})
3
Link to this function

index_to_position(n, index)

View Source
index_to_position(t(), pos_integer()) :: position()

Returns a position tuple for the given atomics index.

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> Abit.Matrix.index_to_position(matrix, 10)
{0, 9}
Link to this function

max(matrix)

View Source (since 0.2.3)
max(t()) :: integer()

Returns largest integer in matrix.

Examples

iex> matrix = Abit.Matrix.new(10, 10, seed_fun: fn {row, col} -> row * col end)
iex> matrix |> Abit.Matrix.max()
81
Link to this function

min(matrix)

View Source (since 0.2.3)
min(t()) :: integer()

Returns smallest integer in matrix.

Examples

iex> matrix = Abit.Matrix.new(10, 10, seed_fun: fn _ -> 7 end)
iex> matrix |> Abit.Matrix.min()
7
Link to this function

new(m, n, options \\ [])

View Source
new(pos_integer(), pos_integer(), list()) :: t()

Returns a new %Abit.Matrix{} struct.

Options

  • :seed_fun - a function that receives a 2-tuple {row, col} as argument and returns the initial value for the position
  • :signed - whether to have signed or unsigned 64bit integers

Examples

 Abit.Matrix.new(10, 5) # 10 x 5 matrix
 Abit.Matrix.new(10, 5, signed: false) # unsigned integers
 Abit.Matrix.new(10, 5, seed_fun: fn {row, col} -> row * col end) # seed values
Link to this function

position_to_index(matrix, arg)

View Source
position_to_index(t(), position()) :: pos_integer()

Returns atomics index corresponding to the position tuple in the given %Abit.Matrix{} struct.

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.position_to_index({1, 1})
12
iex> matrix |> Abit.Matrix.position_to_index({0, 4})
5
Link to this function

put(matrix, position, value)

View Source
put(t(), position(), integer()) :: :ok

Puts value into matrix at position.

Returns :ok

Examples

iex> matrix = Abit.Matrix.new(10, 10)
iex> matrix |> Abit.Matrix.put({1, 3}, 5)
:ok
Link to this function

rows(matrix)

View Source (since 0.2.3)
rows(t()) :: pos_integer()

Returns row count of matrix.

Examples

iex> matrix = Abit.Matrix.new(4, 8)
iex> matrix |> Abit.Matrix.rows()
4
Link to this function

size(matrix)

View Source (since 0.2.3)
size(t()) :: pos_integer()

Returns size (rows * columns) of matrix.

Examples

iex> matrix = Abit.Matrix.new(5, 5)
iex> matrix |> Abit.Matrix.size()
25
Link to this function

sum(matrix)

View Source (since 0.2.3)
sum(t()) :: integer()

Returns sum of integers in matrix.

Examples

iex> matrix = Abit.Matrix.new(10, 10, seed_fun: fn {row, col} -> row * col end)
iex> matrix |> Abit.Matrix.sum()
2025
Link to this function

to_list_of_lists(matrix)

View Source (since 0.2.3)
to_list_of_lists(t()) :: [[integer()]]

Converts %Abit.Matrix{} struct to list of lists.

Examples

iex> m = Abit.Matrix.new(5, 5, seed_fun: fn {row, col} -> row * col end)
iex> Abit.Matrix.to_list_of_lists(m)
[
  [0, 0, 0, 0, 0],
  [0, 1, 2, 3, 4],
  [0, 2, 4, 6, 8],
  [0, 3, 6, 9, 12],
  [0, 4, 8, 12, 16]
]