test_heap-def.c
Go to the documentation of this file.
00001 
00006 /*
00007 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00008 All rights reserved.
00009 
00010 This file is part of the VLFeat library and is made available under
00011 the terms of the BSD license (see the COPYING file).
00012 */
00013 
00014 #define VL_HEAP_prefix vl_heap_float
00015 #define VL_HEAP_type   float
00016 #include <vl/heap-def.h>
00017 
00018 #include <stdio.h>
00019 
00020 typedef struct _S { int x ; } S ;
00021 int s_cmp (S const * v, vl_uindex a, vl_uindex b)
00022 {
00023   return v[a].x - v[b].x ;
00024 }
00025 void s_swap (S * v, vl_uindex a, vl_uindex b)
00026 {
00027   S t = v[a] ;
00028   v[a] = v[b] ;
00029   v[b] = t ;
00030   printf("Swapping %" VL_FMT_UINDEX "x with %" VL_FMT_UINDEX "\n", a, b) ;
00031 }
00032 
00033 #define VL_HEAP_prefix  s_heap
00034 #define VL_HEAP_type    S
00035 #define VL_HEAP_cmp     s_cmp
00036 #include <vl/heap-def.h>
00037 
00038 #define VL_HEAP_prefix  track_s_heap
00039 #define VL_HEAP_type    S
00040 #define VL_HEAP_cmp     s_cmp
00041 #define VL_HEAP_swap    s_swap
00042 #include <vl/heap-def.h>
00043 
00044 typedef struct _H {
00045   vl_size numNodes ;
00046   int* array ;
00047 } H ;
00048 int h_cmp (H const * h, vl_uindex a, vl_uindex b) {
00049   return h->array[a] - h->array[b] ;
00050 }
00051 void h_swap (H * h, vl_uindex a, vl_uindex b) {
00052   int t = h->array[a] ;
00053   h->array[a] = h->array[b] ;
00054   h->array[b] = t ;
00055 }
00056 #define VL_HEAP_prefix      h_heap
00057 #define VL_HEAP_array       H*
00058 #define VL_HEAP_array_const H const*
00059 #define VL_HEAP_swap        h_swap
00060 #define VL_HEAP_cmp         h_cmp
00061 #include <vl/heap-def.h>
00062 
00063 int
00064 main (int argc VL_UNUSED, char** argv VL_UNUSED)
00065 {
00066   vl_uindex i ;
00067   vl_size numNodes = 0 ;
00068   float data [] = {1.01, 5.02, 8, 0.1, 100, 3, 9, 4, 1.02} ;
00069   S data_s [] = {{5}, {7}, {9}, {1}} ;
00070   S data_s_track [] = {{5}, {7}, {9}, {1}} ;
00071   int data_h [] = {5, 7, 9, 1} ;
00072   H h ;
00073   h.numNodes = 0 ;
00074   h.array = data_h ;
00075 
00076   printf("Pushing heap\n") ;
00077   for (i = 0 ; i < sizeof(data) / sizeof(data[0]) ; ++i) {
00078     printf ("%5" VL_FMT_UINDEX ": %f\n", i, data[i]) ;
00079     vl_heap_float_push (data, &numNodes) ;
00080   }
00081 
00082   printf("Popping heap\n") ;
00083   for (i = 0 ; i < sizeof(data) / sizeof(data[0]) ; ++i) {
00084     printf ("%" VL_FMT_UINDEX ": %f\n", i, data[vl_heap_float_pop (data, &numNodes)]) ;
00085   }
00086 
00087   printf("Refilling, updating fourth element, and popping again\n") ;
00088   for (i = 0 ; i < sizeof(data) / sizeof(data[0]) ; ++i) {
00089     vl_heap_float_push (data, &numNodes) ;
00090   }
00091   printf("%f -> %f\n", data[3], 9.01) ;
00092   data [3] = 9.01 ;
00093   vl_heap_float_update (data, numNodes, 3) ;
00094   for (i = 0 ; i < sizeof(data) / sizeof(data[0]) ; ++i) {
00095     printf ("%" VL_FMT_UINDEX ":  %f\n", i, data[vl_heap_float_pop (data, &numNodes)]) ;
00096   }
00097 
00098   printf("Pushing heap of structures\n") ;
00099   numNodes = 0 ;
00100   for (i = 0 ; i < sizeof(data_s) / sizeof(data_s[0]) ; ++i) {
00101     printf ("s[%" VL_FMT_UINDEX "].x = %d\n", i, data_s[i].x) ;
00102     s_heap_push (data_s, &numNodes) ;
00103   }
00104 
00105   printf("Popping heap of structures\n") ;
00106   for (i = 0 ; i < sizeof(data_s) / sizeof(data_s[0]) ; ++i) {
00107     printf ("s[%" VL_FMT_UINDEX "].x = %d\n", i, data_s[s_heap_pop (data_s, &numNodes)].x) ;
00108   }
00109 
00110   printf("Pushing heap of structures with custom swap\n") ;
00111   numNodes = 0 ;
00112   for (i = 0 ; i < sizeof(data_s) / sizeof(data_s[0]) ; ++i) {
00113     printf ("s[%" VL_FMT_UINDEX "].x = %d\n", i, data_s_track[i].x) ;
00114     track_s_heap_push (data_s_track, &numNodes) ;
00115   }
00116 
00117   printf("Popping heap of structures with custom swap\n") ;
00118   for (i = 0 ; i < sizeof(data_s) / sizeof(data_s[0]) ; ++i) {
00119     printf ("s[%" VL_FMT_UINDEX "].x = %d\n", i, data_s_track
00120                [track_s_heap_pop (data_s_track, &numNodes)].x) ;
00121   }
00122 
00123   printf("Pushing heap of structures with custom container\n") ;
00124   numNodes = 0 ;
00125   for (i = 0 ; i < sizeof(data_h) / sizeof(data_h[0]) ; ++i) {
00126     printf ("s[%" VL_FMT_UINDEX "].x = %d\n", i, h.array[i]) ;
00127     h_heap_push (&h, &h.numNodes) ;
00128   }
00129 
00130   printf("Popping heap of structures with custom container\n") ;
00131   for (i = 0 ; i < sizeof(data_h) / sizeof(data_h[0]) ; ++i) {
00132     printf ("s[%" VL_FMT_UINDEX "].x = %d\n", i, h.array
00133                [h_heap_pop (&h, &h.numNodes)]) ;
00134   }
00135 
00136   return 0 ;
00137 }


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51