Last active
January 3, 2021 07:12
-
-
Save mkitti/651b21ce7ef27fb954f53377a9331723 to your computer and use it in GitHub Desktop.
This file contains 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
module LargeSparseArrays | |
import Base: size, getindex, setindex! | |
export LargeSparseArray | |
struct LargeSparseArray{T,N} <: AbstractArray{T,N} | |
d::Dict{CartesianIndex{N},T} | |
dims::NTuple{N,AbstractUnitRange{Int}} | |
function LargeSparseArray{T}(dims::Int...) where T | |
dim_ranges = Base.OneTo.(dims) | |
dict = Dict{CartesianIndex{length(dims)},T}() | |
new{T,length(dims)}(dict, dim_ranges) | |
end | |
end | |
size(s::LargeSparseArray) = length.(s.dims) | |
getindex(s::LargeSparseArray{T,N}, I::Vararg{Int,N}) where {T,N} = | |
all( (I...,) .∈ s.dims ) ? get(s.d, CartesianIndex(I...), zero(T)) : BoundsError(s, (I...,)) | |
setindex!(s::LargeSparseArray{T,N}, v, I::Vararg{Int,N}) where {T,N} = | |
s.d[ CartesianIndex(I...) ] = v | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment