This module provides utility functions for working with Erlang's :atomics.
Summary
Functions
Deserializes a binary into an :atomics reference.
Checks whether an integer is present in the :atomics reference.
Serializes an :atomics reference into a binary.
Converts an :atomics reference to a list of integers.
Functions
Deserializes a binary into an :atomics reference.
This function takes a binary that was previously serialized using serialize/1
and reconstructs an :atomics reference from it.
The first byte is a format tag: 0 for unsigned values or 1 for signed
values. It is followed by one or more big-endian 64-bit values. Other tags
are reserved for future formats.
Raises ArgumentError when the binary is empty, has an unknown format tag,
contains no values, or has a payload that is not aligned to 64-bit values.
Parameters
binary- A binary containing the serialized:atomicsdata.
Returns
A reference to a new :atomics array containing the deserialized data.
Examples
iex> ref = :atomics.new(2, [])
iex> :atomics.put(ref, 1, 10)
iex> :atomics.put(ref, 2, -20)
iex> serialized = Abit.Atomics.serialize(ref)
iex> deserialized_ref = Abit.Atomics.deserialize(serialized)
iex> Abit.Atomics.to_list(deserialized_ref)
[10, -20]
Checks whether an integer is present in the :atomics reference.
This function checks whether the given integer matches any element in the
provided :atomics reference.
Parameters
atomics_ref- A reference to an:atomicsarray.int- The integer to search for.
Returns
Returns true if the integer is found in the :atomics array, or false otherwise.
Examples
iex> ref = :atomics.new(3, signed: false)
iex> :atomics.put(ref, 1, 10)
iex> :atomics.put(ref, 2, 20)
iex> :atomics.put(ref, 3, 30)
iex> Abit.Atomics.member?(ref, 20)
true
iex> Abit.Atomics.member?(ref, 40)
false
Serializes an :atomics reference into a binary.
This function takes an :atomics reference and returns a binary in which each
64-bit integer is encoded in big-endian format.
Parameters
atomics_ref- A reference to an:atomicsarray.
Returns
A binary containing the serialized :atomics data.
Examples
iex> ref = :atomics.new(2, signed: false)
iex> :atomics.put(ref, 1, 1)
iex> :atomics.put(ref, 2, 2)
iex> Abit.Atomics.serialize(ref)
<<0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2>>
Converts an :atomics reference to a list of integers.
This function takes an :atomics reference and returns a list containing
the value of each element.
Parameters
atomics_ref- A reference to an:atomicsarray.
Returns
A list of integers representing the values stored in the :atomics array.
Examples
iex> ref = :atomics.new(3, signed: false)
iex> :atomics.put(ref, 1, 10)
iex> :atomics.put(ref, 2, 20)
iex> :atomics.put(ref, 3, 30)
iex> Abit.Atomics.to_list(ref)
[10, 20, 30]