1 /*
2 * Copyright (C) 2000, Matias Atria
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 #include <config.h>
20 #include "common.h"
21
listh_init(ListHead * head)22 void listh_init(ListHead *head)
23 {
24 head->head = head->tail = NULL;
25 head->count = 0;
26 }
27
listh_prepend(ListHead * head,List * list)28 void listh_prepend(ListHead *head, List *list)
29 {
30 list->prev = NULL;
31 list->next = head->head;
32 if(head->head)
33 head->head->prev = list;
34 head->head = list;
35 if(!head->tail)
36 head->tail = list;
37 head->count++;
38 }
39
listh_append(ListHead * head,List * list)40 void listh_append(ListHead *head, List *list)
41 {
42 list->next = NULL;
43 list->prev = head->tail;
44 if(head->tail)
45 head->tail->next = list;
46 else
47 head->head = list;
48 head->tail = list;
49 head->count++;
50 }
51
listh_add_before(ListHead * head,List * at,List * list)52 void listh_add_before(ListHead *head, List *at, List *list)
53 {
54 if(at == head->head || head->head == NULL)
55 listh_prepend(head, list);
56 else {
57 list->next = at;
58 list->prev = at->prev;
59 at->prev = list;
60 head->count++;
61 }
62 }
63
listh_add_after(ListHead * head,List * at,List * list)64 void listh_add_after(ListHead *head, List *at, List *list)
65 {
66 if(at == head->tail || !head->tail)
67 listh_append(head, list);
68 else {
69 list->prev = at;
70 list->next = at->next;
71 at->next = list;
72 head->count++;
73 }
74 }
75
listh_remove(ListHead * head,List * list)76 void listh_remove(ListHead *head, List *list)
77 {
78 if(list == head->head) {
79 head->head = list->next;
80 if(head->head)
81 head->head->prev = NULL;
82 } else if(list == head->tail) {
83 head->tail = list->prev;
84 if(head->tail)
85 head->tail->next = NULL;
86 } else {
87 list->next->prev = list->prev;
88 list->prev->next = list->next;
89 }
90 if(--head->count == 0)
91 head->head = head->tail = NULL;
92 }
93
listh_concat(ListHead * h1,ListHead * h2)94 void listh_concat(ListHead *h1, ListHead *h2)
95 {
96 if(h2->head == NULL)
97 ; /* do nothing */
98 else if(h1->tail == NULL)
99 h1->head = h2->head;
100 else {
101 h1->tail->next = h2->head;
102 h2->head->prev = h1->tail;
103 }
104 h1->tail = h2->tail;
105 h1->count += h2->count;
106 }
107
listh_catcon(ListHead * h1,ListHead * h2)108 void listh_catcon(ListHead *h1, ListHead *h2)
109 {
110 if(h2->head == NULL)
111 ; /* do nothing */
112 else if(h1->head == NULL)
113 h1->tail = h2->tail;
114 else {
115 h1->head->prev = h2->tail;
116 h2->tail->next = h1->head;
117 }
118 h1->head = h2->head;
119 h1->count += h2->count;
120 }
121