Skip to content

Commit

Permalink
Optimization.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessymilare committed May 28, 2021
1 parent d2935ec commit 0d0013c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FlagSets"
uuid = "c7ad171a-416d-44a6-be7b-07ee749f090e"
authors = ["Jéssica Milaré <[email protected]>"]
version = "0.2.0"
version = "0.2.1"

[deps]

Expand Down
32 changes: 18 additions & 14 deletions src/FlagSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ Base.union(x::T, y::T) where {T<:FlagSet} = T(basetype(T)(x) | basetype(T)(y))
Base.intersect(x::T, y::T) where {T<:FlagSet} = T(basetype(T)(x) & basetype(T)(y))
Base.setdiff(x::T, y::T) where {T<:FlagSet} = T(basetype(T)(x) & ~basetype(T)(y))
Base.issubset(x::FlagSet{T}, y::FlagSet{T}) where {T<:Integer} = (T(x) & T(y)) == T(x)
Base.in(elt::Symbol, x::T) where {T<:FlagSet} =
!iszero(get(namemap(T), elt, 0) & basetype(T)(x))
Base.in(elt::Symbol, x::T) where {T<:FlagSet} = !iszero(getflag(T, elt, 0) & basetype(T)(x))
Base.:(x::FlagSet{T}, y::FlagSet{T}) where {T<:Integer} = x != y && (T(x) & T(y)) == T(x)

Base.empty(s::T, ::Type{Symbol} = Symbol) where {T<:FlagSet} = T()
Expand Down Expand Up @@ -148,6 +147,16 @@ end
@noinline flagset_argument_error(typename, x) =
throw(ArgumentError(string("invalid value for FlagSet $(typename): $x")))

Base.@pure function getflag(::Type{T}, sym::Symbol, default::Integer) where {T<:FlagSet}
get(namemap(T), sym, basetype(T)(default))
end

Base.@pure function getflag(::Type{T}, sym::Symbol) where {T<:FlagSet}
get(namemap(T), sym) do
flagset_argument_error(T, sym)
end
end

"""
@flagset FlagSetName[::BaseType] flag1[=x] flag2[=y]
Expand Down Expand Up @@ -285,25 +294,20 @@ macro flagset(T::Union{Symbol,Expr}, syms...)
x & $(mask) == x || flagset_argument_error($(Expr(:quote, typename)), x)
return bitcast($(esc(typename)), convert($(basetype), x))
end
function $(esc(typename))(sym::Symbol)
$(esc(typename))(getflag($(esc(typename)), sym))
end
function $(esc(typename))(sym::Symbol, syms::Symbol...)
xi::$(basetype) = get(FlagSets.namemap($(esc(typename))), sym) do
flagset_argument_error($(Expr(:quote, typename)), sym)
end
for sym syms
xi |= get(FlagSets.namemap($(esc(typename))), sym) do
flagset_argument_error($(Expr(:quote, typename)), sym)
end
end
$(esc(typename))(xi)
x = $(esc(typename))(sym)
isempty(syms) ? x : x | $(esc(typename))(syms...)
end
function $(esc(typename))(; $([:($sym::Bool) for sym in fnames_filtered]...))
function $(esc(typename))(; $([Expr(:kw, :($sym::Bool), false) for sym in fnames_filtered]...))
xi::$(basetype) = 0
$([:($sym && (xi |= $value)) for (sym, value) nm]...)
$(esc(typename))(xi)
end
$(esc(typename))() = $(esc(typename))($(zero(basetype)))
function $(esc(typename))(itr)
Base.isiterable(itr) || flagset_argument_error($(Expr(:quote, typename)), sym)
Base.isiterable(itr) || flagset_argument_error($(Expr(:quote, typename)), itr)
$(esc(typename))(itr...)
end

Expand Down

2 comments on commit 0d0013c

@jessymilare
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/37740

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.1 -m "<description of version>" 0d0013cf8d652f8b606824d19e702eead3b28d91
git push origin v0.2.1

Please sign in to comment.