Yomguithereal / mnemonist

Curated collection of data structures for the JavaScript/TypeScript language.
https://yomguithereal.github.io/mnemonist
MIT License
2.26k stars 92 forks source link

CircularBuffer peekLast undefined #223

Closed magicdawn closed 8 months ago

magicdawn commented 8 months ago

using package v0.39.7

const q = new CircularBuffer<boolean>(Array, 2)
{
  q.push(true)
  q.push(true)
  q.push(true)
  // q.push(true)
}
q.push(false)
q.push(true)
console.log(q.toArray()) // [false, true]
console.log(q.peekFirst()) // false
console.log(q.peekLast()) // undefined

const q = new CircularBuffer<boolean>(Array, 2)
{
  q.push(true)
  q.push(true)
  q.push(true)
  q.push(true) // <--- this line is uncommented
}
q.push(false)
q.push(true)
console.log(q.toArray()) // [false, true]
console.log(q.peekFirst()) // false
console.log(q.peekLast()) // true

Maybe https://github.com/Yomguithereal/mnemonist/blob/0.39.7/fixed-deque.js#L138-L139 should be ?

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

or I got something misunderstanding ?

Yomguithereal commented 8 months ago

You are right indeed. I fixed some other stuff related to FixedDeque & CircularBuffer. This is now available in v0.39.8, thanks @magicdawn.