diff --git a/app.go b/app.go index 015cfbe..b70e2d4 100644 --- a/app.go +++ b/app.go @@ -1,6 +1,6 @@ /* * @Author: NorthCity1984 - * @LastEditTime: 2022-04-26 17:05:06 + * @LastEditTime: 2022-05-03 18:51:46 * @Description: * @Website: https://grimoire.cn * Copyright (c) NorthCity1984 All rights reserved. @@ -10,18 +10,18 @@ package main import ( "fmt" - "gitee.com/NorthCityChen/stl-go/bit" + "gitee.com/NorthCityChen/stl-go/math" ) var a = []int{1, 2, 65, 4, 33, 5} func main() { - ans := a[0] - for i := len(a) - 1; i >= 1; i-- { - a[i] = a[i] - a[i-1] - } - b := bit.Init(a) - fmt.Println(b) - ans = b.Ask(6) + // h := heap.Init(a, heap.MinHeap[int]()) + // fmt.Println(h) + + ans := math.C(8, 2) + fmt.Println(ans) + + ans = math.A(12, 2) fmt.Println(ans) } diff --git a/dequeue/dequeue.go b/dequeue/dequeue.go index 01a789f..b064f77 100644 --- a/dequeue/dequeue.go +++ b/dequeue/dequeue.go @@ -1,6 +1,6 @@ /* * @Author: NorthCity1984 - * @LastEditTime: 2022-04-17 12:04:32 + * @LastEditTime: 2022-04-27 13:31:14 * @Description: * @Website: https://grimoire.cn * Copyright (c) NorthCity1984 All rights reserved. @@ -24,6 +24,9 @@ type dequeueNode[T Number] struct { RPointer *dequeueNode[T] } +// 返回对应类型的零值 对比 T(Rune(0))在获取空字符串的情况下快14倍左右 +func zeroValue[T Number]() (value T) { return } + func Init[T Number]() *Dequeue[T] { return &Dequeue[T]{size: 0, leftHead: new(dequeueNode[T]), rightHead: new(dequeueNode[T])} } @@ -50,7 +53,7 @@ func (q *Dequeue[T]) LPush(val T) bool { func (q *Dequeue[T]) LPop() (T, bool) { if q.IsEmpty() { - return T(rune(0)), false + return zeroValue[T](), false } val := q.leftHead.Val q.leftHead = q.leftHead.RPointer @@ -72,7 +75,7 @@ func (q *Dequeue[T]) RPush(val T) bool { func (q *Dequeue[T]) RPop() (T, bool) { if q.IsEmpty() { - return T(rune(0)), false + return zeroValue[T](), false } val := q.rightHead.Val q.rightHead = q.rightHead.LPointer @@ -82,14 +85,14 @@ func (q *Dequeue[T]) RPop() (T, bool) { func (q *Dequeue[T]) LNode() (T, bool) { if q.IsEmpty() { - return T(rune(0)), false + return zeroValue[T](), false } return q.leftHead.Val, true } func (q *Dequeue[T]) RNode() (T, bool) { if q.IsEmpty() { - return T(rune(0)), false + return zeroValue[T](), false } return q.rightHead.Val, true } diff --git a/dequeue/dequeue_test.go b/dequeue/dequeue_test.go new file mode 100644 index 0000000..a1cfbcd --- /dev/null +++ b/dequeue/dequeue_test.go @@ -0,0 +1,50 @@ +/* + * @Author: NorthCity1984 + * @LastEditTime: 2022-04-27 13:36:25 + * @Description: + * @Website: https://grimoire.cn + * Copyright (c) NorthCity1984 All rights reserved. + */ +package dequeue + +import ( + "testing" + // "gitee.com/NorthCityChen/stl-go/dequeue" +) + +// type Number interface { +// int | int64 | float32 | float64 | string +// } + +// func zeroValue[T Number]() (value T) { return } +func TRune[T Number]() T { return T(rune(0)) } + +func BenchmarkZeroValue(b *testing.B) { + for i := 0; i < b.N; i++ { + zeroValue[float64]() + zeroValue[float32]() + zeroValue[int]() + zeroValue[int64]() + zeroValue[string]() + } +} + +func BenchmarkTRune(b *testing.B) { + for i := 0; i < b.N; i++ { + TRune[float64]() + TRune[float32]() + TRune[int64]() + TRune[int]() + TRune[string]() + } +} + +func BenchmarkRunDequeue(b *testing.B) { + dq := Init[int]() + for i := 0; i < b.N; i++ { + dq.LPush(1) + dq.RPush(2) + dq.LPop() + dq.RPop() + } +} diff --git a/heap/heap.go b/heap/heap.go index 5b50c7f..76ab47a 100644 --- a/heap/heap.go +++ b/heap/heap.go @@ -1,6 +1,6 @@ /* * @Author: NorthCity1984 - * @LastEditTime: 2022-04-26 16:06:24 + * @LastEditTime: 2022-04-27 13:19:36 * @Description: * @Website: https://grimoire.cn * Copyright (c) NorthCity1984 All rights reserved. @@ -17,6 +17,9 @@ type heap[T Number] struct { cmp func(T, T) bool } +// 返回对应类型的零值 +func zeroValue[T Number]() (value T) { return } + func (h *heap[T]) up(x int) { for x > 1 && h.cmp(h.array[x], h.array[x/2]) { h.array[x], h.array[x/2] = h.array[x/2], h.array[x] @@ -46,7 +49,7 @@ func (h *heap[T]) Push(v T) { func (h *heap[T]) Pop() T { if h.len == 0 { - return 0 + return zeroValue[T]() } ret := h.array[1] h.array[1], h.array[h.len] = h.array[h.len], h.array[1] @@ -61,8 +64,10 @@ func (h *heap[T]) Top() T { } func Init[T Number](array []T, cmp func(T, T) bool) heap[T] { + newArray := make([]T, len(array)+1) + copy(newArray[1:], array) h := heap[T]{ - array: append([]T{0}, array...), + array: newArray, len: len(array), cmp: cmp, } @@ -71,3 +76,15 @@ func Init[T Number](array []T, cmp func(T, T) bool) heap[T] { } return h } + +func MaxHeap[T Number]() func(T, T) bool { + return func(t1, t2 T) bool { + return t1 > t2 + } +} + +func MinHeap[T Number]() func(T, T) bool { + return func(t1, t2 T) bool { + return t1 < t2 + } +} diff --git a/math/math.go b/math/math.go index d751958..98dee4c 100644 --- a/math/math.go +++ b/math/math.go @@ -1,6 +1,6 @@ /* * @Author: NorthCity1984 - * @LastEditTime: 2022-04-03 09:32:48 + * @LastEditTime: 2022-05-03 18:50:22 * @Description: * @Website: https://grimoire.cn * Copyright (c) NorthCity1984 All rights reserved. @@ -77,3 +77,21 @@ func Sqrt(x float64) float64 { } return res } + +// 排列数: A(3,1)=3 +func A(n, m int) int { + res := 1 + for i := m; i >= 1; i-- { + res *= n //n × n-1 × n-2 × ... n-m,m就是需要减1的次数 + n-- + } + return res +} + +// 组合数:C(3, 2)=3 +func C(n, m int) int { + if m == 0 { + return 1 + } + return C(n-1, m-1) * (n / m) +}