finish open addressing

This commit is contained in:
Kaehvaman 2024-12-06 23:11:06 +04:00
parent b7edf918b1
commit 7c0bdfb34e
6 changed files with 21111 additions and 6661 deletions

View File

@ -1,6 +1,5 @@
#pragma once #pragma once
#define DICT_HASH_OPEN_ADDRESS_C
#define DICT_HASH_CHAIN_C #define DICT_HASH_CHAIN_C
/* /*

View File

@ -1,5 +1,6 @@
#define _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS
#include <string.h> #include <string.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "Dict.h" #include "Dict.h"
#include "fnvhash_32a.h" #include "fnvhash_32a.h"
@ -8,13 +9,15 @@
#ifdef DICT_HASH_OPEN_ADDRESS_C #ifdef DICT_HASH_OPEN_ADDRESS_C
#define MAX_HASH 16384 #define MAX_HASH 16384
#define TABLE_SIZE (MAX_HASH*2)
// Ìàññèâ ñïèñêîâ // Ìàññèâ ñïèñêîâ
char* hashtable[MAX_HASH]; char* hashtable[TABLE_SIZE];
int hash(char* word) { int hash(char* word) {
int hash_value = (int)fnv_32a_str(word, FNV1_32A_INIT); int hash_value = (int)fnv_32a_str(word, FNV1_32A_INIT);
hash_value = TINY_FNV(14, hash_value); hash_value = TINY_FNV(14, hash_value);
return hash_value;
} }
void Insert(char* word) { void Insert(char* word) {
@ -22,11 +25,15 @@ void Insert(char* word) {
while (hashtable[hash_value] != NULL && strcmp(hashtable[hash_value], word) != 0) { while (hashtable[hash_value] != NULL && strcmp(hashtable[hash_value], word) != 0) {
hash_value++; 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)); hashtable[hash_value] = (char*)calloc(strlen(word) + 1, sizeof(char));
if (hashtable[hash_value] == NULL) { if (hashtable[hash_value] == NULL) {
puts("Out of memory"); printf("Out of memory\n");
exit(1); exit(1);
} }
strcpy(hashtable[hash_value], word); strcpy(hashtable[hash_value], word);
@ -36,11 +43,15 @@ void Insert(char* word) {
int Member(char* word) { int Member(char* word) {
int hash_value = hash(word); int hash_value = hash(word);
int i = 0;
while (hashtable[hash_value] != NULL) { while (hashtable[hash_value] != NULL) {
if (strcmp(hashtable[hash_value], word) == 0) { if (strcmp(hashtable[hash_value], word) == 0) {
return 1; return 1;
} }
hash_value++;
if (hash_value >= TABLE_SIZE) {
printf("Index out of range\n");
exit(1);
}
} }
return 0; return 0;
@ -57,7 +68,9 @@ void Create() {
Âûçûâàåòñÿ ïîñëå îêîí÷àíèÿ èñïîëüçîâàíèÿ ñëîâàðÿ. */ Âûçûâàåòñÿ ïîñëå îêîí÷àíèÿ èñïîëüçîâàíèÿ ñëîâàðÿ. */
void Destroy() { void Destroy() {
for (int i = 0; i < MAX_HASH; i++) { for (int i = 0; i < MAX_HASH; i++) {
if (hashtable[i] != NULL) {
free(hashtable[i]); free(hashtable[i]);
}
hashtable[i] = NULL; hashtable[i] = NULL;
} }
} }

View File

@ -15,7 +15,7 @@ char filenameDict[MAX_PATH] = "../Dictionaries/dict0.txt";
char filenameIn[MAX_PATH] = "../Texts/Alice.txt"; char filenameIn[MAX_PATH] = "../Texts/Alice.txt";
char filenameOut[MAX_PATH] = "out/Alice_out.html"; char filenameOut[MAX_PATH] = "out/Alice_out.html";
double results[3][12]; double results[3][15];
void test(int i, int j) { void test(int i, int j) {
// ñîîáùàåì êàêèå ôàéëû îáðàáàòûâàþòñÿ // ñîîáùàåì êàêèå ôàéëû îáðàáàòûâàþòñÿ
@ -36,13 +36,13 @@ void test(int i, int j) {
} }
void test_dicts(int i) { void test_dicts(int i) {
for (int j = 0; j < 4; j++) { for (int j = 0; j < 5; j++) {
sprintf(filenameDict, "../Dictionaries/dict%d.txt", j); sprintf(filenameDict, "../Dictionaries/dict%d.txt", j);
test(i, j); test(i, j);
sprintf(filenameDict, "../Dictionaries/dict%da.txt", j); sprintf(filenameDict, "../Dictionaries/dict%da.txt", j);
test(i, j+4); test(i, j + 5);
sprintf(filenameDict, "../Dictionaries/dict%db.txt", j); sprintf(filenameDict, "../Dictionaries/dict%db.txt", j);
test(i, j+8); test(i, j + 10);
} }
} }
@ -58,7 +58,7 @@ int main() {
test_dicts(2); test_dicts(2);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
for (int j = 0; j < 12; j++) { for (int j = 0; j < 15; j++) {
printf("%7.3lf ", results[i][j]); printf("%7.3lf ", results[i][j]);
} }
printf("\n"); printf("\n");

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long