From 579e20478c41327590c6601c5135dc020e14cf44 Mon Sep 17 00:00:00 2001 From: Jim Pivarski Date: Tue, 8 Aug 2023 18:53:11 -0500 Subject: [PATCH] Empty constructors and appending methods. --- src/AwkwardArray.jl | 24 ++++++++++++++++++++++-- test/runtests.jl | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/src/AwkwardArray.jl b/src/AwkwardArray.jl index 1fe1271..e89bb2e 100644 --- a/src/AwkwardArray.jl +++ b/src/AwkwardArray.jl @@ -10,7 +10,7 @@ Index64 = AbstractArray{Int64,1} ### Content ############################################################## -abstract type Content <: AbstractArray{T where T<:Any,1} end +abstract type Content <: AbstractArray{T where T,1} end function Base.iterate(layout::Content) start = firstindex(layout) @@ -37,10 +37,14 @@ end ### PrimitiveArray ####################################################### -struct PrimitiveArray{ARRAY<:AbstractArray{T,1} where {T<:Any}} <: Content +struct PrimitiveArray{T,ARRAY<:AbstractArray{T,1}} <: Content data::ARRAY end +function PrimitiveArray{T}() where {T} + PrimitiveArray(Vector{T}([])) +end + function is_valid(layout::PrimitiveArray) true end @@ -69,6 +73,10 @@ function Base.:(==)(layout1::PrimitiveArray, layout2::PrimitiveArray) layout1.data == layout2.data end +function push!(layout::PrimitiveArray{T}, x::T) where {T} + Base.push!(layout.data, x) +end + ### ListOffsetArray ###################################################### struct ListOffsetArray{INDEX<:Union{Index32,IndexU32,Index64},CONTENT<:Content} <: Content @@ -76,6 +84,13 @@ struct ListOffsetArray{INDEX<:Union{Index32,IndexU32,Index64},CONTENT<:Content} content::CONTENT end +function ListOffsetArray{ + INDEX, + CONTENT, +}() where {INDEX<:Union{Index32,IndexU32,Index64}} where {CONTENT<:Content} + AwkwardArray.ListOffsetArray(INDEX([0]), CONTENT()) +end + function is_valid(layout::ListOffsetArray) if length(layout.offsets) < 1 return false @@ -126,4 +141,9 @@ function Base.:(==)(layout1::ListOffsetArray, layout2::ListOffsetArray) end end +function end_list!(layout::ListOffsetArray) + Base.push!(layout.offsets, length(layout.content)) + layout.content +end + end diff --git a/test/runtests.jl b/test/runtests.jl index 113445c..b91044b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -18,6 +18,22 @@ using Test @test tmp == 16.5 end + begin + layout = AwkwardArray.PrimitiveArray{Float64}() + @test length(layout) == 0 + AwkwardArray.push!(layout, 1.1) + @test length(layout) == 1 + AwkwardArray.push!(layout, 2.2) + @test length(layout) == 2 + AwkwardArray.push!(layout, 3.3) + @test length(layout) == 3 + AwkwardArray.push!(layout, 4.4) + @test length(layout) == 4 + AwkwardArray.push!(layout, 5.5) + @test length(layout) == 5 + @test layout == AwkwardArray.PrimitiveArray([1.1, 2.2, 3.3, 4.4, 5.5]) + end + begin layout = AwkwardArray.ListOffsetArray( [0, 3, 3, 5], @@ -64,4 +80,28 @@ using Test @test !AwkwardArray.is_valid(layout) end + begin + layout = AwkwardArray.ListOffsetArray{ + AwkwardArray.Index64, + AwkwardArray.PrimitiveArray{Float64}, + }() + sublayout = layout.content + @test length(layout) == 0 + AwkwardArray.push!(sublayout, 1.1) + AwkwardArray.push!(sublayout, 2.2) + AwkwardArray.push!(sublayout, 3.3) + AwkwardArray.end_list!(layout) + @test length(layout) == 1 + AwkwardArray.end_list!(layout) + @test length(layout) == 2 + AwkwardArray.push!(sublayout, 4.4) + AwkwardArray.push!(sublayout, 5.5) + AwkwardArray.end_list!(layout) + @test length(layout) == 3 + @test layout == AwkwardArray.ListOffsetArray( + [0, 3, 3, 5], + AwkwardArray.PrimitiveArray([1.1, 2.2, 3.3, 4.4, 5.5]), + ) + end + end