This file contains 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
indirect enum Tree<Element: Comparable> { | |
enum Color { case R, B } | |
case empty | |
case node(Color, Tree<Element>, Element, Tree<Element>) | |
init() { self = .empty } | |
init( |
This file contains 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
// 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> | |
This file contains 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
////===--- 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 | |
//// |
This file contains 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
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> |
This file contains 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
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 |
This file contains 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
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, |
This file contains 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
////===--- 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 | |
//// |
This file contains 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
// 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 |
This file contains 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 BoundedSequence<Base: Sequence> { | |
let _base: Base | |
} | |
extension BoundedSequence { | |
struct Iterator { | |
enum State { case starting, iterating, ended } | |
var _state: State | |
var _iterator: Base.Iterator | |
} |
This file contains 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
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) |
NewerOlder