79 lines
2.3 KiB
C
79 lines
2.3 KiB
C
/*
|
|
Copyright (C) 2012, Aaron Lindsay <aaron@aclindsay.com>
|
|
|
|
This file is part of Aedrix.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
*/
|
|
|
|
#include <list.h>
|
|
|
|
void init_list(struct dlist_node *n) {
|
|
n->prev = n;
|
|
n->next = n;
|
|
}
|
|
|
|
int list_empty(struct dlist_node *n) {
|
|
return n->next == n;
|
|
}
|
|
|
|
void insert_after(struct dlist_node *n, struct dlist_node *to_add) {
|
|
to_add->next = n->next;
|
|
to_add->next->prev = to_add;
|
|
n->next = to_add;
|
|
to_add->prev = n;
|
|
}
|
|
|
|
void insert_before(struct dlist_node *n, struct dlist_node *to_add) {
|
|
to_add->prev = n->prev;
|
|
to_add->prev->next = to_add;
|
|
n->prev = to_add;
|
|
to_add->next = n;
|
|
}
|
|
|
|
void remove(struct dlist_node *to_remove) {
|
|
if (!list_empty(to_remove)) {
|
|
to_remove->next->prev = to_remove->prev;
|
|
to_remove->prev->next = to_remove->next;
|
|
init_list(to_remove);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Remove a series of nodes from a list. Note that this assumes the list is not
|
|
* empty, and that 'from' comes before 'to' in the list. 'from' and 'to' will
|
|
* both be removed from the list, along with any nodes which come after 'from'
|
|
* but before 'to'.
|
|
*/
|
|
void remove_splice(struct dlist_node *from, struct dlist_node *to) {
|
|
to->next->prev = from->prev;
|
|
from->prev->next = to->next;
|
|
|
|
//make the removed splice become its own wrap-around list, for easy access to the last member when re-splicing
|
|
to->next = from;
|
|
from->prev = to;
|
|
}
|
|
|
|
/*
|
|
* Add a splice of nodes to a list (most likely a series previously removed via
|
|
* remove_splice()).
|
|
*/
|
|
void insert_splice_before(struct dlist_node *n, struct dlist_node *first, struct dlist_node *last) {
|
|
first->prev = n->prev;
|
|
first->prev->next = first;
|
|
n->prev = last;
|
|
last->next = n;
|
|
}
|