RingBuffer
public struct RingBuffer
An implementation of a Circular Buffer.
The ring buffer is agnostic about the data type it stores, and deals directly with bytes.
Note
The buffer internally rounds up capacity
to the nearest page size.
To determine the actual capacity of the buffer, check the return value of capacity
after initialization.
Note
This class is thread-safe.
Seealso
-
Default buffer size.
Declaration
Swift
public static let defaultCapacity: Int = 4096
-
The capacity of the buffer
Declaration
Swift
public var capacity: Int
-
The number of bytes currently stored in the buffer
Declaration
Swift
public var count: Int
-
The start position of the buffer
Declaration
Swift
public private(set) var start: Int = 0
-
The end position of the buffer
Declaration
Swift
public private(set) var end: Int = 0
-
The number (in bytes) of data that is available in this buffer
Declaration
Swift
public var availableData: Int
-
The number (in bytes) of space that is available in this buffer
Declaration
Swift
public var availableSpace: Int
-
Returns true if the buffer is full
Declaration
Swift
public var isFull: Bool
-
Returns true if the buffer is empty, or if it contains 1 NULL byte
Declaration
Swift
public var isEmpty: Bool
-
The base address of the buffer
Declaration
Swift
public var elementPointer: UnsafeMutablePointer<UInt8>
-
Create a new ring buffer with a given capacity
Declaration
Swift
public init(capacity: Int = RingBuffer.defaultCapacity)
Parameters
capacity
The capacity of the new buffer, in bytes
-
Append bytes to the buffer.
Precondition
count > 0 && count <= self.availableSpace
Warning
This method does not do any bounds checking, so be sure that
count
does not exceedavailableSpace
Declaration
Swift
public mutating func append(_ bytes: UnsafeRawPointer, count: Int)
Parameters
bytes
A pointer to the bytes to copy into the data.
count
The number of bytes to copy.
-
Append a buffer to the buffer.
Precondition
buffer.count <= self.availableSpace
Warning
This method does not do any bounds checking, so be sure that
buffer.count
does not exceedself.availableSpace
Declaration
Swift
public mutating func append<SourceType>(_ buffer: UnsafeBufferPointer<SourceType>)
Parameters
buffer
The buffer of bytes to append. The size is calculated from
SourceType
andbuffer.count
. -
Append a sequence of bytes to the ring buffer.
Precondition
count > 0 && count <= self.availableSpace
Warning
This method does not do any bounds checking, so be sure that
contigous.count
does not exceedself.availableSpace
Declaration
Swift
public mutating func append<SourceType>(contentsOf contiguous: ContiguousArray<SourceType>)
Parameters
contiguous
The sequence of bytes to add to the buffer. Internally, this method calls
append<T>(_:UnsafeBufferPointer<T>)
. -
Append the contents of a
Data
structure to the bufferWarning
This method does not do any bounds checking, so be sure that
data.count
does not exceedself.availableSpace
Declaration
Swift
public mutating func append(_ data: Data)
Parameters
data
The data structure to append to the buffer
-
Write into the buffer, given pointer data and length
Precondition
count > 0
Throws
RingBufferError if an error was encounteredDeclaration
Swift
public mutating func write(from bytes: UnsafePointer<UInt8>, count: Int) throws -> Int
Parameters
from
The bytes to write into the buffer
count
The number of bytes to write into the buffer
Return Value
The number of bytes read
-
Write into the buffer, given pointer data and length
Throws
RingBufferError if an error was encounteredDeclaration
Swift
public mutating func write(from buffer: UnsafeBufferPointer<UInt8>) throws -> Int
Parameters
from
The bytes to write into the buffer
length
The number of bytes to write into the buffer
Return Value
The number of bytes written into the buffer
-
Write the contents of a string to the buffer
Throws
A RingBufferError if an error occuredDeclaration
Swift
public mutating func write(string: String) throws -> Int
Parameters
string
The string to write to the buffer
Return Value
The number of bytes written to the buffer
-
Write the contents of a
Data
object to the bufferThrows
RingBufferError if an error occursDeclaration
Swift
public mutating func write(from data: Data) throws -> Int
Parameters
from
The
Data
object that contains the data to writeReturn Value
the number of bytes written
-
Read
count
bytes from the buffer, intointo
Throws
RingBufferError if an error was encounteredDeclaration
Swift
public mutating func read(into data: inout Data, count: Int) throws -> Int
Parameters
into
A
Data
struct to write data intocount
The number of bytes to read
Return Value
The number of bytes read
-
Get a string from the buffer
Precondition
amount
is greater than 0Declaration
Swift
public mutating func gets(_ amount: Int) throws -> String
Parameters
amount
The number of bytes to read
-
Calls a closure with a pointer to the buffer’s contiguous storage.
Often, the optimizer can eliminate bounds checks within an array algorithm, but when that fails, invoking the same algorithm on the buffer pointer passed into your closure lets you trade safety for speed.
The following example shows how you can iterate over the contents of the buffer pointer:
let numbers = [1, 2, 3, 4, 5] let sum = numbers.withUnsafeBufferPointer { buffer -> Int in var result = 0 for i in stride(from: buffer.startIndex, to: buffer.endIndex, by: 2) { result += buffer[i] } return result } // 'sum' == 9
Seealso
withUnsafeMutableBufferPointer
,UnsafeBufferPointer
Declaration
Swift
public func withUnsafeBufferPointer<ContentType, ResultType>( _ body: (UnsafeBufferPointer<ContentType>) throws -> ResultType ) rethrows -> ResultType
Parameters
body
A closure with an
UnsafeBufferPointer
parameter that points to the contiguous storage for the array. Ifbody
has a return value, it is used as the return value for thewithUnsafeBufferPointer(_:)
method. The pointer argument is valid only for the duration of the closure’s execution.Return Value
The return value of the
body
closure parameter, if any. -
Calls the given closure with a pointer to the buffer’s mutable contiguous
Often, the optimizer can eliminate bounds checks within an array algorithm, but when that fails, invoking the same algorithm on the buffer pointer passed into your closure lets you trade safety for speed.
The following example shows modifying the contents of the
UnsafeMutableBufferPointer
argument tobody
alters the contents of the array:var numbers = [1, 2, 3, 4, 5] numbers.withUnsafeMutableBufferPointer { buffer in for i in stride(from: buffer.startIndex, to: buffer.endIndex - 1, by: 2) { swap(&buffer[i], &buffer[i + 1]) } } print(numbers) // Prints "[2, 1, 4, 3, 5]"
Warning
Do not rely on anything about
self
(the buffer that is the target of this method) during the execution of thebody
closure: It may not appear to have its correct value. Instead, use only theUnsafeMutableBufferPointer
argument tobody
.Seealso
withUnsafeBufferPointer
,UnsafeMutableBufferPointer
Declaration
Swift
public func withUnsafeMutableBufferPointer<ContentType, ResultType>( _ body: (UnsafeMutableBufferPointer<ContentType>) throws -> ResultType ) rethrows -> ResultType
Parameters
body
A closure with an
UnsafeMutableBufferPointer
parameter that points to the contiguous storage for the buffer. Ifbody
has a return value, it is used as the return value for thewithUnsafeMutableBufferPointer(_:)
method. The pointer argument is valid only for the duration of the closure’s execution.Return Value
The return value of the
body
closure parameter, if any. -
Access the bytes in the data.
Warning
The byte pointer argument should not be stored and used outside of the lifetime of the call to the closure.Declaration
Swift
public func withUnsafeBytes<ContentType, ResultType>( _ body: (UnsafePointer<ContentType>) throws -> ResultType ) rethrows -> ResultType
-
Clear the buffer of any stored data
Declaration
Swift
public mutating func clear()
-
A human-readable description of the data
Declaration
Swift
public var description: String
-
A human-readable debug description of the data
Declaration
Swift
public var debugDescription: String
-
A human-readable debug description of the data
Declaration
Swift
public var customMirror: Mirror