122 #include <config_auto.h>
125 #include "allheaders.h"
128 LEPT_DLL
const l_uint32 MaxInitPtraSize = 1000001;
148 if (n > MaxInitPtraSize) {
149 L_ERROR(
"n = %d > maxsize = %d\n", __func__, n, MaxInitPtraSize);
155 if ((pa->
array = (
void **)LEPT_CALLOC(n,
sizeof(
void *))) == NULL) {
157 return (
L_PTRA *)ERROR_PTR(
"ptr array not made", __func__, NULL);
201 L_WARNING(
"ptr address is NULL\n", __func__);
204 if ((pa = *ppa) == NULL)
210 for (i = 0; i <= pa->
imax; i++) {
214 }
else if (warnflag) {
215 L_WARNING(
"potential memory leak of %d items in ptra\n",
220 LEPT_FREE(pa->
array);
252 return ERROR_INT(
"pa not defined", __func__, 1);
254 return ERROR_INT(
"item not defined", __func__, 1);
258 return ERROR_INT(
"extension failure", __func__, 1);
259 pa->
array[imax + 1] = (
void *)item;
276 return ERROR_INT(
"pa not defined", __func__, 1);
279 sizeof(
void *) * pa->
nalloc,
280 2 *
sizeof(
void *) * pa->
nalloc)) == NULL)
281 return ERROR_INT(
"new ptr array not returned", __func__, 1);
341 l_int32 i, ihole, imax;
345 return ERROR_INT(
"pa not defined", __func__, 1);
346 if (index < 0 || index > pa->
nalloc)
347 return ERROR_INT(
"index not in [0 ... nalloc]", __func__, 1);
350 return ERROR_INT(
"invalid shiftflag", __func__, 1);
353 if (index == pa->
nalloc) {
355 return ERROR_INT(
"extension failure", __func__, 1);
361 if (pa->
array[index] == NULL) {
362 pa->
array[index] = item;
363 if (item && index > imax)
375 return ERROR_INT(
"extension failure", __func__, 1);
386 nexpected = (l_float32)(imax - pa->
nactual) *
387 (l_float32)((imax - index) / imax);
393 for (ihole = index + 1; ihole <= imax; ihole++) {
394 if (pa->
array[ihole] == NULL)
401 for (i = ihole; i > index; i--)
403 pa->
array[index] = (
void *)item;
404 if (ihole == imax + 1)
436 l_int32 i, imax, fromend, icurrent;
440 return (
void *)ERROR_PTR(
"pa not defined", __func__, NULL);
442 if (index < 0 || index > imax)
443 return (
void *)ERROR_PTR(
"index not in [0 ... imax]", __func__, NULL);
445 item = pa->
array[index];
448 pa->
array[index] = NULL;
451 fromend = (index == imax);
453 for (i = index - 1; i >= 0; i--) {
462 for (icurrent = index, i = index + 1; i <= imax; i++) {
466 pa->
imax = icurrent - 1;
484 return (
void *)ERROR_PTR(
"pa not defined", __func__, NULL);
515 return (
void *)ERROR_PTR(
"pa not defined", __func__, NULL);
517 if (index < 0 || index > imax)
518 return (
void *)ERROR_PTR(
"index not in [0 ... imax]", __func__, NULL);
520 olditem = pa->
array[index];
521 pa->
array[index] = item;
522 if (!item && olditem)
524 else if (item && !olditem)
527 if (freeflag == FALSE)
553 return ERROR_INT(
"pa not defined", __func__, 1);
554 if (index1 == index2)
557 if (index1 < 0 || index1 > imax || index2 < 0 || index2 > imax)
558 return ERROR_INT(
"invalid index: not in [0 ... imax]", __func__, 1);
582 l_int32 i, imax, nactual, index;
585 return ERROR_INT(
"pa not defined", __func__, 1);
588 if (imax + 1 == nactual)
return 0;
591 for (i = 0, index = 0; i <= imax; i++) {
595 pa->
imax = index - 1;
596 if (nactual != index)
597 L_ERROR(
"index = %d; != nactual\n", __func__, index);
618 return ERROR_INT(
"pa not defined", __func__, 1);
621 for (i = 0; i < (imax + 1) / 2; i++)
642 return ERROR_INT(
"pa1 not defined", __func__, 1);
647 for (i = 0; i <= imax; i++) {
687 return ERROR_INT(
"pa not defined", __func__, 1);
689 return ERROR_INT(
"&maxindex not defined", __func__, 1);
690 *pmaxindex = pa->
imax;
713 return ERROR_INT(
"pa not defined", __func__, 1);
715 return ERROR_INT(
"&count not defined", __func__, 1);
743 return (
void *)ERROR_PTR(
"pa not defined", __func__, NULL);
744 if (index < 0 || index >= pa->
nalloc)
745 return (
void *)ERROR_PTR(
"index not in [0 ... nalloc-1]",
748 return pa->
array[index];
773 return (
L_PTRAA *)ERROR_PTR(
"n must be > 0", __func__, NULL);
778 return (
L_PTRAA *)ERROR_PTR(
"ptr array not made", __func__, NULL);
811 L_WARNING(
"ptr address is NULL\n", __func__);
814 if ((paa = *ppaa) == NULL)
818 for (i = 0; i < n; i++) {
823 LEPT_FREE(paa->
ptra);
844 return ERROR_INT(
"paa not defined", __func__, 1);
846 return ERROR_INT(
"&size not defined", __func__, 1);
876 return ERROR_INT(
"paa not defined", __func__, 1);
878 return ERROR_INT(
"pa not defined", __func__, 1);
880 if (index < 0 || index >= n)
881 return ERROR_INT(
"invalid index", __func__, 1);
882 if (paa->
ptra[index] != NULL)
883 return ERROR_INT(
"ptra already stored at index", __func__, 1);
885 paa->
ptra[index] = pa;
918 return (
L_PTRA *)ERROR_PTR(
"paa not defined", __func__, NULL);
920 if (index < 0 || index >= n)
921 return (
L_PTRA *)ERROR_PTR(
"invalid index", __func__, NULL);
923 return (
L_PTRA *)ERROR_PTR(
"invalid accessflag", __func__, NULL);
925 pa = paa->
ptra[index];
927 paa->
ptra[index] = NULL;
956 return (
L_PTRA *)ERROR_PTR(
"paa not defined", __func__, NULL);
960 for (i = 0; i < n; i++) {
L_PTRAA * ptraaCreate(l_int32 n)
ptraaCreate()
l_ok ptraReverse(L_PTRA *pa)
ptraReverse()
l_ok ptraInsert(L_PTRA *pa, l_int32 index, void *item, l_int32 shiftflag)
ptraInsert()
l_ok ptraaGetSize(L_PTRAA *paa, l_int32 *psize)
ptraaGetSize()
L_PTRA * ptraCreate(l_int32 n)
ptraCreate()
l_ok ptraJoin(L_PTRA *pa1, L_PTRA *pa2)
ptraJoin()
l_ok ptraGetMaxIndex(L_PTRA *pa, l_int32 *pmaxindex)
ptraGetMaxIndex()
l_ok ptraSwap(L_PTRA *pa, l_int32 index1, l_int32 index2)
ptraSwap()
L_PTRA * ptraaFlattenToPtra(L_PTRAA *paa)
ptraaFlattenToPtra()
static const l_int32 DefaultInitPtraSize
l_ok ptraGetActualCount(L_PTRA *pa, l_int32 *pcount)
ptraGetActualCount()
l_ok ptraCompactArray(L_PTRA *pa)
ptraCompactArray()
l_ok ptraAdd(L_PTRA *pa, void *item)
ptraAdd()
void * ptraReplace(L_PTRA *pa, l_int32 index, void *item, l_int32 freeflag)
ptraReplace()
void ptraDestroy(L_PTRA **ppa, l_int32 freeflag, l_int32 warnflag)
ptraDestroy()
void * ptraRemove(L_PTRA *pa, l_int32 index, l_int32 flag)
ptraRemove()
void ptraaDestroy(L_PTRAA **ppaa, l_int32 freeflag, l_int32 warnflag)
ptraaDestroy()
void * ptraGetPtrToItem(L_PTRA *pa, l_int32 index)
ptraGetPtrToItem()
L_PTRA * ptraaGetPtra(L_PTRAA *paa, l_int32 index, l_int32 accessflag)
ptraaGetPtra()
l_ok ptraaInsertPtra(L_PTRAA *paa, l_int32 index, L_PTRA *pa)
ptraaInsertPtra()
static l_int32 ptraExtendArray(L_PTRA *pa)
ptraExtendArray()
void * ptraRemoveLast(L_PTRA *pa)
ptraRemoveLast()
void * reallocNew(void **pindata, size_t oldsize, size_t newsize)
reallocNew()