OAIP_Mirror/lab26/TextProcessingDict/DictHashChain.c

116 lines
2.4 KiB
C
Raw Normal View History

2024-12-10 20:55:19 +04:00
#include "Dict.h"
#ifdef DICT_HASH_CHAIN_C
2024-12-06 18:45:55 +04:00
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <stdlib.h>
2024-12-10 20:55:19 +04:00
#include <stdbool.h>
2024-12-06 20:08:12 +04:00
#include "fnvhash_32a.h"
#include "jhash.h"
2024-12-06 18:45:55 +04:00
struct Node {
char* word;
struct Node* next;
};
2024-12-06 20:08:12 +04:00
#define FNV_HASH
#if defined VLASENKO_HASH
#define MAX_HASH 13267
2024-12-06 18:45:55 +04:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct Node* first[MAX_HASH];
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> word
int hash(char* word) {
unsigned long hash_value = 0;
int i = 0;
while (word[i] != '\0') {
hash_value = 31 * hash_value + ((unsigned)(word[i]));
i++;
}
return hash_value % MAX_HASH;
}
2024-12-06 20:08:12 +04:00
#elif defined JHASH
#define MAX_HASH 16384
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct Node* first[MAX_HASH];
int hash(char* word) {
2024-12-06 22:16:50 +04:00
int hash_value = (int)jhash(word, (ub4)strlen(word) + 1, FNV1_32A_INIT);
2024-12-06 20:08:12 +04:00
hash_value = hash_value & hashmask(14);
2024-12-06 22:16:50 +04:00
return hash_value;
2024-12-06 20:08:12 +04:00
}
#elif defined FNV_HASH
#define MAX_HASH 16384
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct Node* first[MAX_HASH];
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 22:16:50 +04:00
return hash_value;
2024-12-06 20:08:12 +04:00
}
#endif
2024-12-06 18:45:55 +04:00
/* INSERT <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <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>.
<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> <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 Insert(char* word) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
int hash_value = hash(word);
newNode->next = first[hash_value];
newNode->word = (char*)calloc(strlen(word) + 1, sizeof(char));
strcpy(newNode->word, word);
first[hash_value] = newNode;
}
/* 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);
struct Node* ptr = first[hash_value];
while (ptr != NULL) {
if (strcmp(ptr->word, word) == 0) {
return 1;
}
ptr = ptr->next;
}
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++)
first[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++) {
while (first[i] != NULL) {
struct Node* delNode = first[i];
first[i] = first[i]->next;
free(delNode->word);
free(delNode);
}
}
}
#endif