LRUCache.prototype.shift not working properly
hilbix opened this issue · comments
LRUCache.prototype.shift()
did not work correctly for me. So I hacked it to call .remove
, and now it works, as long as only unique keys are in the LRU.
See hilbix@a502469
Not sending a pull-request as this probably is not the right fix, as a core function like .shift()
cannot depend on optional function like .remove()
.
Problems with .shift()
, at least:
- It forgets to update the
.size
. - And it does not handle
.tail
Another note why this is not the correct fix:
The LRU is able to keep several identical values. So following tests must not fail:
var c = new LRUCache(4);
c.put(0,1);
c.put(0,2);
c.put(0,3);
c.put(0,4);
assert.equal(c.size, 4);
assert.deepEqual(c.shift(), {key:0, value:4});
assert.deepEqual(c.shift(), {key:0, value:3});
assert.deepEqual(c.shift(), {key:0, value:2});
assert.deepEqual(c.shift(), {key:0, value:1});
assert.equal(c.size, 0); // check .size correct
c.forEach(function(){assert(false)}, undefined, true); // check .tail correct
(Note: Not tested, just typed here for clarity.)