Skip to content

v1.1.12 #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions deque/deque.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ func (c *Deque[T]) Reset() {
func (c *Deque[T]) autoReset() {
c.head, c.tail, c.length = Nil, Nil, 0
c.stack = c.stack[:0]
c.elements = c.elements[:1]
if len(c.elements) > 0 {
c.elements = c.elements[:1]
}
}

func (c *Deque[T]) Len() int {
Expand Down Expand Up @@ -276,11 +278,13 @@ func (c *Deque[T]) doRemove(ele *Element[T]) {
}
}

func (c *Deque[T]) Range(f func(ele *Element[T]) bool) {
for i := c.Get(c.head); i != nil; i = c.Get(i.next) {
if !f(i) {
func (c *Deque[T]) Range(f func(index int, ele *Element[T]) bool) {
var index = 0
for iter := c.Get(c.head); iter != nil; iter = c.Get(iter.next) {
if !f(index, iter) {
break
}
index++
}
}

Expand Down
18 changes: 9 additions & 9 deletions deque/deque_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func TestQueue_Range(t *testing.T) {
assert.Equal(t, q.Len(), count)

var b []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
b = append(b, ele.Value())
return len(b) < 100
})
Expand Down Expand Up @@ -165,7 +165,7 @@ func TestQueue_Pop(t *testing.T) {
q.PopFront()

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -189,7 +189,7 @@ func TestDeque_InsertAfter(t *testing.T) {
q.InsertAfter(3, node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -206,7 +206,7 @@ func TestDeque_InsertAfter(t *testing.T) {
q.InsertAfter(3, node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -229,7 +229,7 @@ func TestDeque_InsertBefore(t *testing.T) {
q.InsertBefore(3, node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -246,7 +246,7 @@ func TestDeque_InsertBefore(t *testing.T) {
q.InsertBefore(3, node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -271,7 +271,7 @@ func TestDeque_Delete(t *testing.T) {
q.Remove(node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -287,7 +287,7 @@ func TestDeque_Delete(t *testing.T) {
q.Remove(node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand All @@ -303,7 +303,7 @@ func TestDeque_Delete(t *testing.T) {
q.Remove(node.Addr())

var arr []int
q.Range(func(ele *Element[int]) bool {
q.Range(func(index int, ele *Element[int]) bool {
arr = append(arr, ele.Value())
return true
})
Expand Down
10 changes: 9 additions & 1 deletion heap/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,16 @@ func (c *underlyingHeap[K, V]) down(i, n int) {
}

func (c *underlyingHeap[K, V]) Update(ele *Element[K, V], value V) {
var down = c.lessFunc(ele.value, value)
ele.value = value
var down bool
if ele.index == 0 {
down = true
} else {
var i = ele.index >> 2
var p = c.data[i]
down = c.lessFunc(p.value, ele.value)
}

if down {
c.down(ele.index, c.Len())
} else {
Expand Down
Loading