Skip to content

Commit

Permalink
v1.1.12 (#14)
Browse files Browse the repository at this point in the history
1. Improving the usability of the hash heap
2. Fix deque array out of bounds
  • Loading branch information
lxzan authored Oct 10, 2024
1 parent bf2c5b2 commit e373935
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
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

0 comments on commit e373935

Please sign in to comment.