Skip to content

Commit

Permalink
Fixing & optimizing FixedDeque & CircularBuffer
Browse files Browse the repository at this point in the history
Fix #223
  • Loading branch information
Yomguithereal committed Feb 2, 2024
1 parent 9119409 commit 13371a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* Fixing `Float64Vector` TS exports (@atombrenner).
* Improving performance of `FixedDeque` `#.push` & `#.pop` methods (@jerome-benoit).
* Fixing some `FixedDeque` & `CircularBuffer` methods.

## 0.39.7

Expand Down
17 changes: 13 additions & 4 deletions circular-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,24 @@ if (typeof Symbol !== 'undefined')
* @return {number} - Returns the new size of the buffer.
*/
CircularBuffer.prototype.push = function(item) {
var index = (this.start + this.size) % this.capacity;
var index = this.start + this.size;

if (index >= this.capacity)
index -= this.capacity;

this.items[index] = item;

// Overwriting?
if (this.size === this.capacity) {

// If start is at the end, we wrap around the buffer
this.start = (index + 1) % this.capacity;
index++;

// Wrapping around?
if (index >= this.capacity) {
this.start = 0;
}
else {
this.start = index;
}

return this.size;
}
Expand Down
14 changes: 7 additions & 7 deletions fixed-deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ FixedDeque.prototype.pop = function() {
if (this.size === 0)
return;

var index = this.start + this.size - 1;
this.size--;

if (index > this.capacity)
index -= this.capacity;
var index = this.start + this.size;

this.size--;
if (index >= this.capacity)
index -= this.capacity;

return this.items[index];
};
Expand Down Expand Up @@ -141,7 +141,7 @@ FixedDeque.prototype.peekLast = function() {

var index = this.start + this.size - 1;

if (index > this.capacity)
if (index >= this.capacity)
index -= this.capacity;

return this.items[index];
Expand All @@ -154,12 +154,12 @@ FixedDeque.prototype.peekLast = function() {
* @return {any}
*/
FixedDeque.prototype.get = function(index) {
if (this.size === 0)
if (this.size === 0 || index >= this.capacity)
return;

index = this.start + index;

if (index > this.capacity)
if (index >= this.capacity)
index -= this.capacity;

return this.items[index];
Expand Down
16 changes: 16 additions & 0 deletions test/circular-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ describe('CircularBuffer', function() {
assert.strictEqual(buffer.get(3), undefined);
});

it('peekLast should not be subject to one-off errors (#223).', function() {
var buffer = new CircularBuffer(Array, 2);

buffer.push(true);
buffer.push(true);
buffer.push(true);

buffer.push(false);
buffer.push(true);

assert.deepStrictEqual(buffer.toArray(), [false, true]);
assert.strictEqual(buffer.peekFirst(), false);
assert.strictEqual(buffer.peekLast(), true);
assert.strictEqual(buffer.get(1), true);
});

it('should be possible to pop the buffer.', function() {
var buffer = new CircularBuffer(Array, 3);

Expand Down

0 comments on commit 13371a6

Please sign in to comment.