-
Notifications
You must be signed in to change notification settings - Fork 1
/
tldlist.c
526 lines (450 loc) · 13.3 KB
/
tldlist.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/**************************************************/
/** Authorship Statement **/
/**************************************************/
/** Author: Willian de Oliveira Barreiros Junior **/
/** Login: 2105514D **/
/** Title of Assignment: AP3 Exercise 1 **/
/**************************************************/
/** This is my own work as defined in the **/
/** Academic Ethics agreement I have signed. **/
/**************************************************/
#include "tldlist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//#define DEBUG
#define AVL
struct tldlist {
TLDNode *root;
long host_count;
Date *begin;
Date *end;
};
struct tldnode {
char *hostname;
long host_count;
long height;
TLDNode *left;
TLDNode *right;
TLDNode *parent;
};
struct tlditerator {
TLDNode *node;
};
/*
* tldnode_new creates a new node with the given hostname
*/
static TLDNode *tldnode_new(char *hostname);
/*
* tldnode_add update the reference to the pointer that shoud receive
* the given hostname. the reference can point to either the alreadly
* existing hostname element or to the left or right subtree where the new
* node was be created.
*/
static void tldnode_add(TLDList *tld, char *hostname, TLDNode *node);
/*
* tldlist_iter_next_postf returns the next element in the list; returns a pointer
* to the TLDNode if successful, NULL if no more elements to return.
* the iterator runs in postfixed notation
*/
TLDNode *tldlist_iter_next_postf(TLDIterator *iter);
/*
* return the deepest node, begins going to the left and than down
*/
static TLDNode *tldnode_find_deepest(TLDNode *node);
/*
* return the deepest node to the left
*/
static TLDNode *tldnode_find_deepest_left(TLDNode *node);
/*
* print the whole tree given the root (not necessary ordered)
*/
#ifdef DEBUG
static void tldnode_printout(TLDNode *this);
#endif
#ifdef AVL
/*********************************/
/** AVL Functions **/
/*********************************/
/*
* update the height values on the given node and its parents
*/
static void tldnode_update_height(TLDNode *node);
/*
* balance the tree using the AVL algorithm
*/
static void tldnode_balance_tree(TLDList *tld, TLDNode *node);
/*
* return the height for a given node, calculated from its children
*/
static long tldnode_calculate_height(TLDNode *node);
/*
* rotate the node to the right
*/
static void tldnode_rotate_right (TLDList *tld, TLDNode *node);
/*
* rotate the node to the left
*/
static void tldnode_rotate_left (TLDList *tld, TLDNode *node);
#endif
/*
* tldlist_create generates a list structure for storing counts against
* top level domains (TLDs)
*
* creates a TLDList that is constrained to the `begin' and `end' Date's
* returns a pointer to the list if successful, NULL if not
*/
TLDList *tldlist_create(Date *begin, Date *end) {
TLDList* tldlist = malloc(sizeof(TLDList));
if (tldlist != NULL) {
tldlist->root = NULL;
tldlist->host_count = 0;
tldlist->begin = begin;
tldlist->end = end;
}
return tldlist;
}
/*
* tldlist_destroy destroys the list structure in `tld'
*
* all heap allocated storage associated with the list is returned to the heap
*/
void tldlist_destroy(TLDList *tld) {
TLDIterator *it = tldlist_iter_create(tld);
TLDNode *node;
while ((node = tldlist_iter_next_postf(it)) != NULL) {
#ifdef DEBUG
printf("freed %s\n", node->hostname);
#endif
free(node->hostname);
free(node);
}
free(tld);
free(it);
}
/*
* tldlist_add adds the TLD contained in `hostname' to the tldlist if
* `d' falls in the begin and end dates associated with the list;
* returns 1 if the entry was counted, 0 if not
*/
int tldlist_add(TLDList *tld, char *hostname, Date *d) {
long i;
// check if the date is whithin the limit
if (date_compare(d,tld->begin) >= 0 && date_compare(tld->end, d) >= 0) {
#ifdef DEBUG
printf("[ADD] Valid hostname, will be added\n");
#endif
// extract the last dot location
for (i=strlen(hostname); i>0 && hostname[i] != '.'; i--);
// check if it is the first element of the tree
if (tld->root == NULL) {
// create a new node and set it as the first root
tld->root = tldnode_new(hostname+i+1);
} else {
tldnode_add(tld, hostname+i+1, tld->root);
}
tld->host_count++;
return 1;
}
#ifdef DEBUG
printf("[ADD] Invalid host, date outside the limit\n");
#endif
return 0;
}
/*
* tldlist_count returns the number of successful tldlist_add() calls since
* the creation of the TLDList
*/
long tldlist_count(TLDList *tld) {
return tld->host_count;
}
/*
* tldlist_iter_create creates an iterator over the TLDList; returns a pointer
* to the iterator if successful, NULL if not
*/
TLDIterator *tldlist_iter_create(TLDList *tld) {
TLDIterator* newiter = malloc(sizeof(TLDIterator));
newiter->node = tldnode_find_deepest_left(tld->root);
return newiter;
}
/*
* tldlist_iter_next returns the next element in the list; returns a pointer
* to the TLDNode if successful, NULL if no more elements to return.
* the iterator runs in infixed notation
*/
TLDNode *tldlist_iter_next(TLDIterator *iter) {
TLDNode* returned = iter->node;
TLDNode *temp;
// if the last iterated element was the root
if (returned == NULL)
return NULL;
// update the iterator referenced node
if ((temp = tldnode_find_deepest_left(iter->node->right)) == NULL) {
if (iter->node->parent != NULL) {
if (iter->node != iter->node->parent->left)
while (iter->node->parent != NULL && iter->node == iter->node->parent->right)
iter->node = iter->node->parent;
if (iter->node->parent == NULL)
iter->node = NULL;
else
iter->node = iter->node->parent;
} else
iter->node = NULL;
} else
iter->node = temp;
return returned;
}
/*
* tldlist_iter_next_postf returns the next element in the list; returns a pointer
* to the TLDNode if successful, NULL if no more elements to return.
* the iterator runs in postfixed notation
*/
TLDNode *tldlist_iter_next_postf(TLDIterator *iter) {
TLDNode* returned = iter->node;
// if the last iterated element was the root
if (returned == NULL)
return NULL;
// if the current element is the root
if (iter->node->parent == NULL) {
iter->node = NULL;
return returned;
}
// update the iterator referenced node
if (iter->node->parent->right != iter->node && iter->node->parent->right != NULL)
iter->node = tldnode_find_deepest(iter->node->parent->right);
else
iter->node = iter->node->parent;
return returned;
}
/*
* tldlist_iter_destroy destroys the iterator specified by `iter'
*/
void tldlist_iter_destroy(TLDIterator *iter) {
free(iter);
}
/*
* tldnode_tldname returns the tld associated with the TLDNode
*/
char *tldnode_tldname(TLDNode *node) {
return node->hostname;
}
/*
* tldnode_count returns the number of times that a log entry for the
* corresponding tld was added to the list
*/
long tldnode_count(TLDNode *node) {
return node->host_count;
}
/*
* tldnode_new creates a new node with the given hostname
*/
static TLDNode *tldnode_new(char *hostname) {
unsigned int i;
TLDNode* newnode = malloc(sizeof(TLDNode));
newnode->hostname = malloc((strlen(hostname) + 1)*sizeof(char));
for (i=0; i<strlen(hostname); i++)
newnode->hostname[i] = tolower(hostname[i]);
newnode->hostname[i] = hostname[i];
newnode->host_count = 1;
newnode->height = 1;
newnode->left = NULL;
newnode->right = NULL;
newnode->parent = NULL;
return newnode;
}
/*
* print the whole tree given the root (not necessary ordered)
*/
#ifdef DEBUG
static void tldnode_printout(TLDNode *this) {
if (this != NULL) {
tldnode_printout(this->left);
printf("hostname: %s - height: %ld\n", this->hostname, this->height);
tldnode_printout(this->right);
}
}
#endif
/*
* tldnode_add update the reference to the pointer that shoud receive
* the given hostname. the reference can point to either the alreadly
* existing hostname element or to the left or right subtree where the new
* node was be created.
*/
static void tldnode_add(TLDList *tld, char *hostname, TLDNode *node) {
int cmp;
cmp = strcmp(hostname, node->hostname);
if (cmp < 0) {
// if lower, move to the left subtree
if (node->left != NULL) {
// if it haven't found yet, keep searching
tldnode_add(tld, hostname, node->left);
} else {
// if the tree has runout and haven't been found node, create a new one
node->left = tldnode_new(hostname);
node->left->parent = node;
#ifdef AVL
tldnode_update_height(node->left);
tldnode_balance_tree(tld, node->left);
#ifdef DEBUG
while(node->parent != NULL)
node = node->parent;
tldnode_printout(node);
printf("\n");
#endif
#endif
}
} else if (cmp > 0) {
// if greater, move to the right subtree
if (node->right != NULL) {
// if it haven't found yet, keep searching
tldnode_add(tld, hostname, node->right);
} else {
// if the tree has runout and haven't been found node, create a new one
node->right = tldnode_new(hostname);
node->right->parent = node;
#ifdef AVL
tldnode_update_height(node->right);
tldnode_balance_tree(tld, node->right);
#ifdef DEBUG
while(node->parent != NULL)
node = node->parent;
tldnode_printout(node);
printf("\n");
#endif
#endif
}
} else {
// if it is the same hostname
node->host_count++;
}
}
/*
* return the deepest node, begins going to the left and than down
*/
static TLDNode *tldnode_find_deepest(TLDNode *node) {
TLDNode *this = node;
// since C will stop the evaluation of || on the first true return,
// if the left subtree != NULL it won't evaluate the right subtree
if (node != NULL && ((this = tldnode_find_deepest(node->left)) != NULL || (this = tldnode_find_deepest(node->right)) != NULL))
return this;
return node;
}
/*
* return the deepest node to the left
*/
static TLDNode *tldnode_find_deepest_left(TLDNode *node) {
if (node != NULL)
while (node->left != NULL)
node = node->left;
return node;
}
#ifdef AVL
/*********************************/
/** AVL Functions **/
/*********************************/
/*
* update the height values on the given node and its parents
*/
static void tldnode_update_height(TLDNode *node) {
while (node != NULL) {
node->height = tldnode_calculate_height(node);
node = node->parent;
}
}
/*
* balance the tree using the AVL algorithm
*/
static void tldnode_balance_tree(TLDList *tld, TLDNode *node) {
long balance;
while (node != NULL) {
// check if the subtree node is unbalanced
if (node->height > 2 && abs(balance = tldnode_calculate_height(node->left) - tldnode_calculate_height(node->right)) > 1) {
if (balance > 0) { // ?-right
if (tldnode_calculate_height(node->left->right) > tldnode_calculate_height(node->left->left)) { // left-right
tldnode_rotate_left(tld, node->left);
if (node->left->left != NULL)
node->left->left->height = tldnode_calculate_height(node->left->left);
node->left->height = tldnode_calculate_height(node->left);
}
tldnode_rotate_right(tld, node);
if (node->right != NULL)
node->right->height = tldnode_calculate_height(node->right);
node->height = tldnode_calculate_height(node);
} else { // ?-left
if (tldnode_calculate_height(node->right->left) > tldnode_calculate_height(node->right->right)) { // right-left
tldnode_rotate_right(tld, node->right);
if (node->right->right != NULL)
node->right->right->height = tldnode_calculate_height(node->right->right);
node->right->height = tldnode_calculate_height(node->right);
}
tldnode_rotate_left(tld, node);
if (node->left != NULL)
node->left->height = tldnode_calculate_height(node->left);
node->height = tldnode_calculate_height(node);
}
}
#ifdef DEBUG
printf("\n");
#endif
// iterate through the tree until it hits the root
node = node->parent;
}
}
/*
* return the height for a given node, calculated from its children
*/
static long tldnode_calculate_height(TLDNode *node) {
if (node == NULL)
return 0;
long height_left = node->left == NULL ? 0 : node->left->height;
long height_right = node->right == NULL ? 0 : node->right->height;
return (height_left > height_right ? height_left : height_right) + 1;
}
/*
* rotate the node to the right
*/
static void tldnode_rotate_right (TLDList *tld, TLDNode *node) {
// check if it is the new first node
if (node->parent != NULL) {
// if not, checks in which side of the parent is the node
if (node->parent->left == node)
node->parent->left = node->left;
else
node->parent->right = node->left;
} else
tld->root = node->left;
node->left->parent = node->parent;
node->parent = node->left;
node->left = node->left->right;
if (node->left != NULL)
node->left->parent = node;
node->parent->right = node;
#ifdef DEBUG
printf("right rot\n");
#endif
}
/*
* rotate the node to the left
*/
static void tldnode_rotate_left (TLDList *tld, TLDNode *node) {
// check if it is the new first node
if (node->parent != NULL) {
// if not, checks in which side of the parent is the node
if (node->parent->right == node)
node->parent->right = node->right;
else
node->parent->left = node->right;
} else
tld->root = node->right;
node->right->parent = node->parent;
node->parent = node->right;
node->right = node->right->left;
if (node->right != NULL)
node->right->parent = node;
node->parent->left = node;
#ifdef DEBUG
printf("left rot\n");
#endif
}
#endif