Skip to content

Commit

Permalink
Merge pull request #11 from JuliaArrays/teh/generalize
Browse files Browse the repository at this point in the history
Support arbitrary AbstractArrays
  • Loading branch information
timholy authored Nov 24, 2017
2 parents 103a3c4 + 1894552 commit 135a274
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
1 change: 0 additions & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
julia 0.6
Compat 0.19
18 changes: 9 additions & 9 deletions src/IndirectArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ __precompile__(true)

module IndirectArrays

using Compat

export IndirectArray

"""
Expand All @@ -13,22 +11,24 @@ creates an array `A` where the values are looked up in the value table,
`values`, using the `index`. Concretely, `A[i,j] =
values[index[i,j]]`.
"""
struct IndirectArray{T,N,I<:Integer} <: AbstractArray{T,N}
index::Array{I,N}
values::Vector{T}
struct IndirectArray{T,N,A<:AbstractArray{<:Integer,N},V<:AbstractVector{T}} <: AbstractArray{T,N}
index::A
values::V

@inline function IndirectArray{T,N,I}(index, values) where {T,N,I}
@inline function IndirectArray{T,N,A,V}(index, values) where {T,N,A,V}
# The typical logic for testing bounds and then using
# @inbounds will not check whether index is inbounds for
# values. So we had better check this on construction.
@boundscheck checkbounds(values, index)
new{T,N,I}(index, values)
new{T,N,A,V}(index, values)
end
end
Base.@propagate_inbounds IndirectArray(index::Array{I,N},values::Vector{T}) where {T,N,I<:Integer} = IndirectArray{T,N,I}(index,values)
Base.@propagate_inbounds IndirectArray(index::AbstractArray{<:Integer,N}, values::AbstractVector{T}) where {T,N} =
IndirectArray{T,N,typeof(index),typeof(values)}(index, values)

Base.size(A::IndirectArray) = size(A.index)
Base.IndexStyle(::Type{<:IndirectArray}) = IndexLinear()
Base.indices(A::IndirectArray) = indices(A.index)
Base.IndexStyle(::Type{IndirectArray{T,N,A,V}}) where {T,N,A,V} = IndexStyle(A)

@inline function Base.getindex(A::IndirectArray, i::Int)
@boundscheck checkbounds(A.index, i)
Expand Down
1 change: 1 addition & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
FixedPointNumbers 0.3.0
Colors
MappedArrays
12 changes: 11 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using IndirectArrays
using IndirectArrays, MappedArrays
using Base.Test, FixedPointNumbers, Colors

colors = [RGB(1,0,0), RGB(0,1,0), RGB(0,0,1)]
Expand Down Expand Up @@ -27,3 +27,13 @@ unsafe_ia(idx, vals) = (@inbounds ret = IndirectArray(idx, vals); ret)
# B = unsafe_ia(index_ob, colors)
# @test_throws BoundsError B[1]
# @test B[2] == RGB(0,0,1)

# Non-Arrays
a = [0.1 0.4;
0.33 1.0]
f(x) = round(Int, 99*x) + 1 # maps 0-1 to 1-100
m = mappedarray(f, a)
cmap = colormap("RdBu", 100)
img = IndirectArray(m, cmap)
@test img == [cmap[11] cmap[41];
cmap[34] cmap[100]]

0 comments on commit 135a274

Please sign in to comment.