| 1 |
#include "xdvi-config.h" |
| 2 |
#include "dl_list.h" |
| 3 |
|
| 4 |
size_t dl_list_len(struct dl_list *list) |
| 5 |
{ |
| 6 |
size_t len = 0; |
| 7 |
struct dl_list *ptr; |
| 8 |
for (ptr = list; ptr != NULL; ptr = ptr->next, len++) { ; } |
| 9 |
return len; |
| 10 |
} |
| 11 |
|
| 12 |
/* |
| 13 |
Insert item to the list and return the result. |
| 14 |
*/ |
| 15 |
struct dl_list * |
| 16 |
dl_list_insert(struct dl_list *list, void *item) |
| 17 |
{ |
| 18 |
struct dl_list *new_elem = xmalloc(sizeof *new_elem); |
| 19 |
new_elem->item = item; |
| 20 |
new_elem->next = NULL; |
| 21 |
new_elem->prev = NULL; |
| 22 |
|
| 23 |
if (list == NULL) { |
| 24 |
list = new_elem; |
| 25 |
} |
| 26 |
else { |
| 27 |
/* append after current position */ |
| 28 |
struct dl_list *ptr = list; |
| 29 |
|
| 30 |
new_elem->next = ptr->next; |
| 31 |
new_elem->prev = ptr; |
| 32 |
|
| 33 |
if (ptr->next != NULL) |
| 34 |
ptr->next->prev = new_elem; |
| 35 |
|
| 36 |
ptr->next = new_elem; |
| 37 |
} |
| 38 |
return new_elem; |
| 39 |
} |
| 40 |
|
| 41 |
/* |
| 42 |
Return head of the list. |
| 43 |
*/ |
| 44 |
struct dl_list * |
| 45 |
dl_list_head(struct dl_list *list) |
| 46 |
{ |
| 47 |
for (; list != NULL && list->prev != NULL; list = list->prev) { ; } |
| 48 |
return list; |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
Put a new item at the front of the list (current front is passed in first argument) |
| 53 |
and return its position. |
| 54 |
*/ |
| 55 |
struct dl_list * |
| 56 |
dl_list_push_front(struct dl_list *list, void *item) |
| 57 |
{ |
| 58 |
struct dl_list *new_elem = xmalloc(sizeof *new_elem); |
| 59 |
new_elem->item = item; |
| 60 |
new_elem->next = NULL; |
| 61 |
new_elem->prev = NULL; |
| 62 |
|
| 63 |
if (list != NULL) { /* prepend to current position */ |
| 64 |
new_elem->next = list; |
| 65 |
list->prev = new_elem; |
| 66 |
} |
| 67 |
return new_elem; |
| 68 |
} |
| 69 |
|
| 70 |
/* |
| 71 |
Truncate list so that current pointer is the last element. |
| 72 |
*/ |
| 73 |
struct dl_list * |
| 74 |
dl_list_truncate(struct dl_list *list) |
| 75 |
{ |
| 76 |
struct dl_list *ptr = list->next; |
| 77 |
struct dl_list *save; |
| 78 |
|
| 79 |
list->next = NULL; |
| 80 |
|
| 81 |
while (ptr != NULL) { |
| 82 |
save = ptr->next; |
| 83 |
free(ptr); |
| 84 |
ptr = save; |
| 85 |
} |
| 86 |
return list; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
Truncate list at the head (i.e. remove the first element from it - head must be passed to this list), |
| 91 |
and return the result. |
| 92 |
*/ |
| 93 |
struct dl_list * |
| 94 |
dl_list_truncate_head(struct dl_list *list) |
| 95 |
{ |
| 96 |
struct dl_list *ptr = list->next; |
| 97 |
if (list->next != NULL) |
| 98 |
list->next->prev = NULL; |
| 99 |
free(list); |
| 100 |
return ptr; |
| 101 |
} |
| 102 |
|
| 103 |
/* |
| 104 |
If the item pointed to by *list isn't the head of the list, remove it, |
| 105 |
set *list to the previous item, and return True. Else return False. |
| 106 |
*/ |
| 107 |
Boolean |
| 108 |
dl_list_remove_item(struct dl_list **list) |
| 109 |
{ |
| 110 |
struct dl_list *ptr = *list; /* item to remove */ |
| 111 |
if (ptr->prev == NULL) |
| 112 |
return False; |
| 113 |
ptr->prev->next = ptr->next; |
| 114 |
if (ptr->next != NULL) |
| 115 |
ptr->next->prev = ptr->prev; |
| 116 |
/* update list */ |
| 117 |
*list = (*list)->prev; |
| 118 |
/* remove item */ |
| 119 |
free(ptr); |
| 120 |
|
| 121 |
return True; |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
dl_list_apply(struct dl_list *list, void (*func)(const void *item)) |
| 126 |
{ |
| 127 |
struct dl_list *ptr; |
| 128 |
for (ptr = list; ptr != NULL; ptr = ptr->next) { |
| 129 |
func(ptr->item); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
Remove all items matching compare_func() from list. Must be called |
| 135 |
with a pointer to the head of the list, which is also returned. |
| 136 |
Returns the number of removed items in `count'. |
| 137 |
*/ |
| 138 |
struct dl_list * |
| 139 |
dl_list_remove(struct dl_list *list, const void *item, |
| 140 |
int *count, |
| 141 |
void **removed_item, |
| 142 |
Boolean (*compare_func)(const void *item1, const void *item2)) |
| 143 |
{ |
| 144 |
struct dl_list *ptr = list; |
| 145 |
while (ptr != NULL) { |
| 146 |
struct dl_list *next = ptr->next; |
| 147 |
if (compare_func(ptr->item, item)) { /* match */ |
| 148 |
*removed_item = ptr->item; |
| 149 |
(*count)++; |
| 150 |
if (ptr->prev != NULL) { |
| 151 |
ptr->prev->next = ptr->next; |
| 152 |
} |
| 153 |
else { /* removed first element */ |
| 154 |
list = list->next; |
| 155 |
} |
| 156 |
|
| 157 |
if (ptr->next != NULL) |
| 158 |
ptr->next->prev = ptr->prev; |
| 159 |
free(ptr); |
| 160 |
ptr = NULL; |
| 161 |
} |
| 162 |
ptr = next; |
| 163 |
} |
| 164 |
return list; |
| 165 |
} |