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_product — Function
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:
Give the name
Ttoeltype(size_tuple)for the purposes of this doc string.The user must ensure
Tsupports:Base.Checked.mul_with_overflowiszerotypemax<==
Calculate the product. In case no element is negative, no element is
typemax(T)and the product does not overflow, return the product. Otherwise returnnothing.
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{})
[...]