File tree 4 files changed +37
-11
lines changed
4 files changed +37
-11
lines changed Original file line number Diff line number Diff line change 4
4
5
5
* Fixing ` Float64Vector ` TS exports (@atombrenner ).
6
6
* Improving performance of ` FixedDeque ` ` #.push ` & ` #.pop ` methods (@jerome-benoit ).
7
+ * Fixing some ` FixedDeque ` & ` CircularBuffer ` methods.
7
8
8
9
## 0.39.7
9
10
Original file line number Diff line number Diff line change @@ -45,15 +45,24 @@ if (typeof Symbol !== 'undefined')
45
45
* @return {number } - Returns the new size of the buffer.
46
46
*/
47
47
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 ;
49
52
50
53
this . items [ index ] = item ;
51
54
52
55
// Overwriting?
53
56
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
+ }
57
66
58
67
return this . size ;
59
68
}
Original file line number Diff line number Diff line change @@ -88,12 +88,12 @@ FixedDeque.prototype.pop = function() {
88
88
if ( this . size === 0 )
89
89
return ;
90
90
91
- var index = this . start + this . size - 1 ;
91
+ this . size -- ;
92
92
93
- if ( index > this . capacity )
94
- index -= this . capacity ;
93
+ var index = this . start + this . size ;
95
94
96
- this . size -- ;
95
+ if ( index >= this . capacity )
96
+ index -= this . capacity ;
97
97
98
98
return this . items [ index ] ;
99
99
} ;
@@ -141,7 +141,7 @@ FixedDeque.prototype.peekLast = function() {
141
141
142
142
var index = this . start + this . size - 1 ;
143
143
144
- if ( index > this . capacity )
144
+ if ( index >= this . capacity )
145
145
index -= this . capacity ;
146
146
147
147
return this . items [ index ] ;
@@ -154,12 +154,12 @@ FixedDeque.prototype.peekLast = function() {
154
154
* @return {any }
155
155
*/
156
156
FixedDeque . prototype . get = function ( index ) {
157
- if ( this . size === 0 )
157
+ if ( this . size === 0 || index >= this . capacity )
158
158
return ;
159
159
160
160
index = this . start + index ;
161
161
162
- if ( index > this . capacity )
162
+ if ( index >= this . capacity )
163
163
index -= this . capacity ;
164
164
165
165
return this . items [ index ] ;
Original file line number Diff line number Diff line change @@ -126,6 +126,22 @@ describe('CircularBuffer', function() {
126
126
assert . strictEqual ( buffer . get ( 3 ) , undefined ) ;
127
127
} ) ;
128
128
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
+
129
145
it ( 'should be possible to pop the buffer.' , function ( ) {
130
146
var buffer = new CircularBuffer ( Array , 3 ) ;
131
147
You can’t perform that action at this time.
0 commit comments