7d233e7184ffab01921a02229f34f2e591ba8f36
[apps/madmutt.git] / lib / list.c
1 /*
2  * written for mutt-ng by:
3  * Rocco Rutte <pdmef@cs.tu-berlin.de>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #include <stddef.h>
11 #include <string.h>
12
13 #include "list.h"
14
15 #include "mem.h"
16
17 list2_t* list_new (void) {
18   return (safe_calloc (1, sizeof (list2_t)));
19 }
20
21 void list_del (list2_t** l, void (*edel) (void**)) {
22   size_t i = 0;
23   if (!l || !*l)
24     return;
25   if (*edel)
26     for (i = 0; i < (*l)->length; i++)
27       edel (&(*l)->data[i]);
28   FREE(&(*l)->data);
29   FREE(l);
30 }
31
32 void list_push_back (list2_t** l, void* p) {
33   if (!*l)
34     *l = list_new ();
35   safe_realloc (&(*l)->data, (++(*l)->length)*sizeof(void*));
36   (*l)->data[(*l)->length-1] = p;
37 }
38
39 void list_push_front (list2_t** l, void* p) {
40   if (!*l)
41     *l = list_new ();
42   safe_realloc (&(*l)->data, (++(*l)->length)*sizeof(void*));
43   if ((*l)->length > 1)
44     memmove (&(*l)->data[1], &(*l)->data[0], ((*l)->length-1)*sizeof(void*));
45   (*l)->data[0] = p;
46 }
47
48 void* list_pop_back (list2_t* l) {
49   void* p = NULL;
50   if (list_empty(l))
51     return (NULL);
52   p = l->data[l->length-1];
53   safe_realloc (&l->data, --(l->length)*sizeof(void*));
54   return (p);
55 }
56
57 void* list_pop_front (list2_t* l) {
58   void* p = NULL;
59   if (list_empty(l))
60     return (NULL);
61   p = l->data[0];
62   memmove (&l->data[0], &l->data[1], (--(l->length))*sizeof(void*));
63   safe_realloc (&l->data, l->length*sizeof(void*));
64   return (p);
65 }
66
67 void* list_pop_idx (list2_t* l, int c) {
68   void* p = NULL;
69   if (list_empty(l) || c < 0 || c >= l->length)
70     return (NULL);
71   if (c == l->length-1)
72     return (list_pop_back (l));
73   p = l->data[c];
74   memmove (&l->data[c], &l->data[c+1], (l->length-c)*sizeof(void*));
75   safe_realloc (&l->data, (--(l->length))*sizeof(void*));
76   return (p);
77 }
78
79 list2_t* list_cpy (list2_t* l) {
80   list2_t* ret = NULL;
81   if (list_empty(l))
82     return (NULL);
83   ret = list_new ();
84   ret->length = l->length;
85   ret->data = safe_malloc (l->length*sizeof(void*));
86   memcpy (ret->data, l->data, l->length*sizeof(void*));
87   return (ret);
88 }
89
90 list2_t* list_dup (list2_t* l, void* (*dup) (void*)) {
91   list2_t* ret = NULL;
92   int i = 0;
93   if (list_empty(l) || !*dup)
94     return (NULL);
95   ret = list_new ();
96   ret->length = l->length;
97   ret->data = safe_malloc (l->length*sizeof(void*));
98   for (i = 0; i < l->length; i++)
99     ret->data[i] = dup (l->data[i]);
100   return (ret);
101 }
102
103 int list_lookup (list2_t* l, int (*cmp) (const void*, const void*), const void* p) {
104   int i = 0;
105   if (list_empty(l) || !*cmp)
106     return (-1);
107   for (i = 0; i < l->length; i++)
108     if (cmp (l->data[i], p) == 0)
109       return (i);
110   return (-1);
111 }