FreeTDS API
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
dlist.tmpl.h
1 /* Dlist - dynamic list
2  * Copyright (C) 2016 Frediano Ziglio
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 #if !defined(DLIST_FUNC) || !defined(DLIST_TYPE) || !defined(DLIST_LIST_TYPE)
21 #error Required defines missing!
22 #endif
23 
24 typedef struct
25 {
26  DLIST_FIELDS(DLIST_TYPE);
28 
29 #define DLIST_FAKE(list) ((DLIST_TYPE *) (((char *) (list)) - TDS_OFFSET(DLIST_TYPE, next)))
30 
31 #if ENABLE_EXTRA_CHECKS
32 void DLIST_FUNC(check)(DLIST_LIST_TYPE *list);
33 #else
34 static inline void DLIST_FUNC(check)(DLIST_LIST_TYPE *list)
35 {
36 }
37 #endif
38 
39 static inline void DLIST_FUNC(init)(DLIST_LIST_TYPE *list)
40 {
41  list->next = list->prev = DLIST_FAKE(list);
42  DLIST_FUNC(check)(list);
43 }
44 
45 static inline DLIST_TYPE *DLIST_FUNC(first)(DLIST_LIST_TYPE *list)
46 {
47  return list->next == DLIST_FAKE(list) ? NULL : list->next;
48 }
49 
50 static inline DLIST_TYPE *DLIST_FUNC(last)(DLIST_LIST_TYPE *list)
51 {
52  return list->prev == DLIST_FAKE(list) ? NULL : list->prev;
53 }
54 
55 static inline DLIST_TYPE *DLIST_FUNC(next)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
56 {
57  return item->next == DLIST_FAKE(list) ? NULL : item->next;
58 }
59 
60 static inline DLIST_TYPE *DLIST_FUNC(prev)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
61 {
62  return item->prev == DLIST_FAKE(list) ? NULL : item->prev;
63 }
64 
65 static inline void DLIST_FUNC(prepend)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
66 {
67  DLIST_FUNC(check)(list);
68  assert(item->next == NULL && item->prev == NULL);
69  list->next->prev = item;
70  item->next = list->next;
71  item->prev = DLIST_FAKE(list);
72  list->next = item;
73  assert(item->next != NULL && item->prev != NULL);
74  DLIST_FUNC(check)(list);
75 }
76 
77 static inline void DLIST_FUNC(append)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
78 {
79  DLIST_FUNC(check)(list);
80  assert(item->next == NULL && item->prev == NULL);
81  list->prev->next = item;
82  item->prev = list->prev;
83  item->next = DLIST_FAKE(list);
84  list->prev = item;
85  assert(item->next != NULL && item->prev != NULL);
86  DLIST_FUNC(check)(list);
87 }
88 
89 static inline void DLIST_FUNC(remove)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
90 {
91  DLIST_TYPE *prev = item->prev, *next = item->next;
92  DLIST_FUNC(check)(list);
93  if (prev)
94  prev->next = next;
95  if (next)
96  next->prev = prev;
97  item->prev = NULL;
98  item->next = NULL;
99  DLIST_FUNC(check)(list);
100 }
101 
102 static inline bool DLIST_FUNC(in_list)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
103 {
104  DLIST_FUNC(check)(list);
105  return item->prev != NULL || item->next != NULL;
106 }
107 
108 #undef DLIST_FAKE
109 #undef DLIST_FUNC
110 #undef DLIST_TYPE
111 #undef DLIST_LIST_TYPE
112 
Definition: dlist.tmpl.h:24