Leptonica 1.68
C Image Processing Library

listtest.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 
00017 /*
00018  * listtest.c
00019  *
00020  *    This file tests the main functions in the generic
00021  *    list facility, given in list.c and list.h.
00022  *
00023  */
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include "allheaders.h"
00028 
00029 
00030 main(int    argc,
00031      char **argv)
00032 {
00033 char        *filein;
00034 l_int32      i, n, w, h, samecount, count;
00035 BOX         *box, *boxc;
00036 BOXA        *boxa, *boxan;
00037 DLLIST      *head, *tail, *head2, *tail2, *elem, *nextelem;
00038 PIX         *pixs, *pixd, *pixd1, *pixd2;
00039 static char  mainName[] = "listtest";
00040 
00041     if (argc != 2)
00042         exit(ERROR_INT(" Syntax:  listtest filein", mainName, 1));
00043 
00044     filein = argv[1];
00045 
00046     boxa = boxan = NULL;
00047 
00048     if ((pixs = pixRead(filein)) == NULL)
00049         exit(ERROR_INT("pix not made", mainName, 1));
00050 
00051         /* start with a boxa */
00052     boxa = pixConnComp(pixs, NULL, 4);
00053     n = boxaGetCount(boxa);
00054 
00055   /*-------------------------------------------------------*
00056    *        Do one of these two ...
00057    *-------------------------------------------------------*/
00058 #if  1
00059         /* listAddToTail(): make a list by adding to tail */
00060     head = NULL;
00061     tail = NULL;
00062     for (i = 0; i < n; i++) {
00063         box = boxaGetBox(boxa, i, L_CLONE);
00064         listAddToTail(&head, &tail, box);
00065     }
00066 #else
00067         /* listAddToHead(): make a list by adding to head */
00068     head = NULL;
00069     for (i = 0; i < n; i++) {
00070         box = boxaGetBox(boxa, i, L_CLONE);
00071         listAddToHead(&head, box);
00072     }
00073 #endif
00074 
00075   /*-------------------------------------------------------*
00076    *        Then do one of these ...
00077    *-------------------------------------------------------*/
00078 #if 1
00079         /* list concatenation */
00080     head2 = NULL;   /* cons up 2nd list from null */
00081     tail2 = NULL;
00082     for (i = 0; i < n; i++) {
00083         box = boxaGetBox(boxa, i, L_CLONE);
00084         listAddToTail(&head2, &tail2, box); 
00085     }
00086     listJoin(&head, &head2);
00087 #endif
00088 
00089     count = listGetCount(head);
00090     fprintf(stderr, "%d items in list\n", count);
00091     listReverse(&head);
00092     count = listGetCount(head);
00093     fprintf(stderr, "%d items in reversed list\n", count);
00094     listReverse(&head);
00095     count = listGetCount(head);
00096     fprintf(stderr, "%d items in doubly reversed list\n", count);
00097 
00098     boxan = boxaCreate(n);
00099 
00100 #if 0 
00101         /* removal of all elements and data from a list, 
00102          * without using BEGIN_LIST_FORWARD macro */
00103     for (elem = head; elem; elem = nextelem) {
00104         nextelem = elem->next;
00105         box = (BOX *)elem->data;
00106         boxaAddBox(boxan, box, L_INSERT);
00107         elem->data = NULL;
00108         listRemoveElement(&head, elem);
00109     }
00110 #endif
00111 
00112 #if 0 
00113         /* removal of all elements and data from a list, 
00114          * using BEGIN_LIST_FORWARD macro */
00115     BEGIN_LIST_FORWARD(head, elem)
00116         box = (BOX *)elem->data;
00117         boxaAddBox(boxan, box, L_INSERT);
00118         elem->data = NULL;
00119         listRemoveElement(&head, elem);
00120     END_LIST
00121 #endif
00122 
00123 #if 0 
00124         /* removal of all elements and data from a list, 
00125          * using BEGIN_LIST_REVERSE macro */
00126     tail = listFindTail(head);
00127     BEGIN_LIST_REVERSE(tail, elem)
00128         box = (BOX *)elem->data;
00129         boxaAddBox(boxan, box, L_INSERT);
00130         elem->data = NULL;
00131         listRemoveElement(&head, elem);
00132     END_LIST
00133 #endif
00134 
00135 #if 0
00136         /* boxa and boxan are same when list made with listAddToHead() */
00137     tail = listFindTail(head);
00138     BEGIN_LIST_REVERSE(tail, elem)
00139         box = (BOX *)elem->data;
00140         boxaAddBox(boxan, box, L_INSERT);
00141         elem->data = NULL;
00142         listRemoveElement(&head, elem);
00143     END_LIST
00144     for (i = 0, samecount = 0; i < n; i++) {
00145         if (boxa->box[i]->w == boxan->box[i]->w  &&
00146             boxa->box[i]->h == boxan->box[i]->h)
00147             samecount++;
00148     }
00149     fprintf(stderr, " num boxes = %d, same count = %d\n",
00150             boxaGetCount(boxa), samecount);
00151 #endif
00152 
00153 #if 0 
00154         /* boxa and boxan are same when list made with listAddToTail() */
00155     BEGIN_LIST_FORWARD(head, elem)
00156         box = (BOX *)elem->data;
00157         boxaAddBox(boxan, box, L_INSERT);
00158         elem->data = NULL;
00159         listRemoveElement(&head, elem);
00160     END_LIST
00161     for (i = 0, samecount = 0; i < n; i++) {
00162         if (boxa->box[i]->w == boxan->box[i]->w  &&
00163             boxa->box[i]->h == boxan->box[i]->h)
00164             samecount++;
00165     }
00166     fprintf(stderr, " num boxes = %d, same count = %d\n",
00167             boxaGetCount(boxa), samecount);
00168 #endif
00169 
00170 #if 0
00171         /* destroy the boxes and then the list */
00172     BEGIN_LIST_FORWARD(head, elem)
00173         box = (BOX *)elem->data;
00174         boxDestroy(&box);
00175         elem->data = NULL;
00176     END_LIST
00177     listDestroy(&head);
00178 #endif
00179 
00180 #if 0 
00181         /* listInsertBefore(): inserting a copy BEFORE each element */
00182     BEGIN_LIST_FORWARD(head, elem)
00183         box = (BOX *)elem->data;
00184         boxc = boxCopy(box);
00185         listInsertBefore(&head, elem, boxc);
00186     END_LIST
00187     BEGIN_LIST_FORWARD(head, elem)
00188         box = (BOX *)elem->data;
00189         boxaAddBox(boxan, box, L_INSERT);
00190         elem->data = NULL;
00191     END_LIST
00192     listDestroy(&head);
00193 #endif
00194 
00195 #if 0 
00196         /* listInsertAfter(): inserting a copy AFTER that element */
00197     BEGIN_LIST_FORWARD(head, elem)
00198         box = (BOX *)elem->data;
00199         boxc = boxCopy(box);
00200         listInsertAfter(&head, elem, boxc);
00201     END_LIST
00202     BEGIN_LIST_FORWARD(head, elem)
00203         box = (BOX *)elem->data;
00204         boxaAddBox(boxan, box, L_INSERT);
00205         elem->data = NULL;
00206         listRemoveElement(&head, elem);
00207     END_LIST
00208 /*    listDestroy(&head); */
00209 #endif
00210 
00211 #if 0
00212         /* test listRemoveFromHead(), to successively
00213          * remove the head of the list for all elements. */
00214     count = 0;
00215     while (head) {
00216         box = listRemoveFromHead(&head);
00217         boxDestroy(&box);
00218         count++;
00219     }
00220     fprintf(stderr, "removed %d items\n", count);
00221 #endif
00222 
00223 #if 0
00224         /* another version to test listRemoveFromHead(), using
00225          * an iterator macro. */
00226     count = 0;
00227     BEGIN_LIST_FORWARD(head, elem)
00228         box = (BOX *)listRemoveFromHead(&head);
00229         boxDestroy(&box);
00230         count++;
00231     END_LIST
00232     fprintf(stderr, "removed %d items\n", count);
00233 #endif
00234 
00235 #if 0
00236         /* test listRemoveFromTail(), to successively remove
00237          * the tail of the list for all elements. */
00238     count = 0;
00239     tail = NULL;   /* will find tail automatically */
00240     while (head) {
00241         box = (BOX *)listRemoveFromTail(&head, &tail);
00242         boxDestroy(&box);
00243         count++;
00244     }
00245     fprintf(stderr, "removed %d items\n", count);
00246 #endif
00247 
00248 #if 0
00249         /* another version to test listRemoveFromTail(), using
00250          * an iterator macro. */
00251     count = 0;
00252     tail = listFindTail(head);  /* need to initialize tail */
00253     BEGIN_LIST_REVERSE(tail, elem)
00254         box = (BOX *)listRemoveFromTail(&head, &tail);
00255         boxDestroy(&box);
00256         count++;
00257     END_LIST
00258     fprintf(stderr, "removed %d items\n", count);
00259 #endif
00260 
00261 #if 0
00262         /* Iterate backwards over the box array, and use
00263          * listFindElement() to find each corresponding data structure
00264          * within the list; then remove it.  Should completely
00265          * destroy the list.   Note that listFindElement()
00266          * returns the cell without removing it from the list! */
00267     n = boxaGetCount(boxa);
00268     for (i = 0, count = 0; i < n; i++) {
00269         box = boxaGetBox(boxa, n - i - 1, L_CLONE);
00270         if (i % 1709 == 0) boxPrintStream(stderr, box);
00271         elem = listFindElement(head, box);
00272         boxDestroy(&box);
00273         if (elem) {  /* found */
00274             box = listRemoveElement(&head, elem);
00275             if (i % 1709 == 0) boxPrintStream(stderr, box);
00276             boxDestroy(&box);
00277             count++;
00278         }
00279     }
00280     fprintf(stderr, "removed %d items\n", count);
00281 #endif
00282 
00283     fprintf(stderr, "boxa count = %d; boxan count = %d\n",
00284                      boxaGetCount(boxa), boxaGetCount(boxan));
00285     boxaGetExtent(boxa, &w, &h, NULL);
00286     fprintf(stderr, "boxa extent = (%d, %d)\n", w, h);
00287     boxaGetExtent(boxan, &w, &h, NULL);
00288     fprintf(stderr, "boxan extent = (%d, %d)\n", w, h);
00289 
00290     pixDestroy(&pixs);
00291     boxaDestroy(&boxa);
00292     boxaDestroy(&boxan);
00293     exit(0);
00294 }
00295 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines