From a3c30efee2653cc9e7c34fd7a6df2056fe705e4e Mon Sep 17 00:00:00 2001 From: NorthCityChen <449293786@qq.com> Date: Fri, 1 Apr 2022 21:53:04 +0800 Subject: [PATCH] first commit --- app.go | 27 +++++++++++++++++++++++ go.mod | 3 +++ math/math.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++ math/math_test.go | 37 +++++++++++++++++++++++++++++++ queue/queue.go | 50 ++++++++++++++++++++++++++++++++++++++++++ readme.md | 10 +++++++++ stack/stack.go | 49 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 231 insertions(+) create mode 100644 app.go create mode 100644 go.mod create mode 100644 math/math.go create mode 100644 math/math_test.go create mode 100644 queue/queue.go create mode 100644 readme.md create mode 100644 stack/stack.go diff --git a/app.go b/app.go new file mode 100644 index 0000000..754446f --- /dev/null +++ b/app.go @@ -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()) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7354a10 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module stl-go + +go 1.18 diff --git a/math/math.go b/math/math.go new file mode 100644 index 0000000..bcc840c --- /dev/null +++ b/math/math.go @@ -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 diff --git a/math/math_test.go b/math/math_test.go new file mode 100644 index 0000000..582c36a --- /dev/null +++ b/math/math_test.go @@ -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) + } +} diff --git a/queue/queue.go b/queue/queue.go new file mode 100644 index 0000000..68b3148 --- /dev/null +++ b/queue/queue.go @@ -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 +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..b95955b --- /dev/null +++ b/readme.md @@ -0,0 +1,10 @@ + +# stl-go + +一些常用的数据结构模板 \ No newline at end of file diff --git a/stack/stack.go b/stack/stack.go new file mode 100644 index 0000000..de8118f --- /dev/null +++ b/stack/stack.go @@ -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 +}