Skip to content

Commit

Permalink
switch LocalArray to DenseArray subtype
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredCrean2 committed Jun 3, 2016
1 parent bf10c5f commit 3232f63
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ export LocalArray, LocalArrayRead, LocalArrayRestore
Object representing the local part of the array, accessing the memory directly.
Supports all the same indexing as a regular Array
"""
type LocalArray{T <: Scalar} <: AbstractArray{T, 1}
type LocalArray{T <: Scalar} <: DenseArray{T, 1}
a::Array{T, 1} # the array object constructed around the pointer
ref::Ref{Ptr{T}} # reference to the pointer to the data
pobj::C.Vec{T}
Expand Down Expand Up @@ -712,7 +712,7 @@ end
@doc """
Get read-only access to the memory underlying a Petsc vector
"""
type LocalArrayRead{T <: Scalar} <: AbstractArray{T, 1}
type LocalArrayRead{T <: Scalar} <: DenseArray{T, 1}
a::Array{T, 1} # the array object constructed around the pointer
ref::Ref{Ptr{T}} # reference to the pointer to the data
pobj::C.Vec{T}
Expand Down Expand Up @@ -753,15 +753,15 @@ end
@doc """
Typealias for both kinds of LocalArrays
"""
typealias LocalArrays Union{LocalArray, LocalArrayRead}
typealias LocalArrays{T} Union{LocalArray{T}, LocalArrayRead{T}}

Base.linearindexing(varr::LocalArrays) = Base.linearindexing(varr.a)
Base.size(varr::LocalArrays) = size(varr.a)
Base.length(varr::LocalArrays) = size(varr)[1]
# indexing
getindex(varr::LocalArrays, i) = getindex(varr.a, i)
setindex!(varr::LocalArray, v, i) = setindex!(varr.a, v, i)

Base.unsafe_convert{T}(::Type{Ptr{T}}, a::LocalArrays{T}) = Base.unsafe_convert(Ptr{T}, a.a)
Base.stride(a::LocalArrays, d::Integer) = stride(a.a, d)
Base.similar(a::LocalArrays, T=eltype(a), dims=size(a)) = similar(a.a, T, dims)
Base.copy(varr::LocalArrays) = deepcopy(varr)
function (==)(x::LocalArrays, y::AbstractArray)
return x.a == y
Expand Down
8 changes: 8 additions & 0 deletions test/vec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,20 @@
vec5 = Vec(Float64, 4)
varr = LocalArray(vec5)
@test length(vec5) == 4
@test length(varr) == length(vec5)
@test stride(varr, 1) == 1
vec5j = [1., 2, 3, 4]
for i=1:length(vec5) varr[i] = vec5j[i] end

@test varr[1] == vec5j[1]
@test varr == vec5j

varr2 = similar(varr)
T2 = eltype(varr)
@test typeof(varr2) == Array{eltype(T2), 1}
ptr = Base.unsafe_convert(Ptr{T2}, varr)
@test ptr == varr.ref[]

LocalArrayRestore(varr)

@test vec5 == vec5j
Expand Down

0 comments on commit 3232f63

Please sign in to comment.