Miki Tebeka avatar Miki Tebeka committed dd6acc5

Stack as package

Comments (0)

Files changed (4)

+syntax: glob
+
+*.6
+*.out
 avg64
 fact
 fib
 fizzbuzz
+hw
+init
+libcount
+map
+nc
+pp
+stack
 t
-map
-pp
+uniq
 wc
-uniq
-libcount
+zfile
+_testmain.go

stack-package/Makefile

+include $(GOROOT)/src/Make.inc
+
+TARG=stack
+GOFILES=\
+	stack.go\
+
+include $(GOROOT)/src/Make.pkg
+

stack-package/stack.go

+/* Stack (FIFO) implementation.
+
+Example usage:
+	s := stack.New(2)
+	s.Push(1)
+	s.Push(2)
+	fmt.Println(s)
+	i, ok := s.Pop()
+*/
+package stack
+
+import (
+	"fmt"
+)
+
+type Stack struct {
+	size  int
+	count int
+	items []int
+}
+
+/* Create new stack with size `size`. */
+func New(size int) *Stack {
+	return &Stack{
+		size:  size,
+		count: 0,
+		items: make([]int, size),
+	}
+}
+
+/* Check if stack is full. */
+func (stack *Stack) IsFull() bool {
+	return stack.Count() == stack.size
+}
+
+/* Number of items in stack. */
+func (stack *Stack) Count() int {
+	return stack.count
+}
+
+/* Check if stack is empty. */
+func (stack *Stack) IsEmpty() bool {
+	return stack.Count() == 0
+}
+
+/* Push item to stack. */
+func (stack *Stack) Push(item int) bool {
+	if stack.IsFull() {
+		return false
+	}
+
+	stack.items[stack.count] = item
+	stack.count++
+
+	return true
+}
+
+/* Pop item from stack. */
+func (stack *Stack) Pop() (int, bool) {
+	if stack.IsEmpty() {
+		return 0, false
+	}
+
+	item := stack.items[stack.count-1]
+	stack.count--
+
+	return item, true
+}
+
+/* String representation. */
+func (stack *Stack) String() string {
+	return fmt.Sprintf("%v", stack.items[:stack.count])
+}

stack-package/stack_test.go

+package stack
+
+import (
+	"testing"
+)
+
+func TestString(t *testing.T) {
+	s := New(10)
+	if s.String() != "[]" {
+		t.Fatal("Bad string - ", s.String())
+	}
+}
+
+func TestCount(t *testing.T) {
+	s := New(10)
+	if s.Count() != 0 {
+		t.Fatal("Not empty")
+	}
+	s.Push(1)
+	s.Push(2)
+	if s.Count() != 2 {
+		t.Fatal("Bad count, should be 2", s.Count())
+	}
+
+	s.Pop()
+	if s.Count() != 1 {
+		t.Fatal("Bad count, should be 1", s.Count())
+	}
+}
+
+func TestPush(t *testing.T) {
+	s := New(2)
+	if !s.Push(1) {
+		t.Fatal("Bad push")
+	}
+	if !s.Push(2) {
+		t.Fatal("Bad push")
+	}
+	if s.Push(3) {
+		t.Fatal("Over limit")
+	}
+	s.Pop()
+	if !s.Push(3) {
+		t.Fatal("Bad push")
+	}
+	if (s.items[0] != 1) || (s.items[1] != 3) {
+		t.Fatal("Bad state")
+	}
+}
+
+func TestPop(t *testing.T) {
+	s := New(2)
+	s.Push(1)
+	s.Push(2)
+
+	if !s.IsFull() {
+		t.Fatal("Not full")
+	}
+
+	i, ok := s.Pop()
+	if !ok || (i != 2) {
+		t.Fatal("Bad pop")
+	}
+
+	i, ok = s.Pop()
+	if !ok || (i != 1) {
+		t.Fatal("Bad pop")
+	}
+
+	i, ok = s.Pop()
+	if ok {
+		t.Fatal("Empty pop")
+	}
+
+	s.Push(3)
+	i, ok = s.Pop()
+	if !ok || (i != 3) {
+		t.Fatal("Bad pop")
+	}
+
+	if !s.IsEmpty() {
+		t.Fatal("Bad count")
+	}
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.