first commit

This commit is contained in:
NorthCityChen 2022-04-01 21:53:04 +08:00
commit a3c30efee2
7 changed files with 231 additions and 0 deletions

27
app.go Normal file
View File

@ -0,0 +1,27 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-01 21:47:35
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package main
import (
"stl-go/queue"
)
func main() {
// s := stack.Init[int]()
// s.Push(12)
// s.Push(13)
// fmt.Println(s.GetTop())
// fmt.Println(s.Pop())
// fmt.Println(s.Pop())
// fmt.Println(s.Pop())
q := queue.Init[int]()
q.Push(12)
q.Push(34)
// fmt.Println(q.Pop())
// fmt.Println(q.Pop())
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module stl-go
go 1.18

55
math/math.go Normal file
View File

@ -0,0 +1,55 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-01 21:04:53
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package math
import "math"
type Number interface {
int | int32 | int64 | float32 | float64
}
func Inf[T Number]() T {
return T(math.Inf(1))
}
func Max[T Number](arg T, args ...T) T {
var maxNum T = arg
for _, val := range args {
if maxNum < val {
maxNum = val
}
}
return maxNum
}
func Min[T Number](arg T, args ...T) T {
var minNum T = arg
for _, val := range args {
if minNum > val {
minNum = val
}
}
return minNum
}
func Sum[T Number](args ...T) (sum T) {
for _, val := range args {
sum += val
}
return sum
}
func Abs[T Number](num T) T {
if num < 0 {
num *= -1
}
return num
}
// sort

37
math/math_test.go Normal file
View File

@ -0,0 +1,37 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-01 20:54:04
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package math_test
import (
"stl-go/math"
"testing"
)
func TestMin(t *testing.T) {
if ans := math.Min(12); ans != 12 {
t.Errorf("min(12) should be 12, not %d", ans)
}
if ans := math.Min(12, 23, 44, 4, 6); ans != 4 {
t.Errorf("min(12, 23, 44, 4, 6) should be 4, not %d", ans)
}
if ans := math.Min[float32](12, 23, 0.5, 88, -1); ans != -1 {
t.Errorf("min(12, 23, 0.5, 88, -1) should be -1, not %f", ans)
}
}
func TestMax(t *testing.T) {
if ans := math.Max(12); ans != 12 {
t.Errorf("max(12) should be 12, not %d", ans)
}
if ans := math.Max(12, 23, 44, 4, 6); ans != 44 {
t.Errorf("max(12, 23, 44, 4, 6) should be 44, not %d", ans)
}
if ans := math.Max[float32](12, 23, 0.5, 88, -1); ans != 88 {
t.Errorf("max(12, 23, 0.5, 88, -1) should be 88, not %f", ans)
}
}

50
queue/queue.go Normal file
View File

@ -0,0 +1,50 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-01 21:46:15
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package queue
type Number interface {
int | int64 | float32 | float64
}
type Queue[T Number] struct {
size int
front *QueueNode[T]
end *QueueNode[T]
}
type QueueNode[T Number] struct {
Val T
Next *QueueNode[T]
}
func Init[T Number]() Queue[T] {
return Queue[T]{size: 0, front: nil, end: nil}
}
func (q *Queue[T]) IsEmpty() bool {
return q.size == 0
}
func (q *Queue[T]) Push(val T) bool {
newNode := QueueNode[T]{Val: val, Next: nil}
q.size++
q.end.Next = &newNode
q.end = &newNode
return true
}
func (q *Queue[T]) GetFront() T { return q.front.Val }
func (q *Queue[T]) Pop() (T, bool) {
if q.IsEmpty() {
return 0, false
}
retVal := q.front.Val
q.front = q.front.Next
return retVal, true
}

10
readme.md Normal file
View File

@ -0,0 +1,10 @@
<!--
* @Author: NorthCity1984
* @LastEditTime: 2022-04-01 13:22:46
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
-->
# stl-go
一些常用的数据结构模板

49
stack/stack.go Normal file
View File

@ -0,0 +1,49 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-01 21:35:59
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package stack
type Number interface {
int | int64 | float32 | float64
}
type Stack[T Number] struct {
size T
top *stackItem[T]
}
type stackItem[T Number] struct {
Val T
Next *stackItem[T]
}
func Init[T Number]() Stack[T] {
return Stack[T]{size: 0, top: nil}
}
func (s *Stack[T]) IsEmpty() bool { return s.size == 0 }
func (s *Stack[T]) Push(val T) bool {
newNode := stackItem[T]{Val: val, Next: s.top}
s.size++
s.top = &newNode
return true
}
func (s *Stack[T]) GetTop() T {
return s.top.Val
}
func (s *Stack[T]) Pop() (T, bool) {
if s.IsEmpty() {
return 0, false
}
retVal := s.top.Val
s.size--
s.top = s.top.Next
return retVal, true
}