1 /*	$NetBSD: list.c,v 1.1.1.1 2008/12/22 00:18:36 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 #include <assert.h>
20 
21 /*
22  * Initialise a list before use.
23  * The list head's next and previous pointers point back to itself.
24  */
25 void dm_list_init(struct dm_list *head)
26 {
27 	head->n = head->p = head;
28 }
29 
30 /*
31  * Insert an element before 'head'.
32  * If 'head' is the list head, this adds an element to the end of the list.
33  */
34 void dm_list_add(struct dm_list *head, struct dm_list *elem)
35 {
36 	assert(head->n);
37 
38 	elem->n = head;
39 	elem->p = head->p;
40 
41 	head->p->n = elem;
42 	head->p = elem;
43 }
44 
45 /*
46  * Insert an element after 'head'.
47  * If 'head' is the list head, this adds an element to the front of the list.
48  */
49 void dm_list_add_h(struct dm_list *head, struct dm_list *elem)
50 {
51 	assert(head->n);
52 
53 	elem->n = head->n;
54 	elem->p = head;
55 
56 	head->n->p = elem;
57 	head->n = elem;
58 }
59 
60 /*
61  * Delete an element from its list.
62  * Note that this doesn't change the element itself - it may still be safe
63  * to follow its pointers.
64  */
65 void dm_list_del(struct dm_list *elem)
66 {
67 	elem->n->p = elem->p;
68 	elem->p->n = elem->n;
69 }
70 
71 /*
72  * Remove an element from existing list and insert before 'head'.
73  */
74 void dm_list_move(struct dm_list *head, struct dm_list *elem)
75 {
76         dm_list_del(elem);
77         dm_list_add(head, elem);
78 }
79 
80 /*
81  * Is the list empty?
82  */
83 int dm_list_empty(const struct dm_list *head)
84 {
85 	return head->n == head;
86 }
87 
88 /*
89  * Is this the first element of the list?
90  */
91 int dm_list_start(const struct dm_list *head, const struct dm_list *elem)
92 {
93 	return elem->p == head;
94 }
95 
96 /*
97  * Is this the last element of the list?
98  */
99 int dm_list_end(const struct dm_list *head, const struct dm_list *elem)
100 {
101 	return elem->n == head;
102 }
103 
104 /*
105  * Return first element of the list or NULL if empty
106  */
107 struct dm_list *dm_list_first(const struct dm_list *head)
108 {
109 	return (dm_list_empty(head) ? NULL : head->n);
110 }
111 
112 /*
113  * Return last element of the list or NULL if empty
114  */
115 struct dm_list *dm_list_last(const struct dm_list *head)
116 {
117 	return (dm_list_empty(head) ? NULL : head->p);
118 }
119 
120 /*
121  * Return the previous element of the list, or NULL if we've reached the start.
122  */
123 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem)
124 {
125 	return (dm_list_start(head, elem) ? NULL : elem->p);
126 }
127 
128 /*
129  * Return the next element of the list, or NULL if we've reached the end.
130  */
131 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem)
132 {
133 	return (dm_list_end(head, elem) ? NULL : elem->n);
134 }
135 
136 /*
137  * Return the number of elements in a list by walking it.
138  */
139 unsigned int dm_list_size(const struct dm_list *head)
140 {
141 	unsigned int s = 0;
142 	const struct dm_list *v;
143 
144 	dm_list_iterate(v, head)
145 	    s++;
146 
147 	return s;
148 }
149