Skip to content

Commit

Permalink
Merge branch 'main' into friends-max-level
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew authored Jul 8, 2024
2 parents d1c98da + ff64eec commit 100599d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
13 changes: 13 additions & 0 deletions pkg/hnsw/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package hnsw

import (
"fmt"
"maps"
"math/bits"
)

Expand Down Expand Up @@ -127,6 +128,18 @@ func NewDistHeap() *DistHeap {
return d
}

func (d *DistHeap) Clone() *DistHeap {
n := &DistHeap{
items: make([]*Item, len(d.items)),
visited: make(map[Id]int, len(d.visited)),
}

copy(n.items, d.items)
maps.Copy(n.visited, d.visited)

return n
}

func (d *DistHeap) PeekMinItem() (*Item, error) {
if d.IsEmpty() {
return nil, EmptyHeapError
Expand Down
33 changes: 32 additions & 1 deletion pkg/hnsw/heap_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package hnsw

import "testing"
import (
"reflect"
"testing"
)

func TestHeap(t *testing.T) {

Expand Down Expand Up @@ -138,6 +141,34 @@ func TestHeap(t *testing.T) {
}
}
})

t.Run("copy", func(t *testing.T) {
m := NewDistHeap()

for i := 0; i <= 10; i++ {
m.Insert(Id(i), float32(10-i))
}

n := m.Clone()

reflect.DeepEqual(m.items, n.items)
reflect.DeepEqual(m.visited, n.visited)

expectedId := Id(10)

for !n.IsEmpty() {
item, err := n.PopMinItem()
if err != nil {
return
}

if item.id != expectedId {
t.Fatalf("expected id to be %v, got %v", expectedId, item.id)
}

expectedId -= 1
}
})
}

func furthestBuildings(heights []int, bricks, ladders int) (int, error) {
Expand Down

0 comments on commit 100599d

Please sign in to comment.