Skip to content

Commit 0961df3

Browse files
committed
Rely on julia-0.5 indexing mechanisms to fix a segfault (julia issue #17328)
1 parent e04b7a8 commit 0961df3

File tree

2 files changed

+11
-33
lines changed

2 files changed

+11
-33
lines changed

src/index.jl

+7-33
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,12 @@
66

77
import Base.getindex, Base.to_index
88

9-
## ambiguity from abstractarray.jl
10-
getindex(a::NamedArray, i::Real) = namedgetindex(a, indices(a.dicts[1], i))
11-
getindex(a::NamedArray, i::AbstractArray) = namedgetindex(a, indices(a.dicts[1], i))
12-
## from subarray.jl
13-
getindex{T}(a::NamedArray{T,1}, ::Colon) = a
14-
159
## special 0-dimensional case
1610
getindex{T}(a::NamedArray{T,0}, i::Real) = getindex(a.array, i)
1711

18-
getindex(a::NamedArray, i) = namedgetindex(a, indices(a.dicts[1], i))
19-
getindex(a::NamedArray, i1, i2) = namedgetindex(a, indices(a.dicts[1], i1), indices(a.dicts[2], i2))
20-
getindex(a::NamedArray, i1, i2, i3) = namedgetindex(a, indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3))
21-
getindex(a::NamedArray, i1, i2, i3, i4) = namedgetindex(a, indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4))
22-
getindex(a::NamedArray, i1, i2, i3, i4, i5) = namedgetindex(a, indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4), indices(a.dicts[5], i5))
23-
getindex(a::NamedArray, i1, i2, i3, i4, i5, I...) = namedgetindex(a, indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4), indices(a.dicts[5], i5), [indices(a.dicts[5+i], ind) for (i,ind) in enumerate(I)]...)
12+
@inline function getindex{T,N}(a::NamedArray{T,N}, I::Vararg{Any,N})
13+
namedgetindex(a, map((d,i)->indices(d, i), a.dicts, I)...)
14+
end
2415

2516
## 0.4-dev functions
2617
if VERSION >= v"0.4.0-dev"
@@ -95,22 +86,8 @@ import Base.setindex!
9586

9687
setindex!{T}(A::NamedArray{T}, x) = setindex!(A, convert(T,x), 1)
9788

98-
setindex!{T}(a::NamedArray{T}, x, i1::Real) = setindex!(a.array, convert(T,x), indices(a.dicts[1],i1))
99-
setindex!{T}(a::NamedArray{T}, x, i1::Real, i2::Real) =
100-
setindex!(a.array, convert(T,x), indices(a.dicts[1], i1), indices(a.dicts[2], i2))
101-
setindex!{T}(a::NamedArray{T}, x, i1::Real, i2::Real, i3::Real) =
102-
setindex!(a.array, convert(T,x), indices(a.dicts[1],i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3))
103-
setindex!{T}(a::NamedArray{T}, x, i1::Real, i2::Real, i3::Real, i4::Real) =
104-
setindex!(a.array, convert(T,x), indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4))
105-
setindex!{T}(a::NamedArray{T}, x, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real) =
106-
setindex!(a.array, convert(T,x), indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4), indices(a.dicts[5], i5))
107-
setindex!{T}(a::NamedArray{T}, x, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, i6::Real) =
108-
setindex!(a.array, convert(T,x), indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4), indices(a.dicts[5], i5), indices(a.dicts[6], i6))
109-
setindex!{T}(a::NamedArray{T}, x, i1::Real, i2::Real, i3::Real, i4::Real, i5::Real, i6::Real, I...) =
110-
setindex!(a.array, convert(T,x), indices(a.dicts[1], i1), indices(a.dicts[2], i2), indices(a.dicts[3], i3), indices(a.dicts[4], i4), indices(a.dicts[5], i5), indices(a.dicts[6], i6), I...)
111-
11289
# n[1:4] = 5
113-
setindex!{T<:Real}(A::NamedArray, x, I::AbstractVector{T}) = setindex!(A.array, x, I)
90+
setindex!{T<:Real}(A::NamedArray, x, I::Union{Colon,AbstractVector{T}}) = setindex!(A.array, x, I)
11491

11592
# n[1:4] = 1:4
11693
## shamelessly copied from array.jl
@@ -120,12 +97,9 @@ function setindex!{T}(A::NamedArray{T}, X::ArrayOrNamed{T}, I::Range{Int})
12097
return A
12198
end
12299

123-
# n[[1,3,4,6]] = 1:4
124-
setindex!{T<:Real}(A::NamedArray, X::AbstractArray, I::AbstractVector{T}) = setindex!(A.array, X, I)
125-
126100
## This takes care of most other cases
127-
function setindex!(A::NamedArray, x, I...)
128-
II = tuple([indices(A.dicts[i], I[i]) for i=1:length(I)]...)
101+
@inline function setindex!{T,N}(A::NamedArray{T,N}, x, I::Vararg{Any,N})
102+
II = map((d,i)->indices(d, i), A.dicts, I)
129103
setindex!(A.array, x, II...)
130104
end
131105

@@ -134,4 +108,4 @@ if VERSION >= v"0.4.0-dev"
134108
setindex!(a::NamedArray, x, it::Base.IteratorsMD.CartesianIndex) = setindex!(a.array, x, it)
135109
end
136110

137-
setindex!(n::NamedArray, x, I::Pair...) = setindex!(n.array, x, indices(n, I...)...)
111+
@inline setindex!{T,N}(n::NamedArray{T,N}, x, I::Vararg{Pair,N}) = setindex!(n.array, x, indices(n, I...)...)

test/test.jl

+4
Original file line numberDiff line numberDiff line change
@@ -244,4 +244,8 @@ t2 = @elapsed sgetindex(n.array)
244244
si, sj = allnames(n)
245245
t3 = @elapsed sgetindex(n, si, sj)
246246

247+
# julia issue #17328
248+
a = NamedArray([1.0,2.0,3.0,4.0])
249+
@test sumabs(a, 1)[1] == 10
250+
247251
println("Timing named index:", t1, " array index:", t2, " named key:", t3)

0 commit comments

Comments
 (0)