mirror of
https://github.com/Kaehvaman/OAIP.git
synced 2025-01-19 00:59:12 +04:00
79 lines
1.7 KiB
C
79 lines
1.7 KiB
C
#include "Dict.h"
|
||
|
||
#ifdef DICT_HASH_OPEN_ADDRESS_C
|
||
|
||
#define _CRT_SECURE_NO_WARNINGS
|
||
#include <string.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <stdbool.h>
|
||
#include "fnvhash_32a.h"
|
||
#include "jhash.h"
|
||
|
||
#define MAX_HASH 16384
|
||
#define TABLE_SIZE (MAX_HASH*2)
|
||
|
||
// Ìàññèâ ñïèñêîâ
|
||
char* hashtable[TABLE_SIZE];
|
||
|
||
int hash(char* word) {
|
||
int hash_value = (int)fnv_32a_str(word, FNV1_32A_INIT);
|
||
hash_value = TINY_FNV(14, hash_value);
|
||
return hash_value;
|
||
}
|
||
|
||
void Insert(char* word) {
|
||
int hash_value = hash(word);
|
||
|
||
while (hashtable[hash_value] != NULL && strcmp(hashtable[hash_value], word) != 0) {
|
||
hash_value++;
|
||
if (hash_value >= TABLE_SIZE) {
|
||
printf("Index out of range\n");
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
hashtable[hash_value] = (char*)calloc(strlen(word) + 1, sizeof(char));
|
||
if (hashtable[hash_value] == NULL) {
|
||
printf("Out of memory\n");
|
||
exit(1);
|
||
}
|
||
strcpy(hashtable[hash_value], word);
|
||
}
|
||
|
||
/* MEMBER – ñîîáùàåò, ÿâëÿåòñÿ ëè óêàçàííûé ýëåìåíò ÷ëåíîì äàííîãî ìíîæåñòâà èëè íåò. */
|
||
int Member(char* word) {
|
||
int hash_value = hash(word);
|
||
|
||
while (hashtable[hash_value] != NULL) {
|
||
if (strcmp(hashtable[hash_value], word) == 0) {
|
||
return 1;
|
||
}
|
||
hash_value++;
|
||
if (hash_value >= TABLE_SIZE) {
|
||
printf("Index out of range\n");
|
||
exit(1);
|
||
}
|
||
}
|
||
|
||
return 0;
|
||
}
|
||
|
||
/* CREATE - ñîçäàåò ñëîâàðü.
|
||
Âûçûâàåòñÿ ïåðåä íà÷àëîì èñïîëüçîâàíèÿ ñëîâàðÿ. */
|
||
void Create() {
|
||
for (int i = 0; i < MAX_HASH; i++)
|
||
hashtable[i] = NULL;
|
||
}
|
||
|
||
/* DESTROY - óíè÷òîæàåò ñëîâàðü.
|
||
Âûçûâàåòñÿ ïîñëå îêîí÷àíèÿ èñïîëüçîâàíèÿ ñëîâàðÿ. */
|
||
void Destroy() {
|
||
for (int i = 0; i < MAX_HASH; i++) {
|
||
free(hashtable[i]);
|
||
hashtable[i] = NULL;
|
||
}
|
||
}
|
||
|
||
#endif
|