Skip to content

Commit

Permalink
Merge branch 'master' into linkedlist2deque
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérôme Benoit authored Feb 2, 2024
2 parents 507288e + 346ff4a commit 36f03f3
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Changelog

## Unreleased
## 0.39.8

* Adding ESM named exports support (@jerome-benoit).
* Fixing `Set` operations CommonJS named export collision by renaming it to `set` (@jerome-benoit).
* Fixing missing `Uint8Vector`, `Uint8ClampedVector`, `Int8Vector`, `Uint16Vector`, `Int16Vector`, `Uint32Vector`, `Int32Vector`, `Float32Vector`, `Float64Vector`, `PointerVector` CommonJS named exports (@jerome-benoit).
* Fixing missing `PointerVector` TS exports (@jerome-benoit).
* 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
18 changes: 12 additions & 6 deletions fixed-deque.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ FixedDeque.prototype.push = function(item) {
if (this.size === this.capacity)
throw new Error('mnemonist/fixed-deque.push: deque capacity (' + this.capacity + ') exceeded!');

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;

Expand Down Expand Up @@ -85,10 +88,13 @@ FixedDeque.prototype.pop = function() {
if (this.size === 0)
return;

const index = (this.start + this.size - 1) % this.capacity;

this.size--;

var index = this.start + this.size;

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

return this.items[index];
};

Expand Down Expand Up @@ -135,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 @@ -148,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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mnemonist",
"version": "0.39.7",
"version": "0.39.8",
"description": "Curated collection of data structures for the JavaScript/TypeScript.",
"scripts": {
"lint": "eslint --cache --ext .js,.mjs ./*.js ./*.mjs ./utils ./test",
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 36f03f3

Please sign in to comment.