start open addressing
This commit is contained in:
parent
e0f63df0c8
commit
b7edf918b1
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "jhash.h"
|
#include "jhash.h"
|
||||||
#include "fnvhash_32a.h"
|
#include "fnvhash_32a.h"
|
||||||
|
#include "crc.h"
|
||||||
|
|
||||||
// Assumes little endian
|
// Assumes little endian
|
||||||
void printBits(size_t const size, void const* const ptr) {
|
void printBits(size_t const size, void const* const ptr) {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define DICT_TREE_C
|
#define DICT_HASH_OPEN_ADDRESS_C
|
||||||
|
#define DICT_HASH_CHAIN_C
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Основные операции СЛОВАРЯ:
|
Основные операции СЛОВАРЯ:
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
#include "fnvhash_32a.h"
|
#include "fnvhash_32a.h"
|
||||||
#include "jhash.h"
|
#include "jhash.h"
|
||||||
|
|
||||||
#ifdef DICT_HASH_C
|
#ifdef DICT_HASH_CHAIN_C
|
||||||
|
|
||||||
struct Node {
|
struct Node {
|
||||||
char* word;
|
char* word;
|
||||||
@ -43,8 +43,9 @@ int hash(char* word) {
|
|||||||
struct Node* first[MAX_HASH];
|
struct Node* first[MAX_HASH];
|
||||||
|
|
||||||
int hash(char* word) {
|
int hash(char* word) {
|
||||||
int hash_value = (int)jhash(word, strlen(word) + 1, FNV1_32A_INIT);
|
int hash_value = (int)jhash(word, (ub4)strlen(word) + 1, FNV1_32A_INIT);
|
||||||
hash_value = hash_value & hashmask(14);
|
hash_value = hash_value & hashmask(14);
|
||||||
|
return hash_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#elif defined FNV_HASH
|
#elif defined FNV_HASH
|
||||||
@ -56,6 +57,7 @@ struct Node* first[MAX_HASH];
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
65
lab26/TextProcessingDict/DictHashOpenAddress.c
Normal file
65
lab26/TextProcessingDict/DictHashOpenAddress.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "Dict.h"
|
||||||
|
#include "fnvhash_32a.h"
|
||||||
|
#include "jhash.h"
|
||||||
|
|
||||||
|
#ifdef DICT_HASH_OPEN_ADDRESS_C
|
||||||
|
|
||||||
|
#define MAX_HASH 16384
|
||||||
|
|
||||||
|
// Ìàññèâ ñïèñêîâ
|
||||||
|
char* hashtable[MAX_HASH];
|
||||||
|
|
||||||
|
int hash(char* word) {
|
||||||
|
int hash_value = (int)fnv_32a_str(word, FNV1_32A_INIT);
|
||||||
|
hash_value = TINY_FNV(14, hash_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Insert(char* word) {
|
||||||
|
int hash_value = hash(word);
|
||||||
|
|
||||||
|
while (hashtable[hash_value] != NULL && strcmp(hashtable[hash_value], word) != 0) {
|
||||||
|
hash_value++;
|
||||||
|
}
|
||||||
|
|
||||||
|
hashtable[hash_value] = (char*)calloc(strlen(word) + 1, sizeof(char));
|
||||||
|
if (hashtable[hash_value] == NULL) {
|
||||||
|
puts("Out of memory");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
strcpy(hashtable[hash_value], word);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* MEMBER – ñîîáùàåò, ÿâëÿåòñÿ ëè óêàçàííûé ýëåìåíò ÷ëåíîì äàííîãî ìíîæåñòâà èëè íåò. */
|
||||||
|
int Member(char* word) {
|
||||||
|
int hash_value = hash(word);
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (hashtable[hash_value] != NULL) {
|
||||||
|
if (strcmp(hashtable[hash_value], word) == 0) {
|
||||||
|
return 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
|
@ -132,7 +132,8 @@
|
|||||||
<ClCompile Include="..\..\hash functions\fnvhash_32a.c" />
|
<ClCompile Include="..\..\hash functions\fnvhash_32a.c" />
|
||||||
<ClCompile Include="..\..\hash functions\jhash.c" />
|
<ClCompile Include="..\..\hash functions\jhash.c" />
|
||||||
<ClCompile Include="DictArray.c" />
|
<ClCompile Include="DictArray.c" />
|
||||||
<ClCompile Include="DictHash.c" />
|
<ClCompile Include="DictHashChain.c" />
|
||||||
|
<ClCompile Include="DictHashOpenAddress.c" />
|
||||||
<ClCompile Include="DictLinkedList.c" />
|
<ClCompile Include="DictLinkedList.c" />
|
||||||
<ClCompile Include="DictSortedArray.c" />
|
<ClCompile Include="DictSortedArray.c" />
|
||||||
<ClCompile Include="DictTree.c" />
|
<ClCompile Include="DictTree.c" />
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
</Filter>
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="DictHash.c">
|
<ClCompile Include="DictHashChain.c">
|
||||||
<Filter>Исходные файлы</Filter>
|
<Filter>Исходные файлы</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
<ClCompile Include="DictLinkedList.c">
|
<ClCompile Include="DictLinkedList.c">
|
||||||
@ -39,6 +39,9 @@
|
|||||||
<ClCompile Include="..\..\hash functions\jhash.c">
|
<ClCompile Include="..\..\hash functions\jhash.c">
|
||||||
<Filter>Исходные файлы</Filter>
|
<Filter>Исходные файлы</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="DictHashOpenAddress.c">
|
||||||
|
<Filter>Исходные файлы</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Dict.h">
|
<ClInclude Include="Dict.h">
|
||||||
|
File diff suppressed because one or more lines are too long
Binary file not shown.
Loading…
Reference in New Issue
Block a user