Created
August 18, 2021 10:28
-
-
Save geor-kasapidi/e6ae803b6ecde7aec072d8ccf712b7c3 to your computer and use it in GitHub Desktop.
Enum set
This file contains hidden or 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
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