Skip to content

Commit 799cd97

Browse files
committed
Added usage example
1 parent 62984c6 commit 799cd97

File tree

1 file changed

+19
-0
lines changed
  • Data Structures.playground/Pages/CircularBuffer.xcplaygroundpage

1 file changed

+19
-0
lines changed

Data Structures.playground/Pages/CircularBuffer.xcplaygroundpage/Contents.swift

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//: [Previous](@previous)
22

3+
import Foundation
4+
35
public struct CircularBuffer<T> {
46
fileprivate var data: [T]
57
fileprivate var head: Int = 0, tail: Int = 0
@@ -168,6 +170,23 @@ extension CircularBuffer: Sequence {
168170

169171
}
170172

173+
extension CircularBuffer: ExpressibleByArrayLiteral {
174+
/// Constructs a circular buffer using an array literal.
175+
public init(arrayLiteral elements: T...) {
176+
self.init(elements, size: elements.count)
177+
}
178+
}
179+
180+
//: Usage
181+
182+
var buffer: CircularBuffer = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
183+
debugPrint("buffer: ", buffer)
184+
185+
while true {
186+
let random = Int(arc4random()) % (99 - 16) + 10
187+
buffer.push(element: random)
188+
debugPrint("buffer: ", buffer)
189+
}
171190

172191
//: [Next](@next)
173192

0 commit comments

Comments
 (0)