eg_ebtree.ex.c

Go to the documentation of this file.
00001 /* EGlib "Efficient General Library" provides some basic structures and
00002  * algorithms commons in many optimization algorithms.
00003  *
00004  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00005  * 
00006  * This library is free software; you can redistribute it and/or modify it
00007  * under the terms of the GNU Lesser General Public License as published by the
00008  * Free Software Foundation; either version 2.1 of the License, or (at your
00009  * option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful, but 
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00013  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00014  * License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public License
00017  * along with this library; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00019  * */
00020 /** @file
00021  * @ingroup EGeBTree
00022  * */
00023 /** @addtogroup EGeBTree */
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 /** @brief usage function, if we give the wrong number of parameters, we return
00037  * an error message and print a help.
00038  * @param program Name of the command from comand-line
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   /* we allways exit with an error code */
00049   exit (1);
00050 }
00051 
00052 /* ========================================================================= */
00053 /** @brief parse external arguments.
00054  * @param argc int, number of parameters to process.
00055  * @param argv char**, array of the parameters.
00056  * @param z unsigned int*, pointer to the number of elements in the tree.
00057  * @param s unsigned int*, pointer to the seed.
00058  * @return zero on success, non-zero otherwise */
00059 static inline int ebt_parseargs (int argc,
00060                                  char **argv,
00061                                  unsigned int *s,
00062                                  unsigned int *z)
00063 {
00064   /* local variables */
00065   int c;
00066   /* set initial values */
00067   *z = 0;
00068   *s = 1;
00069   /* scan the input */
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 /** @brief display the given tree in-order.
00101  * @param out_f output stream.
00102  * @param root tree to display.
00103  * @return zero on success, non-zero otherwise. */
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 /** @brief Tester program for the projection structure and functions
00131  * @param argc int number of comand line options
00132  * @param argv char** array of strings of length argc contaianing the command
00133  * line arguments.
00134  * @return zero on success, non-zero otherwise 
00135  * @par Description:
00136  * This function create a set of 'z' elements in a bbtree, and print the
00137  * resulting tree, perform some random operations.
00138  * */
00139 int main (int argc,
00140           char **argv)
00141 {
00142   /* local variables */
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   /* now process the input */
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   /* first create a random tree */
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   /* now we depopulate the random tree */
00172   for (n = z; --n;)
00173   {
00174     rval = EGeBTreeDel (all_nodes + n);
00175     CHECKRVALG (rval, CLEANUP);
00176     ebt_display (stderr, my_tree);
00177   }
00178   /* re create tree */
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   /* now we create the objective function */
00212 CLEANUP:
00213   if (all_nodes)
00214     EGfree (all_nodes);
00215   return rval;
00216 }
00217 
00218 /* ========================================================================= */
00219 /** @} */
00220 /* end of eg_ebtree.ex.c */

Generated on Mon Jan 30 08:48:53 2006 for EGlib by  doxygen 1.4.5