View Source Abit.Atomics (abit v0.3.3)

This module provides utility functions for working with Erlang's :atomics.

Summary

Functions

Deserializes a binary into an :atomics reference.

Checks if 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

Link to this function

deserialize(arg)

View Source (since 0.3.3)
@spec deserialize(binary()) :: reference()

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.

Parameters

  • binary - A binary containing the serialized :atomics data.

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]
Link to this function

member?(atomics_ref, int)

View Source
@spec member?(reference(), integer()) :: boolean()

Checks if an integer is present in the :atomics reference.

This function checks if the given integer exists as a value in any of the atomics within the provided :atomics reference.

Parameters

  • atomics_ref - A reference to an :atomics array.
  • int - The integer to search for.

Returns

Returns true if the integer is found in the :atomics array, 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
Link to this function

serialize(atomics_ref)

View Source (since 0.3.3)
@spec serialize(reference()) :: binary()

Serializes an :atomics reference into a binary.

This function takes an :atomics reference and returns a binary where each 64-bit integer in the :atomics array is encoded in big-endian format.

Parameters

  • atomics_ref - A reference to an :atomics array.

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>>
@spec to_list(reference()) :: [integer()]

Converts an :atomics reference to a list of integers.

This function takes an :atomics reference and returns a list of integers, where each integer represents the value stored in each atomic.

Parameters

  • atomics_ref - A reference to an :atomics array.

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]