mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-25 17:54:57 +03:00
Generics migration (#237)
* Generics migration This attempts to migrate this library in the least invasive way by preserving as much of the original API as possible. It does not change the tests in a meaningful way nor does it attempt to upgrade any logic that can be simplified or improved with generics. This is purely an API migration, and still requires a lot of additional work to be fully ready. * Fix a few broken tests around serialization * Add v2 suffix * Temporarily change mod name for testing * Rename module to /v2
This commit is contained in:
@@ -11,32 +11,33 @@ package arraystack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emirpasic/gods/lists/arraylist"
|
||||
"github.com/emirpasic/gods/stacks"
|
||||
"strings"
|
||||
|
||||
"github.com/emirpasic/gods/v2/lists/arraylist"
|
||||
"github.com/emirpasic/gods/v2/stacks"
|
||||
)
|
||||
|
||||
// Assert Stack implementation
|
||||
var _ stacks.Stack = (*Stack)(nil)
|
||||
var _ stacks.Stack[int] = (*Stack[int])(nil)
|
||||
|
||||
// Stack holds elements in an array-list
|
||||
type Stack struct {
|
||||
list *arraylist.List
|
||||
type Stack[T comparable] struct {
|
||||
list *arraylist.List[T]
|
||||
}
|
||||
|
||||
// New instantiates a new empty stack
|
||||
func New() *Stack {
|
||||
return &Stack{list: arraylist.New()}
|
||||
func New[T comparable]() *Stack[T] {
|
||||
return &Stack[T]{list: arraylist.New[T]()}
|
||||
}
|
||||
|
||||
// Push adds a value onto the top of the stack
|
||||
func (stack *Stack) Push(value interface{}) {
|
||||
func (stack *Stack[T]) Push(value T) {
|
||||
stack.list.Add(value)
|
||||
}
|
||||
|
||||
// Pop removes top element on stack and returns it, or nil if stack is empty.
|
||||
// Second return parameter is true, unless the stack was empty and there was nothing to pop.
|
||||
func (stack *Stack) Pop() (value interface{}, ok bool) {
|
||||
func (stack *Stack[T]) Pop() (value T, ok bool) {
|
||||
value, ok = stack.list.Get(stack.list.Size() - 1)
|
||||
stack.list.Remove(stack.list.Size() - 1)
|
||||
return
|
||||
@@ -44,29 +45,29 @@ func (stack *Stack) Pop() (value interface{}, ok bool) {
|
||||
|
||||
// Peek returns top element on the stack without removing it, or nil if stack is empty.
|
||||
// Second return parameter is true, unless the stack was empty and there was nothing to peek.
|
||||
func (stack *Stack) Peek() (value interface{}, ok bool) {
|
||||
func (stack *Stack[T]) Peek() (value T, ok bool) {
|
||||
return stack.list.Get(stack.list.Size() - 1)
|
||||
}
|
||||
|
||||
// Empty returns true if stack does not contain any elements.
|
||||
func (stack *Stack) Empty() bool {
|
||||
func (stack *Stack[T]) Empty() bool {
|
||||
return stack.list.Empty()
|
||||
}
|
||||
|
||||
// Size returns number of elements within the stack.
|
||||
func (stack *Stack) Size() int {
|
||||
func (stack *Stack[T]) Size() int {
|
||||
return stack.list.Size()
|
||||
}
|
||||
|
||||
// Clear removes all elements from the stack.
|
||||
func (stack *Stack) Clear() {
|
||||
func (stack *Stack[T]) Clear() {
|
||||
stack.list.Clear()
|
||||
}
|
||||
|
||||
// Values returns all elements in the stack (LIFO order).
|
||||
func (stack *Stack) Values() []interface{} {
|
||||
func (stack *Stack[T]) Values() []T {
|
||||
size := stack.list.Size()
|
||||
elements := make([]interface{}, size, size)
|
||||
elements := make([]T, size, size)
|
||||
for i := 1; i <= size; i++ {
|
||||
elements[size-i], _ = stack.list.Get(i - 1) // in reverse (LIFO)
|
||||
}
|
||||
@@ -74,7 +75,7 @@ func (stack *Stack) Values() []interface{} {
|
||||
}
|
||||
|
||||
// String returns a string representation of container
|
||||
func (stack *Stack) String() string {
|
||||
func (stack *Stack[T]) String() string {
|
||||
str := "ArrayStack\n"
|
||||
values := []string{}
|
||||
for _, value := range stack.list.Values() {
|
||||
@@ -85,6 +86,6 @@ func (stack *Stack) String() string {
|
||||
}
|
||||
|
||||
// Check that the index is within bounds of the list
|
||||
func (stack *Stack) withinRange(index int) bool {
|
||||
func (stack *Stack[T]) withinRange(index int) bool {
|
||||
return index >= 0 && index < stack.list.Size()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user