Skip to content
This repository has been archived by the owner on Jun 9, 2024. It is now read-only.

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdevbear committed Sep 18, 2023
1 parent 83ce375 commit c690147
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/ds/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,16 @@ func (s *stack[T]) expandIfRequired() {
return
}
newCapacity := max(s.initialCapacity, (s.capacity*resizeRatio)/two)
newBuf := make([]T, newCapacity)
s.buf = append(s.buf, newBuf...)
s.buf = append(s.buf, make([]T, newCapacity)...)
s.capacity *= resizeRatio
}

// shrinkIfRequired shrinks the stack if the size is less than the capacity/resizeRatio.
func (s *stack[T]) shrinkIfRequired() {
if newCap := max(s.initialCapacity, s.capacity/resizeRatio); s.size < newCap {
newBuf := make([]T, newCap)
if newCapacity := max(s.initialCapacity, s.capacity/resizeRatio); s.size < newCapacity {
newBuf := make([]T, newCapacity)
copy(newBuf, s.buf)
s.buf = newBuf
s.capacity = newCap
s.capacity = newCapacity
}
}

0 comments on commit c690147

Please sign in to comment.