Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export CircularMatrix #25

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/CircularArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Arrays with fixed size and circular indexing.
"""
module CircularArrays

export CircularArray, CircularVector
export CircularArray, CircularVector, CircularMatrix

"""
CircularArray{T, N, A} <: AbstractArray{T, N}
Expand Down Expand Up @@ -106,13 +106,28 @@ Create a `CircularVector` wrapping the array `data`.
"""
CircularVector(data::AbstractArray{T, 1}) where T = CircularVector{T}(data)

"""
CircularMatrix(data)

Create a `CircularMatrix` wrapping the array `data`.
"""
CircularMatrix(data::AbstractArray{T, 2}) where T = CircularMatrix{T}(data)


"""
CircularVector(def, size)

Create a `CircularVector` of size `size` filled with value `def`.
"""
CircularVector(def::T, size::Int) where T = CircularVector{T}(fill(def, size))

"""
CircularMatrix(def, size)

Create a `CircularMatrix` of size `size` filled with value `def`.
"""
CircularMatrix(def::T, size::NTuple{2, Integer}) where T = CircularMatrix{T}(fill(def, size))

Base.empty(::CircularVector{T}, ::Type{U}=T) where {T,U} = CircularVector{U}(U[])

function Base.deleteat!(a::CircularVector, i::Integer)
Expand Down
12 changes: 8 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ end

@testset "matrix" begin
b_arr = [2 4 6 8; 10 12 14 16; 18 20 22 24]
a1 = CircularArray(b_arr)
a1 = CircularMatrix(b_arr)
@test size(a1) == (3, 4)
@test parent(a1) == b_arr

Expand Down Expand Up @@ -179,21 +179,25 @@ end

@test !isa(a1, CircularVector)
@test !isa(a1, AbstractVector)
@test isa(a1, AbstractMatrix)
@test isa(a1, AbstractArray)

@test size(reshape(a1, (2, 2, 3))) == (2, 2, 3)

a2 = CircularArray(4, (2, 3))
a2 = CircularMatrix(4, (2, 3))
@test isa(a2, CircularMatrix{Int})
@test isa(a2, CircularArray{Int, 2})

a3 = @inferred(a2 .+ 1)
@test a3 isa CircularMatrix{Int64}
@test a3 isa CircularArray{Int64, 2}
@test a3 == CircularArray(5, (2, 3))

@testset "doubly circular" begin
a = CircularArray(b_arr)
da = CircularArray(a)
a = CircularMatrix(b_arr)
da = CircularMatrix(a)

@test da isa CircularMatrix
@test all(a[i, j] == da[i, j] for i in -8:8, j in -8:8)
@test all(a[i] == da[i] for i in -50:50)
end
Expand Down