This commit is contained in:
NorthCityChen 2022-04-26 14:26:02 +08:00
parent cecb8fe8fb
commit 0ab851e49f
5 changed files with 122 additions and 28 deletions

36
app.go
View File

@ -1,6 +1,6 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-04 16:09:13
* @LastEditTime: 2022-04-26 14:25:09
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
@ -8,28 +8,24 @@
package main
import (
// "githuh"
"fmt"
"gitee.com/NorthCityChen/stl-go/random"
"gitee.com/NorthCityChen/stl-go/heap"
)
var a = []int{1, 2, 65, 4, 5}
func main() {
// // ans := math.Exp2(0)
// ans := math.Sqrt(-8)
// // ans2 := math1.Sqrt(-8)
// fmt.Println(ans)
// // fmt.Println(ans2)
// string
// queue
// queue
// random.RandStringBytesMaskImprSrcSB(5)
// fmt.Println(r)
// ret := randoom.RandStr(45)
fmt.Println(random.RandStr(45))
fmt.Println(random.RandStr(45))
fmt.Println(random.RandStr(45))
fmt.Println(random.RandStr(45))
fmt.Println(random.RandStr(45))
h := heap.Init(a, false)
fmt.Println(h)
h.Push(77)
fmt.Println(h)
fmt.Println(h.Top())
fmt.Println(h.Pop())
fmt.Println(h.Pop())
fmt.Println(h.Pop())
fmt.Println(h.Pop())
fmt.Println(h.Pop())
fmt.Println(h.Pop())
fmt.Println(h.Pop())
}

View File

@ -1,6 +1,6 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-02 12:39:15
* @LastEditTime: 2022-04-17 12:04:32
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
@ -8,7 +8,7 @@
package dequeue
type Number interface {
int | int64 | float32 | float64
int | int64 | float32 | float64 | string
}
// dequeue: Based on Double Linked List
@ -50,7 +50,7 @@ func (q *Dequeue[T]) LPush(val T) bool {
func (q *Dequeue[T]) LPop() (T, bool) {
if q.IsEmpty() {
return 0, false
return T(rune(0)), false
}
val := q.leftHead.Val
q.leftHead = q.leftHead.RPointer
@ -72,7 +72,7 @@ func (q *Dequeue[T]) RPush(val T) bool {
func (q *Dequeue[T]) RPop() (T, bool) {
if q.IsEmpty() {
return 0, false
return T(rune(0)), false
}
val := q.rightHead.Val
q.rightHead = q.rightHead.LPointer
@ -82,14 +82,14 @@ func (q *Dequeue[T]) RPop() (T, bool) {
func (q *Dequeue[T]) LNode() (T, bool) {
if q.IsEmpty() {
return 0, false
return T(rune(0)), false
}
return q.leftHead.Val, true
}
func (q *Dequeue[T]) RNode() (T, bool) {
if q.IsEmpty() {
return 0, false
return T(rune(0)), false
}
return q.rightHead.Val, true
}

98
heap/heap.go Normal file
View File

@ -0,0 +1,98 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-26 14:24:36
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.
*/
package heap
type Number interface {
int | int64 | float32 | float64
}
type Heap[T Number] struct {
array []T
len int
isMax bool
}
func (h *Heap[T]) up(x int) {
if h.isMax {
for x > 1 && h.array[x] > h.array[x/2] {
h.array[x], h.array[x/2] = h.array[x/2], h.array[x]
x /= 2
}
} else {
for x > 1 && h.array[x] < h.array[x/2] {
h.array[x], h.array[x/2] = h.array[x/2], h.array[x]
x /= 2
}
}
}
func (h *Heap[T]) down(x int) {
// fmt.Println(h.len)
if h.isMax {
for x*2 <= h.len {
t := x * 2
if t+1 <= h.len && h.array[t+1] > h.array[t] {
t++
}
if h.array[t] <= h.array[x] {
break
}
h.array[x], h.array[t] = h.array[t], h.array[x]
x = t
}
} else {
for x*2 <= h.len {
t := x * 2
if t+1 <= h.len && h.array[t+1] > h.array[t] {
t++
}
if h.array[t] >= h.array[x] {
break
}
h.array[x], h.array[t] = h.array[t], h.array[x]
x = t
}
}
}
func (h *Heap[T]) Push(v T) {
h.array = append(h.array, v)
h.len++
h.up(h.len)
}
func (h *Heap[T]) Pop() T {
if h.len == 0 {
return 0
}
ret := h.array[1]
h.array[1], h.array[h.len] = h.array[h.len], h.array[1]
h.array = h.array[:h.len]
h.len--
h.down(1)
return ret
}
func (h *Heap[T]) Top() T {
return h.array[1]
}
func Init[T Number](array []T, isMax bool) Heap[T] {
h := Heap[T]{
array: append([]T{0}, array...),
len: len(array),
isMax: isMax,
}
for i := len(h.array) - 1; i >= 1; i-- {
h.down(i)
}
return h
}

View File

@ -1,6 +1,6 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-04 16:11:37
* @LastEditTime: 2022-04-04 19:11:44
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.

View File

@ -1,6 +1,6 @@
/*
* @Author: NorthCity1984
* @LastEditTime: 2022-04-02 13:19:33
* @LastEditTime: 2022-04-13 21:41:20
* @Description:
* @Website: https://grimoire.cn
* Copyright (c) NorthCity1984 All rights reserved.