Skip to content

Instantly share code, notes, and snippets.

View airspeedswift's full-sized avatar

Ben Cohen airspeedswift

View GitHub Profile
@airspeedswift
airspeedswift / UnsafeCollection.swift
Last active August 29, 2015 14:12
Minimal example of a collection using UnsafeMutablePointer
// Note this is a class not a struct, so it does NOT have value semantics
public class UnsafeCollection<T> {
private var _len: Int = 0
private var _buflen: Int = 0
private var _buf: UnsafeMutablePointer<T> = nil
public func removeAll(keepCapacity: Bool = false) {
_buf.destroy(_len)
_len = 0
if !keepCapacity {
@airspeedswift
airspeedswift / keybase.md
Created January 7, 2015 16:55
Keybase verification

Keybase proof

I hereby claim:

  • I am airspeedswift on github.
  • I am airspeedvelocity (https://keybase.io/airspeedvelocity) on keybase.
  • I have a public key whose fingerprint is BAC5 2735 2DC2 8822 2A8B EFC1 ECB5 1983 1587 0DBE

To claim this, I am signing this object:

@airspeedswift
airspeedswift / COWTree.swift
Last active March 5, 2017 13:20
Swift copy-on-write behaviour for a struct using HeapBuffer
// ideally we would define this inside the tree struct
private class _Node<T: Comparable> {
var _value: T
var _left: _Node<T>? = nil
var _right: _Node<T>? = nil
init(value: T) { _value = value }
}
@airspeedswift
airspeedswift / NibbleSort.swift
Created January 28, 2015 18:51
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 {
@airspeedswift
airspeedswift / 1.swift
Created February 5, 2015 22:04
3 Dynamic Type Crashes
import Foundation
protocol P {
func f(i: Int)
}
func g<T: P>(t: T) {
let f = { t.dynamicType.f($0) }
}
@airspeedswift
airspeedswift / match.swift
Last active February 3, 2018 23:23
Minimalist Regex Matcher in Swift
/// Brian Kernighan's article in Beautiful Code talks about the minimalist regex matcher written by
/// Rob Pike for their Practice of Programming book as "one of the best examples of recursion that I
/// have ever seen, and it shows the power of C pointers".
///
/// Swift strings don't use pointers, and the original code relied heavily on the last character of a
/// C string being `\0`, but you can reproduce many of the nice aspects of the original C code using a
/// combination of slicing and `dropFirst`, the `first` function that returns an optional you can then
/// compare to a non-optional character, and the Swift 1.2 `if...let...where`
///
/// In theory no string copying should be happening since the slices are just a subrange view on the
@airspeedswift
airspeedswift / ProtocolsVsGenerics.swift
Last active November 25, 2015 18:50
Timing the difference between protocols and protocol-constrained generics
import Darwin
import CoreFoundation.CFDate
protocol NumberGeneratorType {
mutating func generateNumber() -> Int
}
struct RandomGenerator: NumberGeneratorType {
func generateNumber() -> Int {
return Int(arc4random_uniform(10))
@airspeedswift
airspeedswift / AnyObjVs.swift
Last active August 29, 2015 14:17
AnyObject.property is slower than ActualClass.property
import Foundation
@objc class MyClass: NSObject {
var someProperty: Int
init(_ x: Int) { someProperty = x }
}
let classes: [AnyObject] = (0..<10_000).map { _ in MyClass(Int(arc4random())) }
func timeRun<T>(name: String, f: ()->T) -> String {
let start = CFAbsoluteTimeGetCurrent()
@airspeedswift
airspeedswift / prime.c
Last active May 9, 2016 15:40
Swift vs C Prime Number Benchmark
// code originally adapted from https://github.com/DuncanMC/SwiftPerformanceBenchmark
#include <stdlib.h>
#include <stdio.h>
#include <CoreFoundation/CFDate.h>
void updateTotal(int newTotal, CFAbsoluteTime startTime) {
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime totalTime = now - startTime;
@airspeedswift
airspeedswift / shuffle.swift
Created April 4, 2015 22:06
Fisher-Yates Shuffle in Swift 1.2
import Darwin
func shuffle<C: MutableCollectionType where C.Index.Distance == Int>(var list: C) -> C {
var n = count(list)
if n == 0 { return list }
let oneBeforeEnd = advance(list.startIndex, n.predecessor())
for i in list.startIndex..<oneBeforeEnd {
let ran = Int(arc4random_uniform(UInt32(n--)))
let j = advance(i,ran)
swap(&list[i], &list[j])