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
position()
View Sourceposition() :: {row :: non_neg_integer(), col :: non_neg_integer()}
t()
View Sourcet() :: %Abit.Matrix{ atomics_ref: reference(), m: pos_integer(), n: pos_integer() }
Link to this section Functions
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
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
Returns column count of matrix.
Examples
iex> matrix = Abit.Matrix.new(4, 8)
iex> matrix |> Abit.Matrix.columns()
8
compare_exchange(matrix, position, expected, desired)
View Source (since 0.2.3)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
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
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
index_to_position(n, index)
View Sourceindex_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}
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
Returns smallest integer in matrix
.
Examples
iex> matrix = Abit.Matrix.new(10, 10, seed_fun: fn _ -> 7 end)
iex> matrix |> Abit.Matrix.min()
7
new(m, n, options \\ [])
View Sourcenew(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
position_to_index(matrix, arg)
View Sourceposition_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
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
Returns row count of matrix.
Examples
iex> matrix = Abit.Matrix.new(4, 8)
iex> matrix |> Abit.Matrix.rows()
4
Returns size (rows * columns) of matrix.
Examples
iex> matrix = Abit.Matrix.new(5, 5)
iex> matrix |> Abit.Matrix.size()
25
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
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]
]