Skip to content

Commit 13371a6

Browse files
committed
Fixing & optimizing FixedDeque & CircularBuffer
Fix #223
1 parent 9119409 commit 13371a6

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

89
## 0.39.7
910

circular-buffer.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,24 @@ if (typeof Symbol !== 'undefined')
4545
* @return {number} - Returns the new size of the buffer.
4646
*/
4747
CircularBuffer.prototype.push = function(item) {
48-
var index = (this.start + this.size) % this.capacity;
48+
var index = this.start + this.size;
49+
50+
if (index >= this.capacity)
51+
index -= this.capacity;
4952

5053
this.items[index] = item;
5154

5255
// Overwriting?
5356
if (this.size === this.capacity) {
54-
55-
// If start is at the end, we wrap around the buffer
56-
this.start = (index + 1) % this.capacity;
57+
index++;
58+
59+
// Wrapping around?
60+
if (index >= this.capacity) {
61+
this.start = 0;
62+
}
63+
else {
64+
this.start = index;
65+
}
5766

5867
return this.size;
5968
}

fixed-deque.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ FixedDeque.prototype.pop = function() {
8888
if (this.size === 0)
8989
return;
9090

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

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

96-
this.size--;
95+
if (index >= this.capacity)
96+
index -= this.capacity;
9797

9898
return this.items[index];
9999
};
@@ -141,7 +141,7 @@ FixedDeque.prototype.peekLast = function() {
141141

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

144-
if (index > this.capacity)
144+
if (index >= this.capacity)
145145
index -= this.capacity;
146146

147147
return this.items[index];
@@ -154,12 +154,12 @@ FixedDeque.prototype.peekLast = function() {
154154
* @return {any}
155155
*/
156156
FixedDeque.prototype.get = function(index) {
157-
if (this.size === 0)
157+
if (this.size === 0 || index >= this.capacity)
158158
return;
159159

160160
index = this.start + index;
161161

162-
if (index > this.capacity)
162+
if (index >= this.capacity)
163163
index -= this.capacity;
164164

165165
return this.items[index];

test/circular-buffer.js

+16
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,22 @@ describe('CircularBuffer', function() {
126126
assert.strictEqual(buffer.get(3), undefined);
127127
});
128128

129+
it('peekLast should not be subject to one-off errors (#223).', function() {
130+
var buffer = new CircularBuffer(Array, 2);
131+
132+
buffer.push(true);
133+
buffer.push(true);
134+
buffer.push(true);
135+
136+
buffer.push(false);
137+
buffer.push(true);
138+
139+
assert.deepStrictEqual(buffer.toArray(), [false, true]);
140+
assert.strictEqual(buffer.peekFirst(), false);
141+
assert.strictEqual(buffer.peekLast(), true);
142+
assert.strictEqual(buffer.get(1), true);
143+
});
144+
129145
it('should be possible to pop the buffer.', function() {
130146
var buffer = new CircularBuffer(Array, 3);
131147

0 commit comments

Comments
 (0)