]> mj.ucw.cz Git - libucw.git/commitdiff
Heap: Documented position tracking
authorMartin Mares <mj@ucw.cz>
Tue, 30 Oct 2012 20:38:53 +0000 (21:38 +0100)
committerMartin Mares <mj@ucw.cz>
Tue, 30 Oct 2012 20:38:53 +0000 (21:38 +0100)
ucw/doc/relnotes.txt
ucw/heap.h

index 74f4f781a4f09076ef5cac9658146a5adcb453ea..eb675ea252a7d535e4c18e31e95ed5d544945061 100644 (file)
@@ -10,7 +10,8 @@ WIP
    a proper insert (previously, it was just cleanup after insertion performed
    by the caller), similarly `HEAP_INCREASE` and `HEAP_DECREASE`. The `HEAP_DELMIN`
    operation was renamed to `HEAP_DELETE_MIN`. New operations `HEAP_REPLACE` and
-   `HEAP_REPLACE_MIN` have been added.
+   `HEAP_REPLACE_MIN` have been added. If you need to track positions of elements
+   in the heap, please check the notes at individual functions.
 ** Multiple instances of the configuration parser are supported.
 ** *Incompatible:* As there may be more instances, we can no longer use
    global variables to control the configuration system. In particular,
index ad560af4f5e4dc787c0890e86886e32e6a296db8..74e8a12445c39a266b66a465c8c6d4f7b7d97454 100644 (file)
  *
  * The first element `heap[1]` is always lower or equal to all other elements.
  *
+ * Position tracking
+ * -----------------
+ *
+ * As a heap does not support efficient lookup of an element by value, all functions
+ * acting on existing heap elements need to obtain the position of the element in the
+ * heap. This position has to be tracked by the caller, usually in the supplied swap
+ * callback.
+ *
+ * However, there are some caveats noted in the descriptions of individual functions.
+ *
  * [[macros]]
  * Macros
  * ------
@@ -70,6 +80,8 @@
 /**
  * Shuffle the items `heap[1]`, ..., `heap[num]` to get a valid heap.
  * This operation takes linear time.
+ *
+ * Position tracking: Position of `heap[i]` must be initialized to `i` before calling.
  **/
 #define HEAP_INIT(type,heap,num,less,swap)                                             \
   do {                                                                                 \
@@ -87,6 +99,8 @@
 /**
  * Delete the minimum element `heap[1]` in `O(log(n))` time. The @num variable is decremented.
  * The removed value is moved just after the resulting heap (`heap[num + 1]`).
+ *
+ * Position tracking: Fully automatic.
  **/
 #define HEAP_DELETE_MIN(type,heap,num,less,swap)                                       \
   do {                                                                                 \
 /**
  * Insert a new element @elt to the heap. The @num variable is incremented.
  * This operation takes `O(log(n))` time.
+ *
+ * Position tracking: The position of the new element must be initialized to @num+1
+ * before calling this macro.
  **/
 #define HEAP_INSERT(type,heap,num,less,swap,elt)                                       \
   do {                                                                                 \
 /**
  * Increase `heap[pos]` to a new value @elt (greater or equal to the previous value).
  * The time complexity is `O(log(n))`.
+ *
+ * Position tracking: Fully automatic.
  **/
 #define HEAP_INCREASE(type,heap,num,less,swap,pos,elt)                                 \
   do {                                                                                 \
 /**
  * Decrease `heap[pos]` to a new value @elt (less or equal to the previous value).
  * The time complexity is `O(log(n))`.
+ *
+ * Position tracking: Fully automatic.
  **/
 #define HEAP_DECREASE(type,heap,num,less,swap,pos,elt)                                 \
   do {                                                                                 \
  * Change `heap[pos]` to a new value @elt. The time complexity is `O(log(n))`.
  * If you know that the new value is always smaller or always greater, it is faster
  * to use `HEAP_DECREASE` or `HEAP_INCREASE` respectively.
+ *
+ * Position tracking: Fully automatic.
  **/
 #define HEAP_REPLACE(type,heap,num,less,swap,pos,elt)                                  \
   do {                                                                                 \
 
 /**
  * Replace the minimum `heap[pos]` by a new value @elt. The time complexity is `O(log(n))`.
+ *
+ * Position tracking: Fully automatic.
  **/
 #define HEAP_REPLACE_MIN(type,heap,num,less,swap,elt)                                  \
   HEAP_INCREASE(type,heap,num,less,swap,1,elt)
 /**
  * Delete an arbitrary element, given by its position. The @num variable is decremented.
  * The operation takes `O(log(n))` time.
+ *
+ * Position tracking: Fully automatic.
  **/
 #define HEAP_DELETE(type,heap,num,less,swap,pos)                                       \
   do {                                                                                 \