-
Notifications
You must be signed in to change notification settings - Fork 15
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
Showing
4 changed files
with
88 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ authors = ["Stefan Krastanov <[email protected]>"] | |
version = "0.3.2" | ||
|
||
[deps] | ||
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa" | ||
ConcurrentSim = "6ed1e86c-fcaf-46a9-97e0-2b26a2cdb499" | ||
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae" | ||
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" | ||
|
@@ -21,6 +22,7 @@ QuantumSymbolics = "efa7fd63-0460-4890-beb7-be1bbdfbaeae" | |
Reexport = "189a3867-3050-52da-a836-e630ba90ab69" | ||
ResumableFunctions = "c5292f4c-5179-55e1-98c5-05642aab7184" | ||
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" | ||
SumTypes = "8e1ec7a9-0e02-4297-b0fe-6433085c89f2" | ||
|
||
[weakdeps] | ||
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" | ||
|
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
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,59 @@ | ||
function tag!(ref::RegRef, tag::Tag) | ||
push!(ref.reg.tags[ref.idx], tag) | ||
end | ||
|
||
"""Wildcard for use with the tag querying functionality. | ||
See also: [`query`](@ref), [`tag!`](@ref), [`tag_types`](@ref)""" | ||
struct Wildcard end | ||
|
||
"""A wilcard instance for use with the tag querying functionality. | ||
See also: [`query`](@ref), [`tag!`](@ref), [`tag_types`](@ref)""" | ||
const W = Wildcard() | ||
const ❓ | ||
|
||
function query(reg::Register, tag::Tag) | ||
i = findfirst(set -> tag ∈ set, reg.tags) | ||
isnothing(i) ? nothing : (reg.refs[i], tag) | ||
end | ||
|
||
_query_and() = true | ||
_query_and(a::Bool) = a | ||
_query_and(a::Bool, b::Bool) = a && b | ||
|
||
# Create a query function for each combination of tag arguments and/or wildcard arguments | ||
for (tagsymbol, tagvariant) in pairs(tag_types) | ||
sig = methods(tagvariant)[1].sig.parameters[2:end] | ||
args = (:a, :b, :c, :d)[1:length(sig)] | ||
argssig = [:($a::$t) for (a,t) in zip(args, sig)] | ||
|
||
eval(quote function tag!(ref::RegRef, $(argssig...)) | ||
tag!(ref, ($tagvariant)($(args...))) | ||
end end) | ||
|
||
eval(quote function query(reg::Register, $(argssig...)) | ||
query(reg, ($tagvariant)($(args...))) | ||
end end) | ||
|
||
int_idx_all = [i for (i,s) in enumerate(sig) if s == Int] | ||
int_idx_combs = powerset(int_idx_all, 1) | ||
for idx in int_idx_combs | ||
complement_idx = tuple(setdiff(int_idx_all, idx)...) | ||
sig_wild = collect(sig) | ||
sig_wild[idx] .= Wildcard | ||
argssig_wild = [:($a::$t) for (a,t) in zip(args, sig_wild)] | ||
nonwild_checks = [:(tag.data[i]==$(args[i])) for i in complement_idx] | ||
eval(quote function query(reg::Register, $(argssig_wild...)) | ||
for (reg_idx, tags) in enumerate(reg.tags) | ||
for tag in tags | ||
if isvariant(tag, $(tagsymbol)) | ||
if _query_all($(nonwild_checks...)) end | ||
return (reg_idx, tag) | ||
end | ||
end | ||
end | ||
end end) | ||
end | ||
|
||
end |
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,18 @@ | ||
export tags | ||
|
||
@sum_type Tag :hidden begin | ||
Symbol(::Symbol) | ||
SymbolInt(::Symbol, ::Int) | ||
SymbolIntInt(::Symbol, ::Int, ::Int) | ||
Type(::DataType) | ||
TypeInt(::DataType, ::Int) | ||
TypeIntInt(::DataType, ::Int, ::Int) | ||
TypeSymbol(::DataType, ::Symbol) | ||
TypeSymbolInt(::DataType, ::Symbol, ::Int) | ||
TypeSymbolIntInt(::DataType, ::Symbol, ::Int, ::Int) | ||
end | ||
|
||
"""Tag types available in the taggging and tag-querying system. | ||
See also: [`query`](@ref), [`tag!`](@ref), [`Wildcard`](@ref)""" | ||
const tag_types = Tag' |