78 #include "allheaders.h" 80 static const l_int32 MIN_BUFFER_SIZE = 20;
83 #define SWAP_ITEMS(i, j) { void *tempitem = lh->array[(i)]; \ 84 lh->array[(i)] = lh->array[(j)]; \ 85 lh->array[(j)] = tempitem; } 107 PROCNAME(
"lheapCreate");
109 if (nalloc < MIN_BUFFER_SIZE)
110 nalloc = MIN_BUFFER_SIZE;
113 if ((lh = (
L_HEAP *)LEPT_CALLOC(1,
sizeof(
L_HEAP))) == NULL)
114 return (
L_HEAP *)ERROR_PTR(
"lh not made", procName, NULL);
115 if ((lh->
array = (
void **)LEPT_CALLOC(nalloc,
sizeof(
void *))) == NULL) {
117 return (
L_HEAP *)ERROR_PTR(
"ptr array not made", procName, NULL);
151 PROCNAME(
"lheapDestroy");
154 L_WARNING(
"ptr address is NULL\n", procName);
157 if ((lh = *plh) == NULL)
161 for (i = 0; i < lh->
n; i++)
162 LEPT_FREE(lh->
array[i]);
163 }
else if (lh->
n > 0) {
164 L_WARNING(
"memory leak of %d items in lheap!\n", procName, lh->
n);
168 LEPT_FREE(lh->
array);
189 PROCNAME(
"lheapAdd");
192 return ERROR_INT(
"lh not defined", procName, 1);
194 return ERROR_INT(
"item not defined", procName, 1);
219 PROCNAME(
"lheapExtendArray");
222 return ERROR_INT(
"lh not defined", procName, 1);
225 sizeof(
void *) * lh->
nalloc,
226 2 *
sizeof(
void *) * lh->
nalloc)) == NULL)
227 return ERROR_INT(
"new ptr array not returned", procName, 1);
246 PROCNAME(
"lheapRemove");
249 return (
void *)ERROR_PTR(
"lh not defined", procName, NULL);
256 lh->
array[lh->
n - 1] = NULL;
273 PROCNAME(
"lheapGetCount");
276 return ERROR_INT(
"lh not defined", procName, 0);
309 l_float32 valp, valc;
311 PROCNAME(
"lheapSwapUp");
314 return ERROR_INT(
"lh not defined", procName, 1);
315 if (index < 0 || index >= lh->
n)
316 return ERROR_INT(
"invalid index", procName, 1);
324 valc = *(l_float32 *)(lh->
array[ic - 1]);
325 valp = *(l_float32 *)(lh->
array[ip - 1]);
328 SWAP_ITEMS(ip - 1, ic - 1);
336 valc = *(l_float32 *)(lh->
array[ic - 1]);
337 valp = *(l_float32 *)(lh->
array[ip - 1]);
340 SWAP_ITEMS(ip - 1, ic - 1);
374 l_float32 valp, valcl, valcr;
376 PROCNAME(
"lheapSwapDown");
379 return ERROR_INT(
"lh not defined", procName, 1);
389 valp = *(l_float32 *)(lh->
array[ip - 1]);
390 valcl = *(l_float32 *)(lh->
array[icl - 1]);
394 SWAP_ITEMS(ip - 1, icl - 1);
397 valcr = *(l_float32 *)(lh->
array[icr - 1]);
398 if (valp <= valcl && valp <= valcr)
400 if (valcl <= valcr) {
401 SWAP_ITEMS(ip - 1, icl - 1);
404 SWAP_ITEMS(ip - 1, icr - 1);
414 valp = *(l_float32 *)(lh->
array[ip - 1]);
415 valcl = *(l_float32 *)(lh->
array[icl - 1]);
419 SWAP_ITEMS(ip - 1, icl - 1);
422 valcr = *(l_float32 *)(lh->
array[icr - 1]);
423 if (valp >= valcl && valp >= valcr)
425 if (valcl >= valcr) {
426 SWAP_ITEMS(ip - 1, icl - 1);
429 SWAP_ITEMS(ip - 1, icr - 1);
457 PROCNAME(
"lheapSort");
460 return ERROR_INT(
"lh not defined", procName, 1);
462 for (i = 0; i < lh->
n; i++)
489 l_int32 i, index, size;
491 PROCNAME(
"lheapSortStrictOrder");
494 return ERROR_INT(
"lh not defined", procName, 1);
497 for (i = 0; i < size; i++) {
499 SWAP_ITEMS(0, index - 1);
505 for (i = 0; i < size / 2; i++)
506 SWAP_ITEMS(i, size - i - 1);
529 PROCNAME(
"lheapPrint");
532 return ERROR_INT(
"stream not defined", procName, 1);
534 return ERROR_INT(
"lh not defined", procName, 1);
536 fprintf(fp,
"\n L_Heap: nalloc = %d, n = %d, array = %p\n",
538 for (i = 0; i < lh->
n; i++)
539 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()
l_ok lheapSortStrictOrder(L_HEAP *lh)
lheapSortStrictOrder()
void lheapDestroy(L_HEAP **plh, l_int32 freeflag)
lheapDestroy()
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
l_int32 lheapGetCount(L_HEAP *lh)
lheapGetCount()
static l_int32 lheapExtendArray(L_HEAP *lh)
lheapExtendArray()
L_HEAP * lheapCreate(l_int32 nalloc, l_int32 direction)
lheapCreate()
void * lheapRemove(L_HEAP *lh)
lheapRemove()
l_ok lheapSort(L_HEAP *lh)
lheapSort()
l_ok lheapSwapDown(L_HEAP *lh)
lheapSwapDown()
l_ok lheapSwapUp(L_HEAP *lh, l_int32 index)
lheapSwapUp()
static const l_int32 INITIAL_BUFFER_ARRAYSIZE