Can I get a code review on this wrapper class inheriting from AbstractArray

Can I please get a code review on this wrapper class inheriting from AbstractArray?

S = WrappedSeries(5)
S[1, :A] += 1
S[end, :A] += 1


import Base: to_index

mutable struct WrappedSeries <: AbstractArray{Int64,2}
  data::CircularBuffer{Vector{Int64}}
end

function WrappedSeries(length::Int)
  data = CircularBuffer{Vector{Int64}}(length)
  fill!(data, [0, 0])
  WrappedSeries(data)
end

@inline Base.getindex(S::WrappedSeries, i::Int, j::Int) = S.data[i][j]
@inline Base.setindex!(S::WrappedSeries, value, i::Int, j::Int) = S.data[i][j] = value
Base.size(S::WrappedSeries) = (length(S.data), 2)
Base.length(S::WrappedSeries) = length(S.data)
Base.eltype(::Type{WrappedSeries}) = Int64
Base.IndexStyle(::Type{<:WrappedSeries}) = IndexCartesian()
Base.convert(::Type{AbstractArray}, cb::WrappedSeries) = [x for x in cb]

@inline function Base.getindex(S::WrappedSeries, r::Int, c::Symbol)
  S[r, to_index(S, c)]
end

@inline function Base.setindex!(S::WrappedSeries, value, r::Int, c::Symbol)
  S[r, to_index(S, c)] = value
  S
end

function to_index(S::WrappedSeries, s::Symbol)
  @match s begin
    :A => 1
    :B => 2
    _ => throw(ArgumentError("Bad arg"))
  end
end
1 Like