Skip to content

Commit d589887

Browse files
committed
Replace _size_inner by exported function innersize
1 parent abc61e6 commit d589887

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/array_of_similar_arrays.jl

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ end
6969
export ArrayOfSimilarArrays
7070

7171
function ArrayOfSimilarArrays{T,M,N}(A::AbstractArray{<:AbstractArray{U,M},N}) where {T,M,N,U}
72-
B = ArrayOfSimilarArrays{T,M,N}(Array{T}(undef, _size_inner(A)..., size(A)...))
72+
B = ArrayOfSimilarArrays{T,M,N}(Array{T}(undef, innersize(A)..., size(A)...))
7373
copyto!(B, A)
7474
end
7575

@@ -87,25 +87,13 @@ Base.convert(R::Type{ArrayOfSimilarArrays{T}}, A::AbstractArray{<:AbstractArray{
8787
Base.convert(R::Type{ArrayOfSimilarArrays}, A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N} = R(A)
8888

8989

90-
function _size_inner(A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N}
91-
s = if !isempty(A)
92-
sz_A = size(A[1])
93-
ntuple(i -> Int(sz_A[i]), Val(M))
94-
else
95-
ntuple(_ -> zero(Int), Val(M))
96-
end
97-
98-
all(X -> size(X) == s, A) || throw(DimensionMismatch("Shape of element arrays of A is not equal, can't determine common shape"))
99-
s
100-
end
101-
102-
@inline function _size_inner(A::ArrayOfSimilarArrays{T,M,N}) where {T,M,N}
90+
@inline function innersize(A::ArrayOfSimilarArrays{T,M,N}) where {T,M,N}
10391
front_tuple(size(A.data), Val{M}())
10492
end
10593

10694

107-
@inline function _length_inner(A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N}
108-
prod(_size_inner(A))
95+
@inline function _innerlength(A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N}
96+
prod(innersize(A))
10997
end
11098

11199

@@ -144,7 +132,7 @@ end
144132

145133

146134
@inline function Base.resize!(A::ArrayOfSimilarArrays{T,M,N}, dims::Vararg{Integer,N}) where {T,M,N}
147-
resize!(A.data, _size_inner(A)..., dims...)
135+
resize!(A.data, innersize(A)..., dims...)
148136
A
149137
end
150138

@@ -157,14 +145,14 @@ end
157145

158146

159147
function Base.resize!(dest::ArrayOfSimilarArrays{T,M,N}, src::ArrayOfSimilarArrays{U,M,N}) where {T,M,N,U}
160-
_size_inner(dest) != _size_inner(src) && throw(DimensionMismatch("Can't append, shape of element arrays of source and dest are not equal"))
148+
innersize(dest) != innersize(src) && throw(DimensionMismatch("Can't append, shape of element arrays of source and dest are not equal"))
161149
append!(dest.data, src.data)
162150
dest
163151
end
164152

165153

166154
function Base.append!(dest::ArrayOfSimilarArrays{T,M,N}, src::ArrayOfSimilarArrays{U,M,N}) where {T,M,N,U}
167-
_size_inner(dest) != _size_inner(src) && throw(DimensionMismatch("Can't append, shape of element arrays of source and dest are not equal"))
155+
innersize(dest) != innersize(src) && throw(DimensionMismatch("Can't append, shape of element arrays of source and dest are not equal"))
168156
append!(dest.data, src.data)
169157
dest
170158
end
@@ -174,7 +162,7 @@ Base.append!(dest::ArrayOfSimilarArrays{T,M,N}, src::AbstractArray{<:AbstractArr
174162

175163

176164
function Base.prepend!(dest::ArrayOfSimilarArrays{T,M,N}, src::ArrayOfSimilarArrays{U,M,N}) where {T,M,N,U}
177-
_size_inner(dest) != _size_inner(src) && throw(DimensionMismatch("Can't prepend, shape of element arrays of source and dest are not equal"))
165+
innersize(dest) != innersize(src) && throw(DimensionMismatch("Can't prepend, shape of element arrays of source and dest are not equal"))
178166
prepend!(dest.data, src.data)
179167
dest
180168
end
@@ -278,7 +266,7 @@ Base.convert(R::Type{ArrayOfSimilarVectors}, A::AbstractArray{<:AbstractVector{T
278266

279267
# Base.@propagate_inbounds function _linear_data_idxs(A::ArrayOfSimilarVectors, i::Integer)
280268
# @boundscheck checkbounds(A, i)
281-
# n_inner = _length_inner(A)
269+
# n_inner = _innerlength(A)
282270
# i0 = firstindex(A.data)
283271
# from = (i - i0) * n_inner + i0
284272
# to = from + n_inner - 1
@@ -288,7 +276,7 @@ Base.convert(R::Type{ArrayOfSimilarVectors}, A::AbstractArray{<:AbstractVector{T
288276

289277
# # Base.@propagate_inbounds function _linear_data_idxs(A::ArrayOfSimilarVectors, idxs::AbstractUnitRange{<:Integer})
290278
# # @boundscheck checkbounds(A, idxs)
291-
# # n_inner = _length_inner(A)
279+
# # n_inner = _innerlength(A)
292280
# # i0 = firstindex(A.data)
293281
# # a = first(idxs)
294282
# # b = last(idxs) + 1

src/functions.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,29 @@ export flatview
4545
flatview(A::AbstractArray) = A
4646

4747
flatview(A::AbstractArray{<:AbstractArray}) = Base.Iterators.flatten(A)
48+
49+
50+
51+
"""
52+
innersize(A:AbstractArray{<:AbstractArray}, [dim])
53+
54+
Returns the size of the element arrays of `A`. Fails if the element arrays
55+
are not of equal size.
56+
"""
57+
function innersize end
58+
export innersize
59+
60+
function innersize(A::AbstractArray{<:AbstractArray{T,M},N}) where {T,M,N}
61+
s = if !isempty(A)
62+
sz_A = size(first(A))
63+
ntuple(i -> Int(sz_A[i]), Val(M))
64+
else
65+
ntuple(_ -> zero(Int), Val(M))
66+
end
67+
68+
all(X -> size(X) == s, A) || throw(DimensionMismatch("Shape of element arrays of A is not equal, can't determine common shape"))
69+
s
70+
end
71+
72+
@inline innersize(A::AbstractArray{<:AbstractArray}, dim::Integer) =
73+
innersize(A)[dim]

0 commit comments

Comments
 (0)