0001 // MARK: - Eager 0002 0003 // MARK: Chunk 0004 0005 public extension CollectionType { 0006 0007 /// Returns an array of arrays of n non-overlapping elements of self 0008 /// - Parameter n: The size of the chunk 0009 /// - Precondition: `n > 0` 0010 /// - SeeAlso: `func window(n: Int) -> [[Self.Generator.Element]]` 0011 /// ```swift 0012 /// [1, 2, 3, 4, 5].chunk(2) 0013 /// 0014 /// [[1, 2], [3, 4], [5]] 0015 /// ``` 0016 @warn_unused_result 0017 public func chunk(n: Index.Distance) -> [SubSequence] { 0018 var res: [SubSequence] = [] 0019 var i = startIndex 0020 var j: Index 0021 while i != endIndex { 0022 j = i.advancedBy(n, limit: endIndex) 0023 res.append(self[i..<j]) 0024 i = j 0025 } 0026 return res 0027 } 0028 } 0029 0030 // MARK: Window 0031 0032 public extension CollectionType { 0033 0034 /// Returns an array of arrays of n overlapping elements of self 0035 /// - Parameter n: The size of the window 0036 /// - SeeAlso: `func chunk(n: Int) -> [[Self.Generator.Element]]` 0037 /// ```swift 0038 /// [1, 2, 3, 4].window(2) 0039 /// 0040 /// [[1, 2], [2, 3], [3, 4]] 0041 /// ``` 0042 @warn_unused_result 0043 func window(n: Index.Distance) -> [SubSequence] { 0044 var ret: [SubSequence] = [] 0045 ret.reserveCapacity(underestimateCount() - numericCast(n)) 0046 var i = startIndex 0047 var j = i.advancedBy(n, limit: endIndex) 0048 while true { 0049 ret.append(self[i..<j]) 0050 i = i.successor() 0051 if j == endIndex { return ret } 0052 j = j.successor() 0053 } 0054 } 0055 } 0056 0057 // MARK: - Lazy 0058 0059 // MARK: Chunk 0060 /// :nodoc: 0061 public struct ChunkGen<C
ChunkWindowSplit.swift:80 public func generate() -> ChunkGen<C> {ChunkWindowSplit.swift:81 return ChunkGen(c: c, n: n, i: c.startIndex): CollectionType> : GeneratorType { 0062 0063 private var c
ChunkWindowSplit.swift:63 private var c: CChunkWindowSplit.swift:64 private let n: C.Index.DistanceChunkWindowSplit.swift:65 private var i: C.IndexChunkWindowSplit.swift:67 public mutating func next() -> C.SubSequence? {: C 0064 private let n
ChunkWindowSplit.swift:68 if i == c.endIndex { return nil }ChunkWindowSplit.swift:69 let j = i.advancedBy(n, limit: c.endIndex)ChunkWindowSplit.swift:71 return c[i..<j]: C.Index.Distance 0065 private var i
ChunkWindowSplit.swift:69 let j = i.advancedBy(n, limit: c.endIndex): C.Index 0066 0067 public mutating func next() -> C.SubSequence? { 0068 if i == c.endIndex { return nil } 0069 let j = i.advancedBy(n, limit: c.endIndex) 0070 defer { i = j } 0071 return c[i..<j] 0072 } 0073 } 0074 /// :nodoc: 0075 public struct ChunkSeq
ChunkWindowSplit.swift:68 if i == c.endIndex { return nil }ChunkWindowSplit.swift:69 let j = i.advancedBy(n, limit: c.endIndex)ChunkWindowSplit.swift:70 defer { i = j }ChunkWindowSplit.swift:71 return c[i..<j]<C
ChunkWindowSplit.swift:96 func chunk(n: Index.Distance) -> ChunkSeq<Self> {ChunkWindowSplit.swift:97 return ChunkSeq(c: self, n: n): CollectionType> : LazySequenceType { 0076 0077 private let c
ChunkWindowSplit.swift:77 private let c: CChunkWindowSplit.swift:78 private let n: C.Index.DistanceChunkWindowSplit.swift:80 public func generate() -> ChunkGen<C> {: C 0078 private let n
ChunkWindowSplit.swift:81 return ChunkGen(c: c, n: n, i: c.startIndex)ChunkWindowSplit.swift:81 return ChunkGen(c: c, n: n, i: c.startIndex): C.Index.Distance 0079 /// :nodoc: 0080 public func generate() -> ChunkGen<C> { 0081 return ChunkGen(c: c, n: n, i: c.startIndex) 0082 } 0083 } 0084 0085 public extension LazyCollectionType { 0086 0087 /// Returns lazily-generated arrays of n non-overlapping elements of self 0088 /// - Parameter n: The size of the chunk 0089 /// - SeeAlso: `func window(n: Int) -> WindowSeq<Self>` 0090 /// ```swift 0091 /// lazy([1, 2, 3, 4, 5]).chunk(2) 0092 /// 0093 /// [1, 2], [3, 4], [5] 0094 /// ``` 0095 @warn_unused_result 0096 func chunk(n: Index.Distance) -> ChunkSeq<Self> { 0097 return ChunkSeq(c: self, n: n) 0098 } 0099 } 0100 0101 // MARK: Window 0102 /// :nodoc: 0103 public struct WindowGen
ChunkWindowSplit.swift:81 return ChunkGen(c: c, n: n, i: c.startIndex)<C
ChunkWindowSplit.swift:127 public func generate() -> WindowGen<C> {ChunkWindowSplit.swift:128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false): CollectionType> : GeneratorType { 0104 0105 private let c
ChunkWindowSplit.swift:105 private let c: CChunkWindowSplit.swift:106 private var i, j: C.IndexChunkWindowSplit.swift:109 mutating public func next() -> C.SubSequence? {: C 0106 private var i
ChunkWindowSplit.swift:111 if j == c.endIndex {ChunkWindowSplit.swift:113 return c[i..<j]ChunkWindowSplit.swift:116 return c[i..<j], j
ChunkWindowSplit.swift:113 return c[i..<j]ChunkWindowSplit.swift:115 defer { (i,j) = (i.successor(),j.successor()) }ChunkWindowSplit.swift:115 defer { (i,j) = (i.successor(),j.successor()) }ChunkWindowSplit.swift:116 return c[i..<j]: C.Index 0107 private var s
ChunkWindowSplit.swift:111 if j == c.endIndex {ChunkWindowSplit.swift:113 return c[i..<j]ChunkWindowSplit.swift:115 defer { (i,j) = (i.successor(),j.successor()) }ChunkWindowSplit.swift:115 defer { (i,j) = (i.successor(),j.successor()) }ChunkWindowSplit.swift:116 return c[i..<j]: Bool 0108 /// :nodoc: 0109 mutating public func next() -> C.SubSequence? { 0110 if s { return nil } 0111 if j == c.endIndex { 0112 s = true 0113 return c[i..<j] 0114 } 0115 defer { (i,j) = (i.successor(),j.successor()) } 0116 return c[i..<j] 0117 } 0118 } 0119 0120 /// :nodoc: 0121 0122 public struct WindowSeq
ChunkWindowSplit.swift:110 if s { return nil }ChunkWindowSplit.swift:112 s = true<C
ChunkWindowSplit.swift:143 func window(n: Index.Distance) -> WindowSeq<Self> {ChunkWindowSplit.swift:144 return WindowSeq(c: self, n: n): CollectionType> : LazySequenceType { 0123 0124 private let c
ChunkWindowSplit.swift:124 private let c: CChunkWindowSplit.swift:125 private let n: C.Index.DistanceChunkWindowSplit.swift:127 public func generate() -> WindowGen<C> {: C 0125 private let n
ChunkWindowSplit.swift:128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false)ChunkWindowSplit.swift:128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false)ChunkWindowSplit.swift:128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false)ChunkWindowSplit.swift:128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false): C.Index.Distance 0126 /// :nodoc: 0127 public func generate() -> WindowGen<C> { 0128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false) 0129 } 0130 } 0131 0132 public extension LazyCollectionType { 0133 0134 /// Returns lazily-generated arrays of n overlapping elements of self 0135 /// - Parameter n: The size of the window 0136 /// - SeeAlso: `func chunk(n: Int) -> ChunkSeq<Self>` 0137 /// ```swift 0138 /// lazy([1, 2, 3, 4]).window(2) 0139 /// 0140 /// [1, 2], [2, 3], [3, 4] 0141 /// ``` 0142 @warn_unused_result 0143 func window(n: Index.Distance) -> WindowSeq<Self> { 0144 return WindowSeq(c: self, n: n) 0145 } 0146 } 0147
ChunkWindowSplit.swift:128 return WindowGen(c: c, i: c.startIndex, j: c.startIndex.advancedBy(n, limit: c.endIndex), s: false)