This commit is contained in:
krahets
2024-04-12 03:16:45 +08:00
parent 8b8168bb31
commit b5f94abec9
10 changed files with 217 additions and 101 deletions

View File

@ -1458,18 +1458,21 @@ index = hash(key) % capacity
/* 基於陣列實現的雜湊表 */
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]);
@ -1503,13 +1506,13 @@ index = hash(key) % capacity
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);
@ -1527,13 +1530,13 @@ index = hash(key) % capacity
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++;
@ -1549,13 +1552,13 @@ index = hash(key) % capacity
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++;