-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65969ca
commit f188fef
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 i∈P.domain) | ||
end | ||
|
||
|
||
function Base.size(P::GamsMask) | ||
return Tuple(length(P.universe[i]) for i∈P.domain) | ||
end | ||
|
||
function Base.axes(P::GamsMask) | ||
return Tuple([i for i∈P.universe[d]] for d∈domain(P)) | ||
end | ||
|
||
function _convert_idx(x::Symbol,GU::GamsUniverse,d::Symbol) | ||
@assert (x==d || x∈GU[d]) "Symbol $x is neither a set nor an element of the set $d." | ||
if x == d | ||
return [i for i∈GU[d]] | ||
elseif x∈GU[d] | ||
return [x] | ||
end | ||
end | ||
|
||
function _convert_idx(x::Vector,GU::GamsUniverse,d::Symbol) | ||
@assert (all(i∈GU[d] for i∈x)) "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 i∈x] | ||
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) |