Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

dictionary.c

00001 /*
00002  *  dictionary.c - "Dictionary" data structure, works a bit like std::map.
00003  *                 This file is part of the FreeLCD package.
00004  *
00005  *  $Id: dictionary_8c-source.html,v 1.1 2003/02/16 22:50:41 unicorn Exp $
00006  *
00007  *  This program is free software; you can redistribute it and/or modify it
00008  *  under the terms of the GNU General Public License as published by the
00009  *  Free Software Foundation; either version 2 of the License, or (at your
00010  *  option) any later version.
00011  * 
00012  *  This program is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *  GNU General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU General Public License
00018  *  along with this program; if not, write to the Free Software
00019  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00020  *  MA  02111-1307  USA
00021  *
00022  *  Copyright (c) 2002, 2003, Jeroen van den Berg <unicorn@hippie.nu>
00023  */
00024 
00025 #include <string.h>
00026 #include <stdio.h>
00027 #include <assert.h>
00028 
00029 #include "dictionary.h"
00030 
00031 /*-------------------------------------------------------- dict_lookup --*/
00032 void *
00033 dict_lookup (const dictionary *d, const char *key)
00034 {
00035   dict_pair *top;
00036   dict_pair *middle;
00037   dict_pair *bottom;
00038   int result;
00039 
00040   assert (d);
00041   assert (key);
00042 
00043   top = d->dict;
00044   bottom = d->dict + d->size;
00045 
00046   while (top < bottom)
00047     {
00048       middle = top + ((bottom - top) / 2);
00049 
00050       result = strcmp (key, middle->key);
00051       if (result == 0)
00052         return middle->value;
00053 
00054       if (result < 0)
00055         bottom = middle;
00056       else
00057         top = middle + 1;
00058     }
00059 
00060   return 0;
00061 }
00062 
00063 
00064 
00065 #ifdef UNIT_TEST_DICTIONARY_C
00066 
00067 #include <stdio.h>
00068 
00069 int lookup (const dictionary* dict, const char* key)
00070 {
00071   int *valptr = (int*)(dict_lookup (dict, key));
00072 
00073   return valptr ? *valptr : 999;
00074 }
00075 
00076 int main (int argc, char **argv)
00077 {
00078   int values[] = { 5, 6, 7, 8, 9 };
00079   int result;
00080   int i;
00081   
00082   dict_pair pair_array[] = { 
00083       { "aardbei", &values[0] },
00084       { "banaan", &values[1] },
00085       { "citroen", &values[2] },
00086       { "druif", &values[3] },
00087       { "eland", &values[4] } };
00088 
00089   dictionary dict = { pair_array, 5 };
00090   
00091   for (i = 0 ; i < 5 ; ++i)
00092     {
00093       result = lookup (&dict, pair_array[i].key);
00094       if (result != values[i])
00095         {
00096           printf ("Lookup of parameter %s returned %i, expected %i/n",
00097                   pair_array[i].key, result, values[i]);
00098           exit (1);
00099         }
00100     }
00101 
00102   if (lookup (&dict, "xxx") != 999)
00103     {
00104       printf ("Lookup of non-existing parameter did not fail, " \
00105               "but should have/n");
00106       exit (1);
00107     }
00108 
00109   exit (0);
00110 }
00111 
00112 #endif

Generated on Sun Feb 16 23:39:49 2003 for FreeLCD by doxygen1.2.18