eg_keytab.ex.c

This is a simple example of the usage of symbol tables using key table structure

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 EGkeytab */
00022 /** @addtogroup EGkeytab */
00023 /** @{ */
00024 /* ========================================================================= */
00025 /** @brief This program reads a file given from the input and try to put all
00026  * words within the symbol table. 
00027  * */
00028 /* ========================================================================= */
00029 #include "EGlib.h"
00030 /* ========================================================================= */
00031 /** @brief number of keys to generate */
00032 static int nkeys = 100;
00033 static int nlookup = 1000;
00034 /* ========================================================================= */
00035 /** @brief parsing arguments */
00036 void keytab_usage(char*program)
00037 {
00038   fprintf(stdout,"Usage: %s [options]\n",program);
00039   fprintf(stdout,"Options:\n\t-n n number of keys to generate\n");
00040   fprintf(stdout,"\t-l n number of keys to lookup\n");
00041 }
00042 /* ========================================================================= */
00043 /** @brief parse input */
00044 int keytab_parse(int argc,char**argv)
00045 {
00046   int c;
00047   while((c = getopt(argc,argv,"l:n:")) != EOF)
00048   {
00049     switch(c)
00050     {
00051       case 'l':
00052         nlookup = atoi(optarg);
00053         break;
00054       case 'n':
00055         nkeys = atoi(optarg);
00056         break;
00057       default:
00058         keytab_usage(argv[0]);
00059         return 1;
00060     }
00061   }
00062   return 0;
00063 }
00064 /* ========================================================================= */
00065 /** @brief main function */
00066 int main (int argc,char**argv)
00067 {
00068   int rval=0,i,pos;
00069   uint64_t key;
00070   EGrandState_t g1;
00071   EGkeytab_t keytab;
00072   EGrandInit(&g1);
00073   EGkeytabInit(&keytab);
00074   rval = keytab_parse(argc,argv);
00075   CHECKRVALG(rval,CLEANUP);
00076   /* now we generate the keys */
00077   for( i = 0 ; i < nkeys ; i++)
00078   {
00079     key = EGrand(&g1);
00080     key = key << 32;
00081     key = EGrand(&g1);
00082     rval = EGkeytabAdd( &keytab, key, 1);
00083     rval = 0;
00084   }
00085   for( i = 0 ; i < nlookup ; i++)
00086   {
00087     key = EGrand(&g1);
00088     key = key << 32;
00089     key = EGrand(&g1);
00090     if(EGkeytabLookUp(&keytab,key,&pos))
00091       fprintf(stdout,"Found key %"PRIu64" in position %d\n",key,pos);
00092   }
00093   CLEANUP:
00094   EGkeytabClear(&keytab);
00095   return rval;
00096 }
00097 /* ========================================================================= */
00098 /** @} */

Generated on Tue Oct 27 15:45:36 2009 for EGlib by  doxygen 1.4.6