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

_table.c File Reference

#include "rcs_defs.hh"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "_table.h"
#include "dbg_mem.h"
#include "rcs_prnt.hh"

Include dependency graph for _table.c:

Include dependency graph

Go to the source code of this file.

Functions

int table_new (_RCS_TABLE *table, size_t dsize)
int table_delete (_RCS_TABLE *table)
unsigned long int * table_nth (_RCS_TABLE *table, unsigned long int nth)
int table_add (_RCS_TABLE *table, unsigned long int key, const void *data)
int table_get (_RCS_TABLE *table, unsigned long int key, void *data)
int table_clear (_RCS_TABLE *table, unsigned long int key)
int table_clearall (_RCS_TABLE *table)
void table_print (_RCS_TABLE *table)


Function Documentation

int table_new _RCS_TABLE   table,
size_t    dsize
 

Definition at line 15 of file _table.c.

00016 {
00017   int div, rem;
00018 
00019   table->table = NULL;
00020   table->dsize = dsize;
00021   /* align data to multiple of sizeof(unsigned long int) */
00022   div = dsize / sizeof (unsigned long int);
00023   rem = dsize % sizeof (unsigned long int);
00024   if (rem)
00025     {
00026       table->lsize = div + 1;
00027     }
00028   else
00029     {
00030       table->lsize = div;
00031     }
00032   table->size = 0;
00033   table->count = 0;
00034   return 0;
00035 }

int table_delete _RCS_TABLE   table
 

Definition at line 38 of file _table.c.

00039 {
00040   if (table == NULL)
00041     {
00042       return -1;
00043     }
00044 
00045   if (table->table == NULL)
00046     {
00047       return -1;
00048     }
00049   else
00050     {
00051 #ifdef sparcworks_sun4          /* free is defined as int free(char *)
00052                                    for this platform
00053                                    it should be void free(void *); */
00054       DEBUG_FREE ((char *) table->table);
00055 #else
00056       DEBUG_FREE (table->table);
00057 #endif
00058       return 0;
00059     }
00060 }

unsigned long int* table_nth _RCS_TABLE   table,
unsigned long int    nth
[static]
 

Definition at line 63 of file _table.c.

Referenced by table_add(), table_clear(), table_clearall(), table_get(), and table_print().

00064 {
00065   if (NULL == table)
00066     {
00067       return 0;
00068     }
00069   if (NULL == table->table)
00070     {
00071       return 0;
00072     }
00073   return &table->table[nth * (1 + table->lsize)];
00074 }

int table_add _RCS_TABLE   table,
unsigned long int    key,
const void *    data
 

Definition at line 77 of file _table.c.

00078 {
00079   int t;
00080   _RCS_TABLE old = *table;      /* structure copy */
00081   unsigned long int *ptr;
00082   unsigned long int *oldptr;
00083 
00084   /* check for non-zero key */
00085   if (0 == key)
00086     {
00087       return -1;
00088     }
00089 
00090   if (table == NULL)
00091     {
00092       return -1;
00093     }
00094 
00095   /* if uninitialized, create a table of one and mark the slot free */
00096   if (table->table == NULL)
00097     {
00098       table->size = 1;
00099       table->table = (unsigned long *) DEBUG_CALLOC (table->size,
00100                                                      (1 +
00101                                                       table->lsize) *
00102                                                      sizeof (unsigned long
00103                                                              int));
00104       table->table[0] = 0;
00105     }
00106 
00107   /* add entry at first 0 slot or overwrite a matched key */
00108   for (t = 0; t < table->size; t++)
00109     {
00110       ptr = table_nth (table, t);
00111       if (ptr[0] == 0)
00112         {
00113           ptr[0] = key;
00114           memcpy (&ptr[1], data, table->dsize);
00115           return 0;
00116         }
00117       if (ptr[0] == key)
00118         {
00119           memcpy (&ptr[1], data, table->dsize);
00120           table->count = 1;
00121           return 0;
00122         }
00123     }
00124 
00125   /* if we got here, table must be full-- grow the table,
00126      and add the entry */
00127   table->size *= 2;
00128   table->table = (unsigned long *) DEBUG_CALLOC (table->size,
00129                                                  (1 +
00130                                                   table->lsize) *
00131                                                  sizeof (unsigned long int));
00132   for (t = 0; t < table->size / 2; t++)
00133     {
00134       ptr = table_nth (table, t);
00135       oldptr = table_nth (&old, t);
00136       ptr[0] = oldptr[0];
00137       memcpy (&ptr[1], &oldptr[1], table->dsize);
00138     }
00139   if (NULL != old.table)
00140     {
00141       DEBUG_FREE (old.table);
00142     }
00143   /* add the entry at the first new slot */
00144   ptr = table_nth (table, table->size / 2);
00145   ptr[0] = key;
00146   memcpy (&ptr[1], data, table->dsize);
00147   /* and initialize the rest */
00148   for (t = table->size / 2 + 1; t < table->size; t++)
00149     {
00150       ptr = table_nth (table, t);
00151       ptr[0] = 0;
00152     }
00153   table->count++;
00154   return 0;
00155 }

int table_get _RCS_TABLE   table,
unsigned long int    key,
void *    data
 

Definition at line 158 of file _table.c.

00159 {
00160   int t;
00161   unsigned long int *ptr;
00162 
00163   if (key == 0)
00164     return -1;
00165 
00166   if (table == NULL)
00167     {
00168       return -1;
00169     }
00170 
00171   for (t = 0; t < table->size; t++)
00172     {
00173       ptr = table_nth (table, t);
00174       if (ptr[0] == key)
00175         {
00176           memcpy (data, &ptr[1], table->dsize);
00177           return 0;
00178         }
00179     }
00180   /* got to end with no match-- return invalid flag */
00181   return -1;
00182 }

int table_clear _RCS_TABLE   table,
unsigned long int    key
 

Definition at line 185 of file _table.c.

00186 {
00187   int t;
00188   unsigned long int *ptr;
00189 
00190   if (key == 0)
00191     return -1;
00192 
00193   if (table == NULL)
00194     {
00195       return -1;
00196     }
00197 
00198   for (t = 0; t < table->size; t++)
00199     {
00200       ptr = table_nth (table, t);
00201       if (ptr[0] == key)
00202         {
00203           ptr[0] = 0;
00204           table->count--;
00205           return 0;
00206         }
00207     }
00208   /* got to end with no match-- return invalid flag */
00209   return -1;
00210 }

int table_clearall _RCS_TABLE   table
 

Definition at line 213 of file _table.c.

00214 {
00215   int t;
00216   unsigned long int *ptr;
00217 
00218 
00219   if (table == NULL)
00220     {
00221       return -1;
00222     }
00223 
00224   for (t = 0; t < table->size; t++)
00225     {
00226       ptr = table_nth (table, t);
00227       ptr[0] = 0;
00228     }
00229   table->size = 0;
00230   table->count = 0;
00231   table->dsize = 0;             /* acutal size of each data, in bytes */
00232   table->lsize = 0;             /* upped size of each data, in longs */
00233   if (NULL != table->table)
00234     {
00235       free (table->table);
00236       table->table = NULL;      /* ptr to first entry */
00237     }
00238 
00239   return 0;
00240 }

void table_print _RCS_TABLE   table
 

Definition at line 244 of file _table.c.

00245 {
00246   int t;
00247   unsigned long int *ptr;
00248   unsigned char *cptr;
00249   int i;
00250 
00251   rcs_print ("*** TABLE PRINT ***\n");
00252   if (table == NULL)
00253     {
00254       return;
00255     }
00256 
00257   rcs_print ("size = %d /* # of slots in table */\n", table->size);
00258   rcs_print ("dsize = %d /* actal size of each data, in bytes */\n",
00259              table->dsize);
00260   rcs_print ("lsize = %d /* upped size of each data, in longs */\n",
00261              table->lsize);
00262   rcs_print ("count = %d /* # of full slots */\n", table->count);
00263   for (t = 0; t < table->size; t++)
00264     {
00265       ptr = table_nth (table, t);
00266       if (ptr[0] == 0)
00267         {
00268           continue;
00269         }
00270       rcs_print ("%lu : ", ptr[0]);
00271       cptr = (char *) ptr;
00272       for (i = 0; i < 32 && i < table->dsize + 4; i++)
00273         {
00274           if (0 == i % 4)
00275             {
00276               rcs_print (" ");
00277             }
00278           rcs_print ("%2.2X", cptr[i]);
00279         }
00280       rcs_print ("\n");
00281     }
00282   fflush (stdout);
00283 }


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