-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordament.c
321 lines (272 loc) · 8.8 KB
/
wordament.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "parser.h"
#include "printer.h"
#include "puzzle.h"
#include "queue.h"
#include "box.h"
#include "mergesort.h"
#include "linkedlist.h"
#include "hashmap.h"
#include "hash.h"
// Define color codes for printing.
#define C_NRM "\x1B[0m"
#define C_RED "\x1B[31m"
#define C_GRN "\x1B[32m"
#define C_BLU "\x1B[34m"
#define C_YEL "\x1B[33m"
#define C_MAG "\x1B[35m"
#define C_CYN "\x1B[36m"
// The datastructure built to represent the puzzle.
Puzzle* puzzle;
// The datastructure to contain an efficiently searchable dictionary.
HashMap* dictionary;
// Number of threads to use.
const int tc = 16;
// Size of the puzzle
const int puzzle_size = 4;
// Minimum length of a valid word.
const int min_word_length = 3;
// Datastructure for the threads to write results to.
// Not thread safe, so a concurrency control mechanism should be used!
HashMap* results_map;
LinkedList results_list;
// Declare struct to be used for passing parameters to thread.
// Passing an index is enough for a thread to know which region to validate
// (i.e. which row, column or grid depending on the thread function).
// Additionally a thread needs to know its index (tid) in the result array.
typedef struct {
int index;
} parameters;
// Declare main subroutines and thread functions.
void find_words();
void* find_words_from(void* params);
int validate_path_word(PuzzlePath* path);
int validate_word(char* word);
void add_path_to_results_map(HashMap* results, PuzzlePath* path);
void add_path_to_results_list(LinkedList results, PuzzlePath* path);
void destroy_wordlist(void* data);
void print_header(const char header[]);
// Mutex, since printf is not a reentrant function.
pthread_mutex_t printf_mutex;
pthread_mutex_t result_mutex;
pthread_mutex_t dictionary_mutex;
/**
* Main program 2_bonus.
* Validates the completed puzzle encoded in the specified text file.
*/
int main(int argc, char* argv[]) {
// Print a nice header before all other output.
print_header("*** WORDAMENT ***");
// Try to read the puzzle file.
if (argc < 3) {
printf("Usage: './wordament <puzzle_file_url> <dictionary_file_url>'\n");
return 3;
}
// int n[8] = {2,3,1,4,4,9,7,9};
// void** test_array = calloc(8, sizeof(int*));
// for (int i = 0; i < 8; i++) {
// test_array[i] = box_int(n[i]);
// }
// mergesort(test_array, 8, &ms_compare_ints);
// for (int i = 0; i < 8; i++) {
// printf("%i", remove_int(test_array[i]));
// }
// free(test_array);
// printf("\n");
// return 0;
puzzle = pz_create(4);
parse_puzzle_from_file(argv[1], puzzle);
printf("The parsed puzzle is:\n");
print_puzzle(puzzle);
// Create Prefix HashMap.
dictionary = create_hashmap(1024);
results_map = create_hashmap(1024);
set_hash_function(dictionary, hash_polynomial);
set_hash_function(results_map, hash_polynomial);
parse_dictionary_from_file(argv[2], dictionary);
results_list = ll_create();
find_words();
PuzzleResults* results = pz_build_results_from_list(results_list);
printf("Sorting words by length (descending order)\n");
mergesort((void**) results->paths, results->size, &pz_compare_paths_length_desc);
printf("Printing words:\n");
for (int i = 0; i < results->size; i++) {
PuzzlePath* path = results->paths[i];
char* word = pz_path_to_word(path);
if (get_bucket(results_map, word) == NULL) {
printf("%s\n", word);
insert_data(results_map, word, NULL, NULL);
}
free(word);
}
pz_destroy_results(results);
ll_destroy(results_list, pz_ll_destroy_path);
delete_hashmap(results_map, NULL);
delete_hashmap(dictionary, destroy_wordlist);
pz_destroy(puzzle);
return 0;
}
/**
* Main program that controls all threads that validate the puzzle.
*/
void find_words() {
// Keep handles of all created threads.
pthread_t threads[tc];
// Keep track of all spaces in memory allocated for thread parameters.
parameters* param[tc];
// Create a new thread for each region,
// allocate necessary memory space.
int tid = 0;
while (tid < tc) {
// Allocate memory for thread parameters.
param[tid] = (parameters*) malloc(sizeof(parameters));
param[tid]->index = tid;
// Determine thread function by region (row, column or subgrid).
void* (*func_ptr)(void*);
func_ptr = &find_words_from;
// Create thread
if (pthread_create(&threads[tid], NULL, func_ptr, param[tid])) {
printf("Error: failed to create thread %i.\n", tid);
exit(1);
}
//printf("Launching thread finding words starting at cell %i\n", tid);
++tid;
}
// Join the threads for each region,
// free used memory space.
tid = 0;
while (tid < tc) {
// Wait for thread to finish by joining it,
// report error if this fails.
if (pthread_join(threads[tid], NULL)) {
printf("Error: failed to join thread %i.\n", tid);
exit(2);
}
// Free memory allocated for thread parameters.
free(param[tid]);
++tid;
}
}
void* find_words_from(void* params) {
int start_id = ((parameters*) params)->index;
PuzzleCell* start_cell = pz_get_cell(puzzle, start_id);
PuzzlePath* start_path = pz_create_path();
LinkedList start_paths = pz_add_cell_to_path(start_path, start_cell);
LinkedList bfs = ll_create();
ll_enqueue_all(bfs, start_paths);
ll_destroy(start_paths, NULL);
while (!ll_is_empty(bfs)) {
PuzzlePath* path = (PuzzlePath*) ll_dequeue(bfs, NULL);
PuzzleCell* cell = (PuzzleCell*) ll_get_last(path->cells);
LinkedList neighbors = pz_get_cell_neighbors(puzzle, cell);
while (!ll_is_empty(neighbors)) {
PuzzleCell* nb = ll_dequeue(neighbors, NULL);
LinkedList new_paths = pz_add_cell_to_path(path, nb);
while (!ll_is_empty(new_paths)) {
PuzzlePath* new_path = ll_dequeue(new_paths, NULL);
int length = ll_size(new_path->word);
int valid = 1;
if (length >= 3) valid = validate_path_word(new_path);
if (valid) {
ll_enqueue(bfs, new_path);
if (valid == 2) {
add_path_to_results_list(results_list, new_path);
}
} else {
pz_destroy_path(new_path);
}
}
ll_destroy(new_paths, NULL);
}
ll_destroy(neighbors, NULL);
pz_destroy_path(path);
}
ll_destroy(bfs, pz_ll_destroy_path);
return NULL;
}
int validate_path_word(PuzzlePath* path) {
char* word = pz_path_to_word(path);
int result = validate_word(word);
free(word);
return result;
}
/**
* Validates whether this is either a correct word or the start of one.
* Correct words yield a different result.
*/
int validate_word(char* word) {
pthread_mutex_lock(&dictionary_mutex);
char prefix[4];
for (int i = 0; i < 3; i++) {
prefix[i] = word[i];
}
prefix[3] = '\0';
LinkedList candidates = (LinkedList) get_data(dictionary, prefix);
if (candidates == NULL) {
pthread_mutex_unlock(&dictionary_mutex);
return 0;
}
LinkedNode* candidate = (*candidates);
int matches = 0;
while (candidate != NULL) {
char* other = (char*) candidate->data;
int pos = 3;
while (word[pos] != '\0' && other[pos] != '\0') {
if (word[pos] != other[pos]) {
if (pos - 3 < matches || word[pos] < other[pos]) {
pthread_mutex_unlock(&dictionary_mutex);
return 0;
}
break;
}
if (pos - 2 > matches) matches = pos - 2;
pos++;
}
if (word[pos] == '\0') {
int result = 1;
if (other[pos] == '\0' && pos >= min_word_length) {
// pthread_mutex_lock(&printf_mutex);
// printf("%s\n", word);
// pthread_mutex_unlock(&printf_mutex);
result = 2;
}
pthread_mutex_unlock(&dictionary_mutex);
return result;
}
candidate = candidate->next;
}
pthread_mutex_unlock(&dictionary_mutex);
return 0;
}
/**
* Add a path to a results hashmap datastructure.
*/
void add_path_to_results_map(HashMap* results, PuzzlePath* path) {
char* key = pz_path_to_word(path);
insert_data(results, key, (void*) pz_copy_path(path), NULL);
free(key);
}
/**
* Add a path to a results linked list datastructure.
*/
void add_path_to_results_list(LinkedList results, PuzzlePath* path) {
ll_enqueue(results, (void*) pz_copy_path(path));
}
/**
* Print a header while modulating text color.
*/
void print_header(const char header[]) {
int i = 0;
while (*header != '\0') {
printf("%s%c" C_NRM, C_CYN, *header);
header++;
++i;
}
printf(C_NRM "\n");
}
void destroy_wordlist(void* data) {
LinkedList wordlist = (LinkedList) data;
ll_destroy(wordlist, destroy_char);
}