Skip to content
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

faster update #345

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
29 changes: 16 additions & 13 deletions pkg/hnsw/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ var EmptyHeapError = fmt.Errorf("Empty Heap")

type DistHeap struct {
items []*Item
visited map[Id]bool
visited map[Id]int
}

func NewDistHeap() *DistHeap {
d := &DistHeap{
items: make([]*Item, 0),
visited: make(map[Id]bool),
visited: make(map[Id]int),
}
return d
}
Expand Down Expand Up @@ -86,20 +86,19 @@ func (d *DistHeap) PopMaxItem() (*Item, error) {
return d.Pop(), nil
}
func (d *DistHeap) Insert(id Id, dist float32) {
if d.visited[id] {
for idx, item := range d.items {
if item.id == id {
item.dist = dist
d.Fix(idx)
return
}
}
} else {
index, ok := d.visited[id]

if !ok {
d.Push(&Item{id: id, dist: dist})
d.visited[id] = d.Len() - 1
d.up(d.Len() - 1)
d.visited[id] = true
return
}

d.items[index].dist = dist
d.Fix(index)
}

func (d *DistHeap) Fix(i int) {
if !d.down(i, d.Len()) {
d.up(i)
Expand All @@ -109,7 +108,10 @@ func (d *DistHeap) Fix(i int) {
func (d DistHeap) IsEmpty() bool { return len(d.items) == 0 }
func (d DistHeap) Len() int { return len(d.items) }
func (d DistHeap) Less(i, j int) bool { return d.items[i].dist < d.items[j].dist }
func (d DistHeap) Swap(i, j int) { d.items[i], d.items[j] = d.items[j], d.items[i] }
func (d DistHeap) Swap(i, j int) {
d.visited[d.items[i].id], d.visited[d.items[j].id] = j, i
d.items[i], d.items[j] = d.items[j], d.items[i]
}
func (d *DistHeap) Push(x *Item) {
(*d).items = append((*d).items, x)
}
Expand All @@ -118,5 +120,6 @@ func (d *DistHeap) Pop() *Item {
n := len(old)
x := old[n-1]
(*d).items = old[0 : n-1]
delete(d.visited, x.id)
return x
}
Loading