Skip to content

Commit 6cc7c2f

Browse files
authored
Merge pull request kodecocodes#265 from JaapWijnen/ring-buffer-swift3-migrate
Ring buffer swift3 migrate
2 parents 969ba7c + 87c2324 commit 6cc7c2f

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

Ring Buffer/README.markdown

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Initially, the array is empty and the read (`r`) and write (`w`) pointers are at
1717
Let's add some data to this array. We'll write, or "enqueue", the number `123`:
1818

1919
[ 123, , , , ]
20-
r
20+
r
2121
---> w
2222

2323
Each time we add data, the write pointer moves one step forward. Let's add a few more elements:
@@ -46,7 +46,7 @@ Whoops, the write pointer has reached the end of the array, so there is no more
4646
[ 555, 456, 789, 666, 333 ]
4747
---> w r
4848

49-
We can now read the remaining three items, `666`, `333`, and `555`.
49+
We can now read the remaining three items, `666`, `333`, and `555`.
5050

5151
[ 555, 456, 789, 666, 333 ]
5252
w --------> r
@@ -63,16 +63,15 @@ Here is a very basic implementation in Swift:
6363

6464
```swift
6565
public struct RingBuffer<T> {
66-
private var array: [T?]
67-
private var readIndex = 0
68-
private var writeIndex = 0
69-
66+
fileprivate var array: [T?]
67+
fileprivate var readIndex = 0
68+
fileprivate var writeIndex = 0
69+
7070
public init(count: Int) {
71-
array = [T?](count: count, repeatedValue: nil)
71+
array = [T?](repeating: nil, count: count)
7272
}
73-
74-
/* Returns false if out of space. */
75-
public mutating func write(element: T) -> Bool {
73+
74+
public mutating func write(_ element: T) -> Bool {
7675
if !isFull {
7776
array[writeIndex % array.count] = element
7877
writeIndex += 1
@@ -81,8 +80,7 @@ public struct RingBuffer<T> {
8180
return false
8281
}
8382
}
84-
85-
/* Returns nil if the buffer is empty. */
83+
8684
public mutating func read() -> T? {
8785
if !isEmpty {
8886
let element = array[readIndex % array.count]
@@ -92,19 +90,19 @@ public struct RingBuffer<T> {
9290
return nil
9391
}
9492
}
95-
96-
private var availableSpaceForReading: Int {
93+
94+
fileprivate var availableSpaceForReading: Int {
9795
return writeIndex - readIndex
9896
}
99-
97+
10098
public var isEmpty: Bool {
10199
return availableSpaceForReading == 0
102100
}
103-
104-
private var availableSpaceForWriting: Int {
101+
102+
fileprivate var availableSpaceForWriting: Int {
105103
return array.count - availableSpaceForReading
106104
}
107-
105+
108106
public var isFull: Bool {
109107
return availableSpaceForWriting == 0
110108
}
@@ -130,7 +128,7 @@ In other words, we take the modulo (or the remainder) of the read index and writ
130128
The reason this is a bit controversial is that `writeIndex` and `readIndex` always increment, so in theory these values could become too large to fit into an integer and the app will crash. However, a quick back-of-the-napkin calculation should take away those fears.
131129

132130
Both `writeIndex` and `readIndex` are 64-bit integers. If we assume that they are incremented 1000 times per second, which is a lot, then doing this continuously for one year requires `ceil(log_2(365 * 24 * 60 * 60 * 1000)) = 35` bits. That leaves 28 bits to spare, so that should give you about 2^28 years before running into problems. That's a long time. :-)
133-
131+
134132
To play with this ring buffer, copy the code to a playground and do the following to mimic the example above:
135133

136134
```swift

Ring Buffer/RingBuffer.playground/Contents.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//: Playground - noun: a place where people can play
22

33
public struct RingBuffer<T> {
4-
private var array: [T?]
5-
private var readIndex = 0
6-
private var writeIndex = 0
4+
fileprivate var array: [T?]
5+
fileprivate var readIndex = 0
6+
fileprivate var writeIndex = 0
77

88
public init(count: Int) {
9-
array = [T?](count: count, repeatedValue: nil)
9+
array = [T?](repeating: nil, count: count)
1010
}
1111

12-
public mutating func write(element: T) -> Bool {
12+
public mutating func write(_ element: T) -> Bool {
1313
if !isFull {
1414
array[writeIndex % array.count] = element
1515
writeIndex += 1
@@ -29,15 +29,15 @@ public struct RingBuffer<T> {
2929
}
3030
}
3131

32-
private var availableSpaceForReading: Int {
32+
fileprivate var availableSpaceForReading: Int {
3333
return writeIndex - readIndex
3434
}
3535

3636
public var isEmpty: Bool {
3737
return availableSpaceForReading == 0
3838
}
3939

40-
private var availableSpaceForWriting: Int {
40+
fileprivate var availableSpaceForWriting: Int {
4141
return array.count - availableSpaceForReading
4242
}
4343

Ring Buffer/RingBuffer.playground/timeline.xctimeline

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)