/** * File: array_hash_map.c * Created Time: 2023-03-18 * Author: Guanngxu (446678850@qq.com) */ #include "../utils/common.h" /* 해시 테이블의 기본 크기 */ #define MAX_SIZE 100 /* 키-값 쌍 int->string */ typedef struct { int key; char *val; } Pair; /* 키-값 쌍의 집합 */ typedef struct { void *set; int len; } MapSet; /* 배열로 구현한 해시 테이블 */ typedef struct { Pair *buckets[MAX_SIZE]; } ArrayHashMap; /* 생성자 */ ArrayHashMap *newArrayHashMap() { ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap)); for (int i=0; i < MAX_SIZE; i++) { hmap->buckets[i] = NULL; } return hmap; } /* 소멸자 */ 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); } /* 해시 함수 */ int hashFunc(int key) { int index = key % MAX_SIZE; return index; } /* 조회 연산 */ 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; } /* 추가 연산 */ 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; } /* 삭제 연산 */ void removeItem(ArrayHashMap *hmap, const int key) { int index = hashFunc(key); free(hmap->buckets[index]->val); free(hmap->buckets[index]); hmap->buckets[index] = NULL; } /* 모든 키-값 쌍 반환 */ void pairSet(ArrayHashMap *hmap, MapSet *set) { Pair *entries; int i = 0, index = 0; int total = 0; /* 유효한 키-값 쌍의 개수를 센다 */ 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; } /* 모든 키 반환 */ void keySet(ArrayHashMap *hmap, MapSet *set) { int *keys; int i = 0, index = 0; int total = 0; /* 유효한 키-값 쌍의 개수를 센다 */ 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; } /* 모든 값 반환 */ void valueSet(ArrayHashMap *hmap, MapSet *set) { char **vals; int i = 0, index = 0; int total = 0; /* 유효한 키-값 쌍의 개수를 센다 */ 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; } /* 해시 테이블 출력 */ 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); } /* 실행 코드 */ int main() { /* 해시 테이블 초기화 */ ArrayHashMap *hmap = newArrayHashMap(); /* 추가 연산 */ // 해시 테이블에 (key, value) 키-값 쌍을 추가한다 put(hmap, 12836, "小哈"); put(hmap, 15937, "小啰"); put(hmap, 16750, "小算"); put(hmap, 13276, "小法"); put(hmap, 10583, "小鸭"); printf("\n추가한 후의 해시 테이블:\n키 -> 값\n"); print(hmap); /* 조회 연산 */ // 해시 테이블에 키 key를 입력하여 값 value를 얻는다 const char *name = get(hmap, 15937); printf("\n학번 15937을 입력하여 조회한 이름: %s\n", name); /* 삭제 연산 */ // 해시 테이블에서 (key, value) 키-값 쌍을 삭제한다 removeItem(hmap, 10583); printf("\n10583을 삭제한 후의 해시 테이블:\n키 -> 값\n"); print(hmap); /* 해시 테이블 순회 */ int i; printf("\n키-값 쌍(키->값) 순회\n"); print(hmap); MapSet set; keySet(hmap, &set); int *keys = (int *)set.set; printf("\n키 Key만 순회\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("\n키 Value만 순회\n"); for (i = 0; i < set.len; i++) { printf("%s\n", vals[i]); } free(set.set); delArrayHashMap(hmap); return 0; }