Skip to content

Instantly share code, notes, and snippets.

@wildthink
Created June 19, 2020 17:43
Show Gist options
  • Save wildthink/ffdac2e6bc995473b93fa81a86ca2591 to your computer and use it in GitHub Desktop.
Save wildthink/ffdac2e6bc995473b93fa81a86ca2591 to your computer and use it in GitHub Desktop.
BitMask OptionSets w/ Associated Structures
struct WeekdaySet: OptionSet {
struct Info {
var name: String
var index: Int
}
static var info:[Int:Info] = [:]
let rawValue: UInt8
init(rawValue: UInt8) {
self.rawValue = rawValue
}
init(_ name: String, _ ndx: UInt8) {
self.rawValue = (1 << ndx)
WeekdaySet.info[self.index] = Info(name: name, index: self.index)
}
var index: Int { rawValue.trailingZeroBitCount }
var info: Info { return Self.info[index]! }
static let sunday = WeekdaySet("sunday", 1)
static let monday = WeekdaySet("monday", 2)
static let tuesday = WeekdaySet("tuesday", 3)
static let wednesday = WeekdaySet("wednesday", 4)
static let thursday = WeekdaySet("thursday", 5)
static let friday = WeekdaySet("friday", 6)
static let saturday = WeekdaySet("saturday", 7)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment