Cum se folosește Hash Set

Prezentare teoretică a Hash Set-urilor

Introducere

Temelia implementează hash set-ul folosind structura hash_set_t
1 struct _hash_set_t
2 {
3     // Vector of linked lists, each containing keys with the same hash.
4     vector_t back_bone;
5 };

Exemple

Inițializare

1 hash_set_t set;
2 set = hash_set_new(N);
3 hash_set_delete(set);

Metode de acces

1 PRINT("Empty %d\n", hash_set_is_empty(set));
2 PRINT("Size %d\n", hash_set_get_size(set));
3 PRINT("%d %d\n", hash_set_contains(set, x[5], hash, NULL, int_compare, NULL),
4     hash_set_contains(set, NULL, NULL, NULL, NULL, NULL));

Inserție, căutare, ștergere

Căutarea este simplă, pot obține
  • Vectorul de liste de coliziuni, după funcția de hash dată de utilizator
  • Lista de coliziuni, cu toate cheile având un anumit hash
 1 for (i = 0; i < N * N; i++)
 2 {
 3     result = hash_set_put(set, x[i], hash, NULL, int_compare, NULL);
 4     if (result == 1)
 5         PRINT("Added ");
 6     else
 7         PRINT("Already exists ");
 8     PRINT("%d\n", x[i][0]);
 9 }
10 
11 for (i = 0; i < N * N / 2; i++)
12     hash_set_remove(set, x[i], hash, NULL, int_compare, NULL);
13 
14 backbone_vector = hash_set_get_backbone(hash_set_t hash_set);
15 collision_list = hash_set_get_collision_list(hash_set_t hash_set, int hash_value);

Alte funcții

1 hash_set_clear(set);
2 hash_set_iterate(set, int_handler, stdout);

Disponibil și în: HTML TXT