Skip to content

Commit

Permalink
Initialize GamsMask
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchphillipson committed Nov 30, 2023
1 parent 65969ca commit f188fef
Showing 1 changed file with 98 additions and 0 deletions.
98 changes: 98 additions & 0 deletions src/masking.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
struct GamsMask{N}
universe::GamsUniverse
domain::NTuple{N,Symbol}#Vector{Symbol}
data::Dict{NTuple{N,Symbol},Bool}
description::String
GamsMask(GU,domain::Vararg{Symbol,N};description = "") where {N} = new{N}(GU,domain,Dict{NTuple{N,Symbol},Bool}(),description)
end

function domain(P::GamsMask)
return P.domain
end

function dimension(M::GamsMask)
return length(domain(M))
end

function Base.length(M::GamsMask)
return prod(length(P.universe[i]) for iP.domain)
end


function Base.size(P::GamsMask)
return Tuple(length(P.universe[i]) for iP.domain)
end

function Base.axes(P::GamsMask)
return Tuple([i for iP.universe[d]] for ddomain(P))
end

function _convert_idx(x::Symbol,GU::GamsUniverse,d::Symbol)
@assert (x==d || xGU[d]) "Symbol $x is neither a set nor an element of the set $d."
if x == d
return [i for iGU[d]]
elseif xGU[d]
return [x]
end
end

function _convert_idx(x::Vector,GU::GamsUniverse,d::Symbol)
@assert (all(iGU[d] for ix)) "At least one element of $x is not in set $d"
return x
end

function _convert_idx(x::GamsSet,GU::GamsUniverse,d::Symbol)
@assert x==GU[d] "The set\n\n$x\ndoes not match the domain set $d"
return [i for ix]
end

function _convert_idx(x,GU::GamsUniverse,d::Symbol)
@assert x GU[d] "$x is out of bounds in set $d"
return [x]
end


function Base.getindex(P::GamsMask{N},idx::CartesianIndex{N}) where {N}

GU = P.universe
I = map((x,d) -> GU[d][x].name ,Tuple(idx),domain(P))
return P[I...]
end

function Base.getindex(P::GamsMask{N},idx::Vararg{Any,N}) where {N}
GU = P.universe
idx = map((x,d)->_convert_idx(x,GU,d),idx,domain(P))
X = collect(Iterators.product(idx...))

if length(X) == 1
return get(P.data,X[1],0)
else
return get.(Ref(P.data),X,0)
end
end



Base.setindex!(P::GamsMask{N}, value, I::Vararg{Any,N}) where {N} = (P.data[I] = value)





function Base.summary(io::IO,P::GamsMask)
d = domain(P)
print(io,"Description: $(P.description)\nDomain: $(d)\n\n")
return
end


function Base.show(io::IO, ::MIME"text/plain", P::GamsMask)
summary(io,P)
#println(io,":")
if length(P.data) > 0
print(io,P.data)
end
return
end

Base.show(io::IO, x::GamsMask) = show(convert(IOContext, io), x)

0 comments on commit f188fef

Please sign in to comment.