1 
2 
3 /*
4 A* -------------------------------------------------------------------
5 B* This file contains source code for the PyMOL computer program
6 C* Copyright (c) Schrodinger, LLC.
7 D* -------------------------------------------------------------------
8 E* It is unlawful to modify or remove this copyright notice.
9 F* -------------------------------------------------------------------
10 G* Please see the accompanying LICENSE file for further information.
11 H* -------------------------------------------------------------------
12 I* Additional authors of this source file include:
13 -*
14 -*
15 -*
16 Z* -------------------------------------------------------------------
17 */
18 #ifndef _H_ListMacros
19 #define _H_ListMacros
20 
21 
22 /* simplest possible single-linked list */
23 
24 #include"MemoryDebug.h"
25 #include"Err.h"
26 
27 #define ListInit(List) List = NULL
28 
29 #define ListAppend(List,Elem,Link,ElemType) \
30 { \
31   ElemType *current = (List); \
32   ElemType *previous = NULL; \
33   while(current) \
34 	 { \
35 		previous = current; \
36 		current = current->Link; \
37 	 } \
38   if(previous) \
39 	 previous->Link = Elem; \
40   else \
41 	 (List) = Elem; \
42   (Elem)->Link = NULL; \
43 }
44 
45 #define ListFree(List,Link,ElemType) \
46 { \
47   ElemType *current = List; \
48   ElemType *previous = NULL; \
49   while(current) \
50 	 { \
51       if(previous) \
52 		  mfree(previous); \
53 		previous = current; \
54 		current = current->Link; \
55 	 } \
56   if(previous) \
57 	 mfree(previous); \
58   (List) = NULL; \
59 }
60 
61 #define ListDetach(List,Elem,Link,ElemType) \
62 { \
63   ElemType *current = List; \
64   ElemType *previous = NULL; \
65   while(current) \
66 	 { \
67 		if(current == (Elem)) \
68 		  break; \
69 		previous = current; \
70 		current = current->Link; \
71 	 } \
72   if(current) \
73 	 { \
74 		if(previous) \
75 		  previous->Link = current->Link; \
76         else \
77           (List) = current->Link; \
78 	  (Elem)->Link = NULL; \
79 	 } \
80 }
81 
82 #define ListDelete(List,Elem,Link,ElemType) \
83 { \
84    ElemType *copy = (Elem); \
85    ListDetach(List,copy,Link,ElemType); \
86    mfree(copy); \
87 }
88 
89 #define ListIterate(List,Counter,Link) \
90    ( (Counter) = ((List) ? (((Counter) ? (Counter)->Link : (List))) : NULL))
91 
92 
93 /* Elem handling routines */
94 
95 #define ListElemAlloc(G,Elem,ElemType)		\
96 {						\
97     if(!(Elem))					\
98       {							\
99 	(Elem) = pymol::malloc<ElemType>(1);		\
100 	ErrChkPtr(G,Elem);				\
101       }							\
102 }
103 
104 #define ListElemCalloc(G,Elem,ElemType) \
105 {						\
106   if(!(Elem))						  \
107     {							  \
108       (Elem) = pymol::calloc<ElemType>(1);		  \
109       ErrChkPtr(G,Elem);				  \
110     }							  \
111 }
112 
113 #define ListElemFree(Elem) { mfree(Elem); Elem = NULL; }
114 
115 #endif /* _H_ListMacros */
116