|
|
|
@ -7,7 +7,7 @@
|
|
|
|
|
#include "../utils/common.h"
|
|
|
|
|
|
|
|
|
|
/* 雜湊表預設大小 */
|
|
|
|
|
#define HASHTABLE_CAPACITY 100
|
|
|
|
|
#define 100
|
|
|
|
|
|
|
|
|
|
/* 鍵值對 int->string */
|
|
|
|
|
typedef struct {
|
|
|
|
@ -23,18 +23,21 @@ typedef struct {
|
|
|
|
|
|
|
|
|
|
/* 基於陣列實現的雜湊表 */
|
|
|
|
|
typedef struct {
|
|
|
|
|
Pair *buckets[HASHTABLE_CAPACITY];
|
|
|
|
|
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 < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
for (int i = 0; i < MAX_SIZE; i++) {
|
|
|
|
|
if (hmap->buckets[i] != NULL) {
|
|
|
|
|
free(hmap->buckets[i]->val);
|
|
|
|
|
free(hmap->buckets[i]);
|
|
|
|
@ -45,7 +48,7 @@ void delArrayHashMap(ArrayHashMap *hmap) {
|
|
|
|
|
|
|
|
|
|
/* 雜湊函式 */
|
|
|
|
|
int hashFunc(int key) {
|
|
|
|
|
int index = key % HASHTABLE_CAPACITY;
|
|
|
|
|
int index = key % MAX_SIZE;
|
|
|
|
|
return index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -83,13 +86,13 @@ void pairSet(ArrayHashMap *hmap, MapSet *set) {
|
|
|
|
|
int i = 0, index = 0;
|
|
|
|
|
int total = 0;
|
|
|
|
|
/* 統計有效鍵值對數量 */
|
|
|
|
|
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
for (i = 0; i < MAX_SIZE; i++) {
|
|
|
|
|
if (hmap->buckets[i] != NULL) {
|
|
|
|
|
total++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
entries = malloc(sizeof(Pair) * total);
|
|
|
|
|
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
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);
|
|
|
|
@ -107,13 +110,13 @@ void keySet(ArrayHashMap *hmap, MapSet *set) {
|
|
|
|
|
int i = 0, index = 0;
|
|
|
|
|
int total = 0;
|
|
|
|
|
/* 統計有效鍵值對數量 */
|
|
|
|
|
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
for (i = 0; i < MAX_SIZE; i++) {
|
|
|
|
|
if (hmap->buckets[i] != NULL) {
|
|
|
|
|
total++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
keys = malloc(total * sizeof(int));
|
|
|
|
|
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
for (i = 0; i < MAX_SIZE; i++) {
|
|
|
|
|
if (hmap->buckets[i] != NULL) {
|
|
|
|
|
keys[index] = hmap->buckets[i]->key;
|
|
|
|
|
index++;
|
|
|
|
@ -129,13 +132,13 @@ void valueSet(ArrayHashMap *hmap, MapSet *set) {
|
|
|
|
|
int i = 0, index = 0;
|
|
|
|
|
int total = 0;
|
|
|
|
|
/* 統計有效鍵值對數量 */
|
|
|
|
|
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
for (i = 0; i < MAX_SIZE; i++) {
|
|
|
|
|
if (hmap->buckets[i] != NULL) {
|
|
|
|
|
total++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
vals = malloc(total * sizeof(char *));
|
|
|
|
|
for (i = 0; i < HASHTABLE_CAPACITY; i++) {
|
|
|
|
|
for (i = 0; i < MAX_SIZE; i++) {
|
|
|
|
|
if (hmap->buckets[i] != NULL) {
|
|
|
|
|
vals[index] = hmap->buckets[i]->val;
|
|
|
|
|
index++;
|
|
|
|
|