Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / lvm2 / dist / lib / datastruct / list.c
1 /*      $NetBSD: list.c,v 1.1.1.1 2008/12/22 00:17:54 haad Exp $        */
2
3 /*
4  * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5  * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
6  *
7  * This file is part of LVM2.
8  *
9  * This copyrighted material is made available to anyone wishing to use,
10  * modify, copy, or redistribute it subject to the terms and conditions
11  * of the GNU Lesser General Public License v.2.1.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #include "lib.h"
19
20 /*
21  * Initialise a list before use.
22  * The list head's next and previous pointers point back to itself.
23  */
24 void dm_list_init(struct dm_list *head)
25 {
26         head->n = head->p = head;
27 }
28
29 /*
30  * Insert an element before 'head'.
31  * If 'head' is the list head, this adds an element to the end of the list.
32  */
33 void dm_list_add(struct dm_list *head, struct dm_list *elem)
34 {
35         assert(head->n);
36
37         elem->n = head;
38         elem->p = head->p;
39
40         head->p->n = elem;
41         head->p = elem;
42 }
43
44 /*
45  * Insert an element after 'head'.
46  * If 'head' is the list head, this adds an element to the front of the list.
47  */
48 void dm_list_add_h(struct dm_list *head, struct dm_list *elem)
49 {
50         assert(head->n);
51
52         elem->n = head->n;
53         elem->p = head;
54
55         head->n->p = elem;
56         head->n = elem;
57 }
58
59 /*
60  * Delete an element from its list.
61  * Note that this doesn't change the element itself - it may still be safe
62  * to follow its pointers.
63  */
64 void dm_list_del(struct dm_list *elem)
65 {
66         elem->n->p = elem->p;
67         elem->p->n = elem->n;
68 }
69
70 /*
71  * Remove an element from existing list and insert before 'head'.
72  */
73 void dm_list_move(struct dm_list *head, struct dm_list *elem)
74 {
75         dm_list_del(elem);
76         dm_list_add(head, elem);
77 }
78
79 /*
80  * Is the list empty?
81  */
82 int dm_list_empty(const struct dm_list *head)
83 {
84         return head->n == head;
85 }
86
87 /*
88  * Is this the first element of the list?
89  */
90 int dm_list_start(const struct dm_list *head, const struct dm_list *elem)
91 {
92         return elem->p == head;
93 }
94
95 /*
96  * Is this the last element of the list?
97  */
98 int dm_list_end(const struct dm_list *head, const struct dm_list *elem)
99 {
100         return elem->n == head;
101 }
102
103 /*
104  * Return first element of the list or NULL if empty
105  */
106 struct dm_list *dm_list_first(const struct dm_list *head)
107 {
108         return (dm_list_empty(head) ? NULL : head->n);
109 }
110
111 /*
112  * Return last element of the list or NULL if empty
113  */
114 struct dm_list *dm_list_last(const struct dm_list *head)
115 {
116         return (dm_list_empty(head) ? NULL : head->p);
117 }
118
119 /*
120  * Return the previous element of the list, or NULL if we've reached the start.
121  */
122 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem)
123 {
124         return (dm_list_start(head, elem) ? NULL : elem->p);
125 }
126
127 /*
128  * Return the next element of the list, or NULL if we've reached the end.
129  */
130 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem)
131 {
132         return (dm_list_end(head, elem) ? NULL : elem->n);
133 }
134
135 /*
136  * Return the number of elements in a list by walking it.
137  */
138 unsigned int dm_list_size(const struct dm_list *head)
139 {
140         unsigned int s = 0;
141         const struct dm_list *v;
142
143         dm_list_iterate(v, head)
144             s++;
145
146         return s;
147 }