aboutsummaryrefslogtreecommitdiff
path: root/pkg/stack
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pkg/stack/min.go8
-rw-r--r--pkg/stack/stack.go105
-rw-r--r--pkg/stack/stack_test.go (renamed from pkg/pointer/stack_test.go)24
3 files changed, 129 insertions, 8 deletions
diff --git a/pkg/stack/min.go b/pkg/stack/min.go
new file mode 100644
index 0000000..e97aaed
--- /dev/null
+++ b/pkg/stack/min.go
@@ -0,0 +1,8 @@
+package stack
+
+func min(a, b int) int {
+ if a < b {
+ return a
+ }
+ return b
+}
diff --git a/pkg/stack/stack.go b/pkg/stack/stack.go
new file mode 100644
index 0000000..c307133
--- /dev/null
+++ b/pkg/stack/stack.go
@@ -0,0 +1,105 @@
+package stack
+
+type Stack struct {
+ size int
+ height int
+ data []int
+ next *Stack // Pointer to the next element in the stack stack
+}
+
+func NewStack(size int, next *Stack) *Stack {
+ return &Stack{
+ size: size,
+ height: 0,
+ data: make([]int, size),
+ next: next,
+ }
+}
+
+func (s *Stack) Clear() {
+ s.height = 0
+}
+
+func (s *Stack) Duplicate() {
+ if s.height > 0 {
+ s.Push(s.data[s.height-1])
+ } else {
+ s.Push(0)
+ s.Push(0)
+ }
+}
+
+func (s *Stack) Pop() int {
+ if s.height > 0 {
+ s.height--
+ return s.data[s.height]
+ }
+ return 0
+}
+
+func (s *Stack) Push(value int) {
+ if s.height >= s.size {
+ s.size += 32
+ s.data = append(s.data, make([]int, 32)...)
+ }
+ s.data[s.height] = value
+ s.height++
+}
+
+func (s *Stack) Swap() {
+ a := s.Pop()
+ b := s.Pop()
+ s.Push(a)
+ s.Push(b)
+}
+
+func (s Stack) GetHeights() []int {
+ if s.next != nil {
+ return append(s.next.GetHeights(), s.height)
+ } else {
+ return []int{s.height}
+ }
+}
+
+func (toss *Stack) Transfert(soss *Stack, n int) {
+ // Implements a value transfert between two stacks, intended for use with the '{'
+ // (aka begin) and '}' (aka end) stackstack commands
+ toss.height += n
+ if toss.height > toss.size {
+ toss.data = append(toss.data, make([]int, toss.height-toss.size)...)
+ toss.size = toss.height
+ }
+ for i := 1; i <= min(soss.height, n); i++ {
+ toss.data[toss.height-i] = soss.data[soss.height-i]
+ for i := min(soss.height, n) + 1; i <= n; i++ {
+ toss.data[toss.height-i] = 0
+ }
+ }
+ soss.height -= n
+ if soss.height < 0 {
+ soss.height = 0
+ }
+}
+
+func (s Stack) Next() *Stack {
+ return s.next
+}
+
+func (s *Stack) Discard(n int) {
+ // Implements a discard mechanism intended for use with the '}'(aka end) stackstack command
+ s.height -= n
+ if s.height < 0 {
+ s.height = 0
+ }
+}
+
+func (s *Stack) YCommandPick(n int, h int) {
+ if n > s.height {
+ s.height = 1
+ s.data[0] = 0
+ } else {
+ v := s.data[s.height-n]
+ s.height = h
+ s.Push(v)
+ }
+}
diff --git a/pkg/pointer/stack_test.go b/pkg/stack/stack_test.go
index 35700fd..2f15d3a 100644
--- a/pkg/pointer/stack_test.go
+++ b/pkg/stack/stack_test.go
@@ -1,4 +1,4 @@
-package pointer
+package stack
import (
"testing"
@@ -7,15 +7,15 @@ import (
)
func TestClear(t *testing.T) {
- s := NewStack()
+ s := NewStack(32, nil)
s.Clear()
require.Equal(t, 0, s.height)
}
func TestDupicate(t *testing.T) {
- expected := NewStack()
+ expected := NewStack(32, nil)
expected.height = 2
- s := NewStack()
+ s := NewStack(32, nil)
s.Duplicate()
require.Equal(t, expected.height, s.height)
s.Push(12)
@@ -27,7 +27,7 @@ func TestDupicate(t *testing.T) {
}
func TestPop(t *testing.T) {
- s := NewStack()
+ s := NewStack(32, nil)
v := s.Pop()
require.Equal(t, 0, v)
s.Push(12)
@@ -41,7 +41,7 @@ func TestPop(t *testing.T) {
}
func TestPush(t *testing.T) {
- s := NewStack()
+ s := NewStack(32, nil)
for i := 0; i < 32; i++ {
s.Push(i)
}
@@ -51,8 +51,8 @@ func TestPush(t *testing.T) {
}
func TestSwap(t *testing.T) {
- s := NewStack()
- s2 := NewStack()
+ s := NewStack(32, nil)
+ s2 := NewStack(32, nil)
s.Swap()
s2.Push(0)
s2.Push(0)
@@ -73,3 +73,11 @@ func TestSwap(t *testing.T) {
s.Swap()
require.Equal(t, s2, s)
}
+
+func TestHeights(t *testing.T) {
+ // TODO
+}
+
+func TestTransfert(t *testing.T) {
+ // TODO
+}