Skip to content

Commit 7182958

Browse files
committed
Initial commit
1 parent d5ad121 commit 7182958

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/sparse_array.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ SparseArray{T}(dims::NTuple{N, Int}, default_value::T = zero(T)) where {T, N} =
3838
SparseArray{T}(dims::Vararg{Int, N}) where {T, N} =
3939
SparseArray{T, N}(dims, zero(T))
4040

41+
# Array-like constructor: SparseArray{T, N}(undef, dims...)
42+
SparseArray{T, N}(::UndefInitializer, dims::Vararg{Int, N}) where {T, N} =
43+
SparseArray{T, N}(dims, zero(T))
44+
45+
SparseArray{T, N}(::UndefInitializer, dims::NTuple{N, Int}) where {T, N} =
46+
SparseArray{T, N}(dims, zero(T))
47+
4148
# Constructor from dense array
4249
function SparseArray(A::AbstractArray{T, N}) where {T, N}
4350
sparse_array = SparseArray{T, N}(size(A))

test/test_construction.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ using Test
3636
@test E[3, 1] == 2
3737
@test E[3, 3] == 4
3838
@test nnz(E) == 4 # Only non-zero elements stored
39+
40+
# Array-like constructor with undef
41+
F = SparseArray{Float64, 2}(undef, 2, 3)
42+
@test size(F) == (2, 3)
43+
@test eltype(F) == Float64
44+
@test nnz(F) == 0
45+
@test F.default_value == 0.0
46+
47+
G = SparseArray{Int, 3}(undef, (2, 2, 2))
48+
@test size(G) == (2, 2, 2)
49+
@test eltype(G) == Int
50+
@test nnz(G) == 0
3951
end
4052

4153
end

0 commit comments

Comments
 (0)