81 #include <config_auto.h> 85 #include "allheaders.h" 88 static const l_uint32 MaxPtrArraySize = 100000;
91 #define SWAP_ITEMS(i, j) { void *tempitem = lh->array[(i)]; \ 92 lh->array[(i)] = lh->array[(j)]; \ 93 lh->array[(j)] = tempitem; } 117 PROCNAME(
"lheapCreate");
119 if (n < InitialPtrArraySize || n > MaxPtrArraySize)
124 if ((lh->
array = (
void **)LEPT_CALLOC(n,
sizeof(
void *))) == NULL) {
126 return (
L_HEAP *)ERROR_PTR(
"ptr array not made", procName, NULL);
160 PROCNAME(
"lheapDestroy");
163 L_WARNING(
"ptr address is NULL\n", procName);
166 if ((lh = *plh) == NULL)
170 for (i = 0; i < lh->
n; i++)
171 LEPT_FREE(lh->
array[i]);
172 }
else if (lh->
n > 0) {
173 L_WARNING(
"memory leak of %d items in lheap!\n", procName, lh->
n);
177 LEPT_FREE(lh->
array);
196 PROCNAME(
"lheapAdd");
199 return ERROR_INT(
"lh not defined", procName, 1);
201 return ERROR_INT(
"item not defined", procName, 1);
206 return ERROR_INT(
"extension failed", procName, 1);
228 PROCNAME(
"lheapExtendArray");
231 return ERROR_INT(
"lh not defined", procName, 1);
234 sizeof(
void *) * lh->
nalloc,
235 2 *
sizeof(
void *) * lh->
nalloc)) == NULL)
236 return ERROR_INT(
"new ptr array not returned", procName, 1);
255 PROCNAME(
"lheapRemove");
258 return (
void *)ERROR_PTR(
"lh not defined", procName, NULL);
265 lh->
array[lh->
n - 1] = NULL;
285 PROCNAME(
"lheapGetCount");
288 return ERROR_INT(
"lh not defined", procName, 0);
314 PROCNAME(
"lheapGetElement");
317 return ERROR_PTR(
"lh not defined", procName, NULL);
318 if (index < 0 || index >= lh->
n)
319 return ERROR_PTR(
"invalid index", procName, NULL);
321 return (
void *)lh->
array[index];
345 PROCNAME(
"lheapSort");
348 return ERROR_INT(
"lh not defined", procName, 1);
350 for (i = 0; i < lh->
n; i++)
377 l_int32 i, index, size;
379 PROCNAME(
"lheapSortStrictOrder");
382 return ERROR_INT(
"lh not defined", procName, 1);
388 for (i = 0; i < size; i++) {
390 SWAP_ITEMS(0, index - 1);
396 for (i = 0; i < size / 2; i++)
397 SWAP_ITEMS(i, size - i - 1);
429 l_float32 valp, valc;
431 PROCNAME(
"lheapSwapUp");
434 return ERROR_INT(
"lh not defined", procName, 1);
435 if (index < 0 || index >= lh->
n)
436 return ERROR_INT(
"invalid index", procName, 1);
444 valc = *(l_float32 *)(lh->
array[ic - 1]);
445 valp = *(l_float32 *)(lh->
array[ip - 1]);
448 SWAP_ITEMS(ip - 1, ic - 1);
456 valc = *(l_float32 *)(lh->
array[ic - 1]);
457 valp = *(l_float32 *)(lh->
array[ip - 1]);
460 SWAP_ITEMS(ip - 1, ic - 1);
494 l_float32 valp, valcl, valcr;
496 PROCNAME(
"lheapSwapDown");
499 return ERROR_INT(
"lh not defined", procName, 1);
509 valp = *(l_float32 *)(lh->
array[ip - 1]);
510 valcl = *(l_float32 *)(lh->
array[icl - 1]);
514 SWAP_ITEMS(ip - 1, icl - 1);
517 valcr = *(l_float32 *)(lh->
array[icr - 1]);
518 if (valp <= valcl && valp <= valcr)
520 if (valcl <= valcr) {
521 SWAP_ITEMS(ip - 1, icl - 1);
524 SWAP_ITEMS(ip - 1, icr - 1);
534 valp = *(l_float32 *)(lh->
array[ip - 1]);
535 valcl = *(l_float32 *)(lh->
array[icl - 1]);
539 SWAP_ITEMS(ip - 1, icl - 1);
542 valcr = *(l_float32 *)(lh->
array[icr - 1]);
543 if (valp >= valcl && valp >= valcr)
545 if (valcl >= valcr) {
546 SWAP_ITEMS(ip - 1, icl - 1);
549 SWAP_ITEMS(ip - 1, icr - 1);
576 PROCNAME(
"lheapPrint");
579 return ERROR_INT(
"stream not defined", procName, 1);
581 return ERROR_INT(
"lh not defined", procName, 1);
583 fprintf(fp,
"\n L_Heap: nalloc = %d, n = %d, array = %p\n",
585 for (i = 0; i < lh->
n; i++)
586 fprintf(fp,
"keyval[%d] = %f\n", i, *(l_float32 *)lh->
array[i]);
l_ok lheapAdd(L_HEAP *lh, void *item)
lheapAdd()
l_ok lheapPrint(FILE *fp, L_HEAP *lh)
lheapPrint()
void * lheapGetElement(L_HEAP *lh, l_int32 index)
lheapGetElement()
l_ok lheapSortStrictOrder(L_HEAP *lh)
lheapSortStrictOrder()
void lheapDestroy(L_HEAP **plh, l_int32 freeflag)
lheapDestroy()
l_int32 lheapGetCount(L_HEAP *lh)
lheapGetCount()
L_HEAP * lheapCreate(l_int32 n, l_int32 direction)
lheapCreate()
static l_int32 lheapExtendArray(L_HEAP *lh)
lheapExtendArray()
void * lheapRemove(L_HEAP *lh)
lheapRemove()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()
static const l_int32 InitialPtrArraySize
static l_ok lheapSwapUp(L_HEAP *lh, l_int32 index)
lheapSwapUp()
l_ok lheapSort(L_HEAP *lh)
lheapSort()
static l_ok lheapSwapDown(L_HEAP *lh)
lheapSwapDown()