Sha256: 7b3561256a3d8691bb4fa96c3eb9aa75b3eb0ca41be68fbd2642b05dca63c4c9

Contents?: true

Size: 1.93 KB

Versions: 106

Compression:

Stored size: 1.93 KB

Contents

# Circular Buffer

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:

    [ ][ ][ ][ ][ ][ ][ ]

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

    [ ][ ][ ][1][ ][ ][ ]

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

    [ ][ ][ ][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:

    [ ][ ][ ][ ][ ][3][ ]

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

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

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

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:

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

Finally, if two elements are now removed then what would be returned is
not 3 & 4 but 5 & 6 because A & B overwrote the 3 & the 4 yielding the
buffer with:

    [ ][7][8][9][A][B][ ]

* * * *

For installation and learning resources, refer to the [exercism help page][].

[exercism help page]: http://exercism.io/languages/lfe

To run the provided tests, you will need `make`.
Open a terminal window and run the following from the exercise directory:

```sh
make test
```

You should now be able to see the results of the test suite for the exercise.

## Source

Wikipedia [http://en.wikipedia.org/wiki/Circular_buffer](http://en.wikipedia.org/wiki/Circular_buffer)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Version data entries

106 entries across 106 versions & 1 rubygems

Version Path
trackler-2.2.1.70 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.69 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.68 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.67 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.66 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.65 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.64 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.63 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.62 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.61 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.60 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.59 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.58 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.57 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.56 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.55 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.54 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.53 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.52 tracks/lfe/exercises/circular-buffer/README.md
trackler-2.2.1.51 tracks/lfe/exercises/circular-buffer/README.md