CheckedSizeProduct

CheckedSizeProduct is a software package for the Julia programming language to safely calculate the length of an in-memory dense array given its dimensions.

CheckedSizeProduct.checked_size_productFunction
checked_size_product(size_tuple)

In short; a safe product, suitable for computing, e.g., the value of length(dense_array) from the value of size(dense_array). In case everything is as expected, return the value of the product of the elements of the given tuple. Otherwise, return nothing.

In more detail; given size_tuple, a nonempty homogeneous tuple of Integer-likes:

  1. Give the name T to eltype(size_tuple) for the purposes of this doc string.

  2. The user must ensure T supports:

    • Base.Checked.mul_with_overflow
    • iszero
    • typemax
    • <
    • ==
  3. Calculate the product. In case no element is negative, no element is typemax(T) and the product does not overflow, return the product. Otherwise return nothing.

Throws if isempty(size_tuple) to avoid having to choose a return type arbitrarily.

Usage examples in the REPL

julia> using CheckedSizeProduct

julia> checked_size_product((3, 7))
21

julia> checked_size_product((3, -7))

julia> checked_size_product((0, -7))

julia> checked_size_product((0, 7))
0

julia> checked_size_product(())
ERROR: MethodError: no method matching checked_size_product(::Tuple{})
[...]
source