mirror of
https://github.com/duke-git/lancet.git
synced 2026-11-16 05:12:11 +08:00
fix(package): [queue] check membership only in active slots (#390)
This commit is contained in:
1 parent
7c77aa00fb
commit
e2524f4498
3 files changed
+29
-4
No files matched your search
@@ -136,8 +136,8 @@ func (q *ArrayQueue[T]) Clear() {
|
|||||||
|
|
||||||
// Contain checks if the value is in queue or not
|
// Contain checks if the value is in queue or not
|
||||||
func (q *ArrayQueue[T]) Contain(value T) bool {
|
func (q *ArrayQueue[T]) Contain(value T) bool {
|
||||||
for _, v := range q.data {
|
for i := q.head; i < q.tail; i++ {
|
||||||
if reflect.DeepEqual(v, value) {
|
if reflect.DeepEqual(q.data[i], value) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -108,8 +108,8 @@ func (q *CircularQueue[T]) Clear() {
|
|||||||
|
|
||||||
// Contain checks if the value is in queue or not
|
// Contain checks if the value is in queue or not
|
||||||
func (q *CircularQueue[T]) Contain(value T) bool {
|
func (q *CircularQueue[T]) Contain(value T) bool {
|
||||||
for _, v := range q.data {
|
for i := q.front; i != q.rear; i = (i + 1) % q.capacity {
|
||||||
if reflect.DeepEqual(v, value) {
|
if reflect.DeepEqual(q.data[i], value) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package datastructure
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestQueueContainActiveValues(t *testing.T) {
|
||||||
|
array := NewArrayQueue[int](5)
|
||||||
|
array.Enqueue(1)
|
||||||
|
array.Enqueue(2)
|
||||||
|
array.Dequeue()
|
||||||
|
if array.Contain(1) || !array.Contain(2) {
|
||||||
|
t.Error("array queue includes a dequeued value")
|
||||||
|
}
|
||||||
|
circular := NewCircularQueue[int](4)
|
||||||
|
if circular.Contain(0) {
|
||||||
|
t.Error("empty circular queue contains a zero value")
|
||||||
|
}
|
||||||
|
circular.Enqueue(1)
|
||||||
|
circular.Enqueue(2)
|
||||||
|
circular.Dequeue()
|
||||||
|
circular.Enqueue(3)
|
||||||
|
circular.Enqueue(4)
|
||||||
|
if circular.Contain(0) || circular.Contain(1) || !circular.Contain(4) {
|
||||||
|
t.Error("circular queue did not restrict its search to active values")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user