/** * File: array_hash_map.c * Created Time: 2023-03-18 * Author: Guanngxu (446678850@qq.com) */ #include "../utils/common.h" /* Default size of hashtable */ #define MAX_SIZE 100 /* Key-value pair int->string */ typedef struct { int key; char *val; } Pair; /* Collection of key-value pairs */ typedef struct { void *set; int len; } MapSet; /* Hash table based on array implementation */ typedef struct { Pair *buckets[MAX_SIZE]; } ArrayHashMap; /* Constructor */ ArrayHashMap *newArrayHashMap() { ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap)); for (int i=0; i < MAX_SIZE; i++) { hmap->buckets[i] = NULL; } return hmap; } /* Destructor */ void delArrayHashMap(ArrayHashMap *hmap) { for (int i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { free(hmap->buckets[i]->val); free(hmap->buckets[i]); } } free(hmap); } /* Hash function */ int hashFunc(int key) { int index = key % MAX_SIZE; return index; } /* Query operation */ const char *get(const ArrayHashMap *hmap, const int key) { int index = hashFunc(key); const Pair *Pair = hmap->buckets[index]; if (Pair == NULL) return NULL; return Pair->val; } /* Add operation */ void put(ArrayHashMap *hmap, const int key, const char *val) { Pair *Pair = malloc(sizeof(Pair)); Pair->key = key; Pair->val = malloc(strlen(val) + 1); strcpy(Pair->val, val); int index = hashFunc(key); hmap->buckets[index] = Pair; } /* Remove operation */ void removeItem(ArrayHashMap *hmap, const int key) { int index = hashFunc(key); free(hmap->buckets[index]->val); free(hmap->buckets[index]); hmap->buckets[index] = NULL; } /* Get all key-value pairs */ void pairSet(ArrayHashMap *hmap, MapSet *set) { Pair *entries; int i = 0, index = 0; int total = 0; /* Count effective key-value pairs */ for (i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { total++; } } entries = malloc(sizeof(Pair) * total); for (i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { entries[index].key = hmap->buckets[i]->key; entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1); strcpy(entries[index].val, hmap->buckets[i]->val); index++; } } set->set = entries; set->len = total; } /* Get all keys */ void keySet(ArrayHashMap *hmap, MapSet *set) { int *keys; int i = 0, index = 0; int total = 0; /* Count effective key-value pairs */ for (i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { total++; } } keys = malloc(total * sizeof(int)); for (i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { keys[index] = hmap->buckets[i]->key; index++; } } set->set = keys; set->len = total; } /* Get all values */ void valueSet(ArrayHashMap *hmap, MapSet *set) { char **vals; int i = 0, index = 0; int total = 0; /* Count effective key-value pairs */ for (i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { total++; } } vals = malloc(total * sizeof(char *)); for (i = 0; i < MAX_SIZE; i++) { if (hmap->buckets[i] != NULL) { vals[index] = hmap->buckets[i]->val; index++; } } set->set = vals; set->len = total; } /* Print hash table */ void print(ArrayHashMap *hmap) { int i; MapSet set; pairSet(hmap, &set); Pair *entries = (Pair *)set.set; for (i = 0; i < set.len; i++) { printf("%d -> %s\n", entries[i].key, entries[i].val); } free(set.set); } /* Driver Code */ int main() { /* Initialize hash table */ ArrayHashMap *hmap = newArrayHashMap(); /* Add operation */ // Add key-value pair (key, value) to the hash table put(hmap, 12836, "Ha"); put(hmap, 15937, "Luo"); put(hmap, 16750, "Suan"); put(hmap, 13276, "Fa"); put(hmap, 10583, "Ya"); printf("\nAfter adding, the hashtable is\nKey -> Value\n"); print(hmap); /* Query operation */ // Enter key to the hash table, get value const char *name = get(hmap, 15937); printf("\nInput student ID 15937, found name %s\n", name); /* Remove operation */ // Remove key-value pair (key, value) from the hash table removeItem(hmap, 10583); printf("\nAfter removing 10583, the hashtable is\nKey -> Value\n"); print(hmap); /* Traverse hash table */ int i; printf("\nTraverse key-value pairs Key->Value\n"); print(hmap); MapSet set; keySet(hmap, &set); int *keys = (int *)set.set; printf("\nTraverse keys only\n"); for (i = 0; i < set.len; i++) { printf("%d\n", keys[i]); } free(set.set); valueSet(hmap, &set); char **vals = (char **)set.set; printf("\nTraverse values only\n"); for (i = 0; i < set.len; i++) { printf("%s\n", vals[i]); } free(set.set); delArrayHashMap(hmap); return 0; }