abit v0.1.4 Abit.Counter View Source

Use atomics as an array of counters with n bits per 64 bit integer.

Possible counters:

bits | unsigned value range | signed value range
2      0..3                   -2..1
4      0..15                  -8..7
8      0..255                 -128..127
16     0..65535               -32768..32767
32     0..4294967295          -2147483648..2147483647

If you need 64 bit counters use: Erlang counters

Link to this section Summary

Functions

Increments the value into the counter at index.

Returns the value of counter at index.

Returns a new %Abit.Counter{} struct.

Puts the value into the counter at index.

Link to this section Types

Link to this type

t()

View Source
t() :: %Abit.Counter{
  atomics_ref: reference(),
  counters_bit_size: 2 | 4 | 8 | 16 | 32,
  max: non_neg_integer(),
  min: integer(),
  signed: boolean(),
  size: non_neg_integer()
}

Link to this section Functions

Link to this function

add(counter, index, incr)

View Source
add(t(), non_neg_integer(), integer()) :: :ok

Increments the value into the counter at index.

Returns :ok.

Examples

iex> c = Abit.Counter.new(10, 8)
iex> c |> Abit.Counter.add(7, -12)
iex> c |> Abit.Counter.add(7, -12)
:ok
iex> c |> Abit.Counter.get(7)
-24

Returns the value of counter at index.

Examples

iex> c = Abit.Counter.new(10, 8)
iex> c |> Abit.Counter.get(7)
0
Link to this function

new(size, counters_bit_size, options \\ [])

View Source
new(non_neg_integer(), 2 | 4 | 8 | 16 | 32, list()) :: t()

Returns a new %Abit.Counter{} struct.

  • size - minimum number of counters to have
  • counters_bit_size - how many bits a counter should use

Options:

  • signed - whether to have signed or unsigned counters.

Examples

Abit.Counter.new(100, 8) # minimum 100 counters; 8 bits signed
Abit.Counter.new(10_000, 16, signed: false) # minimum 10_000 counters; 16 bits unsigned
Link to this function

put(counter, index, value)

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

Puts the value into the counter at index.

Returns :ok.

Examples

iex> c = Abit.Counter.new(10, 8)
iex> c |> Abit.Counter.put(7, -12)
:ok
iex> c |> Abit.Counter.get(7)
-12