TutHashReplace


Name

TutHashReplace — replace a value stored in the hash table

Synopsis

T_PTR TutHashReplace(table, key, new_value) 
T_HASH_TABLE table; 
T_PTR key; 
T_PTR new_value; 

Arguments

table — hash table where the value should be replaced

key — the key to the value

new_value — the new value that is stored in the table

Return Values

The old value if successful, or NULL if no old value exists.

Diagnostics

None

Description

TutHashReplace replaces the existing value in the hash table that matches the key with new_value. If the existing value cannot be located, the new_value is inserted in the table. In that case, the call to TutHashReplace is equivalent to a call to TutHashInsert.

After replacing, it is up to you to deallocate the space occupied by the replaced value.

Caution

Hash tables do not manage storage for their keys and values. It is up to the caller to manage the memory allocation of the data structures for the keys and values.

new_value cannot be a null pointer.

See Also

TutHashInsert

Examples

This example replaces a value in the hash bucket:

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(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); 
 
  /* Allocate a replacement entry */ 
   e = TutMalloc(sizeof(ENTRY)); 
   e->name = TutMalloc(25); 
   e->addr = TutMalloc(50); 
  
   /* Set some values in the replacement entry */ 
   strcpy(e->name, "Johnson"); 
   strcpy(e->addr, "555 Main Street"); 
 
   /* Replace the old entry with the replacement entry */ 
   tofree = TutHashReplace(table, e->name, e); 
   if (tofree != NULL) { 
     TutOut("Replaced <%s> with <%s> for <%s>\n",  
            tofree->addr, e->addr, e->name); 
     TutFree(tofree->name); 
     TutFree(tofree->addr); 
     TutFree(tofree); 
   } 
 
   /* Echo the whole table again */ 
   TutOut("\nSecond time:\n"); 
   TutHashTraverse(table, print_entry, NULL);  
} 

The example produces this output:

First time: 
Name <Jones> has address <10 Downing Street> 
Name <Johnson> has address <1234 Mulberry Lane> 
Replaced <1234 Mulberry Lane> with <555 Main Street> for <Johnson> 
 
Second time: 
Name <Jones> has address <10 Downing Street> 
Name <Johnson> has address <555 Main Street> 

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