Skip to content

Instantly share code, notes, and snippets.

@airspeedswift
Created January 28, 2015 18:51
Show Gist options
  • Select an option

  • Save airspeedswift/f4daff4ea5c6de9e1fdf to your computer and use it in GitHub Desktop.

Select an option

Save airspeedswift/f4daff4ea5c6de9e1fdf to your computer and use it in GitHub Desktop.
Nibble Sort
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))
@nadavrot
Copy link
Copy Markdown

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment