Created
November 28, 2018 03:49
-
-
Save tkf/ab9ca58f6629ce3d7c3f0ca6ab9eb88c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using SparseArrays | |
using MappedArrays | |
using MappedArrays: testvalue | |
struct ReadonlyMappedSparseMatrixCSC{Tv, Ti, | |
A <: AbstractSparseMatrix{Tv, Ti}, | |
F, | |
} <: AbstractMappedArray{Tv, 2} | |
# } <: AbstractSparseMatrix{Tv, Ti} | |
f::F | |
data::A | |
end | |
function MappedArrays.mappedarray(f, data::AbstractSparseMatrix) | |
Tv = typeof(f(testvalue(data))) | |
Ti = eltype(rowvals(data)) | |
ReadonlyMappedSparseMatrixCSC{Tv, Ti, typeof(data), typeof(f)}(f, data) | |
end | |
SparseArrays.nonzeros(S::ReadonlyMappedSparseMatrixCSC) = | |
mappedarray(S.f, nonzeros(S.data)) | |
SparseArrays.rowvals(S::ReadonlyMappedSparseMatrixCSC) = rowvals(S.data) | |
SparseArrays.nzrange(S::ReadonlyMappedSparseMatrixCSC, col::Integer) = | |
nzrange(S.data, col) | |
SparseArrays.nnz(S::ReadonlyMappedSparseMatrixCSC) = nnz(S.data) | |
function SparseArrays.findnz(S::ReadonlyMappedSparseMatrixCSC) | |
I, J, V = findnz(S.data) | |
# V is a new array so materialize it here | |
if eltype(V) === eltype(S) | |
map!(S.f, V, V) | |
return (I, J, V) | |
else | |
return (I, J, S.f.(V)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment