File tree 2 files changed +16
-4
lines changed
2 files changed +16
-4
lines changed Original file line number Diff line number Diff line change @@ -11,7 +11,9 @@ class QueueNode {
11
11
}
12
12
13
13
enqueue ( item ) {
14
- if ( this . length === this . size ) return false ;
14
+ if ( this . length === this . size || this . writeIndex >= this . size ) {
15
+ return false ;
16
+ }
15
17
this . buffer [ this . writeIndex ++ ] = item ;
16
18
this . length ++ ;
17
19
return true ;
@@ -21,6 +23,10 @@ class QueueNode {
21
23
if ( this . length === 0 ) return null ;
22
24
const item = this . buffer [ this . readIndex ++ ] ;
23
25
this . length -- ;
26
+ if ( this . length === 0 ) {
27
+ this . readIndex = 0 ;
28
+ this . writeIndex = 0 ;
29
+ }
24
30
return item ;
25
31
}
26
32
}
@@ -34,7 +40,7 @@ class UnrolledQueue {
34
40
constructor ( options = { } ) {
35
41
const { nodeSize } = options ;
36
42
if ( nodeSize ) this . #nodeSize = nodeSize ;
37
- const node = new QueueNode ( { size : nodeSize } ) ;
43
+ const node = new QueueNode ( { size : this . # nodeSize } ) ;
38
44
this . #head = node ;
39
45
this . #tail = node ;
40
46
}
Original file line number Diff line number Diff line change @@ -11,7 +11,9 @@ class QueueNode {
11
11
}
12
12
13
13
enqueue ( item ) {
14
- if ( this . length === this . size ) return false ;
14
+ if ( this . length === this . size || this . writeIndex >= this . size ) {
15
+ return false ;
16
+ }
15
17
this . buffer [ this . writeIndex ++ ] = item ;
16
18
this . length ++ ;
17
19
return true ;
@@ -21,6 +23,10 @@ class QueueNode {
21
23
if ( this . length === 0 ) return null ;
22
24
const item = this . buffer [ this . readIndex ++ ] ;
23
25
this . length -- ;
26
+ if ( this . length === 0 ) {
27
+ this . readIndex = 0 ;
28
+ this . writeIndex = 0 ;
29
+ }
24
30
return item ;
25
31
}
26
32
}
@@ -34,7 +40,7 @@ class UnrolledQueue {
34
40
constructor ( options = { } ) {
35
41
const { nodeSize } = options ;
36
42
if ( nodeSize ) this . #nodeSize = nodeSize ;
37
- const node = new QueueNode ( { size : nodeSize } ) ;
43
+ const node = new QueueNode ( { size : this . # nodeSize } ) ;
38
44
this . #head = node ;
39
45
this . #tail = node ;
40
46
}
You can’t perform that action at this time.
0 commit comments