Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: circular-buffer exercise #535

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,15 @@
"arrays",
"lists"
]
}
},
{
"slug": "circular-buffer",
"name": "Circular Buffer",
"uuid": "c69af85e-f089-4457-a375-63ad29fc266b",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
"concepts": [
Expand Down
58 changes: 58 additions & 0 deletions exercises/practice/circular-buffer/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Instructions

A circular buffer, cyclic buffer or ring buffer is a data structure that uses a single, fixed-size buffer as if it were connected end-to-end.

A circular buffer first starts empty and of some predefined length.
For example, this is a 7-element buffer:

```text
[ ][ ][ ][ ][ ][ ][ ]
```

Assume that a 1 is written into the middle of the buffer (exact starting location does not matter in a circular buffer):

```text
[ ][ ][ ][1][ ][ ][ ]
```

Then assume that two more elements are added — 2 & 3 — which get appended after the 1:

```text
[ ][ ][ ][1][2][3][ ]
```

If two elements are then removed from the buffer, the oldest values inside the buffer are removed.
The two elements removed, in this case, are 1 & 2, leaving the buffer with just a 3:

```text
[ ][ ][ ][ ][ ][3][ ]
```

If the buffer has 7 elements then it is completely full:

```text
[5][6][7][8][9][3][4]
```

When the buffer is full an error will be raised, alerting the client that further writes are blocked until a slot becomes free.

When the buffer is full, the client can opt to overwrite the oldest data with a forced write.
In this case, two more elements — A & B — are added and they overwrite the 3 & 4:

```text
[5][6][7][8][9][A][B]
```

3 & 4 have been replaced by A & B making 5 now the oldest data in the buffer.
Finally, if two elements are removed then what would be returned is 5 & 6 yielding the buffer:

```text
[ ][ ][7][8][9][A][B]
```

Because there is space available, if the client again uses overwrite to store C & D then the space where 5 & 6 were stored previously will be used not the location of 7 & 8.
7 is still the oldest element and the buffer is once again full.

```text
[C][D][7][8][9][A][B]
```
22 changes: 22 additions & 0 deletions exercises/practice/circular-buffer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"lib/circular_buffer.dart"
],
"test": [
"test/circular_buffer_test.dart"
],
"example": [
".meta/lib/example.dart"
],
"invalidator": [
"pubspec.yaml"
]
},
"blurb": "A data structure that uses a single, fixed-size buffer as if it were connected end-to-end.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Circular_buffer"
}
29 changes: 29 additions & 0 deletions exercises/practice/circular-buffer/.meta/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class EmptyBufferException implements Exception {}

class FullBufferException implements Exception {}

class CircularBuffer {
final int size;
final List<int> data = [];
CircularBuffer(this.size);

int read() {
if (data.isEmpty) throw EmptyBufferException();
return data.removeAt(0);
}

void write(int value, {bool force = false}) {
if (size == data.length) {
if (force) {
read();
} else {
throw FullBufferException();
}
}
data.add(value);
}

void clear() {
data.clear();
}
}
52 changes: 52 additions & 0 deletions exercises/practice/circular-buffer/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[28268ed4-4ff3-45f3-820e-895b44d53dfa]
description = "reading empty buffer should fail"

[2e6db04a-58a1-425d-ade8-ac30b5f318f3]
description = "can read an item just written"

[90741fe8-a448-45ce-be2b-de009a24c144]
description = "each item may only be read once"

[be0e62d5-da9c-47a8-b037-5db21827baa7]
description = "items are read in the order they are written"

[2af22046-3e44-4235-bfe6-05ba60439d38]
description = "full buffer can't be written to"

[547d192c-bbf0-4369-b8fa-fc37e71f2393]
description = "a read frees up capacity for another write"

[04a56659-3a81-4113-816b-6ecb659b4471]
description = "read position is maintained even across multiple writes"

[60c3a19a-81a7-43d7-bb0a-f07242b1111f]
description = "items cleared out of buffer can't be read"

[45f3ae89-3470-49f3-b50e-362e4b330a59]
description = "clear frees up capacity for another write"

[e1ac5170-a026-4725-bfbe-0cf332eddecd]
description = "clear does nothing on empty buffer"

[9c2d4f26-3ec7-453f-a895-7e7ff8ae7b5b]
description = "overwrite acts like write on non-full buffer"

[880f916b-5039-475c-bd5c-83463c36a147]
description = "overwrite replaces the oldest item on full buffer"

[bfecab5b-aca1-4fab-a2b0-cd4af2b053c3]
description = "overwrite replaces the oldest item remaining in buffer following a read"

[9cebe63a-c405-437b-8b62-e3fdc1ecec5a]
description = "initial clear does not affect wrapping around"
18 changes: 18 additions & 0 deletions exercises/practice/circular-buffer/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

linter:
rules:
# Error Rules
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- valid_regexps
11 changes: 11 additions & 0 deletions exercises/practice/circular-buffer/lib/circular_buffer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* These implementation definitions enable zero-argument contructors:
* throw EmptyBufferException();
*/

class EmptyBufferException implements Exception {}

class FullBufferException implements Exception {}

class CircularBuffer {
// Put your code here
}
5 changes: 5 additions & 0 deletions exercises/practice/circular-buffer/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'circular_buffer'
environment:
sdk: '>=2.18.0 <3.0.0'
dev_dependencies:
test: '<2.0.0'
139 changes: 139 additions & 0 deletions exercises/practice/circular-buffer/test/circular_buffer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import 'package:circular_buffer/circular_buffer.dart';
import 'package:test/test.dart';

void main() {
test("reading empty buffer should fail", () {
final buff = CircularBuffer(1);
expect(() => buff.read(), throwsA(isA<EmptyBufferException>()));
}, skip: false);

test("can read an item just written", () {
final buff = CircularBuffer(1);
buff.write(1);
var value = buff.read();
expect(value, 1);
}, skip: true);

test("each item may only be read once", () {
final buff = CircularBuffer(1);
buff.write(1);
var value = buff.read();
expect(value, 1);
expect(() => buff.read(), throwsA(isA<EmptyBufferException>()));
}, skip: true);

test("items are read in the order they are written", () {
final buff = CircularBuffer(2);
buff.write(1);
buff.write(2);
var value = buff.read();
expect(value, 1);
value = buff.read();
expect(value, 2);
}, skip: true);

test("full buffer can't be written to", () {
final buff = CircularBuffer(1);
buff.write(1);
expect(() => buff.write(2), throwsA(isA<FullBufferException>()));
}, skip: true);

test("a read frees up capacity for another write", () {
final buff = CircularBuffer(1);
buff.write(1);
var value = buff.read();
expect(value, 1);
buff.write(2);
value = buff.read();
expect(value, 2);
}, skip: true);

test("read position is maintained even across multiple writes", () {
final buff = CircularBuffer(3);
buff.write(1);
buff.write(2);
var value = buff.read();
expect(value, 1);
buff.write(3);
value = buff.read();
expect(value, 2);
value = buff.read();
expect(value, 3);
}, skip: true);

test("items cleared out of buffer can't be read", () {
final buff = CircularBuffer(1);
buff.write(1);
buff.clear();
expect(() => buff.read(), throwsA(isA<EmptyBufferException>()));
}, skip: true);

test("clear frees up capacity for another write", () {
final buff = CircularBuffer(1);
buff.write(1);
buff.clear();
buff.write(2);
var value = buff.read();
expect(value, 2);
}, skip: true);

test("clear does nothing on empty buffer", () {
final buff = CircularBuffer(1);
buff.clear();
buff.write(1);
var value = buff.read();
expect(value, 1);
}, skip: true);

test("overwrite acts like write on non-full buffer", () {
final buff = CircularBuffer(2);
buff.write(1);
buff.write(2, force: true);
var value = buff.read();
expect(value, 1);
value = buff.read();
expect(value, 2);
}, skip: true);

test("overwrite replaces the oldest item on full buffer", () {
final buff = CircularBuffer(2);
buff.write(1);
buff.write(2);
buff.write(3, force: true);
var value = buff.read();
expect(value, 2);
value = buff.read();
expect(value, 3);
}, skip: true);

test("overwrite replaces the oldest item remaining in buffer following a read", () {
final buff = CircularBuffer(3);
buff.write(1);
buff.write(2);
buff.write(3);
var value = buff.read();
expect(value, 1);
buff.write(4);
buff.write(5, force: true);
value = buff.read();
expect(value, 3);
value = buff.read();
expect(value, 4);
value = buff.read();
expect(value, 5);
}, skip: true);

test("initial clear does not affect wrapping around", () {
final buff = CircularBuffer(2);
buff.clear();
buff.write(1);
buff.write(2);
buff.write(3, force: true);
buff.write(4, force: true);
var value = buff.read();
expect(value, 3);
value = buff.read();
expect(value, 4);
expect(() => buff.read(), throwsA(isA<EmptyBufferException>()));
}, skip: true);
}