-
-
Notifications
You must be signed in to change notification settings - Fork 88
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
Add exercise Circular Buffer #664
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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,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] | ||
``` |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"glennj" | ||
], | ||
"files": { | ||
"solution": [ | ||
"circular_buffer.sh" | ||
], | ||
"test": [ | ||
"circular_buffer.bats" | ||
], | ||
"example": [ | ||
".meta/example.sh" | ||
] | ||
}, | ||
"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" | ||
} |
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,102 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Namerefs require bash version 4.3 or greater | ||
bash_version=$((10 * BASH_VERSINFO[0] + BASH_VERSINFO[1])) | ||
if (( bash_version < 43 )); then | ||
echo "This library requires at least bash version 4.3" >&2 | ||
return 4 | ||
fi | ||
|
||
# I'm storing all the data for all circular buffers in a single global | ||
# associative array. | ||
# Empty cells are denoted by empty strings. | ||
# Therefore, an empty string cannot be stored in this circular buffer | ||
|
||
declare -gA _buffer_data=() | ||
|
||
buffer::new() { | ||
local name=$1 | ||
local size=$2 | ||
_buffer_data[size,$name]=$size | ||
buffer::clear "$name" | ||
} | ||
|
||
buffer::destroy() { | ||
local name=$1 | ||
for ((idx = 0; idx < _buffer_data[size,$name]; idx++)); do | ||
unset "_buffer_data[$idx,$name]" | ||
done | ||
unset "_buffer_data[read,$name]" | ||
unset "_buffer_data[write,$name]" | ||
unset "_buffer_data[size,$name]" | ||
} | ||
|
||
buffer::clear() { | ||
local name=$1 | ||
_buffer_data[read,$name]=0 | ||
_buffer_data[write,$name]=0 | ||
for ((idx = 0; idx < _buffer_data[size,$name]; idx++)); do | ||
_buffer_data[$idx,$name]='' | ||
done | ||
} | ||
|
||
buffer::is_empty() { | ||
local name=$1 | ||
local idx=${_buffer_data[read,$name]} | ||
[[ -z ${_buffer_data[$idx,$name]} ]] | ||
} | ||
|
||
buffer::is_full() { | ||
local name=$1 | ||
local idx=${_buffer_data[write,$name]} | ||
[[ -n ${_buffer_data[$idx,$name]} ]] | ||
} | ||
|
||
buffer::read() { | ||
local name=$1 | ||
local -n _read_value=$2 | ||
if buffer::is_empty "$name"; then | ||
return 1 | ||
fi | ||
|
||
local idx=${_buffer_data[read,$name]} | ||
_read_value=${_buffer_data[$idx,$name]} | ||
_buffer_data[$idx,$name]='' | ||
|
||
buffer::incr_pointer "$name" read | ||
} | ||
|
||
buffer::write() { | ||
local overwrite=false | ||
# To use `getopts` in a function, these vars must be local. | ||
local OPTIND OPTARG | ||
while getopts :f opt; do | ||
case $opt in | ||
f) overwrite=true ;; | ||
'?') return 2 ;; | ||
esac | ||
done | ||
shift $((OPTIND - 1)) | ||
|
||
local name=$1 item=$2 | ||
if buffer::is_full "$name"; then | ||
if $overwrite; then | ||
# Read to free up a cell. | ||
# shellcheck disable=SC2034 | ||
local tmp | ||
buffer::read "$name" tmp | ||
else | ||
return 1 | ||
fi | ||
fi | ||
|
||
local idx=${_buffer_data[write,$name]} | ||
_buffer_data[$idx,$name]=$item | ||
|
||
buffer::incr_pointer "$name" write | ||
} | ||
|
||
buffer::incr_pointer() { | ||
local name=$1 p=$2 | ||
_buffer_data[$p,$name]=$(( (_buffer_data[$p,$name] + 1) % _buffer_data[size,$name] )) | ||
} |
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,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" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.