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