-
Notifications
You must be signed in to change notification settings - Fork 12
Simplify indexing #10
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
Changes from 3 commits
ad0e7e8
5e1846a
8a049e7
1329a79
e31f590
a549b56
c3f15db
a3f3273
bcbdc81
f71bf96
a4d79af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,20 +27,30 @@ Alias for [`CircularArray{T,1,A}`](@ref). | |
| """ | ||
| const CircularVector{T} = CircularArray{T, 1} | ||
|
|
||
| @inline clamp_bounds(arr::CircularArray, I::Tuple{Vararg{Int}})::AbstractArray{Int, 1} = map(Base.splat(mod), zip(I, axes(arr.data))) | ||
|
|
||
| CircularArray(data::AbstractArray{T,N}) where {T,N} = CircularArray{T,N}(data) | ||
| CircularArray{T}(data::AbstractArray{T,N}) where {T,N} = CircularArray{T,N}(data) | ||
| CircularArray(def::T, size) where T = CircularArray(fill(def, size)) | ||
|
|
||
| @inline Base.getindex(arr::CircularArray, i::Int) = @inbounds getindex(arr.data, mod(i, Base.axes1(arr.data))) | ||
| @inline Base.setindex!(arr::CircularArray, v, i::Int) = @inbounds setindex!(arr.data, v, mod(i, Base.axes1(arr.data))) | ||
| @inline Base.getindex(arr::CircularArray, I::Vararg{Int}) = @inbounds getindex(arr.data, clamp_bounds(arr, I)...) | ||
| @inline Base.setindex!(arr::CircularArray, v, I::Vararg{Int}) = @inbounds setindex!(arr.data, v, clamp_bounds(arr, I)...) | ||
| @inline Base.getindex(arr::CircularArray, i::Integer) = | ||
| @inbounds getindex(arr.data, mod(i, eachindex(LinearIndices(arr.data)))) | ||
| @inline Base.getindex(arr::CircularArray{T,N}, I::Vararg{<:Integer,N}) where {T,N} = | ||
| @inbounds getindex(arr.data, mod.(I, axes(arr))...) | ||
|
|
||
| @inline Base.setindex!(arr::CircularArray, v, i::Integer) = | ||
| @inbounds setindex!(arr.data, v, mod(i, eachindex(LinearIndices(arr.data)))) | ||
| @inline Base.setindex!(arr::CircularArray{T,N}, v, I::Vararg{<:Integer,N}) where {T,N} = | ||
| @inbounds setindex!(arr.data, v, mod.(I, axes(arr))...) | ||
|
|
||
| @inline Base.size(arr::CircularArray) = size(arr.data) | ||
| @inline Base.axes(arr::CircularArray) = axes(arr.data) | ||
| Base.parent(arr::CircularArray) = arr.data | ||
|
|
||
| @inline Base.checkbounds(::CircularArray, _...) = nothing | ||
| @inline function Base.checkbounds(arr::CircularArray, I...) | ||
| J = Base.to_indices(arr, I) | ||
| length(J) == 1 && return nothing | ||
| length(J) >= ndims(arr) && return nothing | ||
| throw(BoundsError(arr, I)) | ||
| end | ||
|
|
||
| @inline _similar(arr::CircularArray, ::Type{T}, dims) where T = CircularArray(similar(arr.data,T,dims)) | ||
| @inline Base.similar(arr::CircularArray, ::Type{T}, dims::Tuple{Base.DimOrInd, Vararg{Base.DimOrInd}}) where T = _similar(arr,T,dims) | ||
|
|
@@ -50,6 +60,7 @@ CircularArray(def::T, size) where T = CircularArray(fill(def, size)) | |
| CircularVector(data::AbstractArray{T, 1}) where T = CircularVector{T}(data) | ||
| CircularVector(def::T, size::Int) where T = CircularVector{T}(fill(def, size)) | ||
|
|
||
| Base.IndexStyle(::Type{CircularArray{T,N,A}}) where {T,N,A} = IndexStyle(A) | ||
|
||
| Base.IndexStyle(::Type{<:CircularVector}) = IndexLinear() | ||
|
|
||
| end | ||
Uh oh!
There was an error while loading. Please reload this page.