feat: use rb_tree to have faster results

This commit is contained in:
gbrochar 2024-10-20 20:10:21 +02:00
parent 6b31743c38
commit 32760601af
2 changed files with 87 additions and 15 deletions

View File

@ -22,7 +22,7 @@ OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES))
CC = gcc CC = gcc
CFLAGS = -Wall -Werror -Wextra -g2 CFLAGS = -Wall -Werror -Wextra -g3
LIB_NAME = libft LIB_NAME = libft

View File

@ -139,24 +139,79 @@ char *get_sym_char(Elf64_Sym sym, Elf64_Shdr sec) {
return ("n"); return ("n");
} }
int strcmp_nm(t_list *a, t_list *b) { int strcmp_nm(void *a, void *b) {
t_entry *aa = (t_entry *)a;
t_entry *bb = (t_entry *)b;
char *s1 = aa->symbol;
char *s2 = bb->symbol;
size_t a_i = 0;
size_t b_i = 0;
while (s1[a_i] && s2[b_i]) {
bool is_okay = true;
char c = s1[a_i] & 0x11101111;
if (!(c >= 'a' && c <= 'z')) {
++a_i;
is_okay = false;
}
c = s2[b_i] & 0x11101111;
if (!(c >= 'a' && c <= 'z')) {
is_okay = false;
++b_i;
}
if (is_okay) {
printf("comparing %c and %c\n", s1[a_i], s2[b_i]);
if ((s1[a_i] & 0x11101111) != (s2[b_i] & 0x11101111)) {
return s1[a_i] - s2[b_i];
} else {
a_i++;
b_i++;
}
}
}
return s1[a_i] - s2[b_i];
}
int reverse(void *a, void *b) {
return strcmp_nm(b, a);
}
int nosort(void *a, void *b) {
(void)a; (void)a;
(void)b; (void)b;
return 1; return 1;
} }
int reverse(t_list *a, t_list *b) { void put_entry(void *data) {
return strcmp_nm(b, a); t_node *node = (t_node *)data;
t_entry *entry = (t_entry *)node->data;
/* if (node->parent) {
ft_putstr("node has parent: ");
t_node *parent = (t_node *)node->parent;
t_entry *lol = (t_entry *)parent->data;
ft_putstr(lol->string);
} }
if (node->left) {
int nosort(t_list *a, t_list *b) { ft_putstr("node has left: ");
(void)a; t_node *left = (t_node *)node->left;
(void)b; t_entry *lol = (t_entry *)left->data;
return -1; ft_putstr(lol->string);
}
if (node->right) {
ft_putstr("node has right: ");
t_node *right = (t_node *)node->right;
t_entry *lol = (t_entry *)right->data;
ft_putstr(lol->string);
}
if (node->color == RED) {
ft_putstr("RED NODE: ");
} else {
ft_putstr("BLACK NODE: ");
}*/
ft_putstr(entry->string);
} }
int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) { int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_ordering ordering) {
t_list *list = NULL; t_root *tree = NULL;
t_entry *entry = (t_entry *)malloc(sizeof(t_entry)); t_entry *entry = (t_entry *)malloc(sizeof(t_entry));
if (!entry) if (!entry)
return FT_NM_FAILURE; return FT_NM_FAILURE;
@ -251,13 +306,13 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
// ensuite un switch ordering avec un ft_lstinsert (pointeur sur fonction serait plus opti mais osef) // ensuite un switch ordering avec un ft_lstinsert (pointeur sur fonction serait plus opti mais osef)
switch (ordering) { switch (ordering) {
case NOSORT: case NOSORT:
ft_lstinsert(&list, ft_lstnew((void *)entry, sizeof(t_entry)), &nosort); ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &nosort);
break; break;
case REVERSE: case REVERSE:
ft_lstinsert(&list, ft_lstnew((void *)entry, sizeof(t_entry)), &reverse); ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &reverse);
break; break;
case DEFAULT_ORDERING: case DEFAULT_ORDERING:
ft_lstinsert(&list, ft_lstnew((void *)entry, sizeof(t_entry)), &strcmp_nm); ft_rbt_insert(&tree, ft_rbt_new((void *)entry, sizeof(t_entry)), &strcmp_nm);
break; break;
} }
// pas de bst oops :D // pas de bst oops :D
@ -269,15 +324,32 @@ int nm64(t_mapped_file mapped_file, char *path, t_verbosity verbosity, t_orderin
free(str); free(str);
} }
// ici on affiche tout ou no symbols en cas de list vide // ici on affiche tout ou no symbols en cas de list vide
/* char buffer[16384];
size_t buf_i = 0;
size_t entry_string_len = 0;
if (list) { if (list) {
while (list) { while (list) {
t_entry *my_entry = (t_entry *)list->content; t_entry *my_entry = (t_entry *)list->content;
ft_putstr(my_entry->string); entry_string_len = ft_strlen(my_entry->string);
if (buf_i + entry_string_len < 16384) {
ft_memcpy(buffer + buf_i, my_entry->string, entry_string_len);
buf_i += entry_string_len;
} else {
buffer[buf_i] = '\0';
ft_putstr(buffer);
buf_i = 0;
}
//ft_putstr(my_entry->string);
list = list->next; list = list->next;
} }
} }
else { else {
ft_printf("sss", "nm: ", path, ": no symbols\n"); ft_printf("sss", "nm: ", path, ": no symbols\n");
}*/
if (tree) {
ft_putrbt(tree, &put_entry);
} else {
ft_printf("sss", "nm: ", path, ": no symbols\n");
} }
return FT_NM_SUCCESS; return FT_NM_SUCCESS;
} }