|
| 1 | +// |
| 2 | +// BitMaskOption.swift |
| 3 | +// SDL1 |
| 4 | +// |
| 5 | +// Created by Alsey Coleman Miller on 6/6/17. |
| 6 | +// |
| 7 | + |
| 8 | +/// Enum that represents a bit mask flag / option. |
| 9 | +/// |
| 10 | +/// Basically `Swift.OptionSet` for enums. |
| 11 | +public protocol BitMaskOption: RawRepresentable, Hashable, CaseIterable where RawValue: FixedWidthInteger { } |
| 12 | + |
| 13 | +public extension Sequence where Element: BitMaskOption { |
| 14 | + |
| 15 | + /// Convert Swift enums for bit mask options into their raw values OR'd. |
| 16 | + var rawValue: Element.RawValue { |
| 17 | + |
| 18 | + @inline(__always) |
| 19 | + get { return reduce(0, { $0 | $1.rawValue }) } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +public extension BitMaskOption { |
| 24 | + |
| 25 | + /// Whether the enum case is present in the raw value. |
| 26 | + @inline(__always) |
| 27 | + func isContained(in rawValue: RawValue) -> Bool { |
| 28 | + |
| 29 | + return (self.rawValue & rawValue) != 0 |
| 30 | + } |
| 31 | + |
| 32 | + @inline(__always) |
| 33 | + static func from(rawValue: RawValue) -> [Self] { |
| 34 | + |
| 35 | + return Self.allCases.filter { $0.isContained(in: rawValue) } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// MARK: - BitMaskOptionSet |
| 40 | + |
| 41 | +/// Integer-backed array type for `BitMaskOption`. |
| 42 | +/// |
| 43 | +/// The elements are packed in the integer with bitwise math and stored on the stack. |
| 44 | +public struct BitMaskOptionSet <Element: BitMaskOption>: RawRepresentable { |
| 45 | + |
| 46 | + public typealias RawValue = Element.RawValue |
| 47 | + |
| 48 | + public private(set) var rawValue: RawValue |
| 49 | + |
| 50 | + @inline(__always) |
| 51 | + public init(rawValue: RawValue) { |
| 52 | + |
| 53 | + self.rawValue = rawValue |
| 54 | + } |
| 55 | + |
| 56 | + @inline(__always) |
| 57 | + public init() { |
| 58 | + |
| 59 | + self.rawValue = 0 |
| 60 | + } |
| 61 | + |
| 62 | + public static var all: BitMaskOptionSet<Element> { |
| 63 | + |
| 64 | + return BitMaskOptionSet<Element>(rawValue: Element.allCases.rawValue) |
| 65 | + } |
| 66 | + |
| 67 | + @inline(__always) |
| 68 | + public mutating func insert(_ element: Element) { |
| 69 | + |
| 70 | + rawValue = rawValue | element.rawValue |
| 71 | + } |
| 72 | + |
| 73 | + @discardableResult |
| 74 | + public mutating func remove(_ element: Element) -> Bool { |
| 75 | + |
| 76 | + guard contains(element) else { return false } |
| 77 | + |
| 78 | + rawValue = rawValue & ~element.rawValue |
| 79 | + |
| 80 | + return true |
| 81 | + } |
| 82 | + |
| 83 | + @inline(__always) |
| 84 | + public mutating func removeAll() { |
| 85 | + |
| 86 | + self.rawValue = 0 |
| 87 | + } |
| 88 | + |
| 89 | + @inline(__always) |
| 90 | + public func contains(_ element: Element) -> Bool { |
| 91 | + |
| 92 | + return element.isContained(in: rawValue) |
| 93 | + } |
| 94 | + |
| 95 | + public func contains <S: Sequence> (_ other: S) -> Bool where S.Iterator.Element == Element { |
| 96 | + |
| 97 | + for element in other { |
| 98 | + |
| 99 | + guard element.isContained(in: rawValue) |
| 100 | + else { return false } |
| 101 | + } |
| 102 | + |
| 103 | + return true |
| 104 | + } |
| 105 | + |
| 106 | + public var count: Int { |
| 107 | + |
| 108 | + return Element.allCases.reduce(0, { $0 + ($1.isContained(in: rawValue) ? 1 : 0) }) |
| 109 | + } |
| 110 | + |
| 111 | + public var isEmpty: Bool { |
| 112 | + |
| 113 | + return rawValue == 0 |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +// MARK: - Sequence Conversion |
| 118 | + |
| 119 | +public extension BitMaskOptionSet { |
| 120 | + |
| 121 | + init<S: Sequence>(_ sequence: S) where S.Iterator.Element == Element { |
| 122 | + |
| 123 | + self.rawValue = sequence.rawValue |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +// MARK: - Equatable |
| 128 | + |
| 129 | +extension BitMaskOptionSet: Equatable { |
| 130 | + |
| 131 | + public static func == (lhs: BitMaskOptionSet, rhs: BitMaskOptionSet) -> Bool { |
| 132 | + |
| 133 | + return lhs.rawValue == rhs.rawValue |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// MARK: - CustomStringConvertible |
| 138 | + |
| 139 | +extension BitMaskOptionSet: CustomStringConvertible { |
| 140 | + |
| 141 | + public var description: String { |
| 142 | + |
| 143 | + return Element.from(rawValue: rawValue) |
| 144 | + .sorted(by: { $0.rawValue < $1.rawValue }) |
| 145 | + .description |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// MARK: - Hashable |
| 150 | + |
| 151 | +extension BitMaskOptionSet: Hashable { |
| 152 | + |
| 153 | + #if swift(>=4.2) |
| 154 | + public func hash(into hasher: inout Hasher) { |
| 155 | + |
| 156 | + rawValue.hash(into: &hasher) |
| 157 | + } |
| 158 | + #else |
| 159 | + public var hashValue: Int { |
| 160 | + |
| 161 | + return rawValue.hashValue |
| 162 | + } |
| 163 | + #endif |
| 164 | +} |
| 165 | + |
| 166 | +// MARK: - ExpressibleByArrayLiteral |
| 167 | + |
| 168 | +extension BitMaskOptionSet: ExpressibleByArrayLiteral { |
| 169 | + |
| 170 | + public init(arrayLiteral elements: Element...) { |
| 171 | + |
| 172 | + self.init(elements) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +// MARK: - ExpressibleByIntegerLiteral |
| 177 | + |
| 178 | +extension BitMaskOptionSet: ExpressibleByIntegerLiteral { |
| 179 | + |
| 180 | + public init(integerLiteral value: UInt64) { |
| 181 | + |
| 182 | + self.init(rawValue: numericCast(value)) |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +// MARK: - Sequence |
| 187 | + |
| 188 | +extension BitMaskOptionSet: Sequence { |
| 189 | + |
| 190 | + public func makeIterator() -> IndexingIterator<[Element]> { |
| 191 | + |
| 192 | + return Element.from(rawValue: rawValue).makeIterator() |
| 193 | + } |
| 194 | +} |
0 commit comments