Skip to content

Instantly share code, notes, and snippets.

@hsavit1
hsavit1 / rbt.swift
Created October 31, 2015 18:46 — forked from airspeedswift/rbt.swift
Red Black Tree with indirect and pattern matching for Swift 2.0b4
enum Color { case R, B }
indirect enum Tree<Element: Comparable> {
case Empty
case Node(Color,Tree<Element>,Element,Tree<Element>)
init() { self = .Empty }
init(_ x: Element, color: Color = .B,
left: Tree<Element> = .Empty, right: Tree<Element> = .Empty)
@hsavit1
hsavit1 / list.swift
Created October 31, 2015 18:46 — forked from airspeedswift/list.swift
Swift Singly Linked List
private enum ListNode<Element> {
case End
indirect case Node(Element, tag: Int, next: ListNode<Element>)
/// Computed property to fetch the tag. .End has an
/// implicit tag of zero.
var tag: Int {
switch self {
case .End: return 0
case let .Node(_, tag: n, _):
@hsavit1
hsavit1 / mergesort.swift
Created October 31, 2015 18:46 — forked from airspeedswift/mergesort.swift
Stable Swift merge sort
extension Array where Element: Comparable
{
mutating func mergesortInPlace() {
var tmp: [Generator.Element] = []
tmp.reserveCapacity(numericCast(self.count))
func merge(lo: Int, _ mi: Int, _ hi: Int) {
tmp.removeAll(keepCapacity: true)
tmp.extend(self[lo..<hi])
@hsavit1
hsavit1 / RBTree.swift
Last active November 1, 2015 19:21 — forked from airspeedswift/RBTree.swift
red black tree updated for 2.1b2
private enum ListNode<Element> {
case End
indirect case Node(Element, next: ListNode<Element>)
func cons(x: Element) -> ListNode<Element> {
return .Node(x, next: self)
}
}
public struct ListIndex<Element> {
@hsavit1
hsavit1 / protocolextension.swift
Created October 31, 2015 18:47 — forked from airspeedswift/protocolextension.swift
Protocol vs generic type extensions - overload resolution
// works for any index type
extension CollectionType {
var mid: Index {
return advance(startIndex,
distance(startIndex,endIndex) / 2
)
}
}
// and specialize for random-access index types
@hsavit1
hsavit1 / SubSliceView.swift
Created October 31, 2015 18:47 — forked from airspeedswift/SubSliceView.swift
Anything can be Sliceable
public struct SubSliceView<C: CollectionType>: Sliceable {
let collection: C
let bounds: Range<C.Index>
public var startIndex: C.Index { return bounds.startIndex }
public var endIndex: C.Index { return bounds.endIndex }
public subscript(idx: C.Index) -> C.Generator.Element
@hsavit1
hsavit1 / mirror.swift
Created October 31, 2015 18:53 — forked from airspeedswift/mirror.swift
Extracting properties using reflect()
struct Properties {
// someday soon these can be private
var _name: String, _nameModificationDate: Int
var _shape: String, _shapeModificationDate: Int
init(name: String, nameModificationDate: Int,
shape: String, shapeModificationDate: Int) {
self._name = name
self._nameModificationDate = nameModificationDate
self._shape = shape
@hsavit1
hsavit1 / ProtocolsVsGenerics.swift
Created October 31, 2015 18:54 — forked from airspeedswift/ProtocolsVsGenerics.swift
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))
Removed NSFileProviderExtension
Removed UIAccelerometerDelegate
Removed UIActionSheet
Removed UIActionSheetDelegate
Removed UIActivity
Removed UIActivityItemProvider
Removed UIActivityViewController
Removed UIAlertView
Removed UIAlertViewDelegate
Removed UIApplicationShortcutIcon
public enum Error: ErrorType, CustomDebugStringConvertible {
// Add error enumerations here
case FirstError(source: String, reason: String)
public var debugDescription: String {
let mirror = Mirror(reflecting: self)
guard let item = mirror.children.first else {fatalError("Missing error enumeration item")}
guard let myEnumeration = item.label else {fatalError("Missing error enumeration label")}