Skip to content

Instantly share code, notes, and snippets.

@geor-kasapidi
Created August 18, 2021 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geor-kasapidi/e6ae803b6ecde7aec072d8ccf712b7c3 to your computer and use it in GitHub Desktop.
Save geor-kasapidi/e6ae803b6ecde7aec072d8ccf712b7c3 to your computer and use it in GitHub Desktop.
Enum set
struct EnumSet<T: RawRepresentable & CaseIterable>: OptionSet where T.RawValue == Int {
let rawValue: T.RawValue
init(rawValue: T.RawValue) {
self.rawValue = rawValue
}
init(_ value: T) {
self.rawValue = 1 << value.rawValue
}
var values: [T] {
T.allCases.filter(self.contains)
}
func contains(_ member: T) -> Bool {
self.contains(.init(member))
}
@discardableResult
mutating func insert(_ newMember: T) -> Bool {
self.insert(.init(newMember)).inserted
}
@discardableResult
mutating func remove(_ member: T) -> Bool {
self.remove(.init(member)) != nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment