1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
/*====================================================================*
- Copyright (C) 2001 Leptonica. All rights reserved.
- This software is distributed in the hope that it will be
- useful, but with NO WARRANTY OF ANY KIND.
- No author or distributor accepts responsibility to anyone for the
- consequences of using this software, or for whether it serves any
- particular purpose or works at all, unless he or she says so in
- writing. Everyone is granted permission to copy, modify and
- redistribute this source code, for commercial or non-commercial
- purposes, with the following restrictions: (1) the origin of this
- source code must not be misrepresented; (2) modified versions must
- be plainly marked as such; and (3) this notice may not be removed
- or altered from any source or modified source distribution.
*====================================================================*/
/*
* listtest.c
*
* This file tests the main functions in the generic
* list facility, given in list.c and list.h.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "allheaders.h"
main(int argc,
char **argv)
{
char *filein;
l_int32 i, n, w, h, samecount, count;
BOX *box, *boxc;
BOXA *boxa, *boxan;
DLLIST *head, *tail, *head2, *tail2, *elem, *nextelem;
PIX *pixs, *pixd, *pixd1, *pixd2;
static char mainName[] = "listtest";
if (argc != 2)
exit(ERROR_INT(" Syntax: listtest filein", mainName, 1));
filein = argv[1];
boxa = boxan = NULL;
if ((pixs = pixRead(filein)) == NULL)
exit(ERROR_INT("pix not made", mainName, 1));
/* start with a boxa */
boxa = pixConnComp(pixs, NULL, 4);
n = boxaGetCount(boxa);
/*-------------------------------------------------------*
* Do one of these two ...
*-------------------------------------------------------*/
#if 1
/* listAddToTail(): make a list by adding to tail */
head = NULL;
tail = NULL;
for (i = 0; i < n; i++) {
box = boxaGetBox(boxa, i, L_CLONE);
listAddToTail(&head, &tail, box);
}
#else
/* listAddToHead(): make a list by adding to head */
head = NULL;
for (i = 0; i < n; i++) {
box = boxaGetBox(boxa, i, L_CLONE);
listAddToHead(&head, box);
}
#endif
/*-------------------------------------------------------*
* Then do one of these ...
*-------------------------------------------------------*/
#if 1
/* list concatenation */
head2 = NULL; /* cons up 2nd list from null */
tail2 = NULL;
for (i = 0; i < n; i++) {
box = boxaGetBox(boxa, i, L_CLONE);
listAddToTail(&head2, &tail2, box);
}
listJoin(&head, &head2);
#endif
count = listGetCount(head);
fprintf(stderr, "%d items in list\n", count);
listReverse(&head);
count = listGetCount(head);
fprintf(stderr, "%d items in reversed list\n", count);
listReverse(&head);
count = listGetCount(head);
fprintf(stderr, "%d items in doubly reversed list\n", count);
boxan = boxaCreate(n);
#if 0
/* removal of all elements and data from a list,
* without using BEGIN_LIST_FORWARD macro */
for (elem = head; elem; elem = nextelem) {
nextelem = elem->next;
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
listRemoveElement(&head, elem);
}
#endif
#if 0
/* removal of all elements and data from a list,
* using BEGIN_LIST_FORWARD macro */
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
listRemoveElement(&head, elem);
END_LIST
#endif
#if 0
/* removal of all elements and data from a list,
* using BEGIN_LIST_REVERSE macro */
tail = listFindTail(head);
BEGIN_LIST_REVERSE(tail, elem)
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
listRemoveElement(&head, elem);
END_LIST
#endif
#if 0
/* boxa and boxan are same when list made with listAddToHead() */
tail = listFindTail(head);
BEGIN_LIST_REVERSE(tail, elem)
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
listRemoveElement(&head, elem);
END_LIST
for (i = 0, samecount = 0; i < n; i++) {
if (boxa->box[i]->w == boxan->box[i]->w &&
boxa->box[i]->h == boxan->box[i]->h)
samecount++;
}
fprintf(stderr, " num boxes = %d, same count = %d\n",
boxaGetCount(boxa), samecount);
#endif
#if 0
/* boxa and boxan are same when list made with listAddToTail() */
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
listRemoveElement(&head, elem);
END_LIST
for (i = 0, samecount = 0; i < n; i++) {
if (boxa->box[i]->w == boxan->box[i]->w &&
boxa->box[i]->h == boxan->box[i]->h)
samecount++;
}
fprintf(stderr, " num boxes = %d, same count = %d\n",
boxaGetCount(boxa), samecount);
#endif
#if 0
/* destroy the boxes and then the list */
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxDestroy(&box);
elem->data = NULL;
END_LIST
listDestroy(&head);
#endif
#if 0
/* listInsertBefore(): inserting a copy BEFORE each element */
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxc = boxCopy(box);
listInsertBefore(&head, elem, boxc);
END_LIST
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
END_LIST
listDestroy(&head);
#endif
#if 0
/* listInsertAfter(): inserting a copy AFTER that element */
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxc = boxCopy(box);
listInsertAfter(&head, elem, boxc);
END_LIST
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)elem->data;
boxaAddBox(boxan, box, L_INSERT);
elem->data = NULL;
listRemoveElement(&head, elem);
END_LIST
/* listDestroy(&head); */
#endif
#if 0
/* test listRemoveFromHead(), to successively
* remove the head of the list for all elements. */
count = 0;
while (head) {
box = listRemoveFromHead(&head);
boxDestroy(&box);
count++;
}
fprintf(stderr, "removed %d items\n", count);
#endif
#if 0
/* another version to test listRemoveFromHead(), using
* an iterator macro. */
count = 0;
BEGIN_LIST_FORWARD(head, elem)
box = (BOX *)listRemoveFromHead(&head);
boxDestroy(&box);
count++;
END_LIST
fprintf(stderr, "removed %d items\n", count);
#endif
#if 0
/* test listRemoveFromTail(), to successively remove
* the tail of the list for all elements. */
count = 0;
tail = NULL; /* will find tail automatically */
while (head) {
box = (BOX *)listRemoveFromTail(&head, &tail);
boxDestroy(&box);
count++;
}
fprintf(stderr, "removed %d items\n", count);
#endif
#if 0
/* another version to test listRemoveFromTail(), using
* an iterator macro. */
count = 0;
tail = listFindTail(head); /* need to initialize tail */
BEGIN_LIST_REVERSE(tail, elem)
box = (BOX *)listRemoveFromTail(&head, &tail);
boxDestroy(&box);
count++;
END_LIST
fprintf(stderr, "removed %d items\n", count);
#endif
#if 0
/* Iterate backwards over the box array, and use
* listFindElement() to find each corresponding data structure
* within the list; then remove it. Should completely
* destroy the list. Note that listFindElement()
* returns the cell without removing it from the list! */
n = boxaGetCount(boxa);
for (i = 0, count = 0; i < n; i++) {
box = boxaGetBox(boxa, n - i - 1, L_CLONE);
if (i % 1709 == 0) boxPrintStream(stderr, box);
elem = listFindElement(head, box);
boxDestroy(&box);
if (elem) { /* found */
box = listRemoveElement(&head, elem);
if (i % 1709 == 0) boxPrintStream(stderr, box);
boxDestroy(&box);
count++;
}
}
fprintf(stderr, "removed %d items\n", count);
#endif
fprintf(stderr, "boxa count = %d; boxan count = %d\n",
boxaGetCount(boxa), boxaGetCount(boxan));
boxaGetExtent(boxa, &w, &h, NULL);
fprintf(stderr, "boxa extent = (%d, %d)\n", w, h);
boxaGetExtent(boxan, &w, &h, NULL);
fprintf(stderr, "boxan extent = (%d, %d)\n", w, h);
pixDestroy(&pixs);
boxaDestroy(&boxa);
boxaDestroy(&boxan);
exit(0);
}
|