Skip to content

Commit

Permalink
rename StackArray to Array
Browse files Browse the repository at this point in the history
  • Loading branch information
BulkBeing committed Oct 1, 2022
1 parent d9ca671 commit d85364f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions structure/stack/stack_array.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,54 @@ var ErrStackEmpty = errors.New("stack is empty")
/*
The methods can also be implemented directly on the slice.
```
type StackArray[T any] []T
type Array[T any] []T
```
However, this exposes the underlaying storage (slice) outside the package.
A struct is used instead, so that the underlying storage is not accessible outside the package.
*/

// StackArray is an implementation of stack with slice as underlying storage.
// Array is an implementation of stack with slice as underlying storage.
// ```
// stack := new(StackArray[int])
// stack := stack.NewArray[int]()
// ```
type StackArray[T any] struct {
type Array[T any] struct {
store []T
}

func NewStackArray[T any]() *StackArray[T] {
return new(StackArray[T])
func NewArray[T any]() *Array[T] {
return new(Array[T])
}

// Push inserts a new element to the stack
// Push on a nil stack will panic
func (s *StackArray[T]) Push(val T) {
func (s *Array[T]) Push(val T) {
s.store = append(s.store, val)
}

// Peek the last inserted element without removing it from the stack
// If the stack is empty, ErrStackEmpty error is returned
func (s *StackArray[T]) Peek() (T, error) {
func (s *Array[T]) Peek() (T, error) {
var element T
if s.Empty() {
return element, ErrStackEmpty
}
return s.store[s.Len()-1], nil
}

func (s *StackArray[T]) Len() int {
func (s *Array[T]) Len() int {
if s == nil {
return 0
}
return len(s.store)
}

func (s *StackArray[T]) Empty() bool {
func (s *Array[T]) Empty() bool {
return s.Len() == 0
}

// Pop returns last inserted element and removes it from the underlaying storage
// If the stack is empty, ErrStackEmpty error is returned
func (s *StackArray[T]) Pop() (T, error) {
func (s *Array[T]) Pop() (T, error) {
var element T
if s.Empty() {
return element, ErrStackEmpty
Expand All @@ -73,15 +73,15 @@ func (s *StackArray[T]) Pop() (T, error) {

// Clear removes all elements.
// The allocated capacity remains the same and will be reused for subsequent push operations
func (s *StackArray[T]) Clear() {
func (s *Array[T]) Clear() {
if s == nil {
return
}
s.store = s.store[:0]
}

// Truncate removes all elements and underlaying storage
func (s *StackArray[T]) Truncate() {
func (s *Array[T]) Truncate() {
if s == nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion structure/stack/stack_array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

func Test_StackArray(t *testing.T) {
stack := NewStackArray[int]()
stack := NewArray[int]()
_, err := stack.Peek()
if !errors.Is(err, ErrStackEmpty) {
t.Errorf("Expected error ErrStackEmpty from Peek operation, got %v", err)
Expand Down

0 comments on commit d85364f

Please sign in to comment.