eg_keytab.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 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   EGlib_info();
00073   EGlib_version();
00074   EGrandInit(&g1);
00075   EGkeytabInit(&keytab);
00076   rval = keytab_parse(argc,argv);
00077   CHECKRVALG(rval,CLEANUP);
00078   /* set signal and limits */
00079   EGsigSet(rval,CLEANUP);
00080   EGsetLimits(3600.0,4294967295UL);
00081   /* now we generate the keys */
00082   for( i = 0 ; i < nkeys ; i++)
00083   {
00084     key = EGrand(&g1);
00085     key = key << 32;
00086     key = EGrand(&g1);
00087     rval = EGkeytabAdd( &keytab, key, 1);
00088     rval = 0;
00089   }
00090   for( i = 0 ; i < nlookup ; i++)
00091   {
00092     key = EGrand(&g1);
00093     key = key << 32;
00094     key = EGrand(&g1);
00095     if(EGkeytabLookUp(&keytab,key,&pos))
00096       fprintf(stdout,"Found key %"PRIu64" in position %d\n",key,pos);
00097   }
00098   CLEANUP:
00099   EGkeytabClear(&keytab);
00100   return rval;
00101 }
00102 /* ========================================================================= */
00103 /** @} */