Exemplo n.º 1
0
/*
 * Resizing
 * The number of elements in a hash table is not always known when creating the table.
 * If the number of elements grows too large, it will seriously reduce the performance of most hash table operations.
 * If the number of elements are reduced, the hash table will waste memory. That is why we provide a function for resizing the table.
 * Resizing a hash table is not as easy as a realloc(). All hash values must be recalculated and each element must be inserted into its new position.
 * We create a temporary obj_hash_table_t object (newtbl) to be used while building the new hashes.
 * This allows us to reuse hashtable_insert() and hashtable_remove(), when moving the elements to the new table.
 * After that, we can just free the old table and copy the elements from newtbl to hashtbl.
 */
hashtable_rc_t obj_hashtable_resize(obj_hash_table_t *hashtblP, hash_size_t sizeP)
{
  obj_hash_table_t       newtbl;
  hash_size_t        n;
  obj_hash_node_t       *node,*next;

  if (hashtblP == NULL) {
    return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
  }

  newtbl.size     = sizeP;
  newtbl.hashfunc = hashtblP->hashfunc;

  if(!(newtbl.nodes=calloc(sizeP, sizeof(obj_hash_node_t*)))) return HASH_TABLE_SYSTEM_ERROR;

  for(n=0; n<hashtblP->size; ++n) {
    for(node=hashtblP->nodes[n]; node; node=next) {
      next = node->next;
      obj_hashtable_insert(&newtbl, node->key, node->key_size, node->data);
      obj_hashtable_remove(hashtblP, node->key, node->key_size);
    }
  }

  free(hashtblP->nodes);
  hashtblP->size=newtbl.size;
  hashtblP->nodes=newtbl.nodes;

  return HASH_TABLE_OK;
}
Exemplo n.º 2
0
//-----------------------------------------------------------------------------
int rrc_ue_ral_delete_all_thresholds_type(unsigned int mod_idP, ral_link_param_type_t *param_type_pP)
//-----------------------------------------------------------------------------
{
    hashtable_rc_t           rc;
    rrc_ral_threshold_key_t *key;
    rrc_ral_threshold_key_t *keys = NULL;
    unsigned int             num_keys = 0;
    int                      return_code = 0;

    rc =  obj_hashtable_get_keys(UE_rrc_inst[mod_idP].ral_meas_thresholds, (void*)&keys, &num_keys);
    if (rc == HASH_TABLE_OK) {
        key = keys;
        while (num_keys > 0) {
            if (memcmp(&key->link_param_type, param_type_pP, sizeof(ral_link_param_type_t)) == 0) {
                rc = obj_hashtable_remove (UE_rrc_inst[mod_idP].ral_meas_thresholds, key, sizeof(rrc_ral_threshold_key_t));
                if (rc != HASH_TABLE_OK) {
                    return_code = -1;
                }
            }
            key = &key[1];
            num_keys--;
        }
    } else {
        return_code = -1;
    }

    if (keys != NULL) {
        free(keys);
    }
    return return_code;
}
Exemplo n.º 3
0
//-----------------------------------------------------------------------------
int rrc_ue_ral_delete_threshold(unsigned int mod_idP, ral_link_param_type_t *param_type_pP, ral_threshold_t *threshold_pP)
//-----------------------------------------------------------------------------
{
    hashtable_rc_t           rc;
    rrc_ral_threshold_key_t  ref_key;

    memcpy(&ref_key.link_param_type, param_type_pP, sizeof(ral_link_param_type_t));
    memcpy(&ref_key.threshold,       threshold_pP,  sizeof(ral_threshold_t));
    rc = obj_hashtable_remove (UE_rrc_inst[mod_idP].ral_meas_thresholds, (void*)&ref_key, sizeof(rrc_ral_threshold_key_t));
    if (rc == HASH_TABLE_OK) {
        return 0;
    } else {
        return -1;
    }
}
Exemplo n.º 4
0
/*
   Resizing
   The number of elements in a hash table is not always known when creating the table.
   If the number of elements grows too large, it will seriously reduce the performance of most hash table operations.
   If the number of elements are reduced, the hash table will waste memory. That is why we provide a function for resizing the table.
   Resizing a hash table is not as easy as a realloc(). All hash values must be recalculated and each element must be inserted into its new position.
   We create a temporary obj_hash_table_t object (newtbl) to be used while building the new hashes.
   This allows us to reuse hashtable_insert() and hashtable_free(), when moving the elements to the new table.
   After that, we can just free the old table and copy the elements from newtbl to hashtbl.
*/
hashtable_rc_t
obj_hashtable_resize (
  obj_hash_table_t * const hashtblP,
  const hash_size_t sizeP)
{
  obj_hash_table_t                        newtbl;
  hash_size_t                             n;
  obj_hash_node_t                        *node,
                                         *next;
  void                                   *dummy = NULL;

  if (hashtblP == NULL) {
#if HASHTABLE_DEBUG
    fprintf (stderr, "obj_hashtable_resize return BAD_PARAMETER_HASHTABLE\n");
#endif
    return HASH_TABLE_BAD_PARAMETER_HASHTABLE;
  }

  newtbl.size = sizeP;
  newtbl.hashfunc = hashtblP->hashfunc;

  if (!(newtbl.nodes = calloc (sizeP, sizeof (obj_hash_node_t *))))
    return HASH_TABLE_SYSTEM_ERROR;

  for (n = 0; n < hashtblP->size; ++n) {
    for (node = hashtblP->nodes[n]; node; node = next) {
      next = node->next;
#warning "TODO obj_hashtable_resize()"
      obj_hashtable_insert (&newtbl, node->key, node->key_size, node->data);
      obj_hashtable_remove (hashtblP, node->key, node->key_size, &dummy);
    }
  }

  free (hashtblP->nodes);
  hashtblP->size = newtbl.size;
  hashtblP->nodes = newtbl.nodes;
#if HASHTABLE_DEBUG
  fprintf (stdout, "obj_hashtable_resize return OK\n");
#endif
  return HASH_TABLE_OK;
}