-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_table.cpp
More file actions
170 lines (141 loc) · 3.67 KB
/
Copy pathhash_table.cpp
File metadata and controls
170 lines (141 loc) · 3.67 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "hash_table.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
static unsigned long long pow (int x, int k);
static void add_to_table (struct hash_table* table, const char* str);
static unsigned long long get_hash (const char* str);
static void node_dump (struct Node* node);
static void node_ctor (struct Node* pos, const char* str);
static int get_qant (struct hash_table* table, const char* str);
unsigned long long pow (int x, int k)
{
unsigned long long res = 1;
if (k == 0)
return 1;
for (int i = 0; i < k; i++)
res *= x;
return res;
}
unsigned long long get_hash (const char* str)
{
unsigned long long hash = 0;
int p = 31;
for (size_t i = 0; i < strlen(str); i++)
{
//printf("%c[%d]", str[i], str[i]);
if (p_degrees[i] == 0)
p_degrees[i] = pow(p, i);
hash += (unsigned long long)(str[i] - 'a') * p_degrees[i];
}
hash = hash % TABLE_SIZE;
//printf("str - %s hash - %u\n", str, hash);
return (unsigned long long )hash;
}
void work_with_table (struct hash_table* table, char* buffer, enum table_mode mode)
{
int n = 0, ptr = 0;
char* str = NULL;
int ans = 0;
size_t len = strlen(buffer);
for (size_t i = 0; i < len; i++)
{
ptr = i;
while (buffer[i] != ' ' && buffer[i] != '\0')
{
n++;
i++;
}
str = (char*)calloc(n + 1, sizeof(char));
strncpy(str, &buffer[ptr], n);
n = 0;
if (mode == fill)
add_to_table(table, str);
else if (mode == find)
{
ans = get_qant(table, str);
printf("%d ", ans);
}
free(str);
}
}
int get_qant (struct hash_table* table, const char* str)
{
unsigned long long hash = get_hash(str);
struct Node* pos = &table->arr[hash];
if (pos->next == NULL)
return 0;
pos = pos->next;
while (strcmp(pos->data, str) != 0)
{
if (pos->next != NULL)
pos = pos->next;
else
return 0;
}
return pos->qant;
}
void add_to_table (struct hash_table* table, const char* str)
{
unsigned long long hash = get_hash(str);
struct Node* pos = &table->arr[hash];
int new_node = 1;
// if (pos->next == NULL)
// {
// node_ctor(pos, str);
// pos->qant++;
// return;
// }
while (pos->next != NULL)
{
pos = pos->next;
if (strcmp(pos->data, str) == 0)
{
new_node = 0;
break;
}
}
if (new_node)
{
pos->next = (struct Node*)calloc(1, sizeof(struct Node));
pos = pos->next;
node_ctor(pos, str);
}
pos->qant++;
//node_dump(pos);
}
void node_ctor (struct Node* pos, const char* str)
{
pos->data = (char*)calloc(strlen(str) + 1, sizeof(char));
strncpy(pos->data, str, strlen(str));
}
void table_ctor (struct hash_table* table)
{
table->arr = (struct Node*)calloc(TABLE_SIZE, sizeof(struct Node));
}
void table_dtor (struct hash_table* table)
{
struct Node* temp = NULL;
struct Node* pos = NULL;
for (int i = 0; i < TABLE_SIZE; i++)
{
pos = table->arr[i].next;
while (pos->next != NULL)
{
temp = pos;
pos = pos->next;
free(temp);
}
}
free(table->arr);
}
void node_dump (struct Node* node)
{
printf("------------NODE_DUMP-----------\n");
printf("adress - %p\n", node);
//printf("hash - %u\n", node->hash);
printf("data - %s\n", node->data);
printf("next - %p\n", node->next);
printf("qabt - %d\n", node->qant);
printf("-------------DUMP_END-----------\n");
}