cleaned up old CVS left overs
[xboard.git] / lists.c
1 /*
2  * lists.c -- Functions to implement a double linked list XBoard
3  *
4  * Copyright 1995,2009 Free Software Foundation, Inc.
5  *
6  * ------------------------------------------------------------------------
7  *
8  * GNU XBoard is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or (at
11  * your option) any later version.
12  *
13  * GNU XBoard is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see http://www.gnu.org/licenses/.  *
20  *
21  *------------------------------------------------------------------------
22  ** See the file ChangeLog for a revision history.  */
23
24 /*
25  * This file could well be a part of backend.c, but I prefer it this
26  * way.
27  */
28
29 #include "config.h"
30
31 #include <stdio.h>
32 #if STDC_HEADERS
33 # include <stdlib.h>
34 #endif /* not STDC_HEADERS */
35
36 #include "common.h"
37 #include "lists.h"
38
39
40
41 /* Check, if List l is empty; returns TRUE, if it is, FALSE
42  * otherwise.
43  */
44 int ListEmpty(l)
45     List *l;
46 {
47     return(l->head == (ListNode *) &l->tail);
48 }
49
50
51 /* Initialize a list. Must be executed before list is used.
52  */
53 void ListNew(l)
54     List *l;
55 {
56     l->head = (ListNode *) &l->tail;
57     l->tail = NULL;
58     l->tailPred = (ListNode *) l;
59 }
60
61
62 /* Remove node n from the list it is inside.
63  */
64 void ListRemove(n)
65     ListNode *n;
66 {
67     if (n->succ != NULL) {  /*  Be safe  */
68         n->pred->succ = n->succ;
69         n->succ->pred = n->pred;
70         n->succ = NULL;     /*  Mark node as no longer being member */
71         n->pred = NULL;     /*  of a list.                          */
72     }
73 }
74
75
76 /* Delete node n.
77  */
78 void ListNodeFree(n)
79     ListNode *n;
80 {
81     if (n) {
82         ListRemove(n);
83         free(n);
84     }
85 }
86
87
88 /* Create a list node with size s. Returns NULL, if out of memory.
89  */
90 ListNode *ListNodeCreate(s)
91     size_t s;
92 {
93     ListNode *n;
94
95     if ((n = (ListNode*) malloc(s))) {
96         n->succ = NULL; /*  Mark node as not being member of a list.    */
97         n->pred = NULL;
98     }
99     return(n);
100 }
101
102
103 /* Insert node n into the list of node m after m.
104  */
105 void ListInsert(m, n)
106     ListNode *m, *n;
107 {
108     n->succ = m->succ;
109     n->pred = m;
110     m->succ = n;
111     n->succ->pred = n;
112 }
113
114
115 /* Add node n to the head of list l.
116  */
117 void ListAddHead(l, n)
118     List *l;
119     ListNode *n;
120 {
121     ListInsert((ListNode *) &l->head, n);
122 }
123
124
125 /* Add node n to the tail of list l.
126  */
127 void ListAddTail(l, n)
128     List *l;
129     ListNode *n;
130 {
131     ListInsert((ListNode *) l->tailPred, n);
132 }
133
134
135 /* Return element with number n of list l. (NULL, if n doesn't exist.)
136  * Counting starts with 0.
137  */
138 ListNode *ListElem(l, n)
139     List *l;
140     int n;
141 {
142     ListNode *ln;
143
144     for (ln = l->head;  ln->succ;  ln = ln->succ) {
145         if (!n--) {
146             return (ln);
147         }
148     }
149
150     return(NULL);
151 }