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