-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.c
380 lines (335 loc) · 9.86 KB
/
posts.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* Copyright (c) 2024, Andrei Rusanescu <andreirusanescu154gmail.com>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "users.h"
#include "posts.h"
/**
* @param post_counter used for the ID of the posts
*/
static unsigned int post_counter = 1;
/**
* @brief Helper function for create_post
* @param title String for the title of the post
* if it is NULL, then the return is a repost
* @param user_id ID of the user that authored the post
* @return Returns a post / repost based on the value of title
*/
static
post_t *make_post(char *title, unsigned int user_id) {
post_t *post = (post_t *)calloc(1, sizeof(post_t));
post->id = post_counter;
post->size = 0;
post->user_id = user_id;
if (title) {
post->title = malloc(MAX_TITLE_LEN);
strcpy(post->title, title);
post->events = calloc(1, sizeof(post_t *));
post->capacity = 1;
} else {
post->events = NULL;
post->capacity = 0;
}
post->v = calloc(MAX_PEOPLE, sizeof(unsigned int));
post->total_likes = 0;
return post;
}
void create_post(posts_t **posts) {
char *name = strtok(NULL, " \n");
char *title = strtok(NULL, "\"");
unsigned int uid = get_user_id(name);
post_t *post = make_post(title, uid);
if ((*posts)->size == (*posts)->capacity) {
(*posts)->capacity <<= 1;
(*posts)->posts = realloc((*posts)->posts, sizeof(post_t *) * (*posts)->capacity);
}
(*posts)->posts[(*posts)->size] = post;
++(*posts)->size;
printf("Created \"%s\" for %s\n", title, name);
++post_counter;
}
unsigned int find_pos(posts_t *posts, unsigned int tree_id) {
unsigned int l = 0, r = posts->size - 1, mid;
while (l <= r) {
mid = l + (r - l) / 2;
if (posts->posts[mid]->id == tree_id)
return mid;
else if (posts->posts[mid]->id > tree_id)
r = mid - 1;
else
l = mid + 1;
}
return 0;
}
post_t *find_repost(post_t *current_post, unsigned int repost_id,
post_t **prev, unsigned int *pos) {
if (current_post->id == repost_id)
return current_post;
post_t *found;
for (unsigned int i = 0; i < current_post->size; ++i) {
*prev = current_post;
found = find_repost(current_post->events[i], repost_id, prev, pos);
if (found) {
*pos = i;
return found;
}
}
return NULL;
}
void repost(posts_t **posts) {
char *name = strtok(NULL, " \n");
unsigned int post_id = atoi(strtok(NULL, " \n"));
unsigned int repost_id = 0;
char *repost_string = strtok(NULL, " \n");
if (repost_string)
repost_id = atoi(repost_string);
/* Find the correct tree */
unsigned int pos = find_pos(*posts, post_id);
post_t *current_post = (*posts)->posts[pos];
/* Resizable array check */
if (current_post->size == current_post->capacity) {
current_post->capacity <<= 1;
current_post->events = realloc(current_post->events,
current_post->capacity *
sizeof(post_t *));
}
post_t *rep = make_post(NULL, get_user_id(name));
/* Repost for a post */
if (!repost_id) {
current_post->events[current_post->size] = rep;
++current_post->size;
} else {
post_t *dummy = NULL;
post_t *repp = find_repost(current_post, repost_id, &dummy, &pos);
if (repp->size == repp->capacity) {
if (repp->capacity) {
repp->capacity <<= 1;
repp->events = realloc(repp->events, repp->capacity * sizeof(post_t *));
} else {
repp->events = calloc(1, sizeof(post_t *));
repp->capacity = 1;
}
}
repp->events[repp->size] = rep;
++repp->size;
}
printf("Created repost #%d for %s\n", post_counter, name);
++post_counter;
}
/**
* @brief Uses LCA approach to find <val1> and <val2> ancestor
* @param root Root of the post
* @param val1 & @param val2 values to look for
* @return Returns LCA for the two values
*/
static
post_t *lca(post_t *root, unsigned int val1, unsigned int val2) {
if (!root)
return NULL;
if (root->id == val1 || root->id == val2)
return root;
post_t *res = NULL, *found;
int cnt = 0;
for (unsigned int i = 0; i < root->size; ++i) {
found = lca(root->events[i], val1, val2);
/* Counts when a value was found */
if (found) {
++cnt;
/* Saves the common root */
res = found;
}
}
/* If 2 values were found, means the current root is the LCA */
if (cnt == 2)
return root;
/* NULL or the one common root that was found */
return res;
}
void common_repost(posts_t *posts) {
unsigned int tree_id = atoi(strtok(NULL, " \n"));
unsigned int pos = find_pos(posts, tree_id);
unsigned int rep1 = atoi(strtok(NULL, " \n"));
unsigned int rep2 = atoi(strtok(NULL, " \n"));
post_t *current_post = posts->posts[pos];
post_t *tmp = lca(current_post, rep1, rep2);
int res = tmp->id;
printf("The first common repost of %d and %d is %d\n", rep1, rep2, res);
}
void like(posts_t *posts) {
char *name = strtok(NULL, " \n");
unsigned int post_id = atoi(strtok(NULL, " \n"));
unsigned int pos = find_pos(posts, post_id);
char *tmp = strtok(NULL, " \n");
unsigned int repost_id = 0;
post_t *current_post = posts->posts[pos];
if (!tmp) {
if (!current_post->v[get_user_id(name)]) {
current_post->v[get_user_id(name)] = 1;
current_post->total_likes++;
printf("User %s liked post \"%s\"\n", name, current_post->title);
} else {
current_post->v[get_user_id(name)] = 0;
current_post->total_likes--;
printf("User %s unliked post \"%s\"\n", name, current_post->title);
}
} else {
repost_id = atoi(tmp);
post_t *dummy = NULL;
post_t *repp = find_repost(current_post, repost_id, &dummy, &pos);
if (!repp->v[get_user_id(name)]) {
repp->v[get_user_id(name)] = 1;
++repp->total_likes;
printf("User %s liked repost \"%s\"\n", name, current_post->title);
} else {
repp->v[get_user_id(name)] = 0;
--repp->total_likes;
printf("User %s unliked repost \"%s\"\n", name, current_post->title);
}
}
}
/**
* @brief Helper function for @ratio
* @param root Root / Post
* @param max_likes Maximal value for likes hit on this thread
* @param max_id The ID of the post / repost with the <max_likes>
*/
static
void check_ratio(post_t *root, unsigned int *max_likes,
unsigned int *max_id) {
if (!root)
return;
if (root->total_likes > *max_likes) {
*max_likes = root->total_likes;
*max_id = root->id;
} else if (root->total_likes == *max_likes &&
root->id != *max_id) {
if (*max_id > root->id)
*max_id = root->id;
}
for (unsigned int i = 0; i < root->size; ++i)
check_ratio(root->events[i], max_likes, max_id);
}
void ratio(posts_t *posts) {
unsigned int post_id = atoi(strtok(NULL, " \n"));
unsigned int pos = find_pos(posts, post_id);
post_t *current_post = posts->posts[pos];
unsigned int max_likes = current_post->total_likes, max_id = current_post->id;
check_ratio(current_post, &max_likes, &max_id);
if (max_likes != current_post->total_likes)
printf("Post %d got ratio'd by repost %d\n", post_id, max_id);
else
printf("The original post is the highest rated\n");
}
/**
* @brief Helper function for @delete
* Postorder approach of deleting a tree
* @param tree Pointer to the post to be deleted
*/
static
void free_post_tree(post_t **tree) {
if (!*tree) return;
for (unsigned int i = 0; i < (*tree)->size; ++i)
free_post_tree(&(*tree)->events[i]);
free((*tree)->events);
free((*tree)->v);
free((*tree)->title);
free((*tree));
*tree = NULL;
}
void delete(posts_t *posts) {
unsigned int post_id = atoi(strtok(NULL, " \n"));
unsigned int pos = find_pos(posts, post_id);
char *tmp = strtok(NULL, " \n");
post_t *current_post = posts->posts[pos];
if (tmp) {
unsigned int rep_id = atoi(tmp);
post_t *prev = NULL;
post_t *rep = find_repost(current_post, rep_id, &prev, &pos);
for (unsigned int i = pos; i < prev->size - 1; ++i)
prev->events[i] = prev->events[i + 1];
--prev->size;
printf("Deleted repost #%d of post \"%s\"\n", rep_id, current_post->title);
free_post_tree(&rep);
} else {
for (unsigned int i = pos; i < posts->size - 1; ++i)
posts->posts[i] = posts->posts[i + 1];
--posts->size;
printf("Deleted \"%s\"\n", current_post->title);
free_post_tree(¤t_post);
}
}
void get_likes(posts_t *posts) {
unsigned int post_id = atoi(strtok(NULL, " \n"));
unsigned int pos = find_pos(posts, post_id);
char *tmp = strtok(NULL, " \n");
post_t *current_post = posts->posts[pos];
if (!tmp) {
printf("Post \"%s\" has %d likes\n", current_post->title, current_post->total_likes);
} else {
post_t *prev;
post_t *rep = find_repost(current_post, atoi(tmp), &prev, &pos);
printf("Repost #%d has %d likes\n", rep->id, rep->total_likes);
}
}
/**
* @brief Preorder parsing of a tree
* @param root Current root
* @param cnt If cnt is 0 => post, otherwise repost
*/
static
void preorder(post_t *root, int cnt) {
if (!root)
return;
if (!cnt)
printf("\"%s\" - Post by %s\n", root->title, get_user_name(root->user_id));
else
printf("Repost #%d by %s\n", root->id, get_user_name(root->user_id));
for (unsigned int i = 0; i < root->size; ++i)
preorder(root->events[i], cnt + 1);
}
void get_reposts(posts_t *posts) {
unsigned int post_id = atoi(strtok(NULL, " \n"));
unsigned int pos = find_pos(posts, post_id);
char *tmp = strtok(NULL, " \n");
post_t *current_post = posts->posts[pos];
if (!tmp) {
preorder(current_post, 0);
} else {
post_t *prev;
post_t *rep = find_repost(current_post, atoi(tmp), &prev, &pos);
preorder(rep, 1);
}
}
void free_posts(posts_t **posts) {
unsigned int i;
for (i = 0; i < (*posts)->size; ++i)
free_post_tree(&(*posts)->posts[i]);
free((*posts)->posts);
free(*posts);
}
void handle_input_posts(char *input, posts_t *posts)
{
char *commands = strdup(input);
char *cmd = strtok(commands, "\n ");
if (!cmd)
return;
if (!strcmp(cmd, "create"))
create_post(&posts);
else if (!strcmp(cmd, "repost"))
repost(&posts);
else if (!strcmp(cmd, "common-repost"))
common_repost(posts);
else if (!strcmp(cmd, "like"))
like(posts);
else if (!strcmp(cmd, "ratio"))
ratio(posts);
else if (!strcmp(cmd, "delete"))
delete(posts);
else if (!strcmp(cmd, "get-likes"))
get_likes(posts);
else if (!strcmp(cmd, "get-reposts"))
get_reposts(posts);
free(commands);
}