Created
January 28, 2015 18:51
-
-
Save airspeedswift/f4daff4ea5c6de9e1fdf to your computer and use it in GitHub Desktop.
Nibble Sort
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 NibbleCollection: MutableCollectionType { | |
| var val: UInt64 | |
| init(_ val: UInt64) { self.val = val } | |
| typealias Index = UInt64 | |
| let startIndex: UInt64 = 0 | |
| let endIndex: UInt64 = 16 | |
| subscript(idx: UInt64) -> UInt64 { | |
| get { | |
| return (val >> (idx*4)) & 0xf | |
| } | |
| set(n) { | |
| let mask = 0xf << (idx * 4) | |
| val &= ~mask | |
| val |= n << (idx * 4) | |
| } | |
| } | |
| typealias Generator = IndexingGenerator<NibbleCollection> | |
| func generate() -> Generator { return Generator(self) } | |
| } | |
| var c = NibbleCollection(0xbadbeef) | |
| sort(&c) | |
| println(String(c.val, radix: 16)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Airspeedswift, we would like to use your source code to the swift benchmark suite in the swift tree. Will you be willing to release your awesome programs under the Swift open source license?