Пример #1
0
Файл: evidence.c Проект: gaibo/C
int main(void) {
    printf("*****testing hash table*****\n\n");
    htbl* t = ht_new(&good_hash, 10);
    
    FILE* cnets2015 = fopen("cnets2015", "r");
    while (!feof(cnets2015)) {
        char* s = alloc_str(256);
        fgets(s, 256, cnets2015);
        char* l = trim_newline(s);
        free(s);
        if (strlen(l) > 0) {
            ht_ins(l, t);
        }
        free(l);
    }
    fclose(cnets2015);
    
    ht_show(t);
    printf("The hash table has %u buckets with %u entries (load factor %lg).\n\n",
        t->n_buckets, 
        ht_n_entries(t),
        ht_load_factor(t));
    printf("The bucket with the most items in it contains %u items.\n\n",
        ht_max_bucket(t));
    printf("ht_member:\n");
    printf("membership of cnet \"aardvark\" : %i\n", ht_member("aardvark", t));
    printf("membership of cnet \"borja\"    : %i\n", ht_member("borja", t));
    printf("\n");
    
    // resize
    printf("***now resizing***\n");
    ht_resize(&t);
    printf("***resizing complete***\n\n");
    
    // do everything again
    ht_show(t);
    printf("The hash table has %u buckets with %u entries (load factor %lg).\n\n",
        t->n_buckets, 
        ht_n_entries(t),
        ht_load_factor(t));
    printf("The bucket with the most items in it contains %u items.\n\n",
        ht_max_bucket(t));
    printf("ht_member:\n");
    printf("membership of cnet \"aardvark\" : %i\n", ht_member("aardvark", t));
    printf("membership of cnet \"borja\"    : %i\n", ht_member("borja", t));
    printf("\n");
    
    ht_free(t);
    return 0;
}
Пример #2
0
void ht_insert(smb_ht *table, DATA key, DATA value)
{
  unsigned int index;
  if (ht_load_factor(table) >= HASH_TABLE_MAX_LOAD_FACTOR) {
    ht_resize(table);
  }

  // First, probe for the key as if we're trying to return it.  If we find it,
  // we update the existing key.
  index = ht_find_retrieve(table, key);
  if (table->table[index].mark == HT_FULL) {
    table->table[index].value = value;
    return;
  }

  // If we don't find the key, then we find the first open slot or gravestone.
  index = ht_find_insert(table, key);
  table->table[index].key = key;
  table->table[index].value = value;
  table->table[index].mark = HT_FULL;
  table->length++;
}