Skip to content

Commit 2928be9

Browse files
authored
Merge pull request #228 from ReactiveBayes/resizablearray_bugs
Fix `map` and `lastwithindex` bugs in `ResizableArray`
2 parents 4f8613b + 775ac69 commit 2928be9

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/resizable_array.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ end
1717

1818
ResizableArray(::Type{T}) where {T} = ResizableArray{T, Vector{T}, 1}(T[])
1919

20+
similar(::ResizableArray{T, V, N}) where {T, V, N} = ResizableArray(T, Val(N))
21+
2022
function ResizableArray(::Type{T}, ::Val{N}) where {T, N}
2123
data = make_recursive_vector(T, Val(N))
2224
V = typeof(data)
@@ -29,13 +31,16 @@ end
2931

3032
get_recursive_depth(any) = 0
3133

32-
function reltype(v::AbstractVector)
33-
return reltype(first(v))
34+
function reltype(::Type{<:AbstractVector{<:T}}) where {T <: AbstractArray}
35+
return reltype(T)
36+
end
37+
38+
function reltype(::Type{<:AbstractVector{T}}) where {T}
39+
return T
3440
end
35-
reltype(any::T) where {T} = T
3641

3742
function ResizableArray(array::AbstractVector{T}) where {T}
38-
V = reltype(array)
43+
V = reltype(typeof(array))
3944
return ResizableArray{V, Vector{T}, get_recursive_depth(array)}(array)
4045
end
4146

@@ -184,11 +189,6 @@ function Base.iterate(array::ResizableArray, state)
184189
return (array[nindex.I...], isnothing(nstate) ? nothing : (indx, nstate))
185190
end
186191

187-
function Base.map(f, array::ResizableArray{T, V, N}) where {T, V, N}
188-
result = map(f, array.data)
189-
return ResizableArray(result)
190-
end
191-
192192
__length(array::ResizableArray{T, V, N}) where {T, V, N} = __recursive_length(Val(N), array.data)
193193

194194
function __recursive_length(::Val{N}, array) where {N}
@@ -241,7 +241,7 @@ end
241241
function lastwithindex(array::ResizableArray{T, V, N}) where {T, V, N}
242242
for index in reverse(CartesianIndices(reverse(size(array)))) #TODO improve performance of this function since it uses splatting
243243
if isassigned(array, reverse(index.I)...)::Bool
244-
return (index, array[reverse(index.I)...])
244+
return (CartesianIndex(reverse(index.I)), array[reverse(index.I)...])
245245
end
246246
end
247247
end

test/resizable_array_tests.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ end
278278
s[i, j] = i + j
279279
end
280280
end
281-
s[5, 11] = 16
282-
result = map(elem -> elem .+ 1, s)
281+
result = map(elem -> elem + 1, s)
283282
for i in 1:10
284283
for j in 1:10
285284
@test result[i, j] == s[i, j] + 1

0 commit comments

Comments
 (0)