]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/mdvi-lib/list.c
c434e2b1971059f6de583c56d92eaf40883f95c3
[evince.git] / backend / dvi / mdvi-lib / list.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "common.h"
20
21 void    listh_init(ListHead *head)
22 {
23         head->head = head->tail = NULL;
24         head->count = 0;
25 }
26
27 void    listh_prepend(ListHead *head, List *list)
28 {
29         list->prev = NULL;
30         list->next = head->head;
31         if(head->head)
32                 head->head->prev = list;
33         head->head = list;
34         if(!head->tail)
35                 head->tail = list;
36         head->count++;
37 }
38
39 void    listh_append(ListHead *head, List *list)
40 {
41         list->next = NULL;
42         list->prev = head->tail;
43         if(head->tail)
44                 head->tail->next = list;
45         else
46                 head->head = list;
47         head->tail = list;
48         head->count++;
49 }
50
51 void    listh_add_before(ListHead *head, List *at, List *list)
52 {
53         if(at == head->head || head->head == NULL)
54                 listh_prepend(head, list);
55         else {
56                 list->next = at;
57                 list->prev = at->prev;
58                 at->prev = list;
59                 head->count++;
60         }
61 }
62
63 void    listh_add_after(ListHead *head, List *at, List *list)
64 {
65         if(at == head->tail || !head->tail)
66                 listh_append(head, list);
67         else {
68                 list->prev = at;
69                 list->next = at->next;
70                 at->next = list;
71                 head->count++;
72         }
73 }
74
75 void    listh_remove(ListHead *head, List *list)
76 {
77         if(list == head->head) {
78                 head->head = list->next;
79                 if(head->head)
80                         head->head->prev = NULL;
81         } else if(list == head->tail) {
82                 head->tail = list->prev;
83                 if(head->tail)
84                         head->tail->next = NULL;
85         } else {
86                 list->next->prev = list->prev;
87                 list->prev->next = list->next;
88         }
89         if(--head->count == 0)
90                 head->head = head->tail = NULL;
91 }
92
93 void    listh_concat(ListHead *h1, ListHead *h2)
94 {
95         if(h2->head == NULL)
96                 ; /* do nothing */
97         else if(h1->tail == NULL)
98                 h1->head = h2->head;
99         else {
100                 h1->tail->next = h2->head;
101                 h2->head->prev = h1->tail;
102         }
103         h1->tail = h2->tail;
104         h1->count += h2->count;
105 }
106
107 void    listh_catcon(ListHead *h1, ListHead *h2)
108 {
109         if(h2->head == NULL)
110                 ; /* do nothing */
111         else if(h1->head == NULL)
112                 h1->tail = h2->tail;
113         else {
114                 h1->head->prev = h2->tail;
115                 h2->tail->next = h1->head;
116         }
117         h1->head = h2->head;
118         h1->count += h2->count;
119 }