forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RingBuffer.swift
82 lines (71 loc) · 1.93 KB
/
RingBuffer.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Fixed-length ring buffer
In this implementation, the read and write pointers always increment and
never wrap around. On a 64-bit platform that should not get you into trouble
any time soon.
Not thread-safe, so don't read and write from different threads at the same
time! To make this thread-safe for one reader and one writer, it should be
enough to change read/writeIndex += 1 to OSAtomicIncrement64(), but I haven't
tested this...
*/
public struct RingBuffer<T> {
private var array: [T?]
private var readIndex = 0
private var writeIndex = 0
public init(count: Int) {
array = [T?](repeating: nil, count: count)
}
/* Returns false if out of space. */
@discardableResult
public mutating func write(_ element: T) -> Bool {
guard !isFull else { return false }
defer {
writeIndex += 1
}
array[wrapped: writeIndex] = element
return true
}
/* Returns nil if the buffer is empty. */
public mutating func read() -> T? {
guard !isEmpty else { return nil }
defer {
array[wrapped: readIndex] = nil
readIndex += 1
}
return array[wrapped: readIndex]
}
private var availableSpaceForReading: Int {
return writeIndex - readIndex
}
public var isEmpty: Bool {
return availableSpaceForReading == 0
}
private var availableSpaceForWriting: Int {
return array.count - availableSpaceForReading
}
public var isFull: Bool {
return availableSpaceForWriting == 0
}
}
public extension RingBuffer: Sequence {
public func makeIterator() -> AnyIterator<T> {
var index = readIndex
return AnyIterator {
guard index < self.writeIndex else { return nil }
defer {
index += 1
}
return self.array[wrapped: index]
}
}
}
private extension Array {
subscript (wrapped index: Int) -> Element {
get {
return self[index % count]
}
set {
self[index % count] = newValue
}
}
}