This is Unofficial EPICS BASE Doxygen Site
ellLib.c File Reference
#include <stdlib.h>
#include "epicsAssert.h"
#include "ellLib.h"
+ Include dependency graph for ellLib.c:

Go to the source code of this file.

Functions

void ellAdd (ELLLIST *pList, ELLNODE *pNode)
 Adds a node to the end of a list. More...
 
void ellConcat (ELLLIST *pDstList, ELLLIST *pAddList)
 Concatenates a list to the end of another list. The list to be added is left empty. Either list (or both) can be empty at the beginning of the operation. More...
 
void ellDelete (ELLLIST *pList, ELLNODE *pNode)
 Deletes a node from a list. More...
 
void ellExtract (ELLLIST *pSrcList, ELLNODE *pStartNode, ELLNODE *pEndNode, ELLLIST *pDstList)
 Extract a sublist from a list. More...
 
ELLNODEellGet (ELLLIST *pList)
 Deletes and returns the first node from a list. More...
 
ELLNODEellPop (ELLLIST *pList)
 Deletes and returns the last node from a list. More...
 
void ellInsert (ELLLIST *plist, ELLNODE *pPrev, ELLNODE *pNode)
 Inserts a node into a list immediately after a specific node. More...
 
ELLNODEellNth (ELLLIST *pList, int nodeNum)
 Find the Nth node in a list. More...
 
ELLNODEellNStep (ELLNODE *pNode, int nStep)
 Find the list node nStep steps away from a specified node. More...
 
int ellFind (ELLLIST *pList, ELLNODE *pNode)
 Find the index of a specific node in a list. More...
 
void ellFree2 (ELLLIST *pList, FREEFUNC freeFunc)
 Free all the nodes in a list. More...
 
void ellVerify (ELLLIST *pList)
 Verifies that the list is consistent. More...
 

Function Documentation

void ellAdd ( ELLLIST pList,
ELLNODE pNode 
)

Adds a node to the end of a list.

Parameters
pListPointer to list descriptor
pNodePointer to node to be added

Definition at line 24 of file ellLib.c.

25 {
26  pNode->next = NULL;
27  pNode->previous = pList->node.previous;
28 
29  if (pList->count)
30  pList->node.previous->next = pNode;
31  else
32  pList->node.next = pNode;
33 
34  pList->node.previous = pNode;
35  pList->count++;
36 
37  return;
38 }
#define NULL
Definition: catime.c:38
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
void ellConcat ( ELLLIST pDstList,
ELLLIST pAddList 
)

Concatenates a list to the end of another list. The list to be added is left empty. Either list (or both) can be empty at the beginning of the operation.

Parameters
pDstListDestination list
pAddListList to be added to pDstList

Definition at line 46 of file ellLib.c.

47 {
48  if (pAddList->count == 0)
49  return; /* Add list is empty, nothing to add. */
50 
51  if (pDstList->count == 0) {
52  /* Destination list is empty... just transfer the add list over. */
53  pDstList->node.next = pAddList->node.next;
54  pDstList->node.previous = pAddList->node.previous;
55  pDstList->count = pAddList->count;
56  } else {
57  /* Destination list not empty... append the add list. */
58  pDstList->node.previous->next = pAddList->node.next;
59  pAddList->node.next->previous = pDstList->node.previous;
60  pDstList->node.previous = pAddList->node.previous;
61  pDstList->count += pAddList->count;
62  }
63 
64  pAddList->count = 0;
65  pAddList->node.next = NULL;
66  pAddList->node.previous = NULL;
67 
68  return;
69 }
#define NULL
Definition: catime.c:38
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
void ellDelete ( ELLLIST pList,
ELLNODE pNode 
)

Deletes a node from a list.

Parameters
pListPointer to list descriptor
pNodePointer to node to be deleted

Definition at line 75 of file ellLib.c.

76 {
77  if (pList->node.previous == pNode)
78  pList->node.previous = pNode->previous;
79  else
80  pNode->next->previous = pNode->previous;
81 
82  if (pList->node.next == pNode)
83  pList->node.next = pNode->next;
84  else
85  pNode->previous->next = pNode->next;
86 
87  pList->count--;
88 
89  return;
90 }
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
void ellExtract ( ELLLIST pSrcList,
ELLNODE pStartNode,
ELLNODE pEndNode,
ELLLIST pDstList 
)

Extract a sublist from a list.

Parameters
pSrcListPointer to source list
pStartNodeFirst node in pSrcList to be extracted
pEndNodeLast node in pSrcList to be extracted
pDstListPointer to list where to put extracted list

Definition at line 101 of file ellLib.c.

102 {
103  ELLNODE *pnode;
104  int count;
105 
106  /* Cut the list out of the source list (update count later) */
107  if (pStartNode->previous != NULL)
108  pStartNode->previous->next = pEndNode->next;
109  else
110  pSrcList->node.next = pEndNode->next;
111 
112  if (pEndNode->next != NULL) {
113  pEndNode->next->previous = pStartNode->previous;
114  pEndNode->next = NULL;
115  }
116  else
117  pSrcList->node.previous = pStartNode->previous;
118 
119  /* Place the sublist into the destination list */
120  pStartNode->previous = pDstList->node.previous;
121  if (pDstList->count)
122  pDstList->node.previous->next = pStartNode;
123  else
124  pDstList->node.next = pStartNode;
125 
126  pDstList->node.previous = pEndNode;
127 
128  /* Adjust the counts */
129  pnode = pStartNode;
130  count = 1;
131  while(pnode != pEndNode)
132  {
133  pnode = pnode->next;
134  count++;
135  }
136  pSrcList->count -= count;
137  pDstList->count += count;
138 
139  return;
140 }
#define NULL
Definition: catime.c:38
List node type.
Definition: ellLib.h:45
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
int ellFind ( ELLLIST pList,
ELLNODE pNode 
)

Find the index of a specific node in a list.

Parameters
pListPointer to list to search
pNodePointer to node to search for
Returns
The node's index, or -1 if it cannot be found on the list.
Note
The first node has index 1.

Definition at line 258 of file ellLib.c.

259 {
260  ELLNODE *got = pList->node.next;
261  int count = 1;
262 
263  while ((got != pNode) && (got != NULL)) {
264  got = got->next;
265  count++;
266  }
267  if (got == NULL)
268  return -1;
269 
270  return count;
271 }
#define NULL
Definition: catime.c:38
List node type.
Definition: ellLib.h:45
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
void ellFree2 ( ELLLIST pList,
FREEFUNC  freeFunc 
)

Free all the nodes in a list.

This routine empties a list, calling freeFunc() for every node on it.

Parameters
pListList from which to free all nodes
freeFuncThe free() routine to be called
Note
The nodes in the list are free()'d on the assumption that the node structures were malloc()'d one-at-a-time and that the ELLNODE structure is the first member of the parent structure.

Definition at line 282 of file ellLib.c.

283 {
284  ELLNODE *nnode = pList->node.next;
285  ELLNODE *pnode;
286 
287  while (nnode != NULL)
288  {
289  pnode = nnode;
290  nnode = nnode->next;
291  freeFunc(pnode);
292  }
293  pList->node.next = NULL;
294  pList->node.previous = NULL;
295  pList->count = 0;
296 }
#define NULL
Definition: catime.c:38
List node type.
Definition: ellLib.h:45
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
ELLNODE* ellGet ( ELLLIST pList)

Deletes and returns the first node from a list.

Parameters
pListPointer to list from which to get node
Returns
Pointer to the first node from the list, or NULL if the list is empty

Definition at line 147 of file ellLib.c.

148 {
149  ELLNODE *pnode = pList->node.next;
150 
151  if (pnode != NULL)
152  ellDelete(pList, pnode);
153 
154  return pnode;
155 }
#define NULL
Definition: catime.c:38
List node type.
Definition: ellLib.h:45
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
void ellDelete(ELLLIST *pList, ELLNODE *pNode)
Deletes a node from a list.
Definition: ellLib.c:75
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
void ellInsert ( ELLLIST plist,
ELLNODE pPrev,
ELLNODE pNode 
)

Inserts a node into a list immediately after a specific node.

Parameters
plistPointer to list into which to insert node
pPrevPointer to the node after which to insert
pNodePointer to the node to be inserted
Note
If pPrev is NULL pNode will be inserted at the head of the list

Definition at line 178 of file ellLib.c.

179 {
180  if (pPrev != NULL) {
181  pNode->previous = pPrev;
182  pNode->next = pPrev->next;
183  pPrev->next = pNode;
184  } else {
185  pNode->previous = NULL;
186  pNode->next = plist->node.next;
187  plist->node.next = pNode;
188  }
189 
190  if (pNode->next == NULL)
191  plist->node.previous = pNode;
192  else
193  pNode->next->previous = pNode;
194 
195  plist->count++;
196 
197  return;
198 }
#define NULL
Definition: catime.c:38
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
ELLNODE* ellNStep ( ELLNODE pNode,
int  nStep 
)

Find the list node nStep steps away from a specified node.

Parameters
pNodeThe known node
nStepHow many steps to take, may be negative to step backwards
Returns
Pointer to the node nStep nodes from pNode, or NULL if there is no such node in the list.

Definition at line 237 of file ellLib.c.

238 {
239  if (nStep > 0) {
240  while ((pNode != NULL) && nStep) {
241  pNode = pNode->next;
242  nStep--;
243  }
244  } else {
245  while ((pNode != NULL) && nStep) {
246  pNode = pNode->previous;
247  nStep++;
248  }
249  }
250  return pNode;
251 }
#define NULL
Definition: catime.c:38
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
ELLNODE* ellNth ( ELLLIST pList,
int  nodeNum 
)

Find the Nth node in a list.

Parameters
pListPointer to list from which to find node
nodeNumIndex of the node to be found
Returns
Pointer to the element at index nodeNum in pList, or NULL if there is no such node in the list.
Note
The first node has index 1.

Definition at line 205 of file ellLib.c.

206 {
207  ELLNODE *pnode;
208 
209  if ((nodeNum < 1) || (pList->count == 0))
210  return NULL;
211 
212  if (nodeNum > pList->count/2) {
213  if (nodeNum > pList->count)
214  return NULL;
215 
216  pnode = pList->node.previous;
217  nodeNum = pList->count - nodeNum;
218  while(nodeNum) {
219  pnode = pnode->previous;
220  nodeNum--;
221  }
222  return pnode;
223  }
224 
225  pnode = pList->node.next;
226  while (--nodeNum > 0)
227  pnode = pnode->next;
228 
229  return pnode;
230 }
#define NULL
Definition: catime.c:38
List node type.
Definition: ellLib.h:45
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
struct ELLNODE * next
Pointer to next node in list.
Definition: ellLib.h:46
int count
Number of nodes on the list.
Definition: ellLib.h:58
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
ELLNODE* ellPop ( ELLLIST pList)

Deletes and returns the last node from a list.

Parameters
pListPointer to list from which to get node
Returns
Pointer to the last node from the list, or NULL if the list is empty

Definition at line 162 of file ellLib.c.

163 {
164  ELLNODE *pnode = pList->node.previous;
165 
166  if (pnode != NULL)
167  ellDelete(pList, pnode);
168 
169  return pnode;
170 }
#define NULL
Definition: catime.c:38
List node type.
Definition: ellLib.h:45
struct ELLNODE * previous
Pointer to previous node in list.
Definition: ellLib.h:47
void ellDelete(ELLLIST *pList, ELLNODE *pNode)
Deletes a node from a list.
Definition: ellLib.c:75
ELLNODE node
Pointers to the first and last nodes on list.
Definition: ellLib.h:57
void ellVerify ( ELLLIST pList)

Verifies that the list is consistent.

Parameters
pListList to be verified

Definition at line 304 of file ellLib.c.

305 {
306  ELLNODE *pNode;
307  ELLNODE *pNext;
308  int count = 0;
309 
310  assert (pList);
311 
312  pNode = ellFirst(pList);
313  if (pNode) {
314  assert (ellPrevious(pNode) == NULL);
315  while (1) {
316  count++;
317  pNext = ellNext(pNode);
318  if (pNext) {
319  assert (ellPrevious(pNext) == pNode);
320  } else {
321  break;
322  }
323  pNode = pNext;
324  }
325  assert (ellNext(pNode) == NULL);
326  }
327 
328  assert (pNode == ellLast(pList));
329  assert (count == ellCount(pList));
330 }
#define assert(exp)
Declare that a condition should be true.
Definition: epicsAssert.h:70
#define ellCount(PLIST)
Report the number of nodes in a list.
Definition: ellLib.h:84
#define NULL
Definition: catime.c:38
#define ellPrevious(PNODE)
Find the previous node in list.
Definition: ellLib.h:104
#define ellNext(PNODE)
Find the next node in list.
Definition: ellLib.h:99
#define ellLast(PLIST)
Find the last node in list.
Definition: ellLib.h:94
List node type.
Definition: ellLib.h:45
#define ellFirst(PLIST)
Find the first node in list.
Definition: ellLib.h:89