Sync usage with man page.
[netbsd-mini2440.git] / external / gpl2 / lvm2 / dist / libdm / datastruct / list.c
blob8cbde5c81436eb37ab3c508fb7f7a0d1f85cffba
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved.
5 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
7 * This file is part of LVM2.
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.
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
18 #include "lib.h"
19 #include <assert.h>
22 * Initialise a list before use.
23 * The list head's next and previous pointers point back to itself.
25 void dm_list_init(struct dm_list *head)
27 head->n = head->p = head;
31 * Insert an element before 'head'.
32 * If 'head' is the list head, this adds an element to the end of the list.
34 void dm_list_add(struct dm_list *head, struct dm_list *elem)
36 assert(head->n);
38 elem->n = head;
39 elem->p = head->p;
41 head->p->n = elem;
42 head->p = elem;
46 * Insert an element after 'head'.
47 * If 'head' is the list head, this adds an element to the front of the list.
49 void dm_list_add_h(struct dm_list *head, struct dm_list *elem)
51 assert(head->n);
53 elem->n = head->n;
54 elem->p = head;
56 head->n->p = elem;
57 head->n = elem;
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.
65 void dm_list_del(struct dm_list *elem)
67 elem->n->p = elem->p;
68 elem->p->n = elem->n;
72 * Remove an element from existing list and insert before 'head'.
74 void dm_list_move(struct dm_list *head, struct dm_list *elem)
76 dm_list_del(elem);
77 dm_list_add(head, elem);
81 * Is the list empty?
83 int dm_list_empty(const struct dm_list *head)
85 return head->n == head;
89 * Is this the first element of the list?
91 int dm_list_start(const struct dm_list *head, const struct dm_list *elem)
93 return elem->p == head;
97 * Is this the last element of the list?
99 int dm_list_end(const struct dm_list *head, const struct dm_list *elem)
101 return elem->n == head;
105 * Return first element of the list or NULL if empty
107 struct dm_list *dm_list_first(const struct dm_list *head)
109 return (dm_list_empty(head) ? NULL : head->n);
113 * Return last element of the list or NULL if empty
115 struct dm_list *dm_list_last(const struct dm_list *head)
117 return (dm_list_empty(head) ? NULL : head->p);
121 * Return the previous element of the list, or NULL if we've reached the start.
123 struct dm_list *dm_list_prev(const struct dm_list *head, const struct dm_list *elem)
125 return (dm_list_start(head, elem) ? NULL : elem->p);
129 * Return the next element of the list, or NULL if we've reached the end.
131 struct dm_list *dm_list_next(const struct dm_list *head, const struct dm_list *elem)
133 return (dm_list_end(head, elem) ? NULL : elem->n);
137 * Return the number of elements in a list by walking it.
139 unsigned int dm_list_size(const struct dm_list *head)
141 unsigned int s = 0;
142 const struct dm_list *v;
144 dm_list_iterate(v, head)
145 s++;
147 return s;