Skip to content

Commit cded82c

Browse files
committed
use CheckedSizeProduct.jl for implementing checked_dims
Fixes JuliaArrays#89
1 parent 2f729fe commit cded82c

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@ uuid = "3821ddf9-e5b5-40d5-8e25-6813ab96b5e2"
33
authors = ["Mosè Giordano <[email protected]>"]
44
version = "0.1.0"
55

6+
[deps]
7+
CheckedSizeProduct = "1b2cc9da-92e3-4b71-9788-30fc110eefda"
8+
69
[compat]
10+
CheckedSizeProduct = "1"
711
julia = "1.10"

src/FixedSizeArray.jl

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,32 +96,23 @@ Base.isassigned(a::FixedSizeArray, i::Int) = isassigned(a.mem, i)
9696

9797
# safe product of a tuple of integers, for calculating dimensions size
9898

99-
checked_dims_impl(a::Int, ::Tuple{}, have_overflow::Bool) = (a, have_overflow)
100-
function checked_dims_impl(a::Int, t::Tuple{Int,Vararg{Int,N}}, have_overflow::Bool) where {N}
101-
b = first(t)
102-
(m, o) = Base.Checked.mul_with_overflow(a, b)
103-
r = Base.tail(t)::NTuple{N,Int}
104-
checked_dims_impl(m, r, have_overflow | o)::Tuple{Int,Bool}
105-
end
106-
10799
checked_dims(::Tuple{}) = 1
108100
function checked_dims(t::Tuple{Int,Vararg{Int,N}}) where {N}
109-
any_is_zero = any(iszero, t)::Bool
110-
any_is_negative = any((x -> x < false), t)::Bool
111-
any_is_typemax = any((x -> x == typemax(x)), t)::Bool
112-
a = first(t)
113-
r = Base.tail(t)::NTuple{N,Int}
114-
(product, have_overflow) = checked_dims_impl(a, r, false)::Tuple{Int,Bool}
115-
if any_is_negative
116-
throw(ArgumentError("array dimension size can't be negative"))
117-
end
118-
if any_is_typemax
119-
throw(ArgumentError("array dimension size can't be the maximum representable value"))
120-
end
121-
if have_overflow & !any_is_zero
122-
throw(ArgumentError("array dimensions too great, can't represent length"))
101+
product = checked_size_product(t)
102+
if product isa Int
103+
product
104+
else
105+
if product.any_is_negative
106+
throw(ArgumentError("array dimension size can't be negative"))
107+
end
108+
if product.any_is_typemax
109+
throw(ArgumentError("array dimension size can't be the maximum representable value"))
110+
end
111+
if product.is_not_representable
112+
throw(ArgumentError("array dimensions too great, can't represent length"))
113+
end
114+
throw(ArgumentError("unexpected"))
123115
end
124-
product
125116
end
126117

127118
# broadcasting

src/FixedSizeArrays.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module FixedSizeArrays
22

3+
using CheckedSizeProduct
4+
35
const default_underlying_storage_type = (@isdefined Memory) ? Memory : Vector
46

57
const optional_memory = (@isdefined Memory) ? (Memory,) : ()

0 commit comments

Comments
 (0)