Skip to content

Instantly share code, notes, and snippets.

View airspeedswift's full-sized avatar

Ben Cohen airspeedswift

View GitHub Profile
@airspeedswift
airspeedswift / rbtree.swift
Created April 15, 2024 05:01
Red black tree in Swift 5.10
indirect enum Tree<Element: Comparable> {
enum Color { case R, B }
case empty
case node(Color, Tree<Element>, Element, Tree<Element>)
init() { self = .empty }
init(
@airspeedswift
airspeedswift / list.swift
Created March 23, 2024 18:48
A Basic Noncopyable Singly Linked List in Swift
// To run this code on a Mac with Xcode installed:
// Download the latest toolchain from https://www.swift.org/download/#trunk-development-main and install it
// From a command line:
// export TOOLCHAINS=`plutil -extract CFBundleIdentifier raw -o - /Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist`
// xcrun swiftc -parse-as-library -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature MoveOnlyPartialConsumption -Xfrontend -disable-round-trip-debug-types -enable-experimental-feature BorrowingSwitch linkedlist.swift
struct Box<Wrapped: ~Copyable>: ~Copyable {
private let pointer: UnsafeMutablePointer<Wrapped>
////===--- EitherSequence.swift - A sequence type-erasing two sequences -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
@airspeedswift
airspeedswift / OneSidedRanges.swift
Last active April 21, 2023 17:14
One-sided Range operators
postfix operator ..< { }
prefix operator ..< { }
struct RangeStart<I: ForwardIndexType> { let start: I }
struct RangeEnd<I: ForwardIndexType> { let end: I }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I>
{ return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I>
@airspeedswift
airspeedswift / MyArray.swift
Last active October 8, 2022 19:52
Array using ManagedBuffer
private class MyArrayBuffer<Element>: ManagedBuffer<Int,Element> {
func clone() -> MyArrayBuffer<Element> {
return self.withUnsafeMutablePointerToElements { elements -> MyArrayBuffer<Element> in
return MyArrayBuffer<Element>.create(self.allocatedElementCount) { newBuf in
newBuf.withUnsafeMutablePointerToElements { newElems->Void in
newElems.initializeFrom(elements, count: self.value)
}
return self.value
@airspeedswift
airspeedswift / Swift57RedBlackTree.swift
Created June 28, 2022 19:51
Swift red/black persistent tree
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,
////===--- EitherCollection.swift - A collection of two different types -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
@airspeedswift
airspeedswift / RemoveDuplicates.swift
Last active January 22, 2021 22:34
Removing duplicates
// removes all but first occurrence from a sequence, returning an array.
// requires elements to be hashable, not just equatable, but the alternative
// of using contains is very inefficient
// alternatively, could require comparable, sort, and remove adjacent dupes
func uniq<S: SequenceType, E: Hashable where E==S.Generator.Element>(seq: S) -> [E] {
var seen: [S.Generator.Element:Int] = [:]
return filter(seq) { !seen.updateValue(1, forKey: $0).hasValue }
}
// TODO: a version that takes a custom comparator function, say for lexicographic deduping
@airspeedswift
airspeedswift / Bounded.swift
Created October 26, 2018 04:33
Bound a sequence with a start and end marker
struct BoundedSequence<Base: Sequence> {
let _base: Base
}
extension BoundedSequence {
struct Iterator {
enum State { case starting, iterating, ended }
var _state: State
var _iterator: Base.Iterator
}
@airspeedswift
airspeedswift / rbt.swift
Created July 22, 2015 12:40
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)