ToonEx.Btoon.ElementType (toon_ex v1.3.1)

Copy Markdown View Source

Element type selectors and raw numeric encoding helpers.

Element types cover the fixed-width numeric types shared by TypedArray payloads and Schema fields (:int8, :uint8, :int16, :uint16, :int32, :uint32, :int64, :uint64, :float32, :float64) plus the composite types used only by Schema fields (:null, :bool, :string, :binary, :array, :object).

All multi-byte values are little-endian (see ToonEx.Btoon design notes).

Deterministic type selection

detect_type/1 maps a homogeneous numeric list to the narrowest signed integer type that represents every value losslessly, or :float32 when every float survives the float32 round-trip and :float64 otherwise. Every list therefore has exactly one encoding.

Summary

Types

Numeric element types (typed arrays and raw buffers).

Functions

Decodes a raw buffer into a list of numbers.

Decodes the first element of a buffer, returning {value, rest}.

Detects whether a list of maps is a valid columnar object table.

Detects the narrowest element type for a homogeneous numeric list.

Alias of size/1 for numeric types (raises for composite types).

Encodes a single number to its raw little-endian representation.

Whether a double survives the IEEE-754 float32 round-trip exactly.

Integer range for a numeric type: {min, max} for integer types, nil for float types.

Encodes a list of numbers into a contiguous raw buffer.

Whether the type is a fixed-width numeric type.

Element size in bytes (0 for variable-length composite types).

Maps a wire selector byte to a type atom. Raises ArgumentError for unknown selectors.

Maps a wire selector byte to a type atom or nil.

Maps a numeric type atom to its wire selector byte.

Whether the type may appear as a TypedArray element type.

Types

numeric()

@type numeric() ::
  :int8
  | :uint8
  | :int16
  | :uint16
  | :int32
  | :uint32
  | :int64
  | :uint64
  | :float32
  | :float64

Numeric element types (typed arrays and raw buffers).

Functions

buffer_to_list(type, data)

@spec buffer_to_list(ToonEx.Btoon.Types.element_type(), binary()) :: [number()]

Decodes a raw buffer into a list of numbers.

decode_raw(type, arg2)

@spec decode_raw(ToonEx.Btoon.Types.element_type(), binary()) :: {number(), binary()}

Decodes the first element of a buffer, returning {value, rest}.

detect_object_table(rows)

@spec detect_object_table([%{optional(String.t()) => term()}]) ::
  {:ok, [String.t()], [ToonEx.Btoon.Types.element_type()], [[number()]]}
  | :error

Detects whether a list of maps is a valid columnar object table.

All maps must share the same (sorted) key set and each column must be a homogeneous numeric column. Returns {:ok, names, types, columns} or :error.

detect_type(values)

@spec detect_type([number()]) :: {:ok, ToonEx.Btoon.Types.element_type()} | :error

Detects the narrowest element type for a homogeneous numeric list.

Returns {:ok, type} or :error when the list is empty, mixed, or contains integers outside the int64 range.

element_size(type)

@spec element_size(ToonEx.Btoon.Types.element_type()) :: pos_integer()

Alias of size/1 for numeric types (raises for composite types).

encode_raw(type, value)

@spec encode_raw(ToonEx.Btoon.Types.element_type(), number()) :: binary()

Encodes a single number to its raw little-endian representation.

f32_exact?(value)

@spec f32_exact?(float()) :: boolean()

Whether a double survives the IEEE-754 float32 round-trip exactly.

int_range(arg1)

@spec int_range(ToonEx.Btoon.Types.element_type()) :: {integer(), integer()} | nil

Integer range for a numeric type: {min, max} for integer types, nil for float types.

list_to_buffer(type, values)

@spec list_to_buffer(ToonEx.Btoon.Types.element_type(), [number()]) :: binary()

Encodes a list of numbers into a contiguous raw buffer.

numeric?(type)

@spec numeric?(ToonEx.Btoon.Types.element_type()) :: boolean()

Whether the type is a fixed-width numeric type.

size(type)

Element size in bytes (0 for variable-length composite types).

type_atom(byte)

@spec type_atom(byte()) :: ToonEx.Btoon.Types.element_type()

Maps a wire selector byte to a type atom. Raises ArgumentError for unknown selectors.

type_atom_or_nil(byte)

@spec type_atom_or_nil(byte()) :: ToonEx.Btoon.Types.element_type() | nil

Maps a wire selector byte to a type atom or nil.

type_byte(arg1)

@spec type_byte(ToonEx.Btoon.Types.element_type()) :: byte() | nil

Maps a numeric type atom to its wire selector byte.

Returns nil for unknown or composite types.

typed_array_type?(type)

@spec typed_array_type?(ToonEx.Btoon.Types.element_type()) :: boolean()

Whether the type may appear as a TypedArray element type.