Add circular-buffer exercise (#193)

This commit is contained in:
Erik Schierboom
2024-05-21 06:57:13 +02:00
committed by GitHub
parent 0ca2e66367
commit 9a85711ae2
9 changed files with 502 additions and 0 deletions

View File

@@ -532,6 +532,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "circular-buffer",
"name": "Circular Buffer",
"uuid": "2442f2fe-3304-4d28-9539-b23a31e24c22",
"practices": [],
"prerequisites": [],
"difficulty": 7
}
]
},

View File

@@ -0,0 +1,29 @@
# Instructions append
This exercise introduces a couple of concepts you might not have seen before:
## User-defined namespace
Words in 8th all belong to a namespace, e.g. `a:new` refers to the `new` word in the `a` namespace.
One can also define custom namespaces, which is what we do at the top of the `circular-buffer.8th` file:
```8th
ns: cb
```
This means that all words within that file can be accessed via `cb:<word>`, which is a great way to group related functionality.
For more information, check out the [namespaces][namespaces] documentation.
## Exceptions
The `read` and `write` words are both expected to throw an exception when they're called on a circular buffer with an invalid state.
For more information, check the [exceptions and error handling][exceptions-and-error-handling] documentation.
## Objects
While you're free to implement the circular buffer as you like, you could consider using 8th's [object support][objects].
[namespaces]: https://8th-dev.com/manual.html#namespaces
[exceptions-and-error-handling]: https://8th-dev.com/manual.html#exceptions-and-error-handling
[objects]: https://8th-dev.com/manual.html#objects

View File

@@ -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]
```

View File

@@ -0,0 +1,19 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"circular-buffer.8th"
],
"test": [
"test.8th"
],
"example": [
".meta/example.8th"
]
},
"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"
}

View File

@@ -0,0 +1,68 @@
ns: cb
: count \ o -- n
"count" o:@ nip
;
: capacity \ o -- n
"capacity" o:@ nip
;
: items \ o -- a
"items" o:@ nip
;
: head \ o -- n
"head" o:@ nip
;
: tail \ o -- n
"tail" o:@ nip
;
: read \ o -- o x
>r
r@ count 0 n:= if r> "circular buffer empty" throw ;then
r@ items r@ tail a:_@
r@ tail n:1+ r@ capacity n:/mod drop r@ "tail" rot o:! drop
r@ count n:1- r@ "count" rot o:!
swap
;
: overwrite \ o x -- o
swap >r
r@ items r@ head rot a:! r@ "items" rot o:!
r@ head n:1+ r@ capacity n:/mod drop "head" swap o:!
r@ count r@ capacity n:=
if
r@ head "tail" swap o:!
else
r@ count n:1+ "count" swap o:!
then
;
: write \ o x -- o
swap >r
r@ capacity r@ count n:= if drop r> "circular buffer full" throw ;then
r> swap cb:overwrite
;
: clear \ o
"head" 0 o:!
"tail" 0 o:!
"count" 0 o:!
;
: new \ n -- o
{
class: "circular-buffer",
methods: {
read: ' read,
write: ' write,
overwrite: ' overwrite
}
} o:new swap
2dup "capacity" swap o:! drop
n:1- ' noop 0 rot a:generate "items" swap o:!
cb:clear
;

View File

@@ -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"

View File

@@ -0,0 +1,17 @@
ns: cb
: read \ x -- x x'
;
: write \ x x' -- x
;
: overwrite \ x x' -- x
;
: new \ n -- x
;

View File

@@ -0,0 +1,173 @@
needs console/loaded
\ -----------------------------------------------------------------
ns: test
-1 var, test-count
var tests-passed
var tests-failed
var tests-skipped
true var, run-test
\ Some utility words
: test-passed \ s x x -- \\ test name, expected value, actual value
2drop
1 tests-passed n:+!
con:green con:onBlack . space " ... OK" . con:white con:onBlack cr
;
: test-skipped \ s --
1 tests-skipped n:+!
con:cyan con:onBlack . space " ... SKIPPED" . con:white con:onBlack cr
;
: test-failed \ s x x -- \\ test name, expected value, actual value
1 tests-failed n:+!
rot
con:red con:onBlack . space " ... FAIL" . con:white con:onBlack cr
" Actual: «" . . "»" . cr
" Expected: «" . . "»" . cr cr
;
: isword? \ x -- x f
dup >kind ns:w n:=
;
: run-test? \ -- T
run-test @ if true else "RUN_ALL_TESTS" getenv n:>bool then
;
\ Num passed + num skipped + num failed should == num tests
: all-tests-run? \ -- T
tests-passed @ tests-skipped @ tests-failed @ n:+ n:+
test-count @ n:=
;
\ returns true if x is a date, false otherwise
: date? \ x -- x T
dup >kind ns:d n:=
;
\ adapted from 8th forum -- https://8th-dev.com/forum/index.php/topic,2745.0.html
: eq? \ x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
\ same kind: try different comparators
number? if n:= ;then
string? if s:= ;then
array? if ' eq? a:= 2nip ;then
map? if ' eq? m:= 2nip ;then
date? if d:= ;then
\ otherwise fall back to 'lazy evaluation'
l: =
;
: eps_eq? \ n x x -- T
\ are the items the same kind?
2dup >kind swap >kind n:=
!if 2drop false ;then
number? !if 2drop false ;then
rot n:~=
;
: check-depth \ ... n -- ...
dup>r
n:1+ depth n:=
!if
con:red con:onBlack
"PANIC: expected stack depth to be " . r> . cr
"Stack is:" . cr
.s cr
255 die
then
rdrop
;
\ -----------------------------------------------------------------
\ status report at end of run
( all-tests-run?
!if con:red con:onBlack "... FAIL - not all tests completed" . con:white con:onBlack cr then
) onexit
\ Print a summary of the tests run
( con:white con:onBlack
test-count @ . space "tests planned - " .
tests-passed @ . space "passed - " .
tests-skipped @ . space "skipped - " .
tests-failed @ . space "failed" . cr
) onexit
\ -----------------------------------------------------------------
\ The public-facing words
\ -----------------------------------------------------------------
: equal? \ s x w -- | s w x --
run-test? !if 2drop test-skipped ;; then
isword? !if swap then
w:exec
3 check-depth
2dup \ so test-failed can show actual and expected
eq? if test-passed else test-failed then
;
: approx_equal? \ s x w n -- | s w x n --
run-test? !if 3drop test-skipped ;; then
-rot isword? !if swap then
w:exec
4 check-depth
3dup \ so test-failed can show actual and expected
eps_eq?
if rot drop test-passed else rot drop test-failed then
;
: true? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
true swap dup \ so test-failed can show actual and expected
if test-passed else test-failed then
;
: false? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
false swap dup \ so test-failed can show actual and expected
!if test-passed else test-failed then
;
: null? \ s w --
run-test? !if drop test-skipped ;; then
w:exec
2 check-depth
null swap dup \ so test-failed can show actual and expected
G:null? nip if test-passed else test-failed then
;
: SKIP-REST-OF-TESTS false run-test ! ;
: tests \ n --
test-count !
;
\ Set the exit status:
\ 0 = all OK
\ 1 = not all tests were run (some error occurred)
\ 2 = some tests failed
: end-of-tests \ --
all-tests-run?
if
tests-failed @ 0 n:= if 0 else 2 then
else
1
then
die
;

View File

@@ -0,0 +1,78 @@
"circular-buffer.8th" f:include
needs exercism/test
with: test
14 tests
: throws? \ w -- T
catch dup if nip then nip
;
"reading empty buffer should fail"
( 1 cb:new ' cb:read throws? )
true?
SKIP-REST-OF-TESTS
"can read an item just written"
( 1 cb:new 1 cb:write cb:read nip )
1
equal?
"each item may only be read once"
( 1 cb:new 1 cb:write cb:read drop ' cb:read throws? )
true?
"items are read in the order they are written"
( 2 cb:new 1 cb:write 2 cb:write cb:read swap cb:read nip 2 a:close )
[1, 2]
equal?
"full buffer can't be written to"
( 1 cb:new 1 cb:write 2 ' cb:write throws? )
true?
"a read frees up capacity for another write"
( 1 cb:new 1 cb:write cb:read swap 2 cb:write cb:read nip 2 a:close )
[1, 2]
equal?
"read position is maintained even across multiple writes"
( 3 cb:new 1 cb:write 2 cb:write cb:read swap 3 cb:write cb:read swap cb:read nip 3 a:close )
[1, 2, 3]
equal?
"items cleared out of buffer can't be read"
( 1 cb:new 1 cb:write cb:clear ' cb:read throws? )
true?
"clear frees up capacity for another write"
( 1 cb:new 1 cb:write cb:clear 2 cb:write cb:read nip )
2
equal?
"clear does nothing on empty buffer"
( 1 cb:new cb:clear 1 cb:write cb:read nip )
1
equal?
"overwrite acts like write on non-full buffer"
( 2 cb:new 1 cb:write 2 cb:overwrite cb:read swap cb:read nip 2 a:close )
[1, 2]
equal?
"overwrite replaces the oldest item on full buffer"
( 2 cb:new 1 cb:write 2 cb:write 3 cb:overwrite cb:read swap cb:read nip 2 a:close )
[2, 3]
equal?
"overwrite replaces the oldest item remaining in buffer following a read"
( 3 cb:new 1 cb:write 2 cb:write 3 cb:write cb:read swap 4 cb:write 5 cb:overwrite cb:read swap cb:read swap cb:read nip 4 a:close )
[1, 3, 4, 5]
equal?
"initial clear does not affect wrapping around"
( 2 cb:new cb:clear 1 cb:write 2 cb:write 3 cb:overwrite 4 cb:overwrite cb:read drop cb:read drop ' cb:read throws? )
true?
end-of-tests
;with