Skip to content

Commit

Permalink
Add Reset capability for stacks
Browse files Browse the repository at this point in the history
  • Loading branch information
JesseCoretta committed Aug 6, 2023
1 parent 9df2477 commit 04ef4f1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,24 @@ func (r *stack) insert(x any, left int) (ok bool) {
return
}

/*
Reset will silently iterate and delete each slice found within
the receiver, leaving it unpopulated but still retaining its
active configuration. Nothing is returned.
*/
func (r Stack) Reset() {
r.stack.reset()
}

/*
reset is a private method called by Stack.Reset.
*/
func (r *stack) reset() {
for i := r.ulen(); i > 0; i-- {
r.remove(i - 1)
}
}

/*
Remove will remove and return the Nth slice from the index,
along with a success-indicative boolean value. A value of
Expand Down
19 changes: 19 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,25 @@ func TestBasic_withCapacity(t *testing.T) {
}
}

func TestReset(t *testing.T) {
b := Basic()
b.Push(
float64(3.14159),
float64(-9.378),
float64(139.104),
)
if b.Len() != 3 {
t.Errorf("%s failed: want '%d', got: '%d' [%s]", t.Name(), 3, b.Len(), b)
}

b.Reset()

if b.Len() != 0 {
sl, _ := b.Index(0)
t.Errorf("%s failed: want '%d', got: '%d' [%#v]", t.Name(), 0, b.Len(), sl)
}
}

func ExampleBasic() {
b := Basic()
b.Push(
Expand Down

0 comments on commit 04ef4f1

Please sign in to comment.