src/libs/hash_lib.c
void hash_table_access_replace(Hash_Table* table, int64 key,
                               void* replacement) {
  // {{{ access hash table using simple key compare
  uns               index    = HASH_INDEX(table, key);
  Hash_Table_Entry* bucket   = table->entries[index];
  Hash_Table_Entry* new_hash = NULL;
  Hash_Table_Entry* temp;
  Hash_Table_Entry* prev      = NULL;
  Flag              new_entry = FALSE;
  UNUSED(new_entry);
  ASSERT(0, replacement);
  ASSERT(0, index < table->buckets);
  for(temp = bucket; temp != NULL; temp = temp->next) {
    if(temp->key == key) {
      /* May not want to free the memory in case there are other valid pointers
         to it. ASSERT(0,temp->data); free(table->data_size, temp->data);
      */
      temp->data = replacement;
      return;
    } else {
      prev = temp;
    }
  }
  table->count++;
  new_entry = TRUE;
  new_hash  = (Hash_Table_Entry*)smalloc(sizeof(Hash_Table_Entry));
  ASSERT(0, new_hash);
  new_hash->key  = key;
  new_hash->next = NULL;
  new_hash->data = replacement;

  if(prev)
    prev->next = new_hash;
  else
    table->entries[index] = new_hash;

  _DEBUGA(0, 0, "smalloc'd %ld bytes for %s (%d entries)\n",
          (unsigned long int)sizeof(Hash_Table_Entry), table->name,
          table->count);
  // }}}
}
/**************************************************************************************/
// hash_table_access_replace: replace the data in an existing entry, or create
// it
//                            if it doesn't exist yet
replace the data in an existing entry, or create
it
if it doesn't exist yet
replace the data in an existing entry or create it if it doesn ' t exist yet
void hash_table_access_replace(Hash_Table* table, int64 key,
                               void* replacement) {
  uns               index    = HASH_INDEX(table, key);
  Hash_Table_Entry* bucket   = table->entries[index];
  Hash_Table_Entry* new_hash = NULL;
  Hash_Table_Entry* temp;
  Hash_Table_Entry* prev      = NULL;
  Flag              new_entry = FALSE;
  UNUSED(new_entry);
  ASSERT(0, replacement);
  ASSERT(0, index < table->buckets);
  for(temp = bucket; temp != NULL; temp = temp->next) {
    if(temp->key == key) {
      temp->data = replacement;
      return;
    } else {
      prev = temp;
    }
  }
  table->count++;
  new_entry = TRUE;
  new_hash  = (Hash_Table_Entry*)smalloc(sizeof(Hash_Table_Entry));
  ASSERT(0, new_hash);
  new_hash->key  = key;
  new_hash->next = NULL;
  new_hash->data = replacement;
  if(prev)
    prev->next = new_hash;
  else
    table->entries[index] = new_hash;
  _DEBUGA(0, 0, "smalloc'd %ld bytes for %s (%d entries)\n",
          (unsigned long int)sizeof(Hash_Table_Entry), table->name,
          table->count);
}
void hash_table_access_replace ( Hash_Table * table , int64 key , void * replacement ) { uns index = HASH_INDEX ( table , key ) ; Hash_Table_Entry * bucket = table -> entries [ index ] ; Hash_Table_Entry * new_hash = NULL ; Hash_Table_Entry * temp ; Hash_Table_Entry * prev = NULL ; Flag new_entry = FALSE ; UNUSED ( new_entry ) ; ASSERT ( 0 , replacement ) ; ASSERT ( 0 , index < table -> buckets ) ; for ( temp = bucket ; temp != NULL ; temp = temp -> next ) { if ( temp -> key == key ) { temp -> data = replacement ; return ; } else { prev = temp ; } } table -> count ++ ; new_entry = TRUE ; new_hash = ( Hash_Table_Entry * ) smalloc ( sizeof ( Hash_Table_Entry ) ) ; ASSERT ( 0 , new_hash ) ; new_hash -> key = key ; new_hash -> next = NULL ; new_hash -> data = replacement ; if ( prev ) prev -> next = new_hash ; else table -> entries [ index ] = new_hash ; _DEBUGA ( 0 , 0 , " \n " , ( unsigned long int ) sizeof ( Hash_Table_Entry ) , table -> name , table -> count ) ; }
hash_table_access_replace: replace the data in an existing entry, or create
it
if it doesn't exist yet
hash_table_access_replace : replace the data in an existing entry or create it if it doesn ' t exist yet
// {{{ access hash table using simple key compare /* May not want to free the memory in case there are other valid pointers
         to it. ASSERT(0,temp->data); free(table->data_size, temp->data);
      */ // }}}