Skip to content

Commit

Permalink
Empty constructors and appending methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpivarski committed Aug 8, 2023
1 parent 2ea89c0 commit 579e204
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/AwkwardArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -69,13 +73,24 @@ 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
offsets::INDEX
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
Expand Down Expand Up @@ -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
40 changes: 40 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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

0 comments on commit 579e204

Please sign in to comment.