Last active
February 19, 2019 23:10
-
-
Save erica/dd1f6616b4124c588cf7 to your computer and use it in GitHub Desktop.
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
import Cocoa | |
func enumCount<EnumType:Hashable>(eType : EnumType.Type) -> Int { | |
if sizeof(eType) == 0 {return 1} | |
if sizeof(eType) == 1 { | |
for hashIndex in 2..<(2 << 8) { | |
if unsafeBitCast(UInt8(hashIndex), eType).hashValue == 0 { | |
return hashIndex} | |
} | |
return 2 << 8 | |
} | |
if sizeof(eType) > 2 {return -1} | |
for hashIndex in ((2 << 8) + 1)..<(2 << 16) { | |
if unsafeBitCast(UInt8(hashIndex), eType).hashValue == 0 { | |
return hashIndex} | |
} | |
return 2 << 16 | |
} | |
func members<EnumType:Hashable>(eType:EnumType.Type) -> [EnumType] { | |
var m = [EnumType]() | |
for i in 0..<enumCount(EnumType) { | |
let j = unsafeBitCast(UInt8(i), EnumType.self) | |
m.append(j) | |
} | |
return m | |
} | |
enum Planets : Int {case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Neptune, Uranus, Pluto} | |
enum Foo : Int {case i = 1, j = 5, k = 9} | |
enum Coin {case Heads, Tails} | |
enum Quark: String {case Up, Down, Top, Bottom, Strange, Charmed} | |
print(members(Planets)) | |
print(members(Foo)) | |
print(members(Coin)) | |
print(members(Quark)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment