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