Skip to content

Commit

Permalink
Add AbstractIndex (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat authored Oct 3, 2023
1 parent b40c1be commit 3e5e934
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Manifest.toml
*.jl.cov
*.jl.*.cov
*.jl.mem
.vscode

generated
.vscode
45 changes: 25 additions & 20 deletions src/indices.jl
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# Index of representation element of type ElemT
# ParmaPolyhedra has its own index type so we have an `AbstractIndex`
# so that it can use default implementations defined here
abstract type AbstractIndex{T,ElemT} end

"""
Index{T, ElemT}
Index{T,ElemT} <: AbstractIndex{T,ElemT}
Index of an element of type `ElemT` in a `Rep{T}`.
"""
struct Index{T, ElemT}
struct Index{T,ElemT} <: AbstractIndex{T,ElemT}
value::Int
end

# Type of the value associated to this index
valuetype(::Union{Index{T, ElemT}, Type{Index{T, ElemT}}}) where {T, ElemT} = ElemT
valuetype(::Union{AbstractIndex{T,ElemT},Type{<:AbstractIndex{T,ElemT}}}) where {T,ElemT} = ElemT

const HyperPlaneIndex{T} = Index{T, <:HyperPlane{T}}
const HalfSpaceIndex{T} = Index{T, <:HalfSpace{T}}
const HIndex{T} = Union{HyperPlaneIndex{T}, HalfSpaceIndex{T}}
const HyperPlaneIndex{T} = AbstractIndex{T,<:HyperPlane{T}}
const HalfSpaceIndex{T} = AbstractIndex{T,<:HalfSpace{T}}
const HIndex{T} = Union{HyperPlaneIndex{T},HalfSpaceIndex{T}}

#const SymPointIndex{T} = Index{T, <:SymPoint{T}}
const PointIndex{T} = Index{T, <:AbstractVector{T}}
const PointIndex{T} = AbstractIndex{T,<:AbstractVector{T}}
#const PIndex{T} = Union{SymPointIndex{T}, PointIndex{T}}
const PIndex{T} = PointIndex{T}
const LineIndex{T} = Index{T, <:Line{T}}
const RayIndex{T} = Index{T, <:Ray{T}}
const RIndex{T} = Union{LineIndex{T}, RayIndex{T}}
const VIndex{T} = Union{PIndex{T}, RIndex{T}}
islin(::Union{Index{T, ElemT}, Type{Index{T, ElemT}}}) where {T, ElemT} = islin(ElemT)
ispoint(::Union{Index{T, ElemT}, Type{Index{T, ElemT}}}) where {T, ElemT} = ispoint(ElemT)
const LineIndex{T} = AbstractIndex{T,<:Line{T}}
const RayIndex{T} = AbstractIndex{T,<:Ray{T}}
const RIndex{T} = Union{LineIndex{T},RayIndex{T}}
const VIndex{T} = Union{PIndex{T},RIndex{T}}
islin(::Union{AbstractIndex{T, ElemT},Type{<:AbstractIndex{T,ElemT}}}) where {T,ElemT} = islin(ElemT)
ispoint(::Union{AbstractIndex{T, ElemT},Type{<:AbstractIndex{T,ElemT}}}) where {T,ElemT} = ispoint(ElemT)

"""
Indices{T, ElemT, RepT<:Rep{T}}
Expand Down Expand Up @@ -54,15 +57,17 @@ const RayIndices{T, RepT} = Indices{T, <:Ray{T}, RepT}
const RIndices{T, RepT} = Union{LineIndices{T, RepT}, RayIndices{T, RepT}}
const VIndices{T, RepT} = Union{PIndices{T, RepT}, RIndices{T, RepT}}

undouble_it(idx::Nothing) = nothing
double_it(idx::Nothing) = nothing
undouble_it(idx::NTuple{2, Index}) = idx[1]
double_it(idx::Index) = idx, idx
undouble_it(::Nothing) = nothing
double_it(::Nothing) = nothing
undouble_it(idx::NTuple{2,AbstractIndex}) = idx[1]
double_it(idx::AbstractIndex) = idx, idx
function Base.iterate(idxs::Indices)
return double_it(startindex(idxs))
end
function Base.iterate(idxs::Indices{T, ElemT},
idx::Index{T, ElemT}) where {T, ElemT}
function Base.iterate(
idxs::Indices{T,ElemT},
idx::AbstractIndex{T,ElemT},
) where {T, ElemT}
return double_it(nextindex(idxs.rep, idx))
end

Expand Down
6 changes: 3 additions & 3 deletions src/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ function Base.eachindex(it::AbstractSingleRepIterator{<:Any, ElemT,
RepT}) where {T, ElemT, RepT<:Rep{T}}
return Indices{T, similar_type(ElemT, FullDim(RepT), T)}(it.p)
end
element_and_index(it, idx::Nothing) = nothing
function element_and_index(it::AbstractSingleRepIterator, idx::Index)
element_and_index(::AbstractSingleRepIterator, ::Nothing) = nothing
function element_and_index(it::AbstractSingleRepIterator, idx::AbstractIndex)
return mapitem(it, get(it.p, idx)), idx
end
function Base.iterate(it::AbstractSingleRepIterator)
idx = undouble_it(iterate(eachindex(it)))
return element_and_index(it, idx)
end
function Base.iterate(it::AbstractSingleRepIterator, idx::Index)
function Base.iterate(it::AbstractSingleRepIterator, idx::AbstractIndex)
idx = undouble_it(iterate(eachindex(it), idx))::Union{Nothing, typeof(idx)}
return element_and_index(it, idx)
end
Expand Down
2 changes: 1 addition & 1 deletion src/lphrep.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function LPHRep(model::MOI.ModelLike, T::Type = Float64)
_model = _MOIModel{T}()
bridged = MOI.Bridges.LazyBridgeOptimizer(_model)
# Only enable constraint bridges that don't create variables and don't add
# any variable bridge so that there is an identity mapping betwenen
# any variable bridge so that there is an identity mapping between
# variables of `model` and polyhedra dimensions.
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.GreaterToLessBridge{T})
MOI.Bridges.add_bridge(bridged, MOI.Bridges.Constraint.LessToGreaterBridge{T})
Expand Down

0 comments on commit 3e5e934

Please sign in to comment.