mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化循环队列test
This commit is contained in:
@@ -53,8 +53,12 @@ func (s *SQueue[ElementType]) Len() int {
|
||||
s.locker.RLock()
|
||||
defer s.locker.RUnlock()
|
||||
|
||||
return s.len()
|
||||
}
|
||||
|
||||
func (s *SQueue[ElementType]) len() int {
|
||||
if s.head <= s.tail {
|
||||
return s.head - s.tail
|
||||
return s.tail - s.head
|
||||
}
|
||||
|
||||
//(len(s.elements)-1-s.head)+(s.tail+1)
|
||||
@@ -101,12 +105,16 @@ func (s *SQueue[ElementType]) RemoveElement(elementNum int) (removeNum int) {
|
||||
s.locker.Lock()
|
||||
defer s.locker.Unlock()
|
||||
|
||||
for ;s.head == s.tail && removeNum >= elementNum;{
|
||||
removeNum++
|
||||
s.head++
|
||||
s.head = s.head%len(s.elements)
|
||||
lens := s.len()
|
||||
if elementNum > lens{
|
||||
removeNum = lens
|
||||
}else{
|
||||
removeNum = elementNum
|
||||
}
|
||||
|
||||
|
||||
s.head = (s.head + removeNum)%len(s.elements)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ func Test_Example(t *testing.T) {
|
||||
t.Log("is empty :", queue.IsEmpty())
|
||||
t.Log("is full :", queue.IsFull())
|
||||
|
||||
//3.游标使用
|
||||
//3.游标使用,打印所有数据
|
||||
cursor := queue.GetCursor()
|
||||
cursor.First()
|
||||
for {
|
||||
@@ -23,7 +23,7 @@ func Test_Example(t *testing.T) {
|
||||
t.Log("elem:", elem)
|
||||
}
|
||||
|
||||
//4.push数据
|
||||
//4.push数据,塞满队列
|
||||
for i := 0; i < 6; i++ {
|
||||
t.Log("push:", queue.Push(i))
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func Test_Example(t *testing.T) {
|
||||
t.Log("is empty :", queue.IsEmpty())
|
||||
t.Log("is full :", queue.IsFull())
|
||||
|
||||
//5.游标遍历
|
||||
//5.使用游标遍历所有数据
|
||||
cursor.First()
|
||||
for {
|
||||
elem, ret := cursor.Next()
|
||||
@@ -41,7 +41,21 @@ func Test_Example(t *testing.T) {
|
||||
t.Log("elem:", elem)
|
||||
}
|
||||
|
||||
//pop数据所有
|
||||
//6.删除2个元素
|
||||
removeNum := queue.RemoveElement(2)
|
||||
t.Log("Remove Num:", removeNum)
|
||||
|
||||
//7.游标遍历
|
||||
cursor.First()
|
||||
for {
|
||||
elem, ret := cursor.Next()
|
||||
if ret == false {
|
||||
break
|
||||
}
|
||||
t.Log("elem:", elem)
|
||||
}
|
||||
|
||||
//8.pop数据所有
|
||||
for i := 0; i < 6; i++ {
|
||||
elem, ret := queue.Pop()
|
||||
t.Log("pop:", elem, "-", ret, " len:", queue.Len())
|
||||
|
||||
Reference in New Issue
Block a user