Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

table.hh

Go to the documentation of this file.
00001 
00002 #ifndef TABLE_HH
00003 #define TABLE_HH
00004 
00005 #ifdef USE_TEMPLATES
00006 
00007 #include "rcs_defs.hh"
00008 
00009 extern "C"
00010 {
00011 #ifndef NO_STDIO
00012 #include <stdio.h>              /* NULL */
00013 #endif
00014 
00015 }
00016 
00017 template < class T > class RCS_TABLE
00018 {
00019 public:
00020   RCS_TABLE ()
00021   {
00022     size = 0;
00023     table = NULL;
00024   };
00025   ~RCS_TABLE ()
00026   {
00027     if (table != NULL)
00028       delete table;
00029   };
00030   int add (unsigned long int key, const T & data);
00031   int get (unsigned long int key, T & data);
00032   int clear (unsigned long int key);
00033   void print (void);
00034 private:
00035   unsigned long int size;
00036   struct RCS_TABLE_ENTRY
00037   {
00038     unsigned long int key;
00039     T data;
00040   }
00041    *table;
00042 };
00043 
00044 /* NOTE:  actual code for template member function definitions must
00045    be included in the header file, since this code does not generate
00046    any executable when compiled-- it needs to be templated before this
00047    can occur. */
00048 
00049 template < class T > int RCS_TABLE < T >::add (unsigned long int key,
00050                                                const T & data)
00051 {
00052   int t;
00053   RCS_TABLE_ENTRY *old;
00054 
00055   /* if uninitialized, create a table of one, and make an entry */
00056   if (table == NULL)
00057     {
00058       size = 1;
00059       table = new RCS_TABLE_ENTRY;
00060       table[0].key = key;
00061       table[0].data = data;
00062       return 0;
00063     }
00064 
00065   /* if we got here, table must have been initialized--
00066      add entry at first 0 slot or overwrite a matched key */
00067   for (t = 0; t < size; t++)
00068     {
00069       if (table[t].key == 0 || table[t].key == key)
00070         {
00071           table[t].key = key;
00072           table[t].data = data;
00073           return 0;
00074         }
00075     }
00076 
00077   /* if we got here, table must be full-- grow the table,
00078      and add the entry */
00079   /* NOTE:  I tried realloc() on table, but it appeared that
00080      the data was corrupted in some places (the third entry, for
00081      example).  realloc() may screw with the alignment, so I
00082      took the safe if less efficient route of copying from the
00083      old table to the new table, and deleting the old. */
00084   old = table;
00085   table = new RCS_TABLE_ENTRY[size * 2];
00086   for (t = 0; t < size; t++)
00087     {
00088       table[t].key = old[t].key;
00089       table[t].data = old[t].data;
00090     }
00091   delete old;
00092   /* add the entry at the first new slot */
00093   table[size].key = key;
00094   table[size].data = data;
00095   /* and initialize the rest */
00096   for (t = size + 1; t < size * 2; t++)
00097     {
00098       table[t].key = 0;
00099     }
00100   size *= 2;
00101   return 0;
00102 }
00103 
00104 template < class T > int RCS_TABLE < T >::get (unsigned long int key,
00105                                                T & data)
00106 {
00107   int t;
00108 
00109   for (t = 0; t < size; t++)
00110     {
00111       if (table[t].key == key)
00112         {
00113           data = table[t].data;
00114           return 0;
00115         }
00116     }
00117   /* got to end with no match-- return invalid flag */
00118   return -1;
00119 }
00120 
00121 template < class T > int RCS_TABLE < T >::clear (unsigned long int key)
00122 {
00123   int t;
00124 
00125   for (t = 0; t < size; t++)
00126     {
00127       if (table[t].key == key)
00128         {
00129           table[t].key = 0;
00130           return 0;
00131         }
00132     }
00133   /* got to end with no match-- return invalid flag */
00134   return -1;
00135 }
00136 
00137 template < class T > void RCS_TABLE < T >::print ()
00138 {
00139   int t;
00140 
00141   for (t = 0; t < size; t++)
00142     {
00143       printf ("%lu\t%X\n", table[t].key, &table[t].data);
00144     }
00145 }
00146 
00147 #else /* no TEMPLATES */
00148 
00149 extern "C"
00150 {
00151 #include "_table.h"
00152 }
00153 
00154 class RCS_TABLE
00155 {
00156 public:
00157   RCS_TABLE (size_t dsize);
00158   ~RCS_TABLE ();
00159   int add (unsigned long int key, const void *data);
00160   int get (unsigned long int key, void *data);
00161   int clear (unsigned long int key);
00162   void print (void);
00163 private:
00164     _RCS_TABLE table;
00165 };
00166 
00167 #endif /* TEMPLATES  */
00168 
00169 #endif /* TABLE_HH */

Generated on Sun Dec 2 15:56:52 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001