Adiciona implementaĆ§Ć£o de lista.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Sun, 26 Apr 2015 19:16:36 +0000 (19:16 +0000)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.eti.br>
Mon, 11 May 2015 00:41:05 +0000 (00:41 +0000)
list.c [new file with mode: 0644]
list.h [new file with mode: 0644]

diff --git a/list.c b/list.c
new file mode 100644 (file)
index 0000000..90068e7
--- /dev/null
+++ b/list.c
@@ -0,0 +1,83 @@
+/*
+ *  Copyright (C) 2012-2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "list.h"
+#include <stdlib.h>
+#include <string.h>
+
+struct item {
+       void *val;
+};
+
+struct list {
+       size_t alen;
+       size_t len;
+       struct item items[];
+};
+
+struct list * list_new(void)
+{
+       struct list *list;
+       size_t alen = 128;
+       list = malloc(sizeof(*list) + alen * sizeof(struct item));
+       if (!list)
+               return NULL;
+       list->alen = alen;
+       list->len = 0;
+       memset(list->items, 0, alen * sizeof(struct item));
+       return list;
+}
+
+int list_add(struct list **list, void *val)
+{
+       unsigned int i;
+       struct list *l = *list;
+       l->items[l->len].val = val;
+       l->len++;
+       if (l->len == l->alen) {
+               struct list *nlist;
+               size_t len = l->alen * sizeof(struct item);
+               size_t nlen = len * 2;
+               nlist = realloc(l, sizeof(*nlist) + nlen);
+               if (!nlist)
+                       goto out;
+               *list = l = nlist;
+               memset(&l->items[l->len], 0, len);
+               l->alen = l->alen * 2;
+       }
+       return 0;
+out:
+       l->items[l->len].val = NULL;
+       return -1;
+}
+
+void * list_get(struct list *list, int pos)
+{
+       unsigned int i;
+       if (pos >= list->len)
+               return NULL;
+       return list->items[pos].val;
+}
+
+void list_free(struct list *list, free_function_t *ifree)
+{
+       int i;
+       for (i = 0; i < list->len; i++)
+               ifree(list->items[i].val);
+       free(list);
+}
diff --git a/list.h b/list.h
new file mode 100644 (file)
index 0000000..43fc8e2
--- /dev/null
+++ b/list.h
@@ -0,0 +1,30 @@
+/*
+ *  Copyright (C) 2012-2015  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _LIST_H
+#define _LIST_H
+
+typedef void (free_function_t)(void *);
+struct list;
+
+struct list * list_new(void);
+int list_add(struct list **list, void *val);
+void * list_get(struct list *list, int pos);
+void list_free(struct list *list, free_function_t *ifree);
+
+#endif