COMBINATORIAL_BLAS  1.6
tommylist.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Andrea Mazzoleni. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
97 #ifndef __TOMMYLIST_H
98 #define __TOMMYLIST_H
99 
100 #include "tommytypes.h"
101 
102 /******************************************************************************/
103 /* list */
104 
109 
115 {
116  *list = 0;
117 }
118 
124 {
125  return *list;
126 }
127 
133 {
134  tommy_node* head = tommy_list_head(list);
135 
136  if (!head)
137  return 0;
138 
139  return head->prev;
140 }
141 
148 {
149  /* one element "circular" prev list */
150  node->prev = node;
151 
152  /* one element "0 terminated" next list */
153  node->next = 0;
154 
155  *list = node;
156 }
157 
165 {
166  tommy_node* head = tommy_list_head(list);
167 
168  /* insert in the "circular" prev list */
169  node->prev = head->prev;
170  head->prev = node;
171 
172  /* insert in the "0 terminated" next list */
173  node->next = head;
174 
175  *list = node;
176 }
177 
185 {
186  /* insert in the "circular" prev list */
187  node->prev = head->prev;
188  head->prev = node;
189 
190  /* insert in the "0 terminated" next list */
191  node->next = 0;
192  node->prev->next = node;
193 }
194 
201 {
202  tommy_node* head = tommy_list_head(list);
203 
204  if (head)
206  else
207  tommy_list_insert_first(list, node);
208 
209  node->data = data;
210 }
211 
218 {
219  tommy_node* head = tommy_list_head(list);
220 
221  if (head)
223  else
224  tommy_list_insert_first(list, node);
225 
226  node->data = data;
227 }
228 
235 {
236  tommy_node* head = tommy_list_head(list);
237 
238  /* remove from the "circular" prev list */
239  head->next->prev = head->prev;
240 
241  /* remove from the "0 terminated" next list */
242  *list = head->next; /* the new head, in case 0 */
243 
244  return head;
245 }
246 
256 {
257  tommy_node* head = tommy_list_head(list);
258 
259  /* remove from the "circular" prev list */
260  if (node->next)
261  node->next->prev = node->prev;
262  else
263  head->prev = node->prev; /* the last */
264 
265  /* remove from the "0 terminated" next list */
266  if (head == node)
267  *list = node->next; /* the new head, in case 0 */
268  else
269  node->prev->next = node->next;
270 
271  return node->data;
272 }
273 
281 void tommy_list_concat(tommy_list* first, tommy_list* second);
282 
291 
297 {
298  return tommy_list_head(list) == 0;
299 }
300 
330 {
331  tommy_node* node = tommy_list_head(list);
332 
333  while (node) {
334  void* data = node->data;
335  node = node->next;
336  func(data);
337  }
338 }
339 
344 {
345  tommy_node* node = tommy_list_head(list);
346 
347  while (node) {
348  void* data = node->data;
349  node = node->next;
350  func(arg, data);
351  }
352 }
353 
354 #endif
355 
tommy_inline void tommy_list_insert_tail_not_empty(tommy_node *head, tommy_node *node)
Definition: tommylist.h:184
tommy_inline void tommy_list_foreach_arg(tommy_list *list, tommy_foreach_arg_func *func, void *arg)
Definition: tommylist.h:343
tommy_inline void tommy_list_insert_head(tommy_list *list, tommy_node *node, void *data)
Definition: tommylist.h:200
tommy_node * tommy_list
Definition: tommylist.h:108
tommy_inline tommy_bool_t tommy_list_empty(tommy_list *list)
Definition: tommylist.h:296
struct tommy_node_struct * next
Definition: tommytypes.h:188
tommy_inline void tommy_list_insert_first(tommy_list *list, tommy_node *node)
Definition: tommylist.h:147
tommy_inline void tommy_list_foreach(tommy_list *list, tommy_foreach_func *func)
Definition: tommylist.h:329
struct tommy_node_struct * prev
Definition: tommytypes.h:194
tommy_inline tommy_node * tommy_list_tail(tommy_list *list)
Definition: tommylist.h:132
tommy_inline void * tommy_list_remove_existing(tommy_list *list, tommy_node *node)
Definition: tommylist.h:255
int tommy_compare_func(const void *obj_a, const void *obj_b)
Definition: tommytypes.h:240
void tommy_list_concat(tommy_list *first, tommy_list *second)
void tommy_list_sort(tommy_list *list, tommy_compare_func *cmp)
tommy_inline tommy_node * tommy_list_remove_head_not_empty(tommy_list *list)
Definition: tommylist.h:234
#define tommy_inline
Definition: tommytypes.h:115
tommy_inline void tommy_list_insert_head_not_empty(tommy_list *list, tommy_node *node)
Definition: tommylist.h:164
tommy_inline void tommy_list_init(tommy_list *list)
Definition: tommylist.h:114
void tommy_foreach_func(void *obj)
Definition: tommytypes.h:284
tommy_inline tommy_node * tommy_list_head(tommy_list *list)
Definition: tommylist.h:123
tommy_inline void tommy_list_insert_tail(tommy_list *list, tommy_node *node, void *data)
Definition: tommylist.h:217
int tommy_bool_t
Definition: tommytypes.h:52
void tommy_foreach_arg_func(void *arg, void *obj)
Definition: tommytypes.h:291