Skip to content

Commit

Permalink
optimize linked list creation for tags
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Aug 27, 2017
1 parent d87f363 commit 39e1dec
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 112 deletions.
50 changes: 22 additions & 28 deletions src/danbooru.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ struct image_tag_db *danbooru_get_image_tags(char *html_content)

/* initialize a tags database to store tags */
struct image_tag_db *tag_db = init_image_tag_db();
struct ll_node *tag_ptrs[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
struct ll_node **tag_ptrs[6] = {
&(tag_db->tags[0]),
&(tag_db->tags[1]),
&(tag_db->tags[2]),
&(tag_db->tags[3]),
&(tag_db->tags[4]),
&(tag_db->tags[5])
};

/* set pointer to beginning of first tag */
tag_contents = strstr(tag_contents, tag_category_uuid);
Expand All @@ -91,35 +98,22 @@ struct image_tag_db *danbooru_get_image_tags(char *html_content)
/* get where the tag name ends is */
int len_tag_name = get_distance(tag_contents, tag_name_end);

/* create the linked list node to store the information */
if (!(tag_db->tags[tag_index])) {
/* malloc memory for node */
tag_db->tags[tag_index] = malloc(sizeof(struct ll_node));
/* malloc memory for char array in node */
tag_db->tags[tag_index]->data = malloc(sizeof(char) * (len_tag_name + 1));
/* set first element to \0 */
tag_db->tags[tag_index]->data[0] = '\0';
/* concatentate tag name to char array */
strncat(tag_db->tags[tag_index]->data,
tag_contents, len_tag_name);

/* set tag_ptrs to it */
tag_ptrs[tag_index] = tag_db->tags[tag_index];
}
else {
/* malloc memory for node */
tag_ptrs[tag_index]->next = malloc(sizeof(struct ll_node));
/* malloc memory for char array in node */
tag_ptrs[tag_index]->next->data = malloc(sizeof(char) * (len_tag_name + 1));
/* set first element to \0 */
tag_ptrs[tag_index]->next->data[0] = '\0';
strncat(tag_ptrs[tag_index]->next->data,

/* allocate memory for node */
*(tag_ptrs[tag_index]) = malloc(LLNODE_SIZE);

(*(tag_ptrs[tag_index]))->next = NULL;
(*(tag_ptrs[tag_index]))->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
(*(tag_ptrs[tag_index]))->data[0] = '\0';
/* concatentate tag name to char array */
strncat((*(tag_ptrs[tag_index]))->data,
tag_contents, len_tag_name);

/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = tag_ptrs[tag_index]->next;
}
tag_ptrs[tag_index]->next = NULL;
/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = &((*(tag_ptrs[tag_index]))->next);


/* increment the amount of tags in this category we currently
* found */
(tag_db->tag_size[tag_index])++;
Expand Down
50 changes: 22 additions & 28 deletions src/gelbooru.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,14 @@ struct image_tag_db *gelbooru_get_image_tags(char *html_content)

/* initialize a tags database to store tags */
struct image_tag_db *tag_db = init_image_tag_db();
struct ll_node *tag_ptrs[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
struct ll_node **tag_ptrs[6] = {
&(tag_db->tags[0]),
&(tag_db->tags[1]),
&(tag_db->tags[2]),
&(tag_db->tags[3]),
&(tag_db->tags[4]),
&(tag_db->tags[5])
};

/* set pointer to beginning of first tag */
tag_contents = strstr(tag_contents, tag_category_uuid);
Expand All @@ -90,35 +97,22 @@ struct image_tag_db *gelbooru_get_image_tags(char *html_content)
/* get where the tag name ends is */
int len_tag_name = get_distance(tag_contents, tag_name_end);

/* create the linked list node to store the information */
if (!(tag_db->tags[tag_index])) {
/* malloc memory for node */
tag_db->tags[tag_index] = malloc(sizeof(struct ll_node));
/* malloc memory for char array in node */
tag_db->tags[tag_index]->data = malloc(sizeof(char) * (len_tag_name + 1));
/* set first element to \0 */
tag_db->tags[tag_index]->data[0] = '\0';
/* concatentate tag name to char array */
strncat(tag_db->tags[tag_index]->data,
tag_contents, len_tag_name);

/* set tag_ptrs to it */
tag_ptrs[tag_index] = tag_db->tags[tag_index];
}
else {
/* malloc memory for node */
tag_ptrs[tag_index]->next = malloc(sizeof(struct ll_node));
/* malloc memory for char array in node */
tag_ptrs[tag_index]->next->data = malloc(sizeof(char) * (len_tag_name + 1));
/* set first element to \0 */
tag_ptrs[tag_index]->next->data[0] = '\0';
strncat(tag_ptrs[tag_index]->next->data,

/* allocate memory for node */
*(tag_ptrs[tag_index]) = malloc(LLNODE_SIZE);

(*(tag_ptrs[tag_index]))->next = NULL;
(*(tag_ptrs[tag_index]))->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
(*(tag_ptrs[tag_index]))->data[0] = '\0';
/* concatentate tag name to char array */
strncat((*(tag_ptrs[tag_index]))->data,
tag_contents, len_tag_name);

/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = tag_ptrs[tag_index]->next;
}
tag_ptrs[tag_index]->next = NULL;
/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = &((*(tag_ptrs[tag_index]))->next);


/* increment the amount of tags in this category we currently
* found */
(tag_db->tag_size[tag_index])++;
Expand Down
50 changes: 22 additions & 28 deletions src/sankakucomplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ struct image_tag_db *sankakucomplex_get_image_tags(char *html_content)

/* initialize a tags database to store tags */
struct image_tag_db *tag_db = init_image_tag_db();
struct ll_node *tag_ptrs[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
struct ll_node **tag_ptrs[6] = {
&(tag_db->tags[0]),
&(tag_db->tags[1]),
&(tag_db->tags[2]),
&(tag_db->tags[3]),
&(tag_db->tags[4]),
&(tag_db->tags[5])
};

while (tag_contents) {
tag_contents = &(tag_contents[category_offset]);
Expand All @@ -98,35 +105,22 @@ struct image_tag_db *sankakucomplex_get_image_tags(char *html_content)
/* get length of tag name */
int len_tag_name = get_distance(tag_contents, tag_name_end);

/* create the linked list node to store the information */
if (!(tag_db->tags[tag_index])) {
/* malloc memory for node */
tag_db->tags[tag_index] = malloc(LLNODE_SIZE);
/* malloc memory for char array in node */
tag_db->tags[tag_index]->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
tag_db->tags[tag_index]->data[0] = '\0';
/* concatentate tag name to char array */
strncat(tag_db->tags[tag_index]->data,
tag_contents, len_tag_name);

/* set tag_ptrs to it */
tag_ptrs[tag_index] = tag_db->tags[tag_index];
}
else {
/* malloc memory for node */
tag_ptrs[tag_index]->next = malloc(LLNODE_SIZE);
/* malloc memory for char array in node */
tag_ptrs[tag_index]->next->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
tag_ptrs[tag_index]->next->data[0] = '\0';
strncat(tag_ptrs[tag_index]->next->data,

/* allocate memory for node */
*(tag_ptrs[tag_index]) = malloc(LLNODE_SIZE);

(*(tag_ptrs[tag_index]))->next = NULL;
(*(tag_ptrs[tag_index]))->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
(*(tag_ptrs[tag_index]))->data[0] = '\0';
/* concatentate tag name to char array */
strncat((*(tag_ptrs[tag_index]))->data,
tag_contents, len_tag_name);

/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = tag_ptrs[tag_index]->next;
}
tag_ptrs[tag_index]->next = NULL;
/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = &((*(tag_ptrs[tag_index]))->next);


/* increment the amount of tags in this category we currently
* found */
(tag_db->tag_size[tag_index])++;
Expand Down
50 changes: 22 additions & 28 deletions src/yandere.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,14 @@ struct image_tag_db *yandere_get_image_tags(char *html_content)

/* initialize a tags database to store tags */
struct image_tag_db *tag_db = init_image_tag_db();
struct ll_node *tag_ptrs[6] = { NULL, NULL, NULL, NULL, NULL, NULL };
struct ll_node **tag_ptrs[6] = {
&(tag_db->tags[0]),
&(tag_db->tags[1]),
&(tag_db->tags[2]),
&(tag_db->tags[3]),
&(tag_db->tags[4]),
&(tag_db->tags[5])
};

while (next_tag_distance > 0) {
/* get the end of the category name */
Expand All @@ -110,35 +117,22 @@ struct image_tag_db *yandere_get_image_tags(char *html_content)
/* get length of tag name */
unsigned int len_tag_name = get_distance(tag_contents, tag_name_end);

/* create the linked list node to store the information */
if (!(tag_db->tags[tag_index])) {
/* malloc memory for node */
tag_db->tags[tag_index] = malloc(LLNODE_SIZE);
/* malloc memory for char array in node */
tag_db->tags[tag_index]->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
tag_db->tags[tag_index]->data[0] = '\0';
/* concatentate tag name to char array */
strncat(tag_db->tags[tag_index]->data,
tag_contents, len_tag_name);

/* set tag_ptrs to it */
tag_ptrs[tag_index] = tag_db->tags[tag_index];
}
else {
/* malloc memory for node */
tag_ptrs[tag_index]->next = malloc(LLNODE_SIZE);
/* malloc memory for char array in node */
tag_ptrs[tag_index]->next->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
tag_ptrs[tag_index]->next->data[0] = '\0';
strncat(tag_ptrs[tag_index]->next->data,

/* allocate memory for node */
*(tag_ptrs[tag_index]) = malloc(LLNODE_SIZE);

(*(tag_ptrs[tag_index]))->next = NULL;
(*(tag_ptrs[tag_index]))->data = malloc(CHAR_SIZE * (len_tag_name + 1));
/* set first element to \0 */
(*(tag_ptrs[tag_index]))->data[0] = '\0';
/* concatentate tag name to char array */
strncat((*(tag_ptrs[tag_index]))->data,
tag_contents, len_tag_name);

/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = tag_ptrs[tag_index]->next;
}
tag_ptrs[tag_index]->next = NULL;
/* set tag_ptrs to its next value */
tag_ptrs[tag_index] = &((*(tag_ptrs[tag_index]))->next);


/* increment the amount of tags in this category we currently
* found */
(tag_db->tag_size[tag_index])++;
Expand Down

0 comments on commit 39e1dec

Please sign in to comment.