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

Sledge heap removal takes O(n) #263

Open
emil916 opened this issue Jul 27, 2021 · 0 comments
Open

Sledge heap removal takes O(n) #263

emil916 opened this issue Jul 27, 2021 · 0 comments
Assignees
Labels

Comments

@emil916
Copy link
Contributor

emil916 commented Jul 27, 2021

Currently in Sledge the MinHeap implementation with Priority Queue has the delete API implemented with O(n) time complexity.

priority_queue_delete_nolock(struct priority_queue *self, void *value)
{
assert(self != NULL);
assert(value != NULL);
assert(!listener_thread_is_running());
assert(!self->use_lock || LOCK_IS_LOCKED(&self->lock));
for (int i = 1; i <= self->size; i++) {
if (self->items[i] == value) {
self->items[i] = self->items[self->size];
self->items[self->size--] = NULL;
priority_queue_percolate_down(self, i);
return 0;
}
}
return -1;
}

It can be reduced to O(logn) by keeping the new array elements' indices as within the struct properties and update it on every change. Sample from the composite codebase:
https://github.com/gwsystems/composite/blob/fa9e07f6790bfb73fe8d043538a8e95b87ad5f90/src/components/lib/util/heap.c#L238

Particularly the index update function u() is helpful:
https://github.com/gwsystems/composite/blob/loader/src/components/lib/util/heap.c#L289-L293

@emil916 emil916 self-assigned this Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant