File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,7 +12,14 @@ class Deque {
1212 shift ( ) {
1313 if ( this . _head === this . _tail ) return undefined
1414 const item = this . _store [ this . _head ]
15- delete this . _store [ this . _head ++ ]
15+ this . _store [ this . _head ] = undefined
16+ this . _head ++
17+
18+ if ( this . _head === this . _tail ) {
19+ this . _head = 0
20+ this . _tail = 0
21+ }
22+
1623 return item
1724 }
1825
@@ -27,23 +34,33 @@ class Deque {
2734 }
2835
2936 remove ( item ) {
30- const newStore = Object . create ( null )
31- const newHead = 0
32- let newTail = 0
33- for ( let i = this . _head ; i < this . _tail ; i ++ ) {
34- const current = this . _store [ i ]
37+ if ( this . _head === this . _tail ) return
38+
39+ const store = this . _store
40+ let write = this . _head
41+
42+ for ( let read = this . _head ; read < this . _tail ; read ++ ) {
43+ const current = store [ read ]
3544 if ( current !== item ) {
36- newStore [ newTail ++ ] = current
45+ store [ write ++ ] = current
3746 }
3847 }
39- this . _store = newStore
40- this . _head = newHead
41- this . _tail = newTail
48+
49+ for ( let i = write ; i < this . _tail ; i ++ ) {
50+ store [ i ] = undefined
51+ }
52+
53+ this . _tail = write
54+
55+ if ( this . _head === this . _tail ) {
56+ this . _head = 0
57+ this . _tail = 0
58+ }
4259 }
4360
4461 forEach ( fn ) {
4562 for ( let i = this . _head ; i < this . _tail ; i ++ ) {
46- fn ( this . _store [ i ] , i )
63+ fn ( this . _store [ i ] )
4764 }
4865 }
4966}
You can’t perform that action at this time.
0 commit comments