Leptonica 1.68
C Image Processing Library
|
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 * heap_reg.c 00018 * 00019 * Tests the heap utility. 00020 */ 00021 00022 #include "allheaders.h" 00023 00024 struct HeapElement { 00025 l_float32 distance; 00026 l_int32 x; 00027 l_int32 y; 00028 }; 00029 typedef struct HeapElement HEAPEL; 00030 00031 static const l_int32 NELEM = 50; 00032 00033 00034 main(int argc, 00035 char **argv) 00036 { 00037 l_int32 i; 00038 l_float32 frand, fval; 00039 HEAPEL *item; 00040 NUMA *na; 00041 L_HEAP *lh; 00042 static char mainName[] = "heap_reg"; 00043 00044 if (argc != 1) 00045 exit(ERROR_INT(" Syntax: heap_reg", mainName, 1)); 00046 00047 /* make a numa of random numbers */ 00048 na = numaCreate(5); 00049 for (i = 0; i < NELEM; i++) { 00050 frand = (l_float32)rand() / (l_float32)RAND_MAX; 00051 numaAddNumber(na, frand); 00052 } 00053 00054 /* make an array of HEAPELs with the same numbers */ 00055 lh = lheapCreate(5, L_SORT_INCREASING); 00056 for (i = 0; i < NELEM; i++) { 00057 numaGetFValue(na, i, &fval); 00058 item = (HEAPEL *)lept_calloc(1, sizeof(HEAPEL)); 00059 item->distance = fval; 00060 lheapAdd(lh, item); 00061 } 00062 lheapPrint(stderr, lh); 00063 00064 /* switch the direction and resort into a heap */ 00065 lh->direction = L_SORT_DECREASING; 00066 lheapSort(lh); 00067 lheapPrint(stderr, lh); 00068 00069 /* resort for strict order */ 00070 lheapSortStrictOrder(lh); 00071 lheapPrint(stderr, lh); 00072 00073 /* switch the direction again and resort into a heap */ 00074 lh->direction = L_SORT_INCREASING; 00075 lheapSort(lh); 00076 lheapPrint(stderr, lh); 00077 00078 /* remove the elements, one at a time */ 00079 for (i = 0; lheapGetCount(lh) > 0; i++) { 00080 item = (HEAPEL *)lheapRemove(lh); 00081 fprintf(stderr, "item %d: %f\n", i, item->distance); 00082 lept_free(item); 00083 } 00084 00085 lheapDestroy(&lh, 1); 00086 numaDestroy(&na); 00087 return 0; 00088 } 00089