-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b43ea28
commit b0e040b
Showing
15 changed files
with
602 additions
and
439 deletions.
There are no files selected for viewing
92 changes: 46 additions & 46 deletions
92
exercises/practice/circular-buffer/.meta/Sources/CircularBuffer/CircularBufferExample.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
exercises/practice/circular-buffer/Sources/CircularBuffer/CircularBuffer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} |
Oops, something went wrong.