Skip to content

Commit

Permalink
Update exercises batch 15 (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 authored Aug 31, 2023
1 parent b43ea28 commit b0e040b
Show file tree
Hide file tree
Showing 15 changed files with 602 additions and 439 deletions.
Original file line number Diff line number Diff line change
@@ -1,66 +1,66 @@
import Foundation

enum CircularBufferError: Error {
case bufferFull
case bufferEmpty
case bufferFull
case bufferEmpty
}

struct CircularBuffer<T: Equatable> {
var buffer: [T?]
let capacity: Int
var writePoint = 0
var readPoint = 0
struct CircularBuffer {
var buffer: [Int?]
let capacity: Int
var writePoint = 0
var readPoint = 0

private var isFull: Bool {
return buffer.compactMap { $0 }.count >= capacity
}
private var isFull: Bool {
return buffer.compactMap { $0 }.count >= capacity
}

private var isEmpty: Bool {
return buffer.compactMap { $0 }.isEmpty
}
private var isEmpty: Bool {
return buffer.compactMap { $0 }.isEmpty
}

init(capacity: Int) {
self.capacity = capacity
buffer = [T?](repeating: nil, count: capacity)
init(capacity: Int) {
self.capacity = capacity
buffer = [Int?](repeating: nil, count: capacity)
}

mutating func write(_ data: Int) throws {
guard !isFull else {
throw CircularBufferError.bufferFull
}

mutating func write(_ data: T) throws {
guard !isFull else {
throw CircularBufferError.bufferFull
}
buffer[writePoint] = data
updateWritePoint()
}

buffer[writePoint] = data
updateWritePoint()
mutating func overwrite(_ data: Int) {
buffer[writePoint] = data
if isFull && writePoint == readPoint {
updateReadPoint()
}
updateWritePoint()
}

mutating func overwrite(_ data: T) {
buffer[writePoint] = data
if isFull && writePoint == readPoint {
updateReadPoint()
}
updateWritePoint()
mutating func read() throws -> Int {
guard let data = buffer[readPoint] else {
throw CircularBufferError.bufferEmpty
}

mutating func read() throws -> T {
guard let data = buffer[readPoint] else {
throw CircularBufferError.bufferEmpty
}

buffer[readPoint] = nil
updateReadPoint()
buffer[readPoint] = nil
updateReadPoint()

return data
}
return data
}

mutating func clear() {
buffer = [T?](repeating: nil, count: capacity)
}
mutating func clear() {
buffer = [Int?](repeating: nil, count: capacity)
}

private mutating func updateWritePoint() {
writePoint = (writePoint + 1) % capacity
}
private mutating func updateWritePoint() {
writePoint = (writePoint + 1) % capacity
}

private mutating func updateReadPoint() {
readPoint = (readPoint + 1) % capacity
}
private mutating func updateReadPoint() {
readPoint = (readPoint + 1) % capacity
}
}
39 changes: 39 additions & 0 deletions exercises/practice/circular-buffer/.meta/template.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import XCTest
@testable import {{exercise|camelCase}}
class {{exercise|camelCase}}Tests: XCTestCase {
let runAll = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false

{% for case in cases %}
{% if forloop.first -%}
func test{{case.description |camelCase }}() {
{% else -%}
func test{{case.description |camelCase }}() throws {
try XCTSkipIf(true && !runAll) // change true to false to run this test
{% endif -%}
var buffer = CircularBuffer(capacity: {{ case.input.capacity }})
{% for operation in case.input.operations %}
{%- if operation.operation == "read" %}
{%- ifnot operation.should_succeed %}
XCTAssertThrowsError(try buffer.read()) { error in
XCTAssertEqual(error as? CircularBufferError, .bufferEmpty)
}
{%- else %}
XCTAssertEqual(try! buffer.read(), {{ operation.expected }})
{%- endif %}
{%- elif operation.operation == "write" %}
{%- ifnot operation.should_succeed %}
XCTAssertThrowsError(try buffer.write({{ operation.item }})) { error in
XCTAssertEqual(error as? CircularBufferError, .bufferFull)
}
{%- else %}
try! buffer.write({{ operation.item }})
{%- endif %}
{%- elif operation.operation == "clear" %}
buffer.clear()
{%- elif operation.operation == "overwrite" %}
buffer.overwrite({{ operation.item }})
{%- endif %}
{%- endfor %}
}
{% endfor -%}
}
30 changes: 15 additions & 15 deletions exercises/practice/circular-buffer/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
import PackageDescription

let package = Package(
name: "CircularBuffer",
products: [
.library(
name: "CircularBuffer",
targets: ["CircularBuffer"]),
],
dependencies: [],
targets: [
.target(
name: "CircularBuffer",
dependencies: []),
.testTarget(
name: "CircularBufferTests",
dependencies: ["CircularBuffer"]),
]
name: "CircularBuffer",
products: [
.library(
name: "CircularBuffer",
targets: ["CircularBuffer"])
],
dependencies: [],
targets: [
.target(
name: "CircularBuffer",
dependencies: []),
.testTarget(
name: "CircularBufferTests",
dependencies: ["CircularBuffer"]),
]
)
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
//Solution goes in Sources
struct CircularBuffer {
// Write your code for the 'CircularBuffer' exercise in this file.
}
Loading

0 comments on commit b0e040b

Please sign in to comment.