Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Monotonic Stack #262

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions examples/monotonickstack/monotonickstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
ms "github.com/emirpasic/gods/v2/stacks/monotonicstack"
)

func main() {
stack := ms.New[int](ms.Inc) // empty
stack.Push(3) // 3
stack.Push(1) // 1
stack.Push(4) // 1, 4
stack.Values() // 4, 1 (LIFO order)
stack.Push(1) // 1, 1
stack.Push(5) // 1, 1, 5
_, _ = stack.Peek() // 5, true
_, _ = stack.Pop() // 5, true
_, _ = stack.Peek() // 1, true
stack.Push(9) // 1, 1, 5, 9
stack.Push(2) // 1, 1, 2
stack.Push(6) // 1, 1, 2, 6
_ = stack.Size() // 4
}
70 changes: 70 additions & 0 deletions stacks/monotonicstack/monotonicstack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package monotonicstack

import (
"cmp"

"github.com/emirpasic/gods/v2/stacks"
"github.com/emirpasic/gods/v2/stacks/arraystack"
"github.com/emirpasic/gods/v2/utils"
)

// Assert Stack implementation
var _ stacks.Stack[int] = (*Stack[int])(nil)

type MonoType int

const (
Inc MonoType = iota
Dec = iota
)

// Stack holds elements in an array-list (as embeded type).
type Stack[T comparable] struct {
stacks.Stack[T]
Type MonoType
Comparator utils.Comparator[T] // Key comparator
}

// New instantiates a monotonic stack with stackType (descreasing or icreasing) based on arraystack.
func New[T cmp.Ordered](stackType MonoType) *Stack[T] {
return NewWith(stackType, arraystack.New[T]())
}

// NewWith instantiates a monotonic stack with stackType and the custom stack.
func NewWith[T cmp.Ordered](stackType MonoType, stack stacks.Stack[T]) *Stack[T] {
return &Stack[T]{
Stack: stack,
Type: stackType,
Comparator: cmp.Compare[T],
}
}

// Push adds a value onto the top of the stack (if needed).
func (s *Stack[T]) Push(value T) {
for s.Stack.Size() > 0 {
top, _ := s.Stack.Peek()

poped := false
switch s.Type {
case Dec: // remove top if top < new value
// if a new element is greater than top of stack then we remove
// elements from the top of the stack
if s.Comparator(top, value) == -1 { // "-1" == <
s.Stack.Pop()
poped = true
}
case Inc: // remove top if top > new value
// pop elements from the stack that are smaller than the new element
if s.Comparator(top, value) == 1 { // "1" == >
s.Stack.Pop()
poped = true
}
}

// should stop if nothing to pop
if !poped {
break
}
}
s.Stack.Push(value)
}
47 changes: 47 additions & 0 deletions stacks/monotonicstack/monotonicstack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package monotonicstack

import (
"slices"
"testing"

"github.com/emirpasic/gods/v2/stacks/linkedliststack"
)

func reverse[T any](xs []T) {
for i, j := 0, len(xs)-1; i < j; i, j = i+1, j-1 {
xs[i], xs[j] = xs[j], xs[i]
}
}

func TestMonotonicStack(t *testing.T) {
tests := []struct {
name string
stack *Stack[int]
in []int
want []int
}{
// arraystack
{"ArrayStack, Inc", New[int](Inc), []int{3, 1, 4, 1, 5, 9, 2, 6}, []int{1, 1, 2, 6}},
{"ArrayStack, Inc Stable", New[int](Inc), []int{1, 3, 10, 15, 17}, []int{1, 3, 10, 15, 17}},
{"ArrayStack, Dec", New[int](Dec), []int{3, 1, 4, 1, 5, 9, 2, 6}, []int{9, 6}},
{"ArrayStack, Dec Stable", New[int](Dec), []int{17, 14, 10, 5, 1}, []int{17, 14, 10, 5, 1}},

// linkedliststack
{"ListStack, Inc", NewWith[int](Inc, linkedliststack.New[int]()), []int{3, 1, 4, 1, 5, 9, 2, 6}, []int{1, 1, 2, 6}},
{"ListStack, Dec", NewWith[int](Dec, linkedliststack.New[int]()), []int{3, 1, 4, 1, 5, 9, 2, 6}, []int{9, 6}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

for _, n := range tt.in {
tt.stack.Push(n)
}
got := tt.stack.Values()
reverse(got) // fix LIFO order

if !slices.Equal(got, tt.want) {
t.Errorf("Got %v expected %v", got, tt.want)
}
})
}
}
39 changes: 39 additions & 0 deletions stacks/monotonicstack/serialization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package monotonicstack

import (
"encoding/json"

"github.com/emirpasic/gods/v2/containers"
)

// Assert Serialization implementation
var _ containers.JSONSerializer = (*Stack[int])(nil)
var _ containers.JSONDeserializer = (*Stack[int])(nil)

// ToJSON outputs the JSON representation of the stack.
func (stack *Stack[T]) ToJSON() ([]byte, error) {
return json.Marshal(stack.Stack.Values())
}

// FromJSON populates the stack from the input JSON representation.
func (stack *Stack[T]) FromJSON(data []byte) error {
var elements []T
if err := json.Unmarshal(data, &elements); err != nil {
return err
}
stack.Stack.Clear()
for _, e := range elements {
stack.Stack.Push(e)
}
return nil
}

// UnmarshalJSON @implements json.Unmarshaler
func (stack *Stack[T]) UnmarshalJSON(bytes []byte) error {
return stack.FromJSON(bytes)
}

// MarshalJSON @implements json.Marshaler
func (stack *Stack[T]) MarshalJSON() ([]byte, error) {
return stack.ToJSON()
}