Skip to content

Commit

Permalink
reordanise
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed Mar 3, 2024
1 parent 13a1adf commit fd32370
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/Lookups/Lookups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ include("indexing.jl")
include("methods.jl")
include("utils.jl")
include("set.jl")
include("beginend.jl")
include("show.jl")

end
52 changes: 5 additions & 47 deletions src/Lookups/lookup_traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ isrev(::Type{<:ReverseOrdered}) = true
"""
Locus <: LookupTrait
Abstract supertype of types that indicate the position of index values
Abstract supertype of types that indicate the position of index values
where they represent [`Intervals`](@ref).
These allow for values array cells to align with the [`Start`](@ref),
Expand All @@ -95,16 +95,16 @@ Indicates a lookup value is for the center of its corresponding array cell.
struct Center <: Locus end

"""
Start <: Locus
Begin <: Locus
Start()
Begin()
Indicates a lookup value is for the start of its corresponding array cell,
in the direction of the lookup index order.
"""
struct Start <: Locus end
struct Begin <: Locus end

const Begin = Start
const Start = Begin

"""
End <: Locus
Expand Down Expand Up @@ -264,45 +264,3 @@ change the `Lookup` type without changing the index values.
struct AutoIndex <: AbstractVector{Int} end

Base.size(::AutoIndex) = (0,)

# Ranges
struct BeginEndRange{A<:Union{Int,Begin,End},B<:Union{Int,Begin,End}} <: AbstractUnitRange{Int}
start::A
stop::B
end
struct BeginEndStepRange{A<:Union{Int,Begin,End},B<:Union{Int,Begin,End}} <: AbstractUnitRange{Int}
start::A
step::Int
stop::B
end
Colon(a::Int, b::Union{Begin,End}) = BeginEndRange(a, b)
Colon(a::Union{Begin,End}, b::Int) = BeginEndRange(a, b)
Colon(a::Union{Begin,End}, b::Union{Begin,End}) = BeginEndRange(a, b)
Colon(a::Union{Int,Begin,End}, b::Union{Type{Begin},Type{End}}) = BeginEndRange(a, b())
Colon(a::Union{Type{Begin},Type{End}}, b::Union{Int,Begin,End}) = BeginEndRange(a(), b)
Colon(a::Union{Type{Begin},Type{End}}, b::Union{Type{Begin},Type{End}}) = BeginEndRange(a(), b())

Colon(a::Int, step::Int, b::Union{Begin,End}) = BeginEndStepRange(a, step, b)
Colon(a::Union{Begin,End}, step::Int, b::Int) = BeginEndStepRange(a, step, b)
Colon(a::Union{Begin,End}, step::Int, b::Union{Begin,End}) = BeginEndStepRange(a, step, b)
Colon(a::Union{Int,Begin,End}, step::Int, b::Union{Type{Begin},Type{End}}) = BeginEndStepRange(a, step, b())
Colon(a::Union{Type{Begin},Type{End}}, step::Int, b::Union{Int,Begin,End}) = BeginEndStepRange(a(), step, b)
Colon(a::Union{Type{Begin},Type{End}}, step::Int, b::Union{Type{Begin},Type{End}}) = BeginEndStepRange(a(), step, b())

Base.to_indices(r::BeginEndRange, inds) = _to_indices(r.a, inds):_to_indices(r.b, inds)
Base.to_indices(r::BeginEndStepRange, inds) = _to_indices(r.a, inds)::r.step:_to_indices(r.b, inds)

_to_indices(a::Int, inds) = a
_to_indices(::Begin, inds) = first(inds)
_to_indices(::End, inds) = last(inds)

struct Lazy{T,F}
f::F
end

for f in (:+, :-, :*, :÷, :|, :&, :mod, :rem)
@eval Base.$f(::Type{T}, i::Int) where T <: Union{Begin,End} = Lazy{T}(Fix1($f, i))
@eval Base.$f(i::Int, ::Type{T}) where T <: Union{Begin,End} = Lazy{T}(Fix2($f, i))
@eval Base.$f(x::Lazy{T}, i::Int) where T = Lazy{T}(Fix1(x.f $f, i))
@eval Base.$f(i::Int, ::Type{T}) where T <: Union{Begin,End} = Lazy{T}(Fix2($f, i))
end
1 change: 1 addition & 0 deletions src/Lookups/selector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function selectindices(l::Lookup, sel::Not; kw...)
indices = selectindices(l, sel.skip; kw...)
return first(to_indices(l, (Not(indices),)))
end
selectindices(l::Lookup, i; kw...) = first(Base.to_indices(l, (i,)))

"""
At <: IntSelector
Expand Down
16 changes: 16 additions & 0 deletions src/array/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ for f in (:getindex, :view, :dotview)
@propagate_inbounds Base.$f(A::AbstractDimArray, I::CartesianIndices) =
Base.$f(A, to_indices(A, (I,))...)
end
@propagate_inbounds function Base.$f(A::AbstractDimVector, i)
x = Base.$f(parent(A), i)
if $f != view || x isa AbstractArray
rebuildsliced(Base.$f, A, x, i)
else
x
end
end
@propagate_inbounds function Base.$f(A::AbstractDimArray, i1, i2, I...)
x = Base.$f(parent(A), i1, i2, I...)
if $f != view || x isa AbstractArray
rebuildsliced(Base.$f, A, x, to_indices(A, (i1, i2, I...)))
else
x
end
end
# Linear indexing forwards to the parent array as it will break the dimensions
@propagate_inbounds Base.$f(A::AbstractDimArray, i::Union{Colon,AbstractArray{<:Integer}}) =
Base.$f(parent(A), i)
Expand Down

0 comments on commit fd32370

Please sign in to comment.