1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
/*
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* Copyright (c) Schrodinger, LLC.
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information.
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-*
-*
-*
Z* -------------------------------------------------------------------
*/
#ifndef _H_ListMacros
#define _H_ListMacros
/* simplest possible single-linked list */
#include"MemoryDebug.h"
#include"Err.h"
#define ListInit(List) List = NULL
#define ListAppend(List,Elem,Link,ElemType) \
{ \
register ElemType *current = (List); \
register ElemType *previous = NULL; \
while(current) \
{ \
previous = current; \
current = current->Link; \
} \
if(previous) \
previous->Link = Elem; \
else \
(List) = Elem; \
(Elem)->Link = NULL; \
}
#define ListInsert(List,Elem,After,Link,ElemType) \
{ \
register ElemType *current = List; \
register ElemType *previous = NULL; \
while(current) \
{ \
if(previous == (After)) \
break; \
previous = current; \
current = current->Link; \
} \
if(previous) \
{ \
(Elem)->Link = current; \
previous->Link = Elem; \
} \
else \
{ \
(Elem)->Link = List; \
(List) = Elem; \
} \
}
#define ListFree(List,Link,ElemType) \
{ \
register ElemType *current = List; \
register ElemType *previous = NULL; \
while(current) \
{ \
if(previous) \
mfree(previous); \
previous = current; \
current = current->Link; \
} \
if(previous) \
mfree(previous); \
(List) = NULL; \
}
#define ListDetach(List,Elem,Link,ElemType) \
{ \
register ElemType *current = List; \
register ElemType *previous = NULL; \
while(current) \
{ \
if(current == (Elem)) \
break; \
previous = current; \
current = current->Link; \
} \
if(current) \
{ \
if(previous) \
previous->Link = current->Link; \
else \
(List) = current->Link; \
(Elem)->Link = NULL; \
} \
}
#define ListDelete(List,Elem,Link,ElemType) \
{ \
register ElemType *copy = (Elem); \
ListDetach(List,copy,Link,ElemType); \
mfree(copy); \
}
#define ListIterate(List,Counter,Link) \
( (Counter) = ((List) ? (((Counter) ? (Counter)->Link : (List))) : NULL))
/* Elem handling routines */
#define ListElemAlloc(G,Elem,ElemType) \
{ \
if(!(Elem)) \
{ \
(Elem) = (ElemType*)mmalloc(sizeof(ElemType)); \
ErrChkPtr(G,Elem); \
} \
}
#define ListElemCalloc(G,Elem,ElemType) \
{ \
if(!(Elem)) \
{ \
(Elem) = (ElemType*)mcalloc(sizeof(ElemType),1); \
ErrChkPtr(G,Elem); \
} \
}
#define ListElemInit(List,Link) (List)->Link = NULL
#define ListElemFree(Elem) { mfree(Elem); Elem = NULL; }
#define GetListLength(VAR, List,Link,ElemType) \
{ \
register ElemType *current = (List); \
int length = 0; \
while(current) \
{ \
current = current->Link; \
length++; \
} \
VAR = length; \
}
/* -- JV
* Doubly linked list macros
* -- JV
*/
/* Create the circular, doubly linked list w/a sentinel node */
#define DListInit(List, Pre, Post, ElemType) \
do { \
List = (ElemType*)malloc(sizeof(ElemType)); \
(List)->Pre = (List)->Post = List; \
} while(0)
/* DListInsert -- Insert Elem at head of list
* List -- any structure with previous and next pointers (a doubly linked list)
* Elem -- the Element to add
* Pre -- pointer to previous element in list
* Post -- pointer to next element in list
*/
#define DListInsert(List,Elem,Pre,Post) \
do {\
(Elem)->Post = List; \
(Elem)->Pre = (List)->Pre; \
(List)->Pre = (Elem); \
(Elem)->Pre->Post = (Elem); \
} while(0)
/* DListRemove -- remove Element from the list, do not delete it
* Elem -- the element to remove
* Pre -- the link to the previous element
* Post -- the link to the next element
*/
#define DListRemove(Elem,Pre,Post) \
do { \
if ((Elem)->Pre && (Elem)->Post) { \
(Elem)->Pre->Post = (Elem)->Post; \
(Elem)->Post->Pre = (Elem)->Pre; \
} \
(Elem)->Pre = (Elem)->Post = NULL; \
} while (0)
/* DListIterate -- Iterate Elem across all items in List using Post as the link to next */
#define DListIterate(List,Elem,Post) \
for((Elem) = (List)->Post; (Elem) != (List); (Elem) = (Elem)->Post)
/* Can similarly do reverse iteration w/Post=Pre */
/* For all these ElemAlloc macros, it calls if(!Elem)
* indicating that all blank incoming Elem's must be initialized
* to NULL. Just calling :ElemType* foo;" won't do.
*/
#define DListElemAlloc(G,Elem,ElemType) \
do { \
if(!(Elem)) \
{ \
(Elem) = (ElemType*)mmalloc(sizeof(ElemType)); \
ErrChkPtr(G,Elem); \
} \
} while (0)
#define DListElemCalloc(G,Elem,ElemType) \
do { \
if(!(Elem)) \
{ \
(Elem) = (ElemType*)mcalloc(sizeof(ElemType),1); \
ErrChkPtr(G,Elem); \
} \
} while (0)
#define DListElemInit(Elem,Pre,Post) (Elem)->Pre = (Elem)->Post = NULL
#define DListElemFree(Elem) { mfree(Elem); Elem = NULL; }
#endif /* _H_ListMacros */
|