Skip to content

Commit

Permalink
feat: from base queue (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew authored Jun 3, 2024
1 parent c0a23a4 commit 667ee83
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
10 changes: 10 additions & 0 deletions pkg/hnsw/pq.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,13 @@ func (bq *BaseQueue) update(item *Item, id Id, dist float32) {
item.dist = dist
heap.Fix(bq, item.index)
}

func FromBaseQueue(bq *BaseQueue, comparator Comparator) *BaseQueue {
newBq := NewBaseQueue(comparator)

for _, item := range bq.items {
newBq.Insert(item.id, item.dist)
}

return newBq
}
44 changes: 22 additions & 22 deletions pkg/hnsw/pq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,29 @@ func TestPQ(t *testing.T) {
t.Errorf("got %d, want %d", res, c.expected)
}
}
})

t.Run("interchange", func(t *testing.T) {
bq := NewBaseQueue(MinComparator{})
for i := 0; i < 100; i++ {
bq.Insert(Id(i), float32(i))
}

incBq := FromBaseQueue(bq, MaxComparator{})

i := Id(99)
for !incBq.IsEmpty() {
item, err := incBq.Peel()
if err != nil {
t.Fatal(err)
}

if item.id != i {
t.Fatalf("got %d, want %d", item.id, i)
}

i -= 1
}
})
}

Expand Down Expand Up @@ -81,25 +103,3 @@ func furthestBuildings(heights []int, bricks, ladders int) (int, error) {

return len(heights) - 1, nil
}

/*
*/

0 comments on commit 667ee83

Please sign in to comment.