diff options
Diffstat (limited to 'src')
43 files changed, 539 insertions, 136 deletions
diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 2c54721..7374a44 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,16 +6,24 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:35:06 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:10:16 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) +/* +** \brief Delete one hash table entry +** \param key Key of entry to delete +** \param del Function to destroy the entry +** \warning The del function HAS to free the key +** \note Do nothing if their is to entry which correspond to key +*/ + +void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_entry*)) { - ft_lstremove_if(ht->entries + ft_hthash(ht, key), + ft_lstremove_if(ht->buckets + ft_hthash(ht, key), ft_inter_htkey_cmp, key, (void (*)(void*))del); } diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index e0442c6..ff362d2 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -5,20 +5,26 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 08:19:06 by cacharle #+# #+# */ -/* Updated: 2020/01/30 08:33:09 by cacharle ### ########.fr */ +/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:10:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)) +/* +** \brief Destroy an hash table. +** \param del Function to delete each entry +** \warning The del function HAS to free the key +*/ + +void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*)) { if (ht == NULL) return ; while (ht->size-- > 0) - ft_lstclear(ht->entries + ht->size, (void (*)(void*))del); - free(ht->entries); + ft_lstdestroy(ht->buckets + ht->size, (void (*)(void*))del); + free(ht->buckets); free(ht); } diff --git a/src/ht/ft_htcontent_new.c b/src/ht/ft_htentry_new.c index 214e125..12a1159 100644 --- a/src/ht/ft_htcontent_new.c +++ b/src/ht/ft_htentry_new.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_htcontent_new.c :+: :+: :+: */ +/* ft_htentry_new.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -13,13 +13,19 @@ #include "libft.h" #include "libft_ht.h" -t_ftht_content *ft_htcontent_new(char *key, void *value) +/* +** \brief Create a new hash table key/value pair. +** \param key Hash entry string key (always duplicated) +** \return Content or NULL if an allocation failed. +*/ + +t_ftht_entry *ft_htentry_new(char *key, void *value) { - t_ftht_content *content; + t_ftht_entry *content; if (key == NULL) return (NULL); - if ((content = (t_ftht_content*)malloc(sizeof(t_ftht_content))) == NULL) + if ((content = (t_ftht_entry*)malloc(sizeof(t_ftht_entry))) == NULL) return (NULL); if ((content->key = ft_strdup(key)) == NULL) { diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 76e4fb2..a6383fe 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,24 +6,30 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/02/19 01:44:41 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 18:02:57 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" +/* +** \brief Retrieve a value with a key +** \param ht Hash table where key is searched +** \param key Searched key +** \return Value void pointer at key or NULL if not found +*/ + void *ft_htget(t_ftht *ht, char *key) { - t_ftht_digest digest; t_ftlst *found; if (ht == NULL || key == NULL) return (NULL); digest = ft_hthash(ht, key); - found = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, key); + found = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (found == NULL) return (NULL); - return (((t_ftht_content*)found->content)->value); + return (((t_ftht_entry*)found->data)->value); } diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index e7e696c..3369d24 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -12,6 +12,14 @@ #include "libft_ht.h" +/* +** \brief Hash a string +** \param ht So that the index is in the hash table bound +** \param key String to hash +** \return Hash +*/ + +// maybe use a less efficient but understandable function t_ftht_digest ft_hthash(t_ftht *ht, char *key) { t_ftht_digest digest; diff --git a/src/ht/ft_htiter.c b/src/ht/ft_htiter.c new file mode 100644 index 0000000..b854993 --- /dev/null +++ b/src/ht/ft_htiter.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:02:24 by charles #+# #+# */ +/* Updated: 2020/04/01 18:02:32 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +/* +** \brief Iterate over entry of hash table +** \param ht Iterated hash table +** \param f Function applied to each entry +*/ + +void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*)) +{ + size_t i; + + i = 0; + while (i < ht->size) + { + ft_lstiter(ht->buckets[i], (void (*)(void*))f); + i++; + } +} diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index d98a724..e5335d2 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,13 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/10 02:16:20 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:23:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" +/* +** \brief Create a new hash table. +** \param size Size of the underlying array of linked list (buckets) +** \return Created hash table or NULL is an allocation failed +*/ + t_ftht *ft_htnew(t_ftsize size) { t_ftht *ht; @@ -21,8 +27,8 @@ t_ftht *ft_htnew(t_ftsize size) return (NULL); if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) return (NULL); - ht->entries = (t_ftht_entry*)ft_calloc(size, sizeof(t_ftht_entry)); - if (ht->entries == NULL) + ht->buckets = (t_ftht_bucket*)ft_calloc(size, sizeof(t_ftht_entry)); + if (ht->buckets == NULL) { free(ht); return (NULL); diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index c7068d5..68d3752 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,40 +6,51 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:44:10 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 18:02:12 by charles ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" -t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value, - void (*del)(t_ftht_content*)) +/* +** \brief Create/Update a entry in hash table. +** \note If `key` already exist in `ht` +** only updates the list node content. +** Else create a new list node in addition the list content. +** \param ht Hash table where the entry is modified +** \param key Key of the new entry +** \param value Value of the new entry +** \param del Destroy function in case the entry is modified. +** \return Pointer to the created entry, NULL if an allocation failed. +*/ + +t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value, + void (*del)(t_ftht_entry*)) { t_ftht_digest digest; - t_ftht_content *content; - t_ftht_entry entry; + t_ftht_entry *content; + t_ftht_bucket bucket; t_ftlst *tmp; if (ht == NULL || key == NULL) return (NULL); - if ((content = ft_htcontent_new(key, value)) == NULL) + if ((content = ft_htentry_new(key, value)) == NULL) return (NULL); digest = ft_hthash(ht, key); - tmp = ft_lstlfind(ht->entries[digest], ft_inter_htkey_cmp, key); + tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (tmp != NULL) { if (del != NULL) - (*del)(tmp->content); - tmp->content = content; - return ((t_ftht_content*)tmp->content); + del(tmp->data); + tmp->data = content; + return ((t_ftht_entry*)tmp->data); } - - if ((entry = ft_lstnew(content)) == NULL) + if ((bucket = ft_lstnew(content)) == NULL) { - free(content); + del(content); return (NULL); } - ft_lstadd_front(ht->entries + digest, entry); + ft_lstpush_front(ht->buckets + digest, bucket); return (content); } diff --git a/src/ht/ft_inter_htkey_cmp.c b/src/ht/ft_inter_htkey_cmp.c index 6f04ecc..e8a0375 100644 --- a/src/ht/ft_inter_htkey_cmp.c +++ b/src/ht/ft_inter_htkey_cmp.c @@ -6,16 +6,20 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:03:14 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:20:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "libft_ht.h" +/* +** Hash table internal function to compare key string in linked list. +*/ + int ft_inter_htkey_cmp(const void *ref_key, const void *content) { if (ref_key == NULL || content == NULL) return (-1); - return (ft_strcmp((char*)ref_key, ((t_ftht_content*)content)->key)); + return (ft_strcmp((char*)ref_key, ((t_ftht_entry*)content)->key)); } diff --git a/src/io/ft_next_line.c b/src/io/ft_next_line.c index 0f4cc2c..74afa71 100644 --- a/src/io/ft_next_line.c +++ b/src/io/ft_next_line.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/31 10:39:38 by cacharle #+# #+# */ -/* Updated: 2020/02/14 03:38:01 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:11:35 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/io/ft_printf/internals/list.c b/src/io/ft_printf/internals/list.c index 99491f4..37f8013 100644 --- a/src/io/ft_printf/internals/list.c +++ b/src/io/ft_printf/internals/list.c @@ -18,7 +18,7 @@ t_flist *list_new(t_pformat *content) if ((lst = (t_flist*)malloc(sizeof(t_flist))) == NULL) return (NULL); - lst->content = content; + lst->data = content; lst->next = NULL; return (lst); } @@ -47,7 +47,7 @@ void list_pop_front(t_flist **lst) if (lst == NULL || *lst == NULL) return ; tmp = (*lst)->next; - free((*lst)->content); + free((*lst)->data); free(*lst); *lst = tmp; } diff --git a/src/io/ft_printf/internals/parse.c b/src/io/ft_printf/internals/parse.c index 33928a0..4650481 100644 --- a/src/io/ft_printf/internals/parse.c +++ b/src/io/ft_printf/internals/parse.c @@ -28,7 +28,7 @@ int parse(const char *format, t_flist **flist) if ((tmp = list_new(parsed)) == NULL) return ((int)list_destroy(flist)); list_push_front(flist, tmp); - format += (*flist)->content->fmt_len; + format += (*flist)->data->fmt_len; } *flist = list_reverse(*flist); return (1); diff --git a/src/lst/ft_lstbsearch.c b/src/lst/ft_lstbsearch.c index d694209..0c48eb0 100644 --- a/src/lst/ft_lstbsearch.c +++ b/src/lst/ft_lstbsearch.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:03:21 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:12:12 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,11 +46,11 @@ static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last, return (NULL); if (mid->next == NULL) { - if (cmp(ref, mid->content) == 0) + if (cmp(ref, mid->data) == 0) return (mid); return (NULL); } - res = cmp(ref, mid->next->content); + res = cmp(ref, mid->next->data); if (res < 0) return (st_lstbsearch_rec(lst, mid, cmp, ref)); else if (res > 0) diff --git a/src/lst/ft_lstdelone.c b/src/lst/ft_lstdelone.c index 63dcc35..3dfbbbb 100644 --- a/src/lst/ft_lstdelone.c +++ b/src/lst/ft_lstdelone.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstdelone_bonus.c :+: :+: :+: */ +/* ft_lstdelone.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,14 +10,18 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Delete list node +** \param del Delete function for node's data +*/ + void ft_lstdelone(t_ftlst *lst, void (*del)(void *)) { if (lst == NULL) return ; if (del != NULL) - (*del)(lst->content); + (*del)(lst->data); free(lst); } diff --git a/src/lst/ft_lstclear.c b/src/lst/ft_lstdestroy.c index 0bacb4f..35da2a5 100644 --- a/src/lst/ft_lstclear.c +++ b/src/lst/ft_lstdestroy.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstclear_bonus.c :+: :+: :+: */ +/* ft_lstdestroy.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,16 +10,20 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -void ft_lstclear(t_ftlst **lst, void (*del)(void *)) +/* +** \brief Destroy a list and set his pointer to NULL +** \param del Delete Function for data of each node +*/ + +void ft_lstdestroy(t_ftlst **lst, void (*del)(void *)) { if (lst == NULL) return ; if (*lst == NULL) return ; - ft_lstclear(&((*lst)->next), del); + ft_lstdestroy(&((*lst)->next), del); ft_lstdelone(*lst, del); *lst = NULL; } diff --git a/src/lst/ft_lstiter.c b/src/lst/ft_lstiter.c index 9b2895b..e46b507 100644 --- a/src/lst/ft_lstiter.c +++ b/src/lst/ft_lstiter.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstiter_bonus.c :+: :+: :+: */ +/* ft_lstiter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,16 +10,20 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Iterate of list +** \param f Funtion applied to data of each node +*/ + void ft_lstiter(t_ftlst *lst, void (*f)(void *)) { if (f == NULL) return ; while (lst != NULL) { - (*f)(lst->content); + (*f)(lst->data); lst = lst->next; } } diff --git a/src/lst/ft_lstlast.c b/src/lst/ft_lstlast.c index 728cbf2..97b853d 100644 --- a/src/lst/ft_lstlast.c +++ b/src/lst/ft_lstlast.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstlast_bonus.c :+: :+: :+: */ +/* ft_lstlast.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,9 +10,13 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Last node +** \return List's last node +*/ + t_ftlst *ft_lstlast(t_ftlst *lst) { if (lst == NULL) diff --git a/src/lst/ft_lstlfind.c b/src/lst/ft_lstlfind.c index 92d37d8..fd7e688 100644 --- a/src/lst/ft_lstlfind.c +++ b/src/lst/ft_lstlfind.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/17 03:04:52 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:03:11 by cacharle ### ########.fr */ +/* Created: 2020/02/27 18:00:37 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:05 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ t_ftlst *ft_lstlfind(t_ftlst *lst, t_ftcompar_func cmp, const void *ref) { if (lst == NULL) return (NULL); - if (cmp(ref, lst->content) == 0) + if (cmp(ref, lst->data) == 0) return (lst); return (ft_lstlfind(lst->next, cmp, ref)); } diff --git a/src/lst/ft_lstlsearch.c b/src/lst/ft_lstlsearch.c index 8d2acc8..11c528c 100644 --- a/src/lst/ft_lstlsearch.c +++ b/src/lst/ft_lstlsearch.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/17 02:57:12 by cacharle #+# #+# */ -/* Updated: 2020/02/17 03:35:45 by cacharle ### ########.fr */ +/* Created: 2020/02/27 16:18:33 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:31 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftcompar_func cmp, const void *ref) { if (lst == NULL) return (ft_lstnew(ref)); - if (cmp(ref, lst->content) == 0) + if (cmp(ref, lst->data) == 0) return (lst); if (lst->next == NULL) { diff --git a/src/lst/ft_lstmap.c b/src/lst/ft_lstmap.c index dda15de..3182bb0 100644 --- a/src/lst/ft_lstmap.c +++ b/src/lst/ft_lstmap.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstmap_bonus.c :+: :+: :+: */ +/* ft_lstmap.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,9 +10,16 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Clone a list and map a function to each node data +** \param lst Origin list +** \param f Function applied to each node's data +** \param del Delete function for cleanning up in case of failed allocation +** \return Mapped clone list +*/ + t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) { t_ftlst *mapped; @@ -23,12 +30,12 @@ t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) mapped = NULL; while (lst != NULL) { - if ((tmp = ft_lstnew((*f)(lst->content))) == NULL) + if ((tmp = ft_lstnew((*f)(lst->data))) == NULL) { - ft_lstclear(&mapped, del); + ft_lstdestroy(&mapped, del); return (NULL); } - ft_lstadd_back(&mapped, tmp); + ft_lstpush_back(&mapped, tmp); lst = lst->next; } return (mapped); @@ -41,9 +48,9 @@ t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) ** ** if (lst == NULL) ** return (NULL); -** if ((tmp = ft_lstnew(lst->content)) == NULL) +** if ((tmp = ft_lstnew(lst->data)) == NULL) ** return (NULL); -** tmp->content = (*f)(tmp->content); +** tmp->data = (*f)(tmp->data); ** tmp->next = ft_lstmap(lst->next, f); ** return (tmp); */ diff --git a/src/lst/ft_lstnew.c b/src/lst/ft_lstnew.c index 11cf223..1616b71 100644 --- a/src/lst/ft_lstnew.c +++ b/src/lst/ft_lstnew.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstnew_bonus.c :+: :+: :+: */ +/* ft_lstnew.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,16 +10,20 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -t_ftlst *ft_lstnew(void const *content) +/* +** \brief Create a list node +** \param data Pointer to data of node +*/ + +t_ftlst *ft_lstnew(void const *data) { t_ftlst *elem; if ((elem = (t_ftlst*)malloc(sizeof(t_ftlst))) == NULL) return (NULL); - elem->content = (void*)content; + elem->data = (void*)data; elem->next = NULL; return (elem); } diff --git a/src/lst/ft_lstpop_front.c b/src/lst/ft_lstpop_front.c index ff386f9..a61350a 100644 --- a/src/lst/ft_lstpop_front.c +++ b/src/lst/ft_lstpop_front.c @@ -5,14 +5,18 @@ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/10/25 03:32:51 by cacharle #+# #+# */ -/* Updated: 2020/01/15 12:46:28 by cacharle ### ########.fr */ +/* Created: 2020/01/30 08:29:58 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:47 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Delete head node and replace it with next node +** \param del Delete function for node data +*/ + void ft_lstpop_front(t_ftlst **lst, void (*del)(void *)) { t_ftlst *tmp; diff --git a/src/lst/ft_lstadd_back.c b/src/lst/ft_lstpush_back.c index 8f39a75..1dca078 100644 --- a/src/lst/ft_lstadd_back.c +++ b/src/lst/ft_lstpush_back.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstadd_back_bonus.c :+: :+: :+: */ +/* ft_lstpush_back.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,10 +10,14 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -void ft_lstadd_back(t_ftlst **alst, t_ftlst *new) +/* +** \brief Push new node to the list end +** \param new Pushed node +*/ + +void ft_lstpush_back(t_ftlst **alst, t_ftlst *new) { if (alst == NULL) return ; diff --git a/src/lst/ft_lstadd_front.c b/src/lst/ft_lstpush_front.c index bcd5ad9..85df649 100644 --- a/src/lst/ft_lstadd_front.c +++ b/src/lst/ft_lstpush_front.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstadd_front_bonus.c :+: :+: :+: */ +/* ft_lstpush_front.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,10 +10,14 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" -void ft_lstadd_front(t_ftlst **alst, t_ftlst *new) +/* +** \brief Push node to list front +** \param new Pushed node +*/ + +void ft_lstpush_front(t_ftlst **alst, t_ftlst *new) { if (alst == NULL || new == NULL) return ; diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c index 5221ae4..4070355 100644 --- a/src/lst/ft_lstremove_if.c +++ b/src/lst/ft_lstremove_if.c @@ -6,13 +6,19 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ -/* Updated: 2020/02/19 02:06:22 by cacharle ### ########.fr */ +/* Updated: 2020/02/28 12:20:51 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Remove node on some condition +** \param cmp Comparison function, return 0 if equal +** \param ref Reference data passed has the first arg of `cmp` +** \param del Delete function to free removed node data +*/ + void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp, const void *ref, t_ftdel_func del) { @@ -20,7 +26,7 @@ void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp, if (lst == NULL || *lst == NULL) return ; - if (cmp(ref, (*lst)->content) == 0) + if (cmp(ref, (*lst)->data) == 0) { saved_next = (*lst)->next; ft_lstdelone(*lst, del); diff --git a/src/lst/ft_lstreverse.c b/src/lst/ft_lstreverse.c index 61c9daf..7c2778d 100644 --- a/src/lst/ft_lstreverse.c +++ b/src/lst/ft_lstreverse.c @@ -10,9 +10,12 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Reverse a list +*/ + void ft_lstreverse(t_ftlst **lst) { *lst = ft_lstreverse_ret(*lst); diff --git a/src/lst/ft_lstreverse_ret.c b/src/lst/ft_lstreverse_ret.c index c115ac5..36c0c5c 100644 --- a/src/lst/ft_lstreverse_ret.c +++ b/src/lst/ft_lstreverse_ret.c @@ -10,9 +10,13 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief Reverse list +** \return Pointer to reversed list +*/ + t_ftlst *ft_lstreverse_ret(t_ftlst *lst) { t_ftlst *tmp; diff --git a/src/lst/ft_lstsize.c b/src/lst/ft_lstsize.c index 922b581..3c6956b 100644 --- a/src/lst/ft_lstsize.c +++ b/src/lst/ft_lstsize.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_lstsize_bonus.c :+: :+: :+: */ +/* ft_lstsize.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,9 +10,13 @@ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_lst.h" +/* +** \brief List size +** \return Number of node in list +*/ + int ft_lstsize(t_ftlst *lst) { int counter; diff --git a/src/lst/ft_lstsort.c b/src/lst/ft_lstsort.c index e1c9913..9945a0f 100644 --- a/src/lst/ft_lstsort.c +++ b/src/lst/ft_lstsort.c @@ -12,6 +12,12 @@ #include "libft_lst.h" +/* +** \brief Sort list +** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater +** \note Use merge sort algorithm +*/ + void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp) { t_ftlst *fast; diff --git a/src/lst/ft_lstsorted_merge.c b/src/lst/ft_lstsorted_merge.c index 4f5332c..995785f 100644 --- a/src/lst/ft_lstsorted_merge.c +++ b/src/lst/ft_lstsorted_merge.c @@ -12,6 +12,14 @@ #include "libft_lst.h" +/* +** \brief Merge sorted lists, the new list is also sorted +** \param l1 First list +** \param l2 Second list +** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater +** \return Pointer to first node of merged list +*/ + t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp) { t_ftlst *merged; @@ -21,7 +29,7 @@ t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp) return (l2); if (l2 == NULL) return (l1); - if (cmp(l1->content, l2->content) < 0) + if (cmp(l1->data, l2->data) < 0) { merged = l1; merged->next = ft_lstsorted_merge(l1->next, l2, cmp); diff --git a/src/mem/ft_memccpy.c b/src/mem/ft_memccpy.c index 0f52242..2f6525a 100644 --- a/src/mem/ft_memccpy.c +++ b/src/mem/ft_memccpy.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:01:53 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:54:03 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:50:29 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,21 +40,21 @@ void *ft_memccpy(void *dest, const void *src, int c, size_t n) lw = *(uint64_t*)src ^ buf; if ((lw - LOMAGIC) & ~lw & HIMAGIC) { - if ( (((uint8_t*)dest)[0] = ((uint8_t*)src)[0]) == (uint8_t)c) + if ((((uint8_t*)dest)[0] = ((uint8_t*)src)[0]) == (uint8_t)c) return ((uint8_t*)dest + 1); - if ( (((uint8_t*)dest)[1] = ((uint8_t*)src)[1]) == (uint8_t)c) + if ((((uint8_t*)dest)[1] = ((uint8_t*)src)[1]) == (uint8_t)c) return ((uint8_t*)dest + 2); - if ( (((uint8_t*)dest)[2] = ((uint8_t*)src)[2]) == (uint8_t)c) + if ((((uint8_t*)dest)[2] = ((uint8_t*)src)[2]) == (uint8_t)c) return ((uint8_t*)dest + 3); - if ( (((uint8_t*)dest)[3] = ((uint8_t*)src)[3]) == (uint8_t)c) + if ((((uint8_t*)dest)[3] = ((uint8_t*)src)[3]) == (uint8_t)c) return ((uint8_t*)dest + 4); - if ( (((uint8_t*)dest)[4] = ((uint8_t*)src)[4]) == (uint8_t)c) + if ((((uint8_t*)dest)[4] = ((uint8_t*)src)[4]) == (uint8_t)c) return ((uint8_t*)dest + 5); - if ( (((uint8_t*)dest)[5] = ((uint8_t*)src)[5]) == (uint8_t)c) + if ((((uint8_t*)dest)[5] = ((uint8_t*)src)[5]) == (uint8_t)c) return ((uint8_t*)dest + 6); - if ( (((uint8_t*)dest)[6] = ((uint8_t*)src)[6]) == (uint8_t)c) + if ((((uint8_t*)dest)[6] = ((uint8_t*)src)[6]) == (uint8_t)c) return ((uint8_t*)dest + 7); - if ( (((uint8_t*)dest)[7] = ((uint8_t*)src)[7]) == (uint8_t)c) + if ((((uint8_t*)dest)[7] = ((uint8_t*)src)[7]) == (uint8_t)c) return ((uint8_t*)dest + 8); } else diff --git a/src/mem/ft_memchr.c b/src/mem/ft_memchr.c index 54780fe..e0266af 100644 --- a/src/mem/ft_memchr.c +++ b/src/mem/ft_memchr.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 09:55:31 by cacharle #+# #+# */ -/* Updated: 2020/02/13 04:28:00 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:50:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ void *ft_memchr(const void *s, int c, size_t n) { lw = *(uint64_t*)s ^ buf; if ((lw - LOMAGIC) & ~lw & HIMAGIC) - break; + break ; n -= 8; s += 8; } diff --git a/src/mem/ft_memset.c b/src/mem/ft_memset.c index ce471a5..9005947 100644 --- a/src/mem/ft_memset.c +++ b/src/mem/ft_memset.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:01:23 by cacharle #+# #+# */ -/* Updated: 2020/01/17 10:39:10 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:49:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ void *ft_memset(void *s, int c, size_t n) { uint64_t buf; - void *cpy; + void *cpy; cpy = s; c = (uint8_t)c; diff --git a/src/str/ft_atoi.c b/src/str/ft_atoi.c index fa31407..d6fa5bb 100644 --- a/src/str/ft_atoi.c +++ b/src/str/ft_atoi.c @@ -12,6 +12,10 @@ #include "libft.h" +/* +** Convert a string to an int +*/ + int ft_atoi(const char *str) { return ((int)ft_strtol(str, (char**)NULL, 10)); diff --git a/src/str/ft_strjoin3.c b/src/str/ft_strjoin3.c new file mode 100644 index 0000000..e5e5530 --- /dev/null +++ b/src/str/ft_strjoin3.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 18:00:49 by charles #+# #+# */ +/* Updated: 2020/04/01 18:01:43 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +/* +** \brief Join 3 strings in a new malloc'd one +** \param s1 String 1 +** \param s2 String 2 +** \param s3 String 3 +** \return The joined string +*/ + +char *ft_strjoin3(char const *s1, char const *s2, char const *s3) +{ + char *joined; + + if (s1 == NULL || s2 == NULL || s3 == NULL) + return (NULL); + if ((joined = (char*)malloc(sizeof(char) + * (ft_strlen(s1) + ft_strlen(s2) + ft_strlen(s3) + 1))) == NULL) + return (NULL); + ft_strcpy(joined, s1); + ft_strcat(joined, s2); + ft_strcat(joined, s3); + return (joined); +} diff --git a/src/str/ft_strlen.c b/src/str/ft_strlen.c index 72405c4..999763e 100644 --- a/src/str/ft_strlen.c +++ b/src/str/ft_strlen.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ -/* Updated: 2020/01/17 11:13:43 by cacharle ### ########.fr */ +/* Updated: 2020/04/01 21:45:38 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,18 +22,22 @@ ** ** 0x7F -> 0b 0111 1111 ** -** null_high = lw & 0x7F7F7F7F // will set the high bit of each byte to 0 -** overflow = null_high + 0x7F7F7F7F // addition will overflow the high bit is one of the other bits was 1. -** -** oring = overflow | lw // the high bit of a byte is set iff any bit in the byte was set -** ones = oring | 0x7F7F7F7F // the high bits and ones everywhere else -** has_no_zero_byte = ~ones // the ones become zeros, if no high bit was set, there was no zero +** null_high = lw & 0x7F7F7F7F // will set the high bit of each byte to 0 +** overflow = null_high + 0x7F7F7F7F // addition will overflow the high bit +** is one of the other bits was 1. ** +** oring = overflow | lw // the high bit of a byte is set +** iff any bit in the byte was set +** ones = oring | 0x7F7F7F7F // the high bits and ones everywhere else +** has_no_zero_byte = ~ones // the ones become zeros, if no high bit +** was set, there was no zero ** ** (lw - 0x01010101) & ~lw & 0x80808080 ** -** overflow = lw - 0x01010101 // overflow the high bit if one was 0 or > 0x80 (0b 1000 0000) 0 || >0x80 -** no_high_bit = ~lw & 0x80808080 // high bit set if the high bit was 0 (i.e < 0x80) 0 || <0x80 +** overflow = lw - 0x01010101 // overflow the high bit if one was +** 0 or > 0x80 (0b 1000 0000) 0 || >0x80 +** no_high_bit = ~lw & 0x80808080 // high bit set if the high bit was +** 0 (i.e < 0x80) 0 || <0x80 ** has_zero = overflow & no_high_bit // (0 || >0x80) && (0 || <0x80) -> 0 && 0 ** ** @@ -52,38 +56,18 @@ size_t ft_strlen(const char *s) { uint64_t *ptr; const char *cpy; - uint64_t lw; + const char *found; cpy = s; while (((uint64_t)cpy & 0b111) != 0) - { - if (*cpy == 0) - return (cpy - s); - cpy++; - } + if (*cpy++ == '\0') + return (cpy - s - 1); ptr = (uint64_t*)cpy; while (TRUE) - { - lw = *ptr++; - if (((lw - LOMAGIC) & HIMAGIC) != 0) + if (((*ptr++ - LOMAGIC) & HIMAGIC) != 0) { cpy = (const char*)(ptr - 1); - if (cpy[0] == '\0') - return (cpy - s); - if (cpy[1] == '\0') - return (cpy - s + 1); - if (cpy[2] == '\0') - return (cpy - s + 2); - if (cpy[3] == '\0') - return (cpy - s + 3); - if (cpy[4] == '\0') - return (cpy - s + 4); - if (cpy[5] == '\0') - return (cpy - s + 5); - if (cpy[6] == '\0') - return (cpy - s + 6); - if (cpy[7] == '\0') - return (cpy - s + 7); + if ((found = ft_memchr(cpy, '\0', sizeof(uint64_t))) != NULL) + return (found - s); } - } } diff --git a/src/util/ft_split_destroy.c b/src/util/ft_split_destroy.c new file mode 100644 index 0000000..bc3f4e6 --- /dev/null +++ b/src/util/ft_split_destroy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:30:55 by cacharle #+# #+# */ +/* Updated: 2020/02/27 17:52:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_util.h" + +void *ft_split_destroy(char **strs) +{ + int i; + + i = -1; + while (strs[++i] != NULL) + free(strs[i]); + free(strs); + return (NULL); +} diff --git a/src/vec/ft_vecdestroy.c b/src/vec/ft_vecdestroy.c new file mode 100644 index 0000000..781e02e --- /dev/null +++ b/src/vec/ft_vecdestroy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:06:22 by charles #+# #+# */ +/* Updated: 2020/04/01 19:09:09 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Destroy a vector +** \param vec Vector to destroy +** \param del Delete function applied to each element of the vector +*/ + +void ft_vecdestroy(t_ftvec *vec, void (*del)(void *elem)) +{ + if (vec == NULL) + return ; + if (del != NULL) + ft_veciter(vec, del); + free(vec->data); + free(vec); +} diff --git a/src/vec/ft_vecgrow.c b/src/vec/ft_vecgrow.c new file mode 100644 index 0000000..bb8b8c7 --- /dev/null +++ b/src/vec/ft_vecgrow.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecgrow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:13:07 by charles #+# #+# */ +/* Updated: 2020/04/01 21:15:20 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Vector Growth factor +*/ + +#define FT_VEC_GROWTH_FACTOR 1.5 + +/* +** \brief Grow the vector capacity by a constant factor +** \param vec Vector to grow +** \return Passed vector of NULL on error +*/ + +t_ftvec *ft_vecgrow(t_ftvec *vec) +{ + size_t new_capacity; + void **new_data; + + if (vec->capacity <= 1) + new_capacity = 2; + else + new_capacity = vec->capacity * FT_VEC_GROWTH_FACTOR; + if ((new_data = (void**)malloc(sizeof(void*) * new_capacity)) == NULL) + return (NULL); + ft_memcpy(new_data, vec->data, vec->size); + free(vec->data); + vec->data = new_data; + vec->capacity = new_capacity; + return (vec); +} diff --git a/src/vec/ft_veciter.c b/src/vec/ft_veciter.c new file mode 100644 index 0000000..ec4a917 --- /dev/null +++ b/src/vec/ft_veciter.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_veciter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:09:51 by charles #+# #+# */ +/* Updated: 2020/04/01 20:15:13 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Iterate a function over elements of a vector +** \param vec Iterated vector +** \param f Function applied to each elements +*/ + +void ft_veciter(t_ftvec *vec, void (*f)(void *elem)) +{ + size_t i; + + i = 0; + while (i < vec->size) + { + f(vec->data[i]); + i++; + } +} diff --git a/src/vec/ft_vecnew.c b/src/vec/ft_vecnew.c new file mode 100644 index 0000000..8a8736a --- /dev/null +++ b/src/vec/ft_vecnew.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:03:49 by charles #+# #+# */ +/* Updated: 2020/04/01 20:00:00 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Create a new vector +** \param capacity Initial capacity of the underlying array +** Can't be lower than 1 +** \return Created vector or NULL on malloc error +*/ + +t_ftvec *ft_vecnew(size_t capacity) +{ + t_ftvec *vec; + + if ((vec = (t_ftvec*)malloc(sizeof(t_ftvec))) == NULL) + return (NULL); + if (capacity == 0) + capacity = 1; + if ((vec->data = (void**)malloc(sizeof(void*) * capacity)) == NULL) + { + free(vec); + return (NULL); + } + vec->capacity = capacity; + vec->size = 0; + return (vec); +} diff --git a/src/vec/ft_vecpop.c b/src/vec/ft_vecpop.c new file mode 100644 index 0000000..5b77b46 --- /dev/null +++ b/src/vec/ft_vecpop.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpop.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:26:27 by charles #+# #+# */ +/* Updated: 2020/04/01 20:26:34 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Pop last element of a vector +** \param vec Vector poped +** \param del Delete function applied to last element +*/ + +void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)) +{ + if (vec->size == 0) + return ; + if (del != NULL) + del(vec->data[vec->size - 1]); + vec->size--; +} diff --git a/src/vec/ft_vecpush.c b/src/vec/ft_vecpush.c new file mode 100644 index 0000000..fc903ef --- /dev/null +++ b/src/vec/ft_vecpush.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecpush.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/01 19:22:20 by charles #+# #+# */ +/* Updated: 2020/04/01 20:20:06 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Push element at the end of vector +** \param vec Vector where element will be pushed +** \param pushed Element to push +** \return Passed vector or NULL if couldnt grow vector +*/ + +t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed) +{ + if (vec->capacity <= vec->size) + if (ft_vecgrow(vec) == NULL) + return (NULL); + vec->data[vec->size] = pushed; + vec->size++; + return (vec); +} |
