Sha256: 4084041cde888052ff0efa4d2c5bea09bcf2f13bbd6486c50d23584292ae6fd9

Contents?: true

Size: 1.47 KB

Versions: 41

Compression:

Stored size: 1.47 KB

Contents

import Foundation

enum CircularBufferError: Error {
    case bufferFull
    case bufferEmpty
}

struct CircularBuffer<T: Equatable> {
    var buffer: [T?]
    let capacity: Int
    var writePoint = 0
    var readPoint = 0
    
    private var isFull: Bool {
        return buffer.flatMap { $0 }.count >= capacity
    }
    
    private var isEmpty: Bool {
        return buffer.flatMap { $0 }.isEmpty
    }
    
    init(capacity: Int) {
        self.capacity = capacity
        buffer = [T?](repeating: nil, count: capacity)
    }
    
    mutating func write(_ data: T) throws {
        guard !isFull else {
            throw CircularBufferError.bufferFull
        }
        
        buffer[writePoint] = data
        updateWritePoint()
    }
    
    mutating func overwrite(_ data: T) {
        buffer[writePoint] = data
        if isFull && writePoint == readPoint {
            updateReadPoint()
        }
        updateWritePoint()
    }
    
    mutating func read() throws -> T {
        guard let data = buffer[readPoint] else {
            throw CircularBufferError.bufferEmpty
        }
        
        buffer[readPoint] = nil
        updateReadPoint()
        
        return data
    }
    
    mutating func clear() {
        buffer = [T?](repeating: nil, count: capacity)
    }
    
    private mutating func updateWritePoint() {
        writePoint = (writePoint + 1) % capacity
    }
    
    private mutating func updateReadPoint() {
        readPoint = (readPoint + 1) % capacity
    }
}

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
trackler-2.2.1.135 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.134 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.133 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.132 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.131 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.130 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.129 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.128 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.127 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.126 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.125 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.124 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.123 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.122 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.121 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.120 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.119 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.118 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.117 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift
trackler-2.2.1.116 tracks/swift/exercises/circular-buffer/Sources/CircularBufferExample.swift