-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
202 lines (169 loc) · 4.65 KB
/
test.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_MAP_SIZE 1000
struct HashMap VocabStore;
struct Array{
char **values;
int length;
};
struct Array array_make(void **elements, int count)
{
struct Array ret;
ret.values = malloc(sizeof(void *) * count);
ret.length = count;
for (int i = 0; i < count; i++) {
ret.values[i] = elements[i];
}
return ret;
}
struct Node
{
char* key;
int value;
struct Node* next;
};
struct HashMap
{
struct Node* table[HASH_MAP_SIZE];
};
unsigned int hash(const char* key)
{
unsigned int hashval = 0;
for (int i = 0; key[i] != '\0'; i++)
{
hashval = key[i] + (hashval << 5) - hashval;
}
return hashval % HASH_MAP_SIZE;
}
struct Node* createNode(const char* key, int value)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->key = strdup(key);
newNode->value = value;
newNode->next = NULL;
return newNode;
}
void initializeHashMap(struct HashMap* hashMap)
{
for (int i = 0; i < HASH_MAP_SIZE; i++)
{
hashMap->table[i] = NULL;
}
}
void hashmap_put(struct HashMap* hashMap, const char* key, int value)
{
unsigned int index = hash(key);
struct Node* newNode = createNode(key, value);
if (hashMap->table[index] == NULL)
{
hashMap->table[index] = newNode;
}
else
{
struct Node* current = hashMap->table[index];
while (current != NULL)
{
if (strcmp(current->key, key) == 0)
{
current->value = value;
free(newNode->key);
free(newNode);
return;
}
current = current->next;
}
newNode->next = hashMap->table[index];
hashMap->table[index] = newNode;
}
}
int hashmap_get(struct HashMap* hashMap, const char* key)
{
unsigned int index = hash(key);
struct Node* current = hashMap->table[index];
while (current != NULL)
{
if (strcmp(current->key, key) == 0)
{
return current->value;
}
current = current->next;
}
return 0;
}
void hashmap_update(struct HashMap* hashMap, const char* key, int value)
{
hashmap_put(hashMap, key, value);
}
#define array(elements...) ({ void *values[] = { elements }; array_make(values, sizeof(values) / sizeof(void *)); })
#define destroy(arr) ({ free(arr.values); })
struct Array split_string(const char *str, const char *delimiter) {
struct Array arr;
arr.values = NULL;
arr.length = 0;
char *token;
char *copy = strdup(str);
token = strtok(copy, delimiter);
while (token != NULL) {
arr.values = realloc(arr.values, sizeof(char *) * (arr.length + 1));
arr.values[arr.length] = strdup(token);
arr.length++;
token = strtok(NULL, delimiter);
}
free(copy);
return arr;
}
int is_element_in_set(const struct Array *set, const char *element) {
for (int i = 0; i < set->length; ++i) {
if (strcmp(set->values[i], element) == 0) {
return 1;
}
}
return -1;
}
void add_element_to_set(struct Array *set, const char *element) {
if (!is_element_in_set(set, element)) {
set->values = realloc(set->values, sizeof(char *) * (set->length + 1));
set->values[set->length] = strdup(element);
set->length++;
}
}
void print_set(const struct Array *set) {
for (int i = 0; i < set->length; ++i) {
printf("%s\n", set->values[i]);
}
}
void destroy_set(struct Array *set) {
for (int i = 0; i < set->length; ++i) {
free(set->values[i]);
}
free(set->values);
set->length = 0;
}
int c_fit(struct Array arr){
for (int i=0 ;i<arr.length;++i){
struct Array set;
set.values = NULL;
set.length = 0;
struct Array splitArr = split_string(arr.values[i], " ");
for (int j = 0; j < splitArr.length; ++j) {
int oldElem = hashmap_get(&VocabStore,splitArr.values[j]);
hashmap_put(&VocabStore,splitArr.values[j],oldElem+1);
}
//destroy(splitArr);
}
return hashmap_get(&VocabStore, "all,") ;
}
int main() {
;
initializeHashMap(&VocabStore);
struct Array arr= array("im all getting on borderlands and i will murder you all",
"I am coming to the borders and I will kill you all,",
"im getting on borderlands and i will kill you all,",
"im coming on borderlands and i will murder you all,",
"im getting on borderlands 2 and i will murder you me all,",
"im getting into borderlands and i can murder you all,");
c_fit(arr);
printf("Value for 'apple': %d\n", hashmap_get(&VocabStore, "all,"));
return 0;
}