Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: add SubVector (fast 1d slices) #3224

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export
SubDArray,
SubOrDArray,
SubString,
SubVector,
SymTridiagonal,
TcpSocket,
TmStruct,
Expand Down
90 changes: 90 additions & 0 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,93 @@ end

summary(s::SubArray) =
string(dims2string(size(s)), " SubArray of ", summary(s.parent))



# SubVectors are fast 1d slices
immutable SubVector{T} <: AbstractArray{T,1}
ptr::Ptr{T}
stride::Int
len::Int
end

SubVector{T}(A::Array{T}, firstindex::Integer, strd::Integer, len::Integer) = SubVector{T}(convert(Ptr{T}, A) + (firstindex - 1)*sizeof(T), int(strd), int(len))

function SubVector{T}(A::Array{T}, indexes::RangeIndex...)
n = 0
for i in indexes
n += !isa(i, Int)
end
if n > 1
error("Must be 1-dimensional")
end
firstindex = 1
strd = 0
len = 1
pstride = 1
for j = 1:length(indexes)
i = indexes[j]
if min(i) < 1 || max(i) > size(A, j)
error(BoundsError)
end
if isa(i, Int)
firstindex += (i-1)*pstride
else
strd = pstride * step(i)
len = length(i)
firstindex += (first(i)-1)*pstride
end
pstride *= size(A,j)
end
SubVector(A, firstindex, strd, len)
end

function SubVector{T}(s::SubVector{T}, index::RangeIndex)
if min(index) < 1 || max(index) > length(s)
error(BoundsError)
end
ptr = s.ptr + (first(index)-1)*s.stride*sizeof(T)
strd = s.stride * step(index)
len = length(index)
SubVector{T}(ptr, strd, len)
end

ndims(s::SubVector) = 1
length(s::SubVector) = s.len
size(s::SubVector) = (s.len,)
size(s::SubVector, d::Integer) = (d == 1) ? s.len : 1
eltype{T}(s::SubVector{T}) = T

similar{T}(s::SubVector, ::Type{T}, sz::NTuple{1, Int}) = Array(T, sz)
function similar{T}(s::SubVector, ::Type{T}, sz::Dims)
n = 0
for len in sz
n += (len > 1)
end
if n > 1
error("Requested type is not a vector")
end
Array(T, sz)
end

unsafe_load(s::SubVector, i::Integer) = unsafe_load(s.ptr, (i-1)*s.stride + 1)

function getindex(s::SubVector, i::Integer)
if 1 <= i <= length(s)
return unsafe_load(s, i)
else
error(BoundsError)
end
end

unsafe_store!{T}(s::SubVector{T}, x, i::Integer) = unsafe_store!(s.ptr, x, (i-1)*s.stride + 1)

function setindex!{T}(s::SubVector{T}, x, i::Integer)
xT = convert(T, x)
if 1 <= i <= length(s)
unsafe_store!(s, xT, i)
else
error(BoundsError)
end
xT
end
13 changes: 13 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ sA[2:5:end] = -1
@test all(sA[2:5:end] .== -1)
@test all(A[5:15:120] .== -1)

# SubVector
A = reshape(1:48, 8, 6)
s = SubVector(A, 1:3:8, 2)
@assert s[2] == A[4,2]
@assert s == A[1:3:8, 2]
s = SubVector(A, 2:3:8, 2)
@assert s == A[2:3:8, 2]
s = SubVector(A, 4, 3:6)
@assert s == vec(A[4, 3:6])
s[1] = 0
@assert A[4,3] == 0
ss = SubVector(s, 4:-2:2)
@assert ss == [44,28]

# get
let
Expand Down