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