X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=lib%2Fheap.h;h=4f83776f5cb0dff38dbf646a702b23775926ebed;hb=1386442ebc5b681b0ded431880198e3beb6da483;hp=a70fb2cd8c806551fe87b995e013913a3f581e4d;hpb=49ed04e2e93a6a5b01058638224621d5c07db01c;p=libucw.git diff --git a/lib/heap.h b/lib/heap.h index a70fb2cd..4f83776f 100644 --- a/lib/heap.h +++ b/lib/heap.h @@ -1,7 +1,8 @@ /* - * Sherlock Library -- Universal Heap Macros + * UCW Library -- Universal Heap Macros * * (c) 2001 Martin Mares + * (c) 2005 Tomas Valla * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. @@ -10,75 +11,78 @@ #define HEAP_BUBBLE_DOWN_J(heap,num,less,swap) \ for (;;) \ { \ - l = 2*j; \ - if (l > num) \ + _l = 2*_j; \ + if (_l > num) \ break; \ - if (less(heap[j],heap[l]) && (l == num || less(heap[j],heap[l+1]))) \ + if (less(heap[_j],heap[_l]) && (_l == num || less(heap[_j],heap[_l+1]))) \ break; \ - if (l != num && less(heap[l+1],heap[l])) \ - l++; \ - swap(heap,j,l,x); \ - j = l; \ + if (_l != num && less(heap[_l+1],heap[_l])) \ + _l++; \ + swap(heap,_j,_l,x); \ + _j = _l; \ } #define HEAP_BUBBLE_UP_J(heap,num,less,swap) \ - while (j > 1) \ + while (_j > 1) \ { \ - u = j/2; \ - if (less(heap[u], heap[j])) \ + _u = _j/2; \ + if (less(heap[_u], heap[_j])) \ break; \ - swap(heap,u,j,x); \ - j = u; \ + swap(heap,_u,_j,x); \ + _j = _u; \ } #define HEAP_INIT(type,heap,num,less,swap) \ do { \ - uns i = num; \ - uns j, l; \ + uns _i = num; \ + uns _j, _l; \ type x; \ - while (i >= 1) \ + while (_i >= 1) \ { \ - j = i; \ + _j = _i; \ HEAP_BUBBLE_DOWN_J(heap,num,less,swap) \ - i--; \ + _i--; \ } \ } while(0) #define HEAP_DELMIN(type,heap,num,less,swap) \ do { \ - uns j, l; \ + uns _j, _l; \ type x; \ swap(heap,1,num,x); \ num--; \ - j = 1; \ + _j = 1; \ HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \ } while(0) #define HEAP_INSERT(type,heap,num,less,swap) \ do { \ - uns j, u; \ + uns _j, _u; \ type x; \ - j = num; \ + _j = num; \ HEAP_BUBBLE_UP_J(heap,num,less,swap); \ } while(0) -#define HEAP_INCREASE(type,heap,num,less,swap) \ +#define HEAP_INCREASE(type,heap,num,less,swap,pos) \ do { \ - uns j, l; \ + uns _j, _l; \ type x; \ - j = 1; \ + _j = pos; \ HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \ } while(0) #define HEAP_DELETE(type,heap,num,less,swap,pos) \ do { \ - uns j, l, u; \ + uns _j, _l, _u; \ type x; \ - j = pos; \ - swap(heap,j,num,x); \ + _j = pos; \ + swap(heap,_j,num,x); \ num--; \ - if (less(heap[j], heap[num+1])) \ + if (less(heap[_j], heap[num+1])) \ HEAP_BUBBLE_UP_J(heap,num,less,swap) \ else \ HEAP_BUBBLE_DOWN_J(heap,num,less,swap); \ } while(0) + +/* Default swapping macro */ +#define HEAP_SWAP(heap,a,b,t) (t=heap[a], heap[a]=heap[b], heap[b]=t)