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