Avatar of brunnock

brunnock's solution

to Circular Buffer in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.

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:

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

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:

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

[D][7][8][9][A][B][C]

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine circular-buffer.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

Wikipedia 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.

circular-buffer.spec.js

var circularBuffer = require('./circular-buffer').circularBuffer;
var bufferEmptyException = require('./circular-buffer').bufferEmptyException;
var bufferFullException = require('./circular-buffer').bufferFullException;

describe('CircularBuffer', function () {
  it('reading an empty buffer throws a BufferEmptyException', function () {
    var buffer = circularBuffer(1);
    expect(buffer.read).toThrow(bufferEmptyException());
  });

  xit('write and read back one item', function () {
    var buffer = circularBuffer(1);
    buffer.write('1');
    expect(buffer.read()).toBe('1');
    expect(buffer.read).toThrow(bufferEmptyException());
  });

  xit('write and read back multiple items', function () {
    var buffer = circularBuffer(2);
    buffer.write('1');
    buffer.write('2');
    expect(buffer.read()).toBe('1');
    expect(buffer.read()).toBe('2');
    expect(buffer.read).toThrow(bufferEmptyException());
  });

  xit('clearing a buffer', function () {
    var buffer = circularBuffer(2);
    buffer.write('1');
    buffer.write('2');
    buffer.clear();
    expect(buffer.read).toThrowError;
    buffer.write('3');
    buffer.write('4');
    expect(buffer.read()).toBe('3');
    expect(buffer.read()).toBe('4');
  });

  xit('alternate write and read', function () {
    var buffer = circularBuffer(2);
    buffer.write('1');
    expect(buffer.read()).toBe('1');
    buffer.write('2');
    expect(buffer.read()).toBe('2');
  });

  xit('reads back oldest item', function () {
    var buffer = circularBuffer(3);
    buffer.write('1');
    buffer.write('2');
    buffer.read();
    buffer.write('3');
    expect(buffer.read()).toBe('2');
    expect(buffer.read()).toBe('3');
  });

  xit('writes of undefined or null don\'t occupy buffer', function () {
    var buffer = circularBuffer(3);
    buffer.write(null);
    buffer.write(undefined); // eslint-disable-line no-undefined
    [1, 2, 3].map(function (i) { buffer.write(i.toString()); });
    expect(buffer.read()).toBe('1');
  });

  xit('writing to a full buffer throws a BufferFullException', function () {
    var buffer = circularBuffer(2);
    buffer.write('1');
    buffer.write('2');
    expect(function () {
      buffer.write('A');
    }).toThrow(bufferFullException());
  });

  xit('forced writes over write oldest item in a full buffer', function () {
    var buffer = circularBuffer(2);
    buffer.write('1');
    buffer.write('2');
    buffer.forceWrite('A');
    expect(buffer.read()).toBe('2');
    expect(buffer.read()).toBe('A');
    expect(buffer.read).toThrow(bufferEmptyException());
  });

  xit('forced writes act like write in a non-full buffer', function () {
    var buffer = circularBuffer(2);
    buffer.write('1');
    buffer.forceWrite('2');
    expect(buffer.read()).toBe('1');
    expect(buffer.read()).toBe('2');
    expect(buffer.read).toThrow(bufferEmptyException());
  });

  xit('alternate force write and read into full buffer', function () {
    var buffer = circularBuffer(5);
    [1, 2, 3].map(function (i) { buffer.write(i.toString()); });
    buffer.read();
    buffer.read();
    buffer.write('4');
    buffer.read();
    [5, 6, 7, 8].map(function (i) { buffer.write(i.toString()); });
    buffer.forceWrite('A');
    buffer.forceWrite('B');
    expect(buffer.read()).toBe('6');
    expect(buffer.read()).toBe('7');
    expect(buffer.read()).toBe('8');
    expect(buffer.read()).toBe('A');
    expect(buffer.read()).toBe('B');
    expect(buffer.read).toThrow(bufferEmptyException());
  });

  xit('multiple buffers don\'t interfere with each other', function () {
    var buffer1 = circularBuffer(1);
    var buffer2 = circularBuffer(1);
    buffer1.write('1');
    expect(buffer2.read).toThrow(bufferEmptyException());
    expect(buffer1.read()).toBe('1');
  });
});
exports.circularBuffer = function(size) {
  return new makeBuffer(size);
}

exports.bufferEmptyException = function () {}
exports.bufferFullException = function () {}
    
function makeBuffer(size) {
  this.buffer = [];
  this.size = size;

  this.read = function() { 
    if (this.buffer.length) {
      return this.buffer.shift();
    } else {
      throw bufferEmptyException;
    }
  }

  this.write = function(x) {
    if (x) {
      if (this.buffer.length < this.size) {
	this.buffer.push(x);
      } else {
	throw bufferFullException;
      }
    }
  }

  this.forceWrite = function(x) {
    if (x) {
      if (this.buffer.length == this.size) this.buffer.shift();
      this.buffer.push(x);
    }
  }

  this.clear = function() {
    this.buffer=[];
  }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?