Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active January 3, 2016 00:47
Show Gist options
  • Save KentarouKanno/4b90f266b5dcccaec4c1 to your computer and use it in GitHub Desktop.
Save KentarouKanno/4b90f266b5dcccaec4c1 to your computer and use it in GitHub Desktop.
Array Struct

Array Struct

★ Array

// --- Struct ---

public struct Array<Element> : CollectionType, MutableCollectionType, _DestructorSafeContainer {
    public var startIndex: Int { get }
    public var endIndex: Int { get }
    public subscript (index: Int) -> Element
    public subscript (subRange: Range<Int>) -> ArraySlice<Element>
}

// --- Extension ---

extension Array : ArrayLiteralConvertible {
    public init(arrayLiteral elements: Element...)
}

extension Array : _ArrayType {
    public init()
    public init<S : SequenceType where S.Generator.Element == _Buffer.Element>(_ s: S)
    public init(count: Int, repeatedValue: Element)
    
    public var count: Int { get }
    public var capacity: Int { get }
    
    public mutating func reserveCapacity(minimumCapacity: Int)
    public mutating func append(newElement: Element)
    public mutating func appendContentsOf<S : SequenceType where S.Generator.Element == Element>(newElements: S)
    public mutating func appendContentsOf<C : CollectionType where C.Generator.Element == Element>(newElements: C)
    public mutating func removeLast() -> Element
    public mutating func insert(newElement: Element, atIndex i: Int)
    public mutating func removeAtIndex(index: Int) -> Element
    public mutating func removeAll(keepCapacity keepCapacity: Bool = default)
}

extension Array : _Reflectable {
}

extension Array : CustomStringConvertible, CustomDebugStringConvertible {
    public var description: String { get }
    public var debugDescription: String { get }
}

extension Array {
    public func withUnsafeBufferPointer<R>(@noescape body: (UnsafeBufferPointer<Element>) throws -> R) rethrows -> R
    public mutating func withUnsafeMutableBufferPointer<R>(@noescape body: (inout UnsafeMutableBufferPointer<Element>) throws -> R) rethrows -> R
}

extension Array {
    public mutating func replaceRange<C : CollectionType where C.Generator.Element == _Buffer.Element>(subRange: Range<Int>, with newElements: C)
}

★ ArrayLiteralConvertible

// --- Protocol ---
public protocol ArrayLiteralConvertible {
    typealias Element
    
    public init(arrayLiteral elements: Self.Element...)
}

★ CollectionType

// --- Protocol ---

public protocol CollectionType : Indexable, SequenceType {

    typealias Generator : GeneratorType = IndexingGenerator<Self>
    public func generate() -> Self.Generator

    typealias SubSequence : Indexable, SequenceType = Slice<Self>
    public subscript (position: Self.Index) -> Self.Generator.Element { get }
    public subscript (bounds: Range<Self.Index>) -> Self.SubSequence { get }

    @warn_unused_result
    public func prefixUpTo(end: Self.Index) -> Self.SubSequence

    @warn_unused_result
    public func suffixFrom(start: Self.Index) -> Self.SubSequence

    @warn_unused_result
    public func prefixThrough(position: Self.Index) -> Self.SubSequence

    public var isEmpty: Bool { get }
    public var count: Self.Index.Distance { get }
    public var first: Self.Generator.Element? { get }
}

// --- Extension ---

extension CollectionType {
    public var isEmpty: Bool { get }
    public var first: Self.Generator.Element? { get }
    public func underestimateCount() -> Int
    public var count: Self.Index.Distance { get }
}

extension CollectionType {
    @warn_unused_result
    public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
    
    @warn_unused_result
    public func dropFirst(n: Int) -> Self.SubSequence
    
    @warn_unused_result
    public func dropLast(n: Int) -> Self.SubSequence
    
    @warn_unused_result
    public func prefix(maxLength: Int) -> Self.SubSequence
    
    @warn_unused_result
    public func suffix(maxLength: Int) -> Self.SubSequence
    
    @warn_unused_result
    public func prefixUpTo(end: Self.Index) -> Self.SubSequence
    
    @warn_unused_result
    public func suffixFrom(start: Self.Index) -> Self.SubSequence
    
    @warn_unused_result
    public func prefixThrough(position: Self.Index) -> Self.SubSequence
    
    @warn_unused_result
    public func split(maxSplit: Int = default, allowEmptySlices: Bool = default, @noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]
}

extension CollectionType {
    @warn_unused_result
    public func indexOf(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Index?
}

extension CollectionType {
    public var indices: Range<Self.Index> { get }
}

extension CollectionType {
    public var lazy: LazyCollection<Self> { get }
}


// --- extension where ---

extension CollectionType where Generator == IndexingGenerator<Self> {
    public func generate() -> IndexingGenerator<Self>
}

extension CollectionType where SubSequence == Slice<Self> {
    public subscript (bounds: Range<Self.Index>) -> Slice<Self> { get }
}

extension CollectionType where SubSequence == Self {
    
    @warn_unused_result
    public mutating func popFirst() -> Self.Generator.Element?
    
    @warn_unused_result
    public mutating func popLast() -> Self.Generator.Element?
}

extension CollectionType where Generator.Element : Equatable {
    @warn_unused_result
    public func split(separator: Self.Generator.Element, maxSplit: Int = default, allowEmptySlices: Bool = default) -> [Self.SubSequence]
}

extension CollectionType where Index : BidirectionalIndexType {
    @warn_unused_result
    public func suffix(maxLength: Int) -> Self.SubSequence
}

extension CollectionType where SubSequence == Self {
    public mutating func removeFirst() -> Self.Generator.Element
}

extension CollectionType where Index : BidirectionalIndexType {
    public var last: Self.Generator.Element? { get }
}

extension CollectionType where Generator.Element : Equatable {
    @warn_unused_result
    public func indexOf(element: Self.Generator.Element) -> Self.Index?
}

extension CollectionType where Generator.Element : CollectionType {
    @warn_unused_result
    public func flatten() -> FlattenCollection<Self>
}

extension CollectionType where Generator.Element : CollectionType, Index : BidirectionalIndexType, Generator.Element.Index : BidirectionalIndexType {
    @warn_unused_result
    public func flatten() -> FlattenBidirectionalCollection<Self>
}

extension CollectionType where Self : _ReverseCollectionType, Self.Base.Index : RandomAccessIndexType {
    public var startIndex: ReverseRandomAccessIndex<Self.Base.Index> { get }
}

extension CollectionType where Index : BidirectionalIndexType {
    @warn_unused_result
    public func reverse() -> ReverseCollection<Self>
}

extension CollectionType where Index : RandomAccessIndexType {
    @warn_unused_result
    public func reverse() -> ReverseRandomAccessCollection<Self>
}

extension CollectionType where Self : _CollectionWrapperType, Self.Index == Self.Base.Index {
    
    public var startIndex: Self.Index { get }
    
    public var endIndex: Self.Index { get }
    public subscript (position: Self.Index) -> Self.Base.Generator.Element { get }
    @warn_unused_result
    public func map<T>(@noescape transform: (Self.Base.Generator.Element) -> T) -> [T]
    @warn_unused_result
    public func filter(@noescape includeElement: (Self.Base.Generator.Element) -> Bool) -> [Self.Base.Generator.Element]
}

★ MutableCollectionType

// --- Protocol ---

public protocol MutableCollectionType : MutableIndexable, CollectionType {
    typealias SubSequence = MutableSlice<Self>
    public subscript (position: Self.Index) -> Self.Generator.Element { get set }
    public subscript (bounds: Range<Self.Index>) -> Self.SubSequence { get set }
}

// --- Extension ---

extension MutableCollectionType {
    public subscript (bounds: Range<Self.Index>) -> MutableSlice<Self>
}

// --- Extension where ---

extension MutableCollectionType where Index : RandomAccessIndexType {
    
    public mutating func partition(range: Range<Self.Index>, isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) -> Bool) -> Self.Index
}

extension MutableCollectionType where Index : RandomAccessIndexType, Generator.Element : Comparable {
    public mutating func partition(range: Range<Self.Index>) -> Self.Index
}

extension MutableCollectionType where Self.Generator.Element : Comparable {
    @warn_unused_result(mutable_variant="sortInPlace")
    public func sort() -> [Self.Generator.Element]
}

extension MutableCollectionType {
    @warn_unused_result(mutable_variant="sortInPlace")
    public func sort(@noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) -> Bool) -> [Self.Generator.Element]
}

extension MutableCollectionType where Self.Index : RandomAccessIndexType, Self.Generator.Element : Comparable {
    public mutating func sortInPlace()
}

extension MutableCollectionType where Self.Index : RandomAccessIndexType {
    public mutating func sortInPlace(@noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) -> Bool)
}

★ _DestructorSafeContainer

// --- Protocol ---

public protocol _DestructorSafeContainer {
}

★ SequenceType

// --- Protocol ---

public protocol SequenceType {

    typealias Generator : GeneratorType

    typealias SubSequence

    @warn_unused_result
    public func generate() -> Self.Generator

    @warn_unused_result
    public func underestimateCount() -> Int

    @warn_unused_result
    public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]

    @warn_unused_result
    public func filter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element]

    public func forEach(@noescape body: (Self.Generator.Element) throws -> ()) rethrows

    @warn_unused_result
    public func dropFirst(n: Int) -> Self.SubSequence

    @warn_unused_result
    public func dropLast(n: Int) -> Self.SubSequence

    @warn_unused_result
    public func prefix(maxLength: Int) -> Self.SubSequence

    @warn_unused_result
    public func suffix(maxLength: Int) -> Self.SubSequence

    @warn_unused_result
    public func split(maxSplit: Int, allowEmptySlices: Bool, @noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.SubSequence]
}

// --- Extension ---

extension SequenceType {
    @warn_unused_result
    public func sort(@noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) -> Bool) -> [Self.Generator.Element]
}

extension SequenceType {
    public var lazy: LazySequence<Self> { get }
}

extension SequenceType {
    @warn_unused_result
    public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
    
    @warn_unused_result
    public func filter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element]
    
    @warn_unused_result
    public func dropFirst(n: Int) -> AnySequence<Self.Generator.Element>
    
    @warn_unused_result
    public func dropLast(n: Int) -> AnySequence<Self.Generator.Element>
    @warn_unused_result
    public func prefix(maxLength: Int) -> AnySequence<Self.Generator.Element>
    @warn_unused_result
    public func suffix(maxLength: Int) -> AnySequence<Self.Generator.Element>
    
    @warn_unused_result
    public func split(maxSplit: Int = default, allowEmptySlices: Bool = default, @noescape isSeparator: (Self.Generator.Element) throws -> Bool) rethrows -> [AnySequence<Self.Generator.Element>]
    
    @warn_unused_result
    public func underestimateCount() -> Int
}

extension SequenceType {
    public func forEach(@noescape body: (Self.Generator.Element) throws -> ()) rethrows
}

extension SequenceType {
    
    @warn_unused_result
    public func dropFirst() -> Self.SubSequence
    
    @warn_unused_result
    public func dropLast() -> Self.SubSequence
}

extension SequenceType {
    @warn_unused_result
    public func enumerate() -> EnumerateSequence<Self>
}

extension SequenceType {
    @warn_unused_result
    public func minElement(@noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element?
    
    @warn_unused_result
    public func maxElement(@noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element?
}

extension SequenceType {
    @warn_unused_result
    public func startsWith<OtherSequence : SequenceType where OtherSequence.Generator.Element == Generator.Element>(other: OtherSequence, @noescape isEquivalent: (Self.Generator.Element, Self.Generator.Element) throws -> Bool) rethrows -> Bool
}

extension SequenceType {
    @warn_unused_result
    public func elementsEqual<OtherSequence : SequenceType where OtherSequence.Generator.Element == Generator.Element>(other: OtherSequence, @noescape isEquivalent: (Self.Generator.Element, Self.Generator.Element) throws -> Bool) rethrows -> Bool
}

extension SequenceType {
    @warn_unused_result
    public func contains(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Bool
}

extension SequenceType {
    @warn_unused_result
    public func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> T
}

extension SequenceType {
    @warn_unused_result
    public func reverse() -> [Self.Generator.Element]
}

extension SequenceType {
    @warn_unused_result
    public func flatMap<S : SequenceType>(transform: (Self.Generator.Element) throws -> S) rethrows -> [S.Generator.Element]
}

extension SequenceType {
    @warn_unused_result
    public func flatMap<T>(@noescape transform: (Self.Generator.Element) throws -> T?) rethrows -> [T]
}

// --- Extension where ---

extension SequenceType where Self.Generator.Element : Comparable {
    @warn_unused_result
    public func sort() -> [Self.Generator.Element]
}

extension SequenceType where Generator.Element : SequenceType {
    @warn_unused_result
    public func flatten() -> FlattenSequence<Self>
}

extension SequenceType where Generator.Element : SequenceType {
    @warn_unused_result
    public func joinWithSeparator<Separator : SequenceType where Separator.Generator.Element == Generator.Element.Generator.Element>(separator: Separator) -> JoinSequence<Self>
}

extension SequenceType where Self.Generator == Self, Self : GeneratorType {
    public func generate() -> Self
}

extension SequenceType where Generator.Element : Equatable {
    @warn_unused_result
    public func split(separator: Self.Generator.Element, maxSplit: Int = default, allowEmptySlices: Bool = default) -> [AnySequence<Self.Generator.Element>]
}

extension SequenceType where Self : _SequenceWrapperType, Self.Generator == Self.Base.Generator {
    
    public func generate() -> Self.Generator
    public func underestimateCount() -> Int
    @warn_unused_result
    public func map<T>(@noescape transform: (Self.Generator.Element) throws -> T) rethrows -> [T]
    @warn_unused_result
    public func filter(@noescape includeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element]
}

extension SequenceType where Generator.Element : Comparable {
    @warn_unused_result
    public func minElement() -> Self.Generator.Element?
    @warn_unused_result
    public func maxElement() -> Self.Generator.Element?
}

extension SequenceType where Generator.Element : Equatable {
    @warn_unused_result
    public func startsWith<OtherSequence : SequenceType where OtherSequence.Generator.Element == Generator.Element>(other: OtherSequence) -> Bool
}

extension SequenceType where Generator.Element : Equatable {
    @warn_unused_result
    public func elementsEqual<OtherSequence : SequenceType where OtherSequence.Generator.Element == Generator.Element>(other: OtherSequence) -> Bool
}

extension SequenceType where Generator.Element : Comparable {
    @warn_unused_result
    public func lexicographicalCompare<OtherSequence : SequenceType where OtherSequence.Generator.Element == Generator.Element>(other: OtherSequence) -> Bool
}

extension SequenceType where Generator.Element : Equatable {
    @warn_unused_result
    public func contains(element: Self.Generator.Element) -> Bool
}

extension SequenceType {
    @warn_unused_result
    public func lexicographicalCompare<OtherSequence : SequenceType where OtherSequence.Generator.Element == Generator.Element>(other: OtherSequence, @noescape isOrderedBefore: (Self.Generator.Element, Self.Generator.Element) throws -> Bool) rethrows -> Bool
}

extension SequenceType where Generator.Element == String {
    @warn_unused_result
    public func joinWithSeparator(separator: String) -> String
}

★ GeneratorType

// --- Protocol ---

public protocol GeneratorType {
    
    typealias Element
    
    @warn_unused_result
    public mutating func next() -> Self.Element?
}

★ Indexable

// --- Protocol ---

public protocol Indexable {

    typealias Index : ForwardIndexType

    public var startIndex: Self.Index { get }
    public var endIndex: Self.Index { get }
    public subscript (position: Self.Index) -> Self._Element { get }
}

★ ForwardIndexType

// --- Protocol ---

public protocol ForwardIndexType : _Incrementable {
    
    typealias Distance : _SignedIntegerType = Int
    
    @warn_unused_result
    public func advancedBy(n: Self.Distance) -> Self
    @warn_unused_result
    public func advancedBy(n: Self.Distance, limit: Self) -> Self
    @warn_unused_result
    public func distanceTo(end: Self) -> Self.Distance
}

// --- extension ---

extension ForwardIndexType {
    @warn_unused_result
    public func advancedBy(n: Self.Distance) -> Self
    @warn_unused_result
    public func advancedBy(n: Self.Distance, limit: Self) -> Self
    @warn_unused_result
    public func distanceTo(end: Self) -> Self.Distance
}

// --- Operator ---

@warn_unused_result
public func ...<Pos : ForwardIndexType where Pos : Comparable>(start: Pos, end: Pos) -> Range<Pos>

@warn_unused_result
public func ...<Pos : ForwardIndexType>(minimum: Pos, maximum: Pos) -> Range<Pos>

@warn_unused_result
public func ..<<Pos : ForwardIndexType where Pos : Comparable>(start: Pos, end: Pos) -> Range<Pos>

@warn_unused_result
public func ..<<Pos : ForwardIndexType>(minimum: Pos, maximum: Pos) -> Range<Pos>

@warn_unused_result
public func ~=<I : ForwardIndexType where I : Comparable>(pattern: Range<I>, value: I) -> Bool

★ _Incrementable

// --- Protocol ---

public protocol _Incrementable : Equatable {
    @warn_unused_result
    public func successor() -> Self
}

// --- Operator ---

prefix public func ++<T : _Incrementable>(inout i: T) -> T
postfix public func ++<T : _Incrementable>(inout i: T) -> T

★ Slice

// --- Struct ---

public struct Slice<Base : Indexable> : CollectionType {
    public typealias Index = Base.Index
    public let startIndex: Base.Index
    public let endIndex: Base.Index
    public subscript (index: Base.Index) -> Base._Element { get }
    public subscript (bounds: Range<Base.Index>) -> Slice<Base> { get }
    
    public init(base: Base, bounds: Range<Base.Index>)
}

★ ArraySlice

// --- Struct ---

public struct ArraySlice<Element> : CollectionType, MutableCollectionType, _DestructorSafeContainer {
    public var startIndex: Int { get }
    public var endIndex: Int { get }
    public subscript (index: Int) -> Element
    public subscript (subRange: Range<Int>) -> ArraySlice<Element>
}

// --- extension ---

extension ArraySlice : ArrayLiteralConvertible {
    public init(arrayLiteral elements: Element...)
}

extension ArraySlice : _ArrayType {
    public init()
    public init<S : SequenceType where S.Generator.Element == _Buffer.Element>(_ s: S)
    public init(count: Int, repeatedValue: Element)
    
    public var count: Int { get }
    public var capacity: Int { get }
    
    public mutating func reserveCapacity(minimumCapacity: Int)
    public mutating func append(newElement: Element)
    public mutating func appendContentsOf<S : SequenceType where S.Generator.Element == Element>(newElements: S)
    public mutating func appendContentsOf<C : CollectionType where C.Generator.Element == Element>(newElements: C)
    public mutating func removeLast() -> Element
    public mutating func insert(newElement: Element, atIndex i: Int)
    public mutating func removeAtIndex(index: Int) -> Element
    public mutating func removeAll(keepCapacity keepCapacity: Bool = default)
}

extension ArraySlice : _Reflectable {
}

extension ArraySlice : CustomStringConvertible, CustomDebugStringConvertible {
    public var description: String { get }
    public var debugDescription: String { get }
}

extension ArraySlice {
    public func withUnsafeBufferPointer<R>(@noescape body: (UnsafeBufferPointer<Element>) throws -> R) rethrows -> R
    public mutating func withUnsafeMutableBufferPointer<R>(@noescape body: (inout UnsafeMutableBufferPointer<Element>) throws -> R) rethrows -> R
}

extension ArraySlice {
    public mutating func replaceRange<C : CollectionType where C.Generator.Element == _Buffer.Element>(subRange: Range<Int>, with newElements: C)
}

★ _ArrayType

// --- Protocol ---

public protocol _ArrayType : RangeReplaceableCollectionType, MutableSliceable, ArrayLiteralConvertible {
    
    public init(count: Int, repeatedValue: Self.Generator.Element)
    
    public var count: Int { get }
    public var capacity: Int { get }
    public var isEmpty: Bool { get }
    public subscript (index: Int) -> Self.Generator.Element { get set }
    
    public mutating func reserveCapacity(minimumCapacity: Int)
    
    public func +=<S : SequenceType where S.Generator.Element == Generator.Element>(inout lhs: Self, rhs: S)
    
    public mutating func insert(newElement: Self.Generator.Element, atIndex i: Int)
    public mutating func removeAtIndex(index: Int) -> Self.Generator.Element
    public init(_ buffer: Self._Buffer)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment