Skip to content

Commit

Permalink
Initial version - Stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
pat42smith committed Aug 28, 2022
0 parents commit ac8d005
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2022 Patrick Smith

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/pat42smith/ds

go 1.19

require github.com/pat42smith/gotest v0.0.1
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/pat42smith/gotest v0.0.1 h1:NaeFsxxbfcZiFkLy8ywx89W9RWKldYW71mln4TOzCzw=
github.com/pat42smith/gotest v0.0.1/go.mod h1:0q1IE4xoC2Nm07gIOXefKF73yrAWBjq90d6RS67dEF8=
45 changes: 45 additions & 0 deletions stack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2022 Patrick Smith
// Use of this source code is subject to the MIT-style license in the LICENSE file.

// Package ds will implement some basic data structures in Go.
//
// For now, only Stack is provided.
package ds

// A Stack is a simple last-in-first-out collection of items.
type Stack[T any] struct {
items []T
}

// Push inserts a value into a Stack.
func (s *Stack[T]) Push(t T) {
s.items = append(s.items, t)
}

// Pop removes the value that was most recently inserted into a Stack, and not yet returned by Pop.
//
// It returns the value that was removed.
// Pop panics when called on an empty Stack.
func (s *Stack[T]) Pop() T {
l := len(s.items)
t := s.items[l-1]
s.items = s.items[:l-1]
return t
}

// Len returns the number of values currently in a Stack.
func (s Stack[T]) Len() int {
return len(s.items)
}

// Empty returns whether a Stack contains no values.
func (s Stack[T]) Empty() bool {
return len(s.items) == 0
}

// PushMany inserts several values into a stack.
//
// The values are inserted in the order given, so they will be returned by Pop in the reverse order.
func (s *Stack[T]) PushMany(ts ...T) {
s.items = append(s.items, ts...)
}
55 changes: 55 additions & 0 deletions stack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2022 Patrick Smith
// Use of this source code is subject to the MIT-style license in the LICENSE file.

// Package ds will implement some basic data structures in Go.
//
// For now, only Stack is provided.
package ds

import (
"testing"

"github.com/pat42smith/gotest"
)

func TestThrees(t *testing.T) {
var s Stack[int]
gotest.Expect(t, 0, s.Len())
gotest.Require(t, s.Empty())

for n := 0; n < 30; n += 3 {
s.Push(n)
gotest.Expect(t, n/3+1, s.Len())
gotest.Require(t, !s.Empty())

s.Push(n + 1)
gotest.Expect(t, n/3+2, s.Len())
gotest.Require(t, !s.Empty())

gotest.Expect(t, n+1, s.Pop())
gotest.Expect(t, n/3+1, s.Len())
gotest.Require(t, !s.Empty())
}

for n := 27; n >= 0; n -= 3 {
gotest.Expect(t, n, s.Pop())
}

gotest.Expect(t, 0, s.Len())
gotest.Require(t, s.Empty())
gotest.MustPanic(t, func() { s.Pop() })
}

func TestPushMany(t *testing.T) {
var s Stack[string]
s.PushMany("alpha", "beta", "gamma", "delta")
s.PushMany()
s.PushMany("un", "deux", "trois")
gotest.Expect(t, "trois", s.Pop())
gotest.Expect(t, "deux", s.Pop())
gotest.Expect(t, "un", s.Pop())
gotest.Expect(t, "delta", s.Pop())
gotest.Expect(t, "gamma", s.Pop())
gotest.Expect(t, "beta", s.Pop())
gotest.Expect(t, "alpha", s.Pop())
}

0 comments on commit ac8d005

Please sign in to comment.