1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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.
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.
26 /* A generic implementation of a list of void-pointers. */
43 /* Private interface */
45 _xcb_list *_xcb_list_new()
48 list = malloc(sizeof(_xcb_list));
52 list->tail = &list->head;
56 static void _xcb_list_clear(_xcb_list *list, XCBListFreeFunc do_free)
59 while((tmp = _xcb_list_remove_head(list)))
64 void _xcb_list_delete(_xcb_list *list, XCBListFreeFunc do_free)
68 _xcb_list_clear(list, do_free);
72 int _xcb_list_insert(_xcb_list *list, void *data)
75 cur = malloc(sizeof(node));
80 cur->next = list->head;
85 int _xcb_list_append(_xcb_list *list, void *data)
88 cur = malloc(sizeof(node));
95 list->tail = &cur->next;
99 void *_xcb_list_peek_head(_xcb_list *list)
103 return list->head->data;
106 void *_xcb_list_remove_head(_xcb_list *list)
109 node *tmp = list->head;
113 list->head = tmp->next;
115 list->tail = &list->head;
120 void *_xcb_list_remove(_xcb_list *list, int (*cmp)(const void *, const void *), const void *data)
123 for(cur = &list->head; *cur; cur = &(*cur)->next)
124 if(cmp(data, (*cur)->data))
127 void *ret = (*cur)->data;
138 void *_xcb_list_find(_xcb_list *list, int (*cmp)(const void *, const void *), const void *data)
141 for(cur = list->head; cur; cur = cur->next)
142 if(cmp(data, cur->data))
152 _xcb_map *_xcb_map_new(void) __attribute__ ((alias ("_xcb_list_new")));
154 void _xcb_map_delete(_xcb_map *q, XCBListFreeFunc do_free)
159 while((tmp = _xcb_list_remove_head(q)))
168 int _xcb_map_put(_xcb_map *q, unsigned int key, void *data)
170 map_pair *cur = malloc(sizeof(map_pair));
175 if(!_xcb_list_append(q, cur))
183 static int match_map_pair(const void *key, const void *pair)
185 return ((map_pair *) pair)->key == *(unsigned int *) key;
188 void *_xcb_map_get(_xcb_map *q, unsigned int key)
190 map_pair *cur = _xcb_list_find(q, match_map_pair, &key);
196 void *_xcb_map_remove(_xcb_map *q, unsigned int key)
198 map_pair *cur = _xcb_list_remove(q, match_map_pair, &key);