Sha256: c939c7a04855c135053ad9353c8d47c4f5eb783e0ede526899f964ce906337ef

Contents?: true

Size: 1.94 KB

Versions: 105

Compression:

Stored size: 1.94 KB

Contents

function CircularBuffer(capacity) {
  var readPoint = 0;
  var writePoint = 0;
  var buffer = new Array(capacity);

  return {
    read: function () {
      if (isBufferEmpty()) { throw new BufferEmptyException(); }
      var data = buffer[readPoint];
      buffer[readPoint] = null;
      updateReadPoint();
      return data;
    },

    write: function (data) {
      updateBuffer(data, function () {
        if (isBufferFull()) { throw new BufferFullException(); }
        buffer[writePoint] = data;
      });
    },

    forceWrite: function (data) {
      updateBuffer(data, function () {
        if (isBufferFull()) { updateReadPoint(); }
        buffer[writePoint] = data;
      });
    },

    clear: function () {
      readPoint = 0;
      writePoint = 0;
      buffer = new Array(capacity);
    },

    isFull: function () {
      return isBufferFull();
    },

    isEmpty: function () {
      return isBufferEmpty();
    }
  };

  function isBufferEmpty() {
    return buffer.every(isEmpty);
  }

  function isBufferFull() {
    return buffer.filter(isFull).length === capacity;
  }

  function updateBuffer(data, callback) {
    if (isEmpty(data)) { return; }
    callback();
    updateWritePoint();
  }

  function updateWritePoint() {
    writePoint = (writePoint + 1) % capacity;
  }

  function updateReadPoint() {
    readPoint = (readPoint + 1) % capacity;
  }

  function isFull(data) {
    return !isEmpty(data);
  }

  function isEmpty(data) {
    return !data;
  }
}

function BufferEmptyException() {
  this.name = 'BufferEmptyException';
  this.message = 'Buffer is empty.';
}

function BufferFullException() {
  this.name = 'BufferFullException';
  this.message = 'Buffer is full.';
}

module.exports = {
  circularBuffer: function (capacity) {
    return new CircularBuffer(capacity);
  },

  bufferEmptyException: function () {
    return new BufferEmptyException();
  },

  bufferFullException: function () {
    return new BufferFullException();
  }
};

Version data entries

105 entries across 105 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.179 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.178 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.177 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.176 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.175 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.174 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.173 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.172 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.171 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.170 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.169 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.167 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.166 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.165 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.164 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.163 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.162 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.161 tracks/javascript/exercises/circular-buffer/example.js
trackler-2.2.1.160 tracks/javascript/exercises/circular-buffer/example.js