Пример #1
0
hash_table::hash_table (size_t size,
                        unsigned (*hash_function) (hash_table_entry_t el_ptr),
                        int (*eq_function) (hash_table_entry_t el1_ptr,
                                            hash_table_entry_t el2_ptr))
{
  hash_table_entry_t *entry_ptr;

  size = higher_prime_number (size);
  entries
    = (hash_table_entry_t *) allocate::malloc (size
                                               * sizeof (hash_table_entry_t));
  this->_size = size;
  this->hash_function = hash_function;
  this->eq_function = eq_function;
  number_of_elements = 0;
  number_of_deleted_elements = 0;
  collisions = 0;
  searches = 0;
  for (entry_ptr = entries; entry_ptr < entries + size; entry_ptr++)
    *entry_ptr = EMPTY_ENTRY;
}
Пример #2
0
Файл: hash.c Проект: 5kg/gdb
struct bfd_hash_entry *
bfd_hash_insert (struct bfd_hash_table *table,
		 const char *string,
		 unsigned long hash)
{
  struct bfd_hash_entry *hashp;
  unsigned int _index;

  hashp = (*table->newfunc) (NULL, table, string);
  if (hashp == NULL)
    return NULL;
  hashp->string = string;
  hashp->hash = hash;
  _index = hash % table->size;
  hashp->next = table->table[_index];
  table->table[_index] = hashp;
  table->count++;

  if (!table->frozen && table->count > table->size * 3 / 4)
    {
      unsigned long newsize = higher_prime_number (table->size);
      struct bfd_hash_entry **newtable;
      unsigned int hi;
      unsigned long alloc = newsize * sizeof (struct bfd_hash_entry *);

      /* If we can't find a higher prime, or we can't possibly alloc
	 that much memory, don't try to grow the table.  */
      if (newsize == 0 || alloc / sizeof (struct bfd_hash_entry *) != newsize)
	{
	  table->frozen = 1;
	  return hashp;
	}

      newtable = ((struct bfd_hash_entry **)
		  objalloc_alloc ((struct objalloc *) table->memory, alloc));
      if (newtable == NULL)
	{
	  table->frozen = 1;
	  return hashp;
	}
      memset (newtable, 0, alloc);

      for (hi = 0; hi < table->size; hi ++)
	while (table->table[hi])
	  {
	    struct bfd_hash_entry *chain = table->table[hi];
	    struct bfd_hash_entry *chain_end = chain;

	    while (chain_end->next && chain_end->next->hash == chain->hash)
	      chain_end = chain_end->next;

	    table->table[hi] = chain_end->next;
	    _index = chain->hash % newsize;
	    chain_end->next = newtable[_index];
	    newtable[_index] = chain;
	  }
      table->table = newtable;
      table->size = newsize;
    }

  return hashp;
}