diff options
| author | Charles Cabergs <me@cacharle.xyz> | 2020-10-11 15:52:52 +0200 |
|---|---|---|
| committer | Charles Cabergs <me@cacharle.xyz> | 2020-10-11 15:52:52 +0200 |
| commit | c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a (patch) | |
| tree | 12f1c827ee063ed3e7038f6a704014e611e4f388 /libft/src/lst/ft_lstbsearch.c | |
| parent | a4ceb5974d1b7dcdd12cc81b7eb07893ea16c8ad (diff) | |
| download | minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.tar.gz minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.tar.bz2 minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.zip | |
Removing libft/minishell_test submodules, Removing subject/README/etc
Diffstat (limited to 'libft/src/lst/ft_lstbsearch.c')
| -rw-r--r-- | libft/src/lst/ft_lstbsearch.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/libft/src/lst/ft_lstbsearch.c b/libft/src/lst/ft_lstbsearch.c new file mode 100644 index 0000000..0c48eb0 --- /dev/null +++ b/libft/src/lst/ft_lstbsearch.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstbsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:12 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_lst.h" + +static t_ftlst *st_lstmiddle(t_ftlst *lst, t_ftlst *last) +{ + t_ftlst *slow; + t_ftlst *fast; + + if (lst == NULL) + return (NULL); + slow = lst; + fast = lst; + while (fast != last) + { + fast = fast->next; + if (fast == last) + break ; + slow = slow->next; + fast = fast->next; + } + return (slow); +} + +static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last, + t_ftcompar_func cmp, const void *ref) +{ + int res; + t_ftlst *mid; + + if (lst == NULL) + return (NULL); + mid = st_lstmiddle(lst, last); + if (mid == NULL) + return (NULL); + if (mid->next == NULL) + { + if (cmp(ref, mid->data) == 0) + return (mid); + return (NULL); + } + res = cmp(ref, mid->next->data); + if (res < 0) + return (st_lstbsearch_rec(lst, mid, cmp, ref)); + else if (res > 0) + return (st_lstbsearch_rec(mid->next->next, NULL, cmp, ref)); + return (mid->next); +} + +t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftcompar_func cmp, + const void *ref) +{ + return (st_lstbsearch_rec(lst, NULL, cmp, ref)); +} |
