-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable.cpp
81 lines (72 loc) · 2.95 KB
/
HashTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// Created by vuniverse on 9/1/21.
//
#include "HashTable.h"
#include "functions.h"
HashTable::HashTable(const std::string& folder)
{
/* Hash Table: Columns:8
*
* Rows: 255 Start node of singly linked list
*
* Every info node have pointer(position in m_file) to next info node that should be in table
* Start node have only position without info
*
* Every first info node have structure like this:
* Pointer to position of next node. Can also be pointer to end iterator or non-valid 8 bytes
*
* Every info node have structure like this:
* Pointer to next node of data info 8 bytes
* Individual no collision hash 4 bytes
* Data position in data m_file 8 bytes
*
*/
m_file = std::fopen((folder + "hashes").c_str(), "wb+" );
m_memory = std::fopen((folder + "values").c_str(), "wb" );
std::fwrite(&m_zero, sizeof( position ), 1, m_memory);
memSize i=0;
do {
std::fwrite(&m_zero, sizeof( position ), 1, m_file);
i++;
} while (i != hash_size * 8);
}
HashTable::~HashTable()
{
std::fclose(m_file );
std::fclose(m_memory );
}
void HashTable::write( const cords& y ,const cords& x, memSize size , std::FILE*& data )
{
//Generate HASHES
cords cord = ( y >= x ) ? ( y - x ) : ( x - y ); //Individual hash for node
byte row = cord % hash_size; //Hash for table
byte column = bitIdentification(y, x ).get(); //generate bit flags to variable flags os type union flags
position pos = writeData(size, data);//Got data position end write data
std::fseek(m_file, 0, SEEK_END );//Go to end of info m_file
auto nodepos = static_cast<unsigned long>( ftell(m_file ) );
//Data info
std::fwrite(&m_zero, sizeof( position ), 1, m_file); //Pointer to next node of data info
std::fwrite(&cord, sizeof( cords ), 1, m_file ); //Individual no collision hash
std::fwrite(&pos, sizeof( position ), 1, m_file ); //Data position in data m_file
setHashTablePosition(column, row, m_file);//Set position in hashtable
position current;
position temp;
do { //Check pointer
current = std::ftell(m_file );
std::fread(&temp, sizeof( position ), 1, m_file );
std::fseek(m_file, static_cast<long>(temp), SEEK_SET );
}while (temp != m_zero );
std::fseek(m_file, static_cast<long>(current), SEEK_SET );
std::fwrite(&nodepos, sizeof( position ), 1, m_file);
std::fseek(m_file, static_cast<long>(current), SEEK_SET);
std::fread(&temp, sizeof( position ), 1, m_file);
}
position HashTable::writeData(const memSize& size, std::FILE*& data)
{
auto temporalityPosition= std::ftell(m_memory );
char *buff = nullptr;
std::fread(buff, size, 1, data);
std::fwrite(&size, sizeof( memSize ), 1, m_memory );
std::fwrite(buff, size, 1, m_memory );
return temporalityPosition;
}