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
capacityThe capacity of the new buffer, in bytes
-
Append bytes to the buffer.
Precondition
count > 0 && count <= self.availableSpaceWarning
This method does not do any bounds checking, so be sure that
countdoes not exceedavailableSpaceDeclaration
Swift
public mutating func append(_ bytes: UnsafeRawPointer, count: Int)Parameters
bytesA pointer to the bytes to copy into the data.
countThe number of bytes to copy.
-
Append a buffer to the buffer.
Precondition
buffer.count <= self.availableSpaceWarning
This method does not do any bounds checking, so be sure that
buffer.countdoes not exceedself.availableSpaceDeclaration
Swift
public mutating func append<SourceType>(_ buffer: UnsafeBufferPointer<SourceType>)Parameters
bufferThe buffer of bytes to append. The size is calculated from
SourceTypeandbuffer.count. -
Append a sequence of bytes to the ring buffer.
Precondition
count > 0 && count <= self.availableSpaceWarning
This method does not do any bounds checking, so be sure that
contigous.countdoes not exceedself.availableSpaceDeclaration
Swift
public mutating func append<SourceType>(contentsOf contiguous: ContiguousArray<SourceType>)Parameters
contiguousThe sequence of bytes to add to the buffer. Internally, this method calls
append<T>(_:UnsafeBufferPointer<T>). -
Append the contents of a
Datastructure to the bufferWarning
This method does not do any bounds checking, so be sure that
data.countdoes not exceedself.availableSpaceDeclaration
Swift
public mutating func append(_ data: Data)Parameters
dataThe data structure to append to the buffer
-
Write into the buffer, given pointer data and length
Precondition
count > 0Throws
RingBufferError if an error was encounteredDeclaration
Swift
public mutating func write(from bytes: UnsafePointer<UInt8>, count: Int) throws -> IntParameters
fromThe bytes to write into the buffer
countThe 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 -> IntParameters
fromThe bytes to write into the buffer
lengthThe 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 -> IntParameters
stringThe string to write to the buffer
Return Value
The number of bytes written to the buffer
-
Write the contents of a
Dataobject to the bufferThrows
RingBufferError if an error occursDeclaration
Swift
public mutating func write(from data: Data) throws -> IntParameters
fromThe
Dataobject that contains the data to writeReturn Value
the number of bytes written
-
Read
countbytes from the buffer, intointoThrows
RingBufferError if an error was encounteredDeclaration
Swift
public mutating func read(into data: inout Data, count: Int) throws -> IntParameters
intoA
Datastruct to write data intocountThe number of bytes to read
Return Value
The number of bytes read
-
Get a string from the buffer
Precondition
amountis greater than 0Declaration
Swift
public mutating func gets(_ amount: Int) throws -> StringParameters
amountThe 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' == 9Seealso
withUnsafeMutableBufferPointer,UnsafeBufferPointerDeclaration
Swift
public func withUnsafeBufferPointer<ContentType, ResultType>( _ body: (UnsafeBufferPointer<ContentType>) throws -> ResultType ) rethrows -> ResultTypeParameters
bodyA closure with an
UnsafeBufferPointerparameter that points to the contiguous storage for the array. Ifbodyhas 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
bodyclosure 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
UnsafeMutableBufferPointerargument tobodyalters 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 thebodyclosure: It may not appear to have its correct value. Instead, use only theUnsafeMutableBufferPointerargument tobody.Seealso
withUnsafeBufferPointer,UnsafeMutableBufferPointerDeclaration
Swift
public func withUnsafeMutableBufferPointer<ContentType, ResultType>( _ body: (UnsafeMutableBufferPointer<ContentType>) throws -> ResultType ) rethrows -> ResultTypeParameters
bodyA closure with an
UnsafeMutableBufferPointerparameter that points to the contiguous storage for the buffer. Ifbodyhas 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
bodyclosure 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
View on GitHub
RingBuffer Struct Reference