00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <limits.h>
00028 #include <math.h>
00029 #include <time.h>
00030 #include <float.h>
00031 #include <getopt.h>
00032 #include "eg_ebtree.h"
00033 #include "eg_mempool.h"
00034 static int verbose = 0;
00035
00036
00037
00038
00039
00040 static inline void ebt_usage (char const *const program)
00041 {
00042 fprintf (stderr, "Usage: %s [options]\n", program);
00043 fprintf (stderr, "Options:\n");
00044 fprintf (stderr, " -v Be verbose on screen\n");
00045 fprintf (stderr, " -s unsigned int, seed of the RNG\n");
00046 fprintf (stderr, " -z unsigned int, number of elements to "
00047 "generate in the tree\n");
00048
00049 exit (1);
00050 }
00051
00052
00053
00054
00055
00056
00057
00058
00059 static inline int ebt_parseargs (int argc,
00060 char **argv,
00061 unsigned int *s,
00062 unsigned int *z)
00063 {
00064
00065 int c;
00066
00067 *z = 0;
00068 *s = 1;
00069
00070 while ((c = getopt (argc, argv, "s:z:v")) != EOF)
00071 {
00072 switch (c)
00073 {
00074 case 's':
00075 *s = atoi (optarg);
00076 break;
00077 case 'z':
00078 *z = atoi (optarg);
00079 break;
00080 case 'v':
00081 verbose = 1;
00082 break;
00083 default:
00084 ebt_usage (argv[0]);
00085 }
00086 }
00087 if (*z < 1)
00088 {
00089 ebt_usage (argv[0]);
00090 return 1;
00091 }
00092 fprintf (stderr, "Running %s with options:\n", argv[0]);
00093 fprintf (stderr, " verbose %s\n", verbose ? "on" : "off");
00094 fprintf (stderr, " size %7u\n", *z);
00095 fprintf (stderr, " seed %7u\n", *s);
00096 return 0;
00097 }
00098
00099
00100
00101
00102
00103
00104 static inline int ebt_display (FILE * out_f,
00105 EGeBTree_t * root)
00106 {
00107 unsigned sz = 0;
00108 EGeBTree_t *it = EGeBTreeGetFirst (root);
00109 if (verbose)
00110 fprintf (out_f, "Display for tree %p:\n", (void *) root);
00111 for (; it; it = EGeBTreeGetNext (it))
00112 {
00113 if (verbose)
00114 fprintf (out_f, "%d ", it - root);
00115 sz++;
00116 }
00117 if (verbose)
00118 fprintf (out_f, "\nReverse order:\n");
00119 for (it = EGeBTreeGetLast (root); it; it = EGeBTreeGetPrev (it))
00120 {
00121 if (verbose)
00122 fprintf (out_f, "%d ", it - root);
00123 }
00124 if (verbose)
00125 fprintf (out_f, "\n\tsize: %u\n", sz);
00126 return 0;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 int main (int argc,
00140 char **argv)
00141 {
00142
00143 EGeBTree_t *all_nodes = 0;
00144 EGeBTree_t *my_tree = 0;
00145 unsigned int n,
00146 z,
00147 s;
00148 int rval = 0;
00149
00150
00151 rval = ebt_parseargs (argc, argv, &s, &z);
00152 CHECKRVALG (rval, CLEANUP);
00153 srandom (s);
00154 my_tree = all_nodes = EGsMalloc (EGeBTree_t, z);
00155 EGeBTreeInit (my_tree);
00156 ebt_display (stderr, my_tree);
00157
00158
00159 for (n = 1; n < z; n++)
00160 {
00161 if (verbose)
00162 fprintf (stderr, "Adding %d to the tree...\n", n);
00163 EGeBTreeInit (all_nodes + n);
00164 if ((n - 1) & 1)
00165 rval = EGeBTreeAddLeft (all_nodes + (n - 1) / 2, all_nodes + n);
00166 else
00167 rval = EGeBTreeAddRight (all_nodes + (n - 1) / 2, all_nodes + n);
00168 CHECKRVALG (rval, CLEANUP);
00169 ebt_display (stderr, my_tree);
00170 }
00171
00172 for (n = z; --n;)
00173 {
00174 rval = EGeBTreeDel (all_nodes + n);
00175 CHECKRVALG (rval, CLEANUP);
00176 ebt_display (stderr, my_tree);
00177 }
00178
00179 for (n = 1; n < z; n++)
00180 {
00181 if (verbose)
00182 fprintf (stderr, "Adding %d to the tree...\n", n);
00183 if ((n - 1) & 1)
00184 rval = EGeBTreeAddLeft (all_nodes + (n - 1) / 2, all_nodes + n);
00185 else
00186 rval = EGeBTreeAddRight (all_nodes + (n - 1) / 2, all_nodes + n);
00187 CHECKRVALG (rval, CLEANUP);
00188 ebt_display (stderr, my_tree);
00189 }
00190 if (verbose)
00191 fprintf (stderr, "delete %d of the tree...\n", z - 1);
00192 rval = EGeBTreeDel (all_nodes + z - 1);
00193 CHECKRVALG (rval, CLEANUP);
00194 ebt_display (stderr, my_tree);
00195 if (verbose)
00196 fprintf (stderr, "replace %d with %d in the tree...\n", z / 2, z - 1);
00197 rval = EGeBTreeReplace (all_nodes + (z / 2), all_nodes + z - 1);
00198 CHECKRVALG (rval, CLEANUP);
00199 ebt_display (stderr, my_tree);
00200 if (verbose)
00201 fprintf (stderr, "split tree at node %d...\n", 1);
00202 rval = EGeBTreeCut (all_nodes + 1);
00203 CHECKRVALG (rval, CLEANUP);
00204 ebt_display (stderr, my_tree);
00205 ebt_display (stderr, all_nodes + 1);
00206 if (verbose)
00207 fprintf (stderr, "glue them back\n");
00208 rval = EGeBTreeAddRight (my_tree, all_nodes + 1);
00209 CHECKRVALG (rval, CLEANUP);
00210 ebt_display (stderr, my_tree);
00211
00212 CLEANUP:
00213 if (all_nodes)
00214 EGfree (all_nodes);
00215 return rval;
00216 }
00217
00218
00219
00220