Skip to content

Commit

Permalink
add circular-buffer, part of 48 in 24
Browse files Browse the repository at this point in the history
  • Loading branch information
kmarker1101 committed Jun 11, 2024
1 parent 1ec1feb commit de6b742
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "circular-buffer",
"name": "Circular Buffer",
"uuid": "d2eaa3fa-42b3-4287-9b87-714a73ac0c65",
"practices": [],
"prerequisites": [],
"difficulty": 5
}
]
},
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]
```
19 changes: 19 additions & 0 deletions exercises/practice/circular-buffer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"kmarker1101"
],
"files": {
"solution": [
"circular-buffer.el"
],
"test": [
"circular-buffer-test.el"
],
"example": [
".meta/example.el"
]
},
"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"
}
69 changes: 69 additions & 0 deletions exercises/practice/circular-buffer/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
;;; circular-buffer.el --- Circular Buffer (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defclass circular-buffer ()
((capacity :initarg :capacity :initform 10)
(buf-modulus :initform 20)
(data :initform nil)
(front :initform 0)
(back :initform 0))
:documentation "Circular buffer class")

(cl-defmethod initialize-instance :after ((buf circular-buffer) &rest _)
(with-slots (capacity buf-modulus data) buf
(setf buf-modulus (* 2 capacity)
data (make-vector capacity nil))))

(cl-defmethod buf-empty-p ((buf circular-buffer))
(with-slots (front back) buf
(= front back)))

(cl-defmethod buf-full-p ((buf circular-buffer))
(with-slots (front back buf-modulus capacity) buf
(= (mod (- back front) buf-modulus) capacity)))

(cl-defmethod next-spot ((buf circular-buffer) s)
(with-slots (buf-modulus) buf
(mod (1+ s) buf-modulus)))

(cl-defmethod advance-front ((buf circular-buffer))
(with-slots (front) buf
(setf front (next-spot buf front))))

(cl-defmethod advance-back ((buf circular-buffer))
(with-slots (back) buf
(setf back (next-spot buf back))))

(cl-defmethod clear ((buf circular-buffer))
(with-slots (front back) buf
(setf front back)))

(cl-defmethod read-buff ((buf circular-buffer))
(with-slots (front data capacity) buf
(if (buf-empty-p buf)
(error "buffer is empty")
(let ((v (aref data (mod front capacity))))
(advance-front buf)
v))))

(cl-defmethod write ((buf circular-buffer) v)
(with-slots (back data capacity) buf
(if (buf-full-p buf)
(error "buffer is full")
(progn
(aset data (mod back capacity) v)
(advance-back buf)))))

(cl-defmethod overwrite ((buf circular-buffer) v)
(with-slots (front) buf
(when (buf-full-p buf)
(advance-front buf))
(write buf v)))

(provide 'circular-buffer)
;;; circular-buffer.el ends here

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"
130 changes: 130 additions & 0 deletions exercises/practice/circular-buffer/circular-buffer-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
;;; circular-buffer-test.el --- Circular Buffer (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "circular-buffer.el")

(defun create-test-buffer (capacity)
"Helper function to create a circular buffer with the given capacity."
(circular-buffer :capacity capacity))

(ert-deftest reading-empty-buffer-should-fail ()
(let ((buf (create-test-buffer 1)))
(should-error (read-buff buf) :type 'error)))


(ert-deftest can-read-an-item-just-written ()
(let ((buf (create-test-buffer 1)))
(write buf 1)
(should (equal (read-buff buf) 1))))


(ert-deftest each-item-may-only-be-read-once ()
(let ((buf (create-test-buffer 1)))
(write buf 1)
(should (equal (read-buff buf) 1))
(should-error (read-buff buf) :type 'error)))


(ert-deftest items-are-read-in-the-order-they-are-written ()
(let ((buf (create-test-buffer 2)))
(write buf 1)
(write buf 2)
(should (equal (read-buff buf) 1))
(should (equal (read-buff buf) 2))))


(ert-deftest full-buffer-cant-be-written-to ()
(let ((buf (create-test-buffer 1)))
(write buf 1)
(should-error (write buf 2) :type 'error)))


(ert-deftest a-read-frees-up-capacity-for-another-write ()
(let ((buf (create-test-buffer 1)))
(write buf 1)
(should (equal (read-buff buf) 1))
(write buf 2)
(should (equal (read-buff buf) 2))))


(ert-deftest read-position-is-maintained-even-across-multiple-writes ()
(let ((buf (create-test-buffer 3)))
(write buf 1)
(write buf 2)
(should (equal (read-buff buf) 1))
(write buf 3)
(should (equal (read-buff buf) 2))
(should (equal (read-buff buf) 3))))


(ert-deftest items-cleared-out-of-buffer-cant-be-read ()
(let ((buf (create-test-buffer 1)))
(write buf 1)
(clear buf)
(should-error (read-buff buf) :type 'error)))


(ert-deftest clear-frees-up-capacity-for-another-write ()
(let ((buf (create-test-buffer 1)))
(write buf 1)
(clear buf)
(write buf 2)
(should (equal (read-buff buf) 2))))


(ert-deftest clear-does-nothing-on-empty-buffer ()
(let ((buf (create-test-buffer 1)))
(clear buf)
(write buf 1)
(should (equal (read-buff buf) 1))))


(ert-deftest overwrite-acts-like-write-on-non-full-buffer ()
(let ((buf (create-test-buffer 2)))
(write buf 1)
(overwrite buf 2)
(should (equal (read-buff buf) 1))
(should (equal (read-buff buf) 2))))


(ert-deftest overwrite-replaces-the-oldest-item-on-full-buffer ()
(let ((buf (create-test-buffer 2)))
(write buf 1)
(write buf 2)
(overwrite buf 3)
(should (equal (read-buff buf) 2))
(should (equal (read-buff buf) 3))))


(ert-deftest overwrite-replaces-the-oldest-item-remaining-in-buffer-following-a-read ()
(let ((buf (create-test-buffer 3)))
(write buf 1)
(write buf 2)
(write buf 3)
(should (equal (read-buff buf) 1))
(write buf 4)
(overwrite buf 5)
(should (equal (read-buff buf) 3))
(should (equal (read-buff buf) 4))
(should (equal (read-buff buf) 5))))


(ert-deftest initial-clear-does-not-affect-wrapping-around ()
(let ((buf (create-test-buffer 2)))
(clear buf)
(write buf 1)
(write buf 2)
(overwrite buf 3)
(overwrite buf 4)
(should (equal (read-buff buf) 3))
(should (equal (read-buff buf) 4))
(should-error (read-buff buf) :type 'error)))


(provide 'circular-buffer-test)
;;; circular-buffer-test.el ends here

13 changes: 13 additions & 0 deletions exercises/practice/circular-buffer/circular-buffer.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;;; circular-buffer.el --- Circular Buffer (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defclass circular-buffer ()
(error "Delete this S-Expression and write your own implementation"))

(provide 'circular-buffer)
;;; circular-buffer.el ends here

0 comments on commit de6b742

Please sign in to comment.