122 #include <config_auto.h>
125 #include "allheaders.h"
128 LEPT_DLL
const l_uint32 MaxInitPtraSize = 1000001;
148 PROCNAME(
"ptraCreate");
150 if (n > MaxInitPtraSize) {
151 L_ERROR(
"n = %d > maxsize = %d\n", procName, n, MaxInitPtraSize);
157 if ((pa->
array = (
void **)LEPT_CALLOC(n,
sizeof(
void *))) == NULL) {
159 return (
L_PTRA *)ERROR_PTR(
"ptr array not made", procName, NULL);
202 PROCNAME(
"ptraDestroy");
205 L_WARNING(
"ptr address is NULL\n", procName);
208 if ((pa = *ppa) == NULL)
214 for (i = 0; i <= pa->
imax; i++) {
218 }
else if (warnflag) {
219 L_WARNING(
"potential memory leak of %d items in ptra\n",
224 LEPT_FREE(pa->
array);
258 return ERROR_INT(
"pa not defined", procName, 1);
260 return ERROR_INT(
"item not defined", procName, 1);
264 return ERROR_INT(
"extension failure", procName, 1);
265 pa->
array[imax + 1] = (
void *)item;
281 PROCNAME(
"ptraExtendArray");
284 return ERROR_INT(
"pa not defined", procName, 1);
287 sizeof(
void *) * pa->
nalloc,
288 2 *
sizeof(
void *) * pa->
nalloc)) == NULL)
289 return ERROR_INT(
"new ptr array not returned", procName, 1);
349 l_int32 i, ihole, imax;
352 PROCNAME(
"ptraInsert");
355 return ERROR_INT(
"pa not defined", procName, 1);
356 if (index < 0 || index > pa->
nalloc)
357 return ERROR_INT(
"index not in [0 ... nalloc]", procName, 1);
360 return ERROR_INT(
"invalid shiftflag", procName, 1);
363 if (index == pa->
nalloc) {
365 return ERROR_INT(
"extension failure", procName, 1);
371 if (pa->
array[index] == NULL) {
372 pa->
array[index] = item;
373 if (item && index > imax)
385 return ERROR_INT(
"extension failure", procName, 1);
396 nexpected = (l_float32)(imax - pa->
nactual) *
397 (l_float32)((imax - index) / imax);
403 for (ihole = index + 1; ihole <= imax; ihole++) {
404 if (pa->
array[ihole] == NULL)
411 for (i = ihole; i > index; i--)
413 pa->
array[index] = (
void *)item;
414 if (ihole == imax + 1)
446 l_int32 i, imax, fromend, icurrent;
449 PROCNAME(
"ptraRemove");
452 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
454 if (index < 0 || index > imax)
455 return (
void *)ERROR_PTR(
"index not in [0 ... imax]", procName, NULL);
457 item = pa->
array[index];
460 pa->
array[index] = NULL;
463 fromend = (index == imax);
465 for (i = index - 1; i >= 0; i--) {
474 for (icurrent = index, i = index + 1; i <= imax; i++) {
478 pa->
imax = icurrent - 1;
495 PROCNAME(
"ptraRemoveLast");
498 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
528 PROCNAME(
"ptraReplace");
531 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
533 if (index < 0 || index > imax)
534 return (
void *)ERROR_PTR(
"index not in [0 ... imax]", procName, NULL);
536 olditem = pa->
array[index];
537 pa->
array[index] = item;
538 if (!item && olditem)
540 else if (item && !olditem)
543 if (freeflag == FALSE)
568 PROCNAME(
"ptraSwap");
571 return ERROR_INT(
"pa not defined", procName, 1);
572 if (index1 == index2)
575 if (index1 < 0 || index1 > imax || index2 < 0 || index2 > imax)
576 return ERROR_INT(
"invalid index: not in [0 ... imax]", procName, 1);
600 l_int32 i, imax, nactual, index;
602 PROCNAME(
"ptraCompactArray");
605 return ERROR_INT(
"pa not defined", procName, 1);
608 if (imax + 1 == nactual)
return 0;
611 for (i = 0, index = 0; i <= imax; i++) {
615 pa->
imax = index - 1;
616 if (nactual != index)
617 L_ERROR(
"index = %d; != nactual\n", procName, index);
637 PROCNAME(
"ptraReverse");
640 return ERROR_INT(
"pa not defined", procName, 1);
643 for (i = 0; i < (imax + 1) / 2; i++)
663 PROCNAME(
"ptraJoin");
666 return ERROR_INT(
"pa1 not defined", procName, 1);
671 for (i = 0; i <= imax; i++) {
710 PROCNAME(
"ptraGetMaxIndex");
713 return ERROR_INT(
"pa not defined", procName, 1);
715 return ERROR_INT(
"&maxindex not defined", procName, 1);
716 *pmaxindex = pa->
imax;
738 PROCNAME(
"ptraGetActualCount");
741 return ERROR_INT(
"pa not defined", procName, 1);
743 return ERROR_INT(
"&count not defined", procName, 1);
770 PROCNAME(
"ptraGetPtrToItem");
773 return (
void *)ERROR_PTR(
"pa not defined", procName, NULL);
774 if (index < 0 || index >= pa->
nalloc)
775 return (
void *)ERROR_PTR(
"index not in [0 ... nalloc-1]",
778 return pa->
array[index];
802 PROCNAME(
"ptraaCreate");
805 return (
L_PTRAA *)ERROR_PTR(
"n must be > 0", procName, NULL);
810 return (
L_PTRAA *)ERROR_PTR(
"ptr array not made", procName, NULL);
842 PROCNAME(
"ptraaDestroy");
845 L_WARNING(
"ptr address is NULL\n", procName);
848 if ((paa = *ppaa) == NULL)
852 for (i = 0; i < n; i++) {
857 LEPT_FREE(paa->
ptra);
877 PROCNAME(
"ptraaGetSize");
880 return ERROR_INT(
"paa not defined", procName, 1);
882 return ERROR_INT(
"&size not defined", procName, 1);
911 PROCNAME(
"ptraaInsertPtra");
914 return ERROR_INT(
"paa not defined", procName, 1);
916 return ERROR_INT(
"pa not defined", procName, 1);
918 if (index < 0 || index >= n)
919 return ERROR_INT(
"invalid index", procName, 1);
920 if (paa->
ptra[index] != NULL)
921 return ERROR_INT(
"ptra already stored at index", procName, 1);
923 paa->
ptra[index] = pa;
955 PROCNAME(
"ptraaGetPtra");
958 return (
L_PTRA *)ERROR_PTR(
"paa not defined", procName, NULL);
960 if (index < 0 || index >= n)
961 return (
L_PTRA *)ERROR_PTR(
"invalid index", procName, NULL);
963 return (
L_PTRA *)ERROR_PTR(
"invalid accessflag", procName, NULL);
965 pa = paa->
ptra[index];
967 paa->
ptra[index] = NULL;
995 PROCNAME(
"ptraaFlattenToPtra");
998 return (
L_PTRA *)ERROR_PTR(
"paa not defined", procName, NULL);
1002 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()