TutHashDelete


Name

TutHashDelete — delete a hash table entry by key

Synopsis

T_PTR TutHashDelete(table, key) 
T_HASH_TABLE table; 
T_PTR key; 

Arguments

table — hash table to delete entry from

key — key used to look up value to be deleted

Return Values

TutHashDelete returns the value that was deleted from the hash table, or NULL if the value could not be found.

Diagnostics

None

Description

TutHashDelete deletes an entry from table. If an entry is found with key, it is deleted from table and the entry is returned. If the entry is not found, NULL is returned.

Caution

Always verify the result of this function. After the deletion, you must free the space occupied by the deleted entry and key. If the key is stored as part of the entry, a single free on the returned pointer is enough to free the space. Otherwise, you need two frees: one for the deleted entry and one for the deleted key.

See Also

TutHashInsert, TutHashBucketSetValue

Examples

This example creates a hash table with string keys, adds and deletes items:

typedef struct entry { 
  T_STR name; 
  T_PTR addr; 
} ENTRY; 
 
T_PTR print_entry(key, value, arg) 
T_PTR key; 
T_PTR value; 
T_PTR arg; 
{ 
  TutOut("Name <%s> has address <%s>\n", key, ((ENTRY 
*)value)->addr); 
   return NULL; 
} 
int main() 
{ 
  T_HASH_TABLE table; 
  ENTRY *e; 
  ENTRY *tofree; 
  /* Initialize the hash table */ 
  table = TutHashCreateStr(4); 
  
   /* Allocate one entry */ 
   e = TutMalloc(sizeof(struct entry)); 
   e->name = TutMalloc(25); 
   e->addr = TutMalloc(50); 
  
  /* Set some values in the entry */ 
   strcpy(e->name, "Johnson"); 
   strcpy(e->addr, "1234 Mulberry Lane"); 
 
   /* Insert the entry into the table */ 
   TutHashInsert(table, e->name, e); 
 
   /* Allocate the second entry */ 
   e = TutMalloc(sizeof(ENTRY)); 
   e->name = TutMalloc(25); 
   e->addr = TutMalloc(50); 
  
  /* Set some values in the second entry */ 
   strcpy(e->name, "Jones"); 
   strcpy(e->addr, "10 Downing Street"); 
 
   /* Insert the second entry into the table */ 
   TutHashInsert(table, e->name, e); 
 
   /* Echo the whole table */ 
   TutOut("\nFirst time:\n"); 
   TutHashTraverse(table, print_entry, NULL); 
 
   /* Delete one entry */ 
   tofree = TutHashDelete(table, "Johnson"); 
   if (tofree != NULL) { 
     TutOut("Deleted entry for <%s>\n", tofree->name); 
     TutFree(tofree->name); 
     TutFree(tofree->addr); 
     TutFree(tofree); 
  } 
   /* Echo the whole table again */ 
   TutOut("\nSecond time:\n"); 
   TutHashTraverse(table, print_entry, (T_PTR)NULL);  
} /* main */ 

This example produces this output:

First time: 
Name <Jones> has address <10 Downing Street> 
Name <Johnson> has address <1234 Mulberry Lane> 
Deleted entry for <Johnson> 
 
Second time: 
Name <Jones> has address <10 Downing Street> 

It is your responsibility to allocate space for the key and for the entry to be inserted before calling TutHashInsert.


TIBCO SmartSockets™ Utilities
Software Release 6.8, July 2006
Copyright © TIBCO Software Inc. All rights reserved
www.tibco.com