NCBI C++ ToolKit
dlist.tmpl.h
Go to the documentation of this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

Go to the SVN repository for this file.

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_NAME) || !defined(DLIST_TYPE) || !defined(DLIST_LIST_TYPE)
21 #error Required defines missing!
22 #endif
23 
24 typedef struct
25 {
28 
29 #undef DLIST_ITEM
30 #define DLIST_ITEM(ring) ((DLIST_TYPE *) (((char *) (ring)) - TDS_OFFSET(DLIST_TYPE, DLIST_NAME(item))))
31 
32 static inline void DLIST_NAME(check)(DLIST_LIST_TYPE *list)
33 {
34 #if ENABLE_EXTRA_CHECKS
35  assert(list != NULL);
36  dlist_ring_check(&list->ring);
37 #endif
38 }
39 
40 static inline void DLIST_NAME(init)(DLIST_LIST_TYPE *list)
41 {
42  list->ring.next = list->ring.prev = &list->ring;
43  DLIST_NAME(check)(list);
44 }
45 
46 static inline DLIST_TYPE *DLIST_NAME(first)(DLIST_LIST_TYPE *list)
47 {
48  return list->ring.next == &list->ring ? NULL : DLIST_ITEM(list->ring.next);
49 }
50 
51 static inline DLIST_TYPE *DLIST_NAME(last)(DLIST_LIST_TYPE *list)
52 {
53  return list->ring.prev == &list->ring ? NULL : DLIST_ITEM(list->ring.prev);
54 }
55 
56 static inline DLIST_TYPE *DLIST_NAME(next)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
57 {
58  return item->DLIST_NAME(item).next == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).next);
59 }
60 
61 static inline DLIST_TYPE *DLIST_NAME(prev)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
62 {
63  return item->DLIST_NAME(item).prev == &list->ring ? NULL : DLIST_ITEM(item->DLIST_NAME(item).prev);
64 }
65 
66 static inline void DLIST_NAME(prepend)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
67 {
68  DLIST_NAME(check)(list);
69  assert(item->DLIST_NAME(item).next == NULL && item->DLIST_NAME(item).prev == NULL);
70  list->ring.next->prev = &item->DLIST_NAME(item);
71  item->DLIST_NAME(item).next = list->ring.next;
72  item->DLIST_NAME(item).prev = &list->ring;
73  list->ring.next = &item->DLIST_NAME(item);
74  assert(item->DLIST_NAME(item).next != NULL && item->DLIST_NAME(item).prev != NULL);
75  DLIST_NAME(check)(list);
76 }
77 
78 static inline void DLIST_NAME(append)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
79 {
80  DLIST_NAME(check)(list);
81  assert(item->DLIST_NAME(item).next == NULL && item->DLIST_NAME(item).prev == NULL);
82  list->ring.prev->next = &item->DLIST_NAME(item);
83  item->DLIST_NAME(item).prev = list->ring.prev;
84  item->DLIST_NAME(item).next = &list->ring;
85  list->ring.prev = &item->DLIST_NAME(item);
86  assert(item->DLIST_NAME(item).next != NULL && item->DLIST_NAME(item).prev != NULL);
87  DLIST_NAME(check)(list);
88 }
89 
90 static inline void DLIST_NAME(remove)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
91 {
92  dlist_ring *prev = item->DLIST_NAME(item).prev, *next = item->DLIST_NAME(item).next;
93  DLIST_NAME(check)(list);
94  if (prev)
95  prev->next = next;
96  if (next)
97  next->prev = prev;
98  item->DLIST_NAME(item).prev = NULL;
99  item->DLIST_NAME(item).next = NULL;
100  DLIST_NAME(check)(list);
101 }
102 
103 static inline bool DLIST_NAME(in_list)(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
104 {
105  DLIST_NAME(check)(list);
106  return item->DLIST_NAME(item).prev != NULL || item->DLIST_NAME(item).next != NULL;
107 }
108 
109 #undef DLIST_ITEM
110 #undef DLIST_NAME
111 #undef DLIST_TYPE
112 #undef DLIST_LIST_TYPE
113 
static void DLIST_NAME() init(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:40
static void DLIST_NAME() prepend(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:66
static void DLIST_NAME() append(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:78
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:46
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:51
static void DLIST_NAME() check(DLIST_LIST_TYPE *list)
Definition: dlist.tmpl.h:32
static DLIST_TYPE *DLIST_NAME() prev(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:61
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:56
static bool DLIST_NAME() in_list(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:103
#define DLIST_ITEM(ring)
Definition: dlist.tmpl.h:30
static void DLIST_NAME() remove(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
Definition: dlist.tmpl.h:90
#define NULL
Definition: ncbistd.hpp:225
#define dlist_ring_check
#define assert(x)
Definition: srv_diag.hpp:58
dlist_ring ring
Definition: dlist.tmpl.h:26
Modified on Tue Apr 09 07:58:50 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 10 07:34:13 2024 by modify_doxy.py rev. 669887
Modified on Thu Apr 11 15:12:07 2024 by modify_doxy.py rev. 669887
Modified on Fri Apr 12 17:20:21 2024 by modify_doxy.py rev. 669887
Modified on Sat Apr 13 11:48:04 2024 by modify_doxy.py rev. 669887
Modified on Sun Apr 14 05:27:53 2024 by modify_doxy.py rev. 669887
Modified on Tue Apr 16 20:12:22 2024 by modify_doxy.py rev. 669887
Modified on Wed Apr 17 13:10:01 2024 by modify_doxy.py rev. 669887