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 if (n < InitialPtrArraySize || n > MaxPtrArraySize)
122 if ((lh->
array = (
void **)LEPT_CALLOC(n,
sizeof(
void *))) == NULL) {
124 return (
L_HEAP *)ERROR_PTR(
"ptr array not made", __func__, NULL);
159 L_WARNING(
"ptr address is NULL\n", __func__);
162 if ((lh = *plh) == NULL)
166 for (i = 0; i < lh->
n; i++)
167 LEPT_FREE(lh->
array[i]);
168 }
else if (lh->
n > 0) {
169 L_WARNING(
"memory leak of %d items in lheap!\n", __func__, lh->
n);
173 LEPT_FREE(lh->
array);
193 return ERROR_INT(
"lh not defined", __func__, 1);
195 return ERROR_INT(
"item not defined", __func__, 1);
200 return ERROR_INT(
"extension failed", __func__, 1);
223 return ERROR_INT(
"lh not defined", __func__, 1);
226 sizeof(
void *) * lh->
nalloc,
227 2 *
sizeof(
void *) * lh->
nalloc)) == NULL)
228 return ERROR_INT(
"new ptr array not returned", __func__, 1);
248 return (
void *)ERROR_PTR(
"lh not defined", __func__, NULL);
255 lh->
array[lh->
n - 1] = NULL;
276 return ERROR_INT(
"lh not defined", __func__, 0);
303 return ERROR_PTR(
"lh not defined", __func__, NULL);
304 if (index < 0 || index >= lh->
n)
305 return ERROR_PTR(
"invalid index", __func__, NULL);
307 return (
void *)lh->
array[index];
332 return ERROR_INT(
"lh not defined", __func__, 1);
334 for (i = 0; i < lh->
n; i++)
361 l_int32 i, index, size;
364 return ERROR_INT(
"lh not defined", __func__, 1);
370 for (i = 0; i < size; i++) {
372 SWAP_ITEMS(0, index - 1);
378 for (i = 0; i < size / 2; i++)
379 SWAP_ITEMS(i, size - i - 1);
411 l_float32 valp, valc;
414 return ERROR_INT(
"lh not defined", __func__, 1);
415 if (index < 0 || index >= lh->
n)
416 return ERROR_INT(
"invalid index", __func__, 1);
424 valc = *(l_float32 *)(lh->
array[ic - 1]);
425 valp = *(l_float32 *)(lh->
array[ip - 1]);
428 SWAP_ITEMS(ip - 1, ic - 1);
436 valc = *(l_float32 *)(lh->
array[ic - 1]);
437 valp = *(l_float32 *)(lh->
array[ip - 1]);
440 SWAP_ITEMS(ip - 1, ic - 1);
474 l_float32 valp, valcl, valcr;
477 return ERROR_INT(
"lh not defined", __func__, 1);
487 valp = *(l_float32 *)(lh->
array[ip - 1]);
488 valcl = *(l_float32 *)(lh->
array[icl - 1]);
492 SWAP_ITEMS(ip - 1, icl - 1);
495 valcr = *(l_float32 *)(lh->
array[icr - 1]);
496 if (valp <= valcl && valp <= valcr)
498 if (valcl <= valcr) {
499 SWAP_ITEMS(ip - 1, icl - 1);
502 SWAP_ITEMS(ip - 1, icr - 1);
512 valp = *(l_float32 *)(lh->
array[ip - 1]);
513 valcl = *(l_float32 *)(lh->
array[icl - 1]);
517 SWAP_ITEMS(ip - 1, icl - 1);
520 valcr = *(l_float32 *)(lh->
array[icr - 1]);
521 if (valp >= valcl && valp >= valcr)
523 if (valcl >= valcr) {
524 SWAP_ITEMS(ip - 1, icl - 1);
527 SWAP_ITEMS(ip - 1, icr - 1);
555 return ERROR_INT(
"stream not defined", __func__, 1);
557 return ERROR_INT(
"lh not defined", __func__, 1);
559 fprintf(fp,
"\n L_Heap: nalloc = %d, n = %d, array = %p\n",
561 for (i = 0; i < lh->
n; i++)
562 fprintf(fp,
"keyval[%d] = %f\n", i, *(l_float32 *)lh->
array[i]);
void lheapDestroy(L_HEAP **plh, l_int32 freeflag)
lheapDestroy()
static l_ok lheapSwapUp(L_HEAP *lh, l_int32 index)
lheapSwapUp()
static const l_int32 InitialPtrArraySize
l_ok lheapSortStrictOrder(L_HEAP *lh)
lheapSortStrictOrder()
l_ok lheapPrint(FILE *fp, L_HEAP *lh)
lheapPrint()
L_HEAP * lheapCreate(l_int32 n, l_int32 direction)
lheapCreate()
void * lheapGetElement(L_HEAP *lh, l_int32 index)
lheapGetElement()
static l_int32 lheapExtendArray(L_HEAP *lh)
lheapExtendArray()
l_ok lheapSort(L_HEAP *lh)
lheapSort()
static l_ok lheapSwapDown(L_HEAP *lh)
lheapSwapDown()
l_int32 lheapGetCount(L_HEAP *lh)
lheapGetCount()
l_ok lheapAdd(L_HEAP *lh, void *item)
lheapAdd()
void * lheapRemove(L_HEAP *lh)
lheapRemove()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()