优化循环队列test

This commit is contained in:
orgin
2022-06-30 10:38:05 +08:00
parent f61fd5d1be
commit ae0ba1d966
2 changed files with 31 additions and 9 deletions

View File

@@ -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
}