GTS Library Reference Manual |
---|
#include <gts.h> GtsEHeapPair;gdouble (*GtsKeyFunc) (gpointer item,gpointer data); GtsEHeap; GtsEHeap* gts_eheap_new (GtsKeyFunc key_func,gpointer data); GtsEHeapPair* gts_eheap_insert (GtsEHeap *heap,gpointer p); GtsEHeapPair* gts_eheap_insert_with_key (GtsEHeap *heap,gpointer p,gdouble key);gpointer gts_eheap_top (GtsEHeap *heap,gdouble *key);gpointer gts_eheap_remove_top (GtsEHeap *heap,gdouble *key);gpointer gts_eheap_remove (GtsEHeap *heap, GtsEHeapPair *p);void gts_eheap_decrease_key (GtsEHeap *heap, GtsEHeapPair *p,gdouble new_key);gdouble gts_eheap_key (GtsEHeap *heap,gpointer p);void gts_eheap_randomized (GtsEHeap *heap,gboolean randomized);void gts_eheap_update (GtsEHeap *heap);void gts_eheap_freeze (GtsEHeap *heap);void gts_eheap_thaw (GtsEHeap *heap);void gts_eheap_foreach (GtsEHeap *heap,GFunc func,gpointer data);guint gts_eheap_size (GtsEHeap *heap);void gts_eheap_destroy (GtsEHeap *heap);
This data structure is similar to the binary heap implementation but adds the two operations gts_eheap_decrease_key()
and gts_eheap_remove()
. Contrary to the binary heap implementation, keys are stored in a GtsEHeapPair structure and comparisons between keys are performed directly (thus saving a call to a comparison function). This structure consequently provides generally faster operations at the expense of memory use. If your comparison function is simple and you don't need the extra functionalities, it is usually better to use a GtsHeap.
typedef struct { gpointer data; gdouble key; guint pos; } GtsEHeapPair;
The extended heap structure.
data ; | Points to the item stored in the heap. |
key ; | Value of the key for this item. |
pos ; | Private field. |
gdouble (*GtsKeyFunc) (gpointer item,gpointer data);
item : | A pointer to an item to be stored in the heap. |
data : | User data passed to |
Returns : | the value of the key for the given item. |
typedef struct _GtsEHeap GtsEHeap;
An opaque data structure describing an extended binary heap.
GtsEHeap* gts_eheap_new (GtsKeyFunc key_func,gpointer data);
key_func : | a GtsKeyFunc or NULL. |
data : | user data to be passed to |
Returns : | a new GtsEHeap using |
GtsEHeapPair* gts_eheap_insert (GtsEHeap *heap,gpointer p);
Inserts a new element p
in the heap.
heap : | a GtsEHeap. |
p : | a pointer to add to the heap. |
Returns : | a GtsEHeapPair describing the position of the element in the heap.
This pointer is necessary for |
GtsEHeapPair* gts_eheap_insert_with_key (GtsEHeap *heap,gpointer p,gdouble key);
Inserts a new element p
in the heap.
heap : | a GtsEHeap. |
p : | a pointer to add to the heap. |
key : | the value of the key associated to |
Returns : | a GtsEHeapPair describing the position of the element in the heap.
This pointer is necessary for |
gpointer gts_eheap_top (GtsEHeap *heap,gdouble *key);
heap : | a GtsEHeap. |
key : | a pointer on a gdouble or NULL. |
Returns : | the element at the top of the heap and optionally (if |
gpointer gts_eheap_remove_top (GtsEHeap *heap,gdouble *key);
Removes the element at the top of the heap and optionally (if key
is not
NULL) returns the value of its key.
heap : | a GtsEHeap. |
key : | a pointer on a gdouble or NULL. |
Returns : | the element at the top of the heap. |
gpointer gts_eheap_remove (GtsEHeap *heap, GtsEHeapPair *p);
Removes element corresponding to p
from heap
in O(log n).
heap : | a GtsEHeap. |
p : | a GtsEHeapPair. |
Returns : | the element just removed from |
void gts_eheap_decrease_key (GtsEHeap *heap, GtsEHeapPair *p,gdouble new_key);
Decreases the value of the key of the element at position p
.
heap : | a GtsEHeap. |
p : | a GtsEHeapPair. |
new_key : | the new value of the key for this element. Must be smaller than the current key. |
gdouble gts_eheap_key (GtsEHeap *heap,gpointer p);
heap : | a GtsEHeap. |
p : | a pointer to be tested; |
Returns : | the value of the key for pointer |
void gts_eheap_randomized (GtsEHeap *heap,gboolean randomized);
heap : | a GtsEHeap. |
randomized : | whether |
void gts_eheap_update (GtsEHeap *heap);
Updates the key of each element of heap
and reorders it.
heap : | a GtsEHeap. |
void gts_eheap_freeze (GtsEHeap *heap);
Freezes the heap. Any subsequent operation will not preserve the heap
property. Used in conjunction with gts_eheap_insert()
and gts_eheap_thaw()
to create a heap in O(n) time.
heap : | a GtsEHeap. |
void gts_eheap_thaw (GtsEHeap *heap);
If heap
has been frozen previously using gts_eheap_freeze()
, reorder it
in O(n) time and unfreeze it.
heap : | a GtsEHeap. |
void gts_eheap_foreach (GtsEHeap *heap,GFunc func,gpointer data);
heap : | a GtsEHeap. |
func : | the function to call for each element in the heap. |
data : | to pass to |
guint gts_eheap_size (GtsEHeap *heap);
heap : | a GtsEHeap. |
Returns : | the number of items in |
<<< Binary heaps | First In First Out heaps >>> |