Delete unused xcb_list functions and refactor others.
[free-sw/xcb/libxcb] / src / xcb_list.c
1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  * 
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  * 
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  * 
20  * Except as contained in this notice, the names of the authors or their
21  * institutions shall not be used in advertising or otherwise to promote the
22  * sale, use or other dealings in this Software without prior written
23  * authorization from the authors.
24  */
25
26 /* A generic implementation of a list of void-pointers. */
27
28 #include <stdlib.h>
29
30 #include "xcb.h"
31 #include "xcbint.h"
32
33 typedef struct node {
34     struct node *next;
35     void *data;
36 } node;
37
38 struct _xcb_list {
39     node *head;
40     node **tail;
41 };
42
43 /* Private interface */
44
45 _xcb_list *_xcb_list_new()
46 {
47     _xcb_list *list;
48     list = malloc(sizeof(_xcb_list));
49     if(!list)
50         return 0;
51     list->head = 0;
52     list->tail = &list->head;
53     return list;
54 }
55
56 void _xcb_list_delete(_xcb_list *list, XCBListFreeFunc do_free)
57 {
58     if(!list)
59         return;
60     while(list->head)
61     {
62         node *cur = list->head;
63         if(do_free)
64             do_free(cur->data);
65         list->head = cur->next;
66         free(cur);
67     }
68     free(list);
69 }
70
71 int _xcb_list_append(_xcb_list *list, void *data)
72 {
73     node *cur;
74     cur = malloc(sizeof(node));
75     if(!cur)
76         return 0;
77     cur->data = data;
78     cur->next = 0;
79
80     *list->tail = cur;
81     list->tail = &cur->next;
82     return 1;
83 }
84
85 void *_xcb_list_peek_head(_xcb_list *list)
86 {
87     if(!list->head)
88         return 0;
89     return list->head->data;
90 }
91
92 void *_xcb_list_remove(_xcb_list *list, int (*cmp)(const void *, const void *), const void *data)
93 {
94     node **cur;
95     for(cur = &list->head; *cur; cur = &(*cur)->next)
96         if(cmp(data, (*cur)->data))
97         {
98             node *tmp = *cur;
99             void *ret = (*cur)->data;
100             *cur = (*cur)->next;
101             if(!*cur)
102                 list->tail = cur;
103
104             free(tmp);
105             return ret;
106         }
107     return 0;
108 }
109
110 void *_xcb_list_find(_xcb_list *list, int (*cmp)(const void *, const void *), const void *data)
111 {
112     node *cur;
113     for(cur = list->head; cur; cur = cur->next)
114         if(cmp(data, cur->data))
115             return cur->data;
116     return 0;
117 }
118
119 typedef struct {
120     unsigned int key;
121     void *value;
122 } map_pair;
123
124 _xcb_map *_xcb_map_new(void) __attribute__ ((alias ("_xcb_list_new")));
125
126 void _xcb_map_delete(_xcb_map *q, XCBListFreeFunc do_free)
127 {
128     if(!q)
129         return;
130     while(q->head)
131     {
132         node *cur = q->head;
133         map_pair *pair = cur->data;
134         if(do_free)
135             do_free(pair->value);
136         q->head = cur->next;
137         free(pair);
138         free(cur);
139     }
140     free(q);
141 }
142
143 int _xcb_map_put(_xcb_map *q, unsigned int key, void *data)
144 {
145     map_pair *cur = malloc(sizeof(map_pair));
146     if(!cur)
147         return 0;
148     cur->key = key;
149     cur->value = data;
150     if(!_xcb_list_append(q, cur))
151     {
152         free(cur);
153         return 0;
154     }
155     return 1;
156 }
157
158 static int match_map_pair(const void *key, const void *pair)
159 {
160     return ((map_pair *) pair)->key == *(unsigned int *) key;
161 }
162
163 void *_xcb_map_remove(_xcb_map *q, unsigned int key)
164 {
165     map_pair *cur = _xcb_list_remove(q, match_map_pair, &key);
166     void *ret;
167     if(!cur)
168         return 0;
169     ret = cur->value;
170     free(cur);
171     return ret;
172 }