Created
May 18, 2016 06:37
-
-
Save externvoid/258e3015aa6329d5a4c901ecda097b49 to your computer and use it in GitHub Desktop.
列挙enumの中身を表示する。rawValueではその意味が判らんので!ただし、このソースの意味がよく判らん。要解読。
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 Foundation | |
| func iterateEnum<T: Hashable>(_: T.Type) -> AnyGenerator<T> { | |
| var cast: (Int -> T)! // reference to clousure | |
| //print(($0).dynamicType) | |
| // what is $0 ?, Int, 0, 1, 2, 3, 4 for nil | |
| switch sizeof(T) { | |
| case 0: return anyGenerator(GeneratorOfOne(unsafeBitCast((), T.self))) | |
| case 1: cast = { unsafeBitCast(UInt8(truncatingBitPattern: $0), T.self) } | |
| // print($0.dynamicType);print($0) | |
| case 2: cast = { unsafeBitCast(UInt16(truncatingBitPattern: $0), T.self) } | |
| case 4: cast = { unsafeBitCast(UInt32(truncatingBitPattern: $0), T.self) } | |
| case 8: cast = { unsafeBitCast(UInt64($0), T.self) } | |
| default: fatalError("cannot be here") | |
| } | |
| print(UInt8(truncatingBitPattern: 1023)) // 255 | |
| print(UInt16(truncatingBitPattern: 1023))// 1023 | |
| //print(sizeof(T)) | |
| var i = 0 | |
| return anyGenerator { | |
| let next = cast(i) | |
| return next.hashValue == i++ ? next : nil | |
| } | |
| } | |
| enum Suit:String { | |
| case Spades = "♠♠" | |
| case Hearts = "♥" | |
| case Diamonds = "♦" | |
| case Clubs = "♣" | |
| } | |
| //print(iterateEnum(Suit)) | |
| for f in iterateEnum(Suit) { | |
| print(f.rawValue) | |
| } | |
| func iterateEnum2<T: Hashable>(_: T.Type) -> AnyGenerator<T> { | |
| var i = 0 | |
| return anyGenerator { | |
| let next = withUnsafePointer(&i) { UnsafePointer<T>($0).memory } | |
| // print(i) | |
| // print(next.dynamicType) | |
| return next.hashValue == i++ ? next : nil | |
| } | |
| } | |
| var i = 0 | |
| //var voidPtr: UnsafePointer<Suit> = unsafeBitCast(0, UnsafePointer<Suit>.self) | |
| //print(voidPtr.memory) | |
| print(withUnsafePointer(&i){(p: UnsafePointer<Int>) -> Suit in | |
| var voidPtr: UnsafePointer<Suit> = unsafeBitCast(p, UnsafePointer<Suit>.self) | |
| print(i) | |
| print(p.memory) | |
| print(voidPtr[0], voidPtr[5]) | |
| return voidPtr.memory}) | |
| i++ | |
| print(withUnsafePointer(&i){(p: UnsafePointer<Int>) -> Suit in | |
| var voidPtr: UnsafePointer<Suit> = unsafeBitCast(p, UnsafePointer<Suit>.self) | |
| print(i) | |
| print(p.memory) | |
| return voidPtr.memory}) | |
| print(withUnsafePointer(&i){UnsafePointer<Suit>($0).memory}) | |
| //print(UnsafePointer<Suit>(&i).memory) | |
| print("--------------") | |
| for f in iterateEnum2(Suit) { | |
| print(f.rawValue) | |
| // print(f.dynamicType) | |
| } | |
| // AnyGenerator<T>のインスタンスにはanyGeneratorに与えたclousureが含まれる | |
| // for..in loopはAnyGeneratorのインスタンスのnext()インスタンスメソッド | |
| // を呼び出す。 | |
| // iterateEnumはgenerate()->AnyGenerator<T>と同じジェネレータである。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment