Struct
IdeHeap
Description
struct IdeHeap {
char* data;
gsize len;
}
Heaps are similar to a partially sorted tree but implemented as an array. They allow for efficient O(1) lookup of the highest priority item as it will always be the first item of the array.
To create a new heap use ide_heap_new().
To add items to the heap, use ide_heap_insert_val()
or
ide_heap_insert_vals()
to insert in bulk.
To access an item in the heap, use ide_heap_index().
To remove an arbitrary item from the heap, use ide_heap_extract_index().
To remove the highest priority item in the heap, use ide_heap_extract().
To free a heap, use ide_heap_unref().
Here is an example that stores integers in a IdeHeap
:
static int
cmpint (gconstpointer a,
gconstpointer b)
{
return *(const gint *)a - *(const gint *)b;
}
int
main (gint argc,
gchar *argv[])
{
IdeHeap *heap;
gint i;
gint v;
heap = ide_heap_new (sizeof (gint), cmpint);
for (i = 0; i < 10000; i++)
ide_heap_insert_val (heap, i);
for (i = 0; i < 10000; i++)
ide_heap_extract (heap, &v);
ide_heap_unref (heap);
}
Structure members
data
- No description available.
len
- No description available.
Constructors
ide_heap_new
Creates a new IdeHeap
. A heap is a tree-like structure stored in
an array that is not fully sorted, but head is guaranteed to be either
the max, or min value based on compare_func
. This is also known as
a priority queue.
Instance methods
ide_heap_unref
Decrements the reference count of heap
by one, freeing the structure
when the reference count reaches zero.