eg_bbtree.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 EGbbtree
00022  * */
00023 /** @addtogroup EGbbtree */
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 #ifdef LINUX
00032 #include <getopt.h>
00033 #endif
00034 #include "eg_mempool.h"
00035 #include "eg_bbtree.h"
00036 /* ========================================================================= */
00037 /** @brief usage function, if we give the wrong number of parameters, we return
00038  * an error message and print a help.
00039  * @param program Name of the command from comand-line
00040  * */
00041 static inline void usage (char const *const program)
00042 {
00043   fprintf (stderr, "Usage: %s [options]\n", program);
00044   fprintf (stderr, "Options:\n");
00045   fprintf (stderr, "    -s   unsigned int, seed of the RNG\n");
00046   fprintf (stderr,
00047            "    -z   unsigned int, number of elements to 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 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:")) != 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     default:
00081       usage (argv[0]);
00082     }
00083   }
00084   if (*z < 1)
00085     usage (argv[0]);
00086   fprintf (stderr, "Running %s with options:\n", argv[0]);
00087   fprintf (stderr, " size         %7u\n", *z);
00088   fprintf (stderr, " seed         %7u\n", *s);
00089   return 0;
00090 }
00091 
00092 /* ========================================================================= */
00093 /** @brief Tester program for the projection structure and functions
00094  * @param argc int number of comand line options
00095  * @param argv char** array of strings of length argc contaianing the command
00096  * line arguments.
00097  * @return zero on success, non-zero otherwise 
00098  * @par Description:
00099  * This function create a set of 'z' elements in a bbtree, and print the
00100  * resulting tree, perform some random operations.
00101  * */
00102 int main (int argc,
00103           char **argv)
00104 {
00105   /* local variables */
00106   EGmemPool_t *mem = EGnewMemPool (1024, EGmemPoolNewSize, 4096);
00107   EGbbtree_t *tree = EGnewBbtree (mem, EGptCompare);
00108   EGbbtreeNode_t *c_node;
00109   unsigned int n,
00110     z,
00111     s;
00112   int rval = 0;
00113 
00114   /* now process the input */
00115   parseargs (argc, argv, &s, &z);
00116   srandom (s);
00117 
00118   /* first create a random tree */
00119   for (n = 0; n < z; n++)
00120   {
00121     fprintf (stderr, "Adding %d to the tree...", n);
00122     c_node = EGbbtreeAdd (tree, (void *) n);
00123     if (c_node && c_node->this == (void *) n)
00124       fprintf (stderr, "done\r");
00125     else
00126     {
00127       fprintf (stderr, "Not found!\n");
00128       exit (1);
00129     }
00130     //EGbbtreeDisplay(tree,EGnullDisplay,stderr);
00131   }
00132   /* now we half depopulate the random tree */
00133   for (n = z / 2; n < z; n++)
00134   {
00135     fprintf (stderr, "Search %d in the tree...", n);
00136     c_node = EGbbtreeFind (tree, (void *) n);
00137     if (c_node && c_node->this == (void *) n)
00138       fprintf (stderr, "done\r");
00139     else
00140     {
00141       fprintf (stderr, "Not found!\n");
00142       exit (1);
00143     }
00144     fprintf (stderr, "Removing %d from the tree    \r", n);
00145     rval = EGbbtreeRemove (tree, c_node);
00146     CHECKRVAL (rval);
00147   }
00148   /* now we half populate the random tree */
00149   for (n = z / 2; n < z; n++)
00150   {
00151     fprintf (stderr, "Adding %d to the tree...", n);
00152     c_node = EGbbtreeAdd (tree, (void *) n);
00153     if (c_node && c_node->this == (void *) n)
00154       fprintf (stderr, "done\r");
00155     else
00156     {
00157       fprintf (stderr, "Not found!\n");
00158       exit (1);
00159     }
00160   }
00161   /* now we half depopulate the random tree */
00162   for (n = 0; n < z / 2; n++)
00163   {
00164     fprintf (stderr, "Search %d in the tree...", n);
00165     c_node = EGbbtreeFind (tree, (void *) n);
00166     if (c_node && c_node->this == (void *) n)
00167       fprintf (stderr, "done\r");
00168     else
00169     {
00170       fprintf (stderr, "Not found!\n");
00171       exit (1);
00172     }
00173     fprintf (stderr, "Removing %d from the tree    \r", n);
00174     rval = EGbbtreeRemove (tree, c_node);
00175     CHECKRVAL (rval);
00176   }
00177   /* now we half populate the random tree */
00178   for (n = 0; n < z / 2; n++)
00179   {
00180     fprintf (stderr, "Adding %d to the tree...", n);
00181     c_node = EGbbtreeAdd (tree, (void *) n);
00182     if (c_node && c_node->this == (void *) n)
00183       fprintf (stderr, "done\r");
00184     else
00185     {
00186       fprintf (stderr, "Not found!\n");
00187       exit (1);
00188     }
00189   }
00190   /* now we depopulate the random tree */
00191   for (n = 0; n < z; n++)
00192   {
00193     fprintf (stderr, "Search %d in the tree...", n);
00194     c_node = EGbbtreeFind (tree, (void *) n);
00195     if (c_node && c_node->this == (void *) n)
00196       fprintf (stderr, "done\r");
00197     else
00198     {
00199       fprintf (stderr, "Not found!\n");
00200       exit (1);
00201     }
00202     fprintf (stderr, "Removing %d from the tree    \r", n);
00203     rval = EGbbtreeRemove (tree, c_node);
00204     CHECKRVAL (rval);
00205   }
00206   /* re-create a random tree */
00207   for (n = 0; n < z; n++)
00208   {
00209     fprintf (stderr, "Adding %d to the tree...", n);
00210     c_node = EGbbtreeAdd (tree, (void *) n);
00211     if (c_node && c_node->this == (void *) n)
00212       fprintf (stderr, "done\r");
00213     else
00214     {
00215       fprintf (stderr, "Not found!\n");
00216       exit (1);
00217     }
00218     //EGbbtreeDisplay(tree,EGnullDisplay,stderr);
00219   }
00220   /* now we clear it */
00221   rval = EGbbtreeClear (tree, nullFree);
00222   CHECKRVAL (rval);
00223 
00224   /* now we free it */
00225   EGfreeBbtree (tree);
00226   EGfreeMemPool (mem);
00227   fprintf (stderr, "\n");
00228 
00229   /* now we create the objective function */
00230   return rval;
00231 }
00232 
00233 /* ========================================================================= */
00234 /* @} */
00235 /* end of eg_bbtree.c */

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