OAIP_Mirror/lab26/TextProcessingDict/DictHashOpenAddress.c

79 lines
1.7 KiB
C
Raw Normal View History

2024-12-10 20:55:19 +04:00
#include "Dict.h"
#ifdef DICT_HASH_OPEN_ADDRESS_C
2024-12-06 22:16:50 +04:00
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
2024-12-06 23:11:06 +04:00
#include <stdio.h>
2024-12-06 22:16:50 +04:00
#include <stdlib.h>
2024-12-10 20:55:19 +04:00
#include <stdbool.h>
2024-12-06 22:16:50 +04:00
#include "fnvhash_32a.h"
#include "jhash.h"
#define MAX_HASH 16384
2024-12-06 23:11:06 +04:00
#define TABLE_SIZE (MAX_HASH*2)
2024-12-06 22:16:50 +04:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-12-06 23:11:06 +04:00
char* hashtable[TABLE_SIZE];
2024-12-06 22:16:50 +04:00
int hash(char* word) {
int hash_value = (int)fnv_32a_str(word, FNV1_32A_INIT);
hash_value = TINY_FNV(14, hash_value);
2024-12-06 23:11:06 +04:00
return hash_value;
2024-12-06 22:16:50 +04:00
}
void Insert(char* word) {
int hash_value = hash(word);
while (hashtable[hash_value] != NULL && strcmp(hashtable[hash_value], word) != 0) {
hash_value++;
2024-12-06 23:11:06 +04:00
if (hash_value >= TABLE_SIZE) {
printf("Index out of range\n");
exit(1);
}
2024-12-06 22:16:50 +04:00
}
hashtable[hash_value] = (char*)calloc(strlen(word) + 1, sizeof(char));
if (hashtable[hash_value] == NULL) {
2024-12-06 23:11:06 +04:00
printf("Out of memory\n");
2024-12-06 22:16:50 +04:00
exit(1);
}
strcpy(hashtable[hash_value], word);
}
/* MEMBER <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD>. */
int Member(char* word) {
int hash_value = hash(word);
while (hashtable[hash_value] != NULL) {
if (strcmp(hashtable[hash_value], word) == 0) {
return 1;
}
2024-12-06 23:11:06 +04:00
hash_value++;
if (hash_value >= TABLE_SIZE) {
printf("Index out of range\n");
exit(1);
}
2024-12-06 22:16:50 +04:00
}
return 0;
}
/* CREATE - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>. */
void Create() {
for (int i = 0; i < MAX_HASH; i++)
hashtable[i] = NULL;
}
/* DESTROY - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>. */
void Destroy() {
for (int i = 0; i < MAX_HASH; i++) {
2024-12-10 20:55:19 +04:00
free(hashtable[i]);
2024-12-06 22:16:50 +04:00
hashtable[i] = NULL;
}
}
#endif