Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

ht_add.c

Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu>
00003 **
00004 ** This library is free software; you can redistribute it and/or
00005 ** modify it under the terms of the GNU Library General Public
00006 ** License as published by the Free Software Foundation; either
00007 ** version 2 of the License, or (at your option) any later version.
00008 **
00009 ** This library is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 ** Library General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU Library General Public
00015 ** License along with this library; if not, write to the Free
00016 ** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00017 ** MA 02111-1307, USA
00018 **
00019 ** @(#)$Id: ht_add.c,v 1.4 2006/07/13 05:36:16 klmitch Exp $
00020 */
00028 #include "dbprim.h"
00029 #include "dbprim_int.h"
00030 
00031 RCSTAG("@(#)$Id: ht_add.c,v 1.4 2006/07/13 05:36:16 klmitch Exp $");
00032 
00033 unsigned long
00034 ht_add(hash_table_t *table, hash_entry_t *entry, db_key_t *key)
00035 {
00036   unsigned long retval;
00037 
00038   initialize_dbpr_error_table(); /* initialize error table */
00039 
00040   if (!ht_verify(table) || !he_verify(entry) || !key) /* verify arguments */
00041     return DB_ERR_BADARGS;
00042 
00043   if (entry->he_table) /* it's already in a table... */
00044     return DB_ERR_BUSY;
00045 
00046   if (table->ht_flags & HASH_FLAG_FREEZE) /* don't add to frozen tables */
00047     return DB_ERR_FROZEN;
00048 
00049   if (!table->ht_table && !(table->ht_flags & HASH_FLAG_AUTOGROW))
00050     return DB_ERR_NOTABLE;
00051 
00052   /* It looks like we could optimize here, but don't be deceived--the table
00053    * may grow between here and when we fill in the entry's hash value, and
00054    * that would change the hash value.
00055    */
00056   if (!ht_find(table, 0, key)) /* don't permit duplicates */
00057     return DB_ERR_DUPLICATE;
00058 
00059   /* grow the table if necessary and allowed */
00060   if ((table->ht_count + 1) > table->ht_rollover &&
00061       (table->ht_flags & HASH_FLAG_AUTOGROW) &&
00062       (retval = ht_resize(table, _hash_fuzz(table->ht_count + 1))))
00063     return retval;
00064 
00065   /* Force the link element to point to the entry */
00066   le_object(&entry->he_elem) = entry;
00067 
00068   /* copy key value into the hash entry */
00069   entry->he_key = *key; /* thank goodness for structure copy! */
00070 
00071   /* get the hash value for the entry */
00072   entry->he_hash =
00073     (*table->ht_func)(table, &entry->he_key) % table->ht_modulus;
00074 
00075   /* Now add the entry to the table... */
00076   if ((retval = ll_add(&table->ht_table[entry->he_hash], &entry->he_elem,
00077                        LINK_LOC_HEAD, 0)))
00078     return retval;
00079 
00080   table->ht_count++; /* OK, now increment element count */
00081 
00082   entry->he_table = table; /* point entry at table */
00083 
00084   return 0;
00085 }

Generated on Sat Jul 15 14:10:33 2006 for DatabasePrimitivesLibrary by  doxygen 1.4.4