121 #include "allheaders.h" 123 static const l_int32 INITIAL_PTR_ARRAYSIZE = 20;
143 PROCNAME(
"ptraCreate");
146 n = INITIAL_PTR_ARRAYSIZE;
149 if ((pa->
array = (
void **)LEPT_CALLOC(n,
sizeof(
void *))) == NULL) {
151 return (
L_PTRA *)ERROR_PTR(
"ptr array not made", procName, NULL);
193 PROCNAME(
"ptraDestroy");
196 L_WARNING(
"ptr address is NULL\n", procName);
199 if ((pa = *ppa) == NULL)
205 for (i = 0; i <= pa->
imax; i++) {
209 }
else if (warnflag) {
210 L_WARNING(
"potential memory leak of %d items in ptra\n",
215 LEPT_FREE(pa->
array);
250 return ERROR_INT(
"pa not defined", procName, 1);
252 return ERROR_INT(
"item not defined", procName, 1);
256 return ERROR_INT(
"extension failure", procName, 1);
257 pa->
array[imax + 1] = (
void *)item;
273 PROCNAME(
"ptraExtendArray");
276 return ERROR_INT(
"pa not defined", procName, 1);
279 sizeof(
void *) * pa->
nalloc,
280 2 *
sizeof(
void *) * pa->
nalloc)) == NULL)
281 return ERROR_INT(
"new ptr array not returned", procName, 1);
341 l_int32 i, ihole, imax;
344 PROCNAME(
"ptraInsert");
347 return ERROR_INT(
"pa not defined", procName, 1);
348 if (index < 0 || index > pa->
nalloc)
349 return ERROR_INT(
"index not in [0 ... nalloc]", procName, 1);
352 return ERROR_INT(
"invalid shiftflag", procName, 1);
355 if (index == pa->
nalloc) {
357 return ERROR_INT(
"extension failure", procName, 1);
363 if (pa->
array[index] == NULL) {
364 pa->
array[index] = item;
365 if (item && index > imax)
377 return ERROR_INT(
"extension failure", procName, 1);
388 nexpected = (l_float32)(imax - pa->
nactual) *
389 (l_float32)((imax - index) / imax);
395 for (ihole = index + 1; ihole <= imax; ihole++) {
396 if (pa->
array[ihole] == NULL)
403 for (i = ihole; i > index; i--)
405 pa->
array[index] = (
void *)item;
406 if (ihole == imax + 1)
438 l_int32 i, imax, fromend, icurrent;
441 PROCNAME(
"ptraRemove");
444 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
446 if (index < 0 || index > imax)
447 return (
void *)ERROR_PTR(
"index not in [0 ... imax]", procName, NULL);
449 item = pa->
array[index];
452 pa->
array[index] = NULL;
455 fromend = (index == imax);
457 for (i = index - 1; i >= 0; i--) {
466 for (icurrent = index, i = index + 1; i <= imax; i++) {
470 pa->
imax = icurrent - 1;
487 PROCNAME(
"ptraRemoveLast");
490 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
520 PROCNAME(
"ptraReplace");
523 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
525 if (index < 0 || index > imax)
526 return (
void *)ERROR_PTR(
"index not in [0 ... imax]", procName, NULL);
528 olditem = pa->
array[index];
529 pa->
array[index] = item;
530 if (!item && olditem)
532 else if (item && !olditem)
535 if (freeflag == FALSE)
560 PROCNAME(
"ptraSwap");
563 return ERROR_INT(
"pa not defined", procName, 1);
564 if (index1 == index2)
567 if (index1 < 0 || index1 > imax || index2 < 0 || index2 > imax)
568 return ERROR_INT(
"invalid index: not in [0 ... imax]", procName, 1);
592 l_int32 i, imax, nactual, index;
594 PROCNAME(
"ptraCompactArray");
597 return ERROR_INT(
"pa not defined", procName, 1);
600 if (imax + 1 == nactual)
return 0;
603 for (i = 0, index = 0; i <= imax; i++) {
607 pa->
imax = index - 1;
608 if (nactual != index)
609 L_ERROR(
"index = %d; != nactual\n", procName, index);
629 PROCNAME(
"ptraReverse");
632 return ERROR_INT(
"pa not defined", procName, 1);
635 for (i = 0; i < (imax + 1) / 2; i++)
655 PROCNAME(
"ptraJoin");
658 return ERROR_INT(
"pa1 not defined", procName, 1);
663 for (i = 0; i <= imax; i++) {
702 PROCNAME(
"ptraGetMaxIndex");
705 return ERROR_INT(
"pa not defined", procName, 1);
707 return ERROR_INT(
"&maxindex not defined", procName, 1);
708 *pmaxindex = pa->
imax;
730 PROCNAME(
"ptraGetActualCount");
733 return ERROR_INT(
"pa not defined", procName, 1);
735 return ERROR_INT(
"&count not defined", procName, 1);
762 PROCNAME(
"ptraGetPtrToItem");
765 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
766 if (index < 0 || index >= pa->
nalloc)
767 return (
void *)ERROR_PTR(
"index not in [0 ... nalloc-1]",
770 return pa->
array[index];
794 PROCNAME(
"ptraaCreate");
797 return (
L_PTRAA *)ERROR_PTR(
"n must be > 0", procName, NULL);
800 return (
L_PTRAA *)ERROR_PTR(
"paa not made", procName, NULL);
803 return (
L_PTRAA *)ERROR_PTR(
"ptr array not made", procName, NULL);
834 PROCNAME(
"ptraaDestroy");
837 L_WARNING(
"ptr address is NULL\n", procName);
840 if ((paa = *ppaa) == NULL)
844 for (i = 0; i < n; i++) {
849 LEPT_FREE(paa->
ptra);
870 PROCNAME(
"ptraaGetSize");
873 return ERROR_INT(
"paa not defined", procName, 1);
875 return ERROR_INT(
"&size not defined", procName, 1);
904 PROCNAME(
"ptraaInsertPtra");
907 return ERROR_INT(
"paa not defined", procName, 1);
909 return ERROR_INT(
"pa not defined", procName, 1);
911 if (index < 0 || index >= n)
912 return ERROR_INT(
"invalid index", procName, 1);
913 if (paa->
ptra[index] != NULL)
914 return ERROR_INT(
"ptra already stored at index", procName, 1);
916 paa->
ptra[index] = pa;
948 PROCNAME(
"ptraaGetPtra");
951 return (
L_PTRA *)ERROR_PTR(
"paa not defined", procName, NULL);
953 if (index < 0 || index >= n)
954 return (
L_PTRA *)ERROR_PTR(
"invalid index", procName, NULL);
956 return (
L_PTRA *)ERROR_PTR(
"invalid accessflag", procName, NULL);
958 pa = paa->
ptra[index];
960 paa->
ptra[index] = NULL;
988 PROCNAME(
"ptraaFlattenToPtra");
991 return (
L_PTRA *)ERROR_PTR(
"paa not defined", procName, NULL);
995 for (i = 0; i < n; i++) {
l_ok ptraReverse(L_PTRA *pa)
ptraReverse()
void * ptraGetPtrToItem(L_PTRA *pa, l_int32 index)
ptraGetPtrToItem()
l_ok ptraSwap(L_PTRA *pa, l_int32 index1, l_int32 index2)
ptraSwap()
void ptraaDestroy(L_PTRAA **ppaa, l_int32 freeflag, l_int32 warnflag)
ptraaDestroy()
l_ok ptraGetActualCount(L_PTRA *pa, l_int32 *pcount)
ptraGetActualCount()
l_ok ptraJoin(L_PTRA *pa1, L_PTRA *pa2)
ptraJoin()
void * ptraRemoveLast(L_PTRA *pa)
ptraRemoveLast()
void * reallocNew(void **pindata, l_int32 oldsize, l_int32 newsize)
reallocNew()
l_ok ptraInsert(L_PTRA *pa, l_int32 index, void *item, l_int32 shiftflag)
ptraInsert()
L_PTRA * ptraCreate(l_int32 n)
ptraCreate()
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()
l_ok ptraCompactArray(L_PTRA *pa)
ptraCompactArray()
l_ok ptraGetMaxIndex(L_PTRA *pa, l_int32 *pmaxindex)
ptraGetMaxIndex()
void * ptraReplace(L_PTRA *pa, l_int32 index, void *item, l_int32 freeflag)
ptraReplace()
l_ok ptraaGetSize(L_PTRAA *paa, l_int32 *psize)
ptraaGetSize()
void ptraDestroy(L_PTRA **ppa, l_int32 freeflag, l_int32 warnflag)
ptraDestroy()
l_ok ptraAdd(L_PTRA *pa, void *item)
ptraAdd()
static l_int32 ptraExtendArray(L_PTRA *pa)
ptraExtendArray()
void * ptraRemove(L_PTRA *pa, l_int32 index, l_int32 flag)
ptraRemove()
L_PTRA * ptraaFlattenToPtra(L_PTRAA *paa)
ptraaFlattenToPtra()
L_PTRAA * ptraaCreate(l_int32 n)
ptraaCreate()