|
| 1 | +/* $Id: srm_llist.c,v 1.1.1.1 2002-04-24 14:26:19 derick Exp $ */ |
| 2 | + |
| 3 | +/* The contents of this file are subject to the Vulcan Logic Public |
| 4 | + * License Version 1.1 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at http://www.vl-srm.net/vlpl/ |
| 7 | + * |
| 8 | + * Software distributed under the License is distributed on an "AS |
| 9 | + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 10 | + * implied. See the License for the specific language governing |
| 11 | + * rights and limitations under the License. |
| 12 | + * |
| 13 | + * The Original Code is vl-srm.net code. |
| 14 | + * |
| 15 | + * The Initial Developer of the Original Code is the Vulcan Logic |
| 16 | + * Group. Portions created by Vulcan Logic Group are Copyright (C) |
| 17 | + * 2000, 2001, 2002 Vulcan Logic Group. All Rights Reserved. |
| 18 | + * |
| 19 | + * Author(s): Sterling Hughes <sterling@php.net> |
| 20 | + */ |
| 21 | + |
| 22 | +#include <stdlib.h> |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +#include "srm_llist.h" |
| 26 | + |
| 27 | +srm_llist *srm_llist_alloc(srm_llist_dtor dtor) |
| 28 | +{ |
| 29 | + srm_llist *l; |
| 30 | + |
| 31 | + l = malloc(sizeof(srm_llist)); |
| 32 | + l->size = 0; |
| 33 | + l->dtor = dtor; |
| 34 | + l->head = NULL; |
| 35 | + l->tail = NULL; |
| 36 | + |
| 37 | + return l; |
| 38 | +} |
| 39 | + |
| 40 | +int srm_llist_insert_next(srm_llist *l, srm_llist_element *e, const void *p) |
| 41 | +{ |
| 42 | + srm_llist_element *ne; |
| 43 | + |
| 44 | + if (!e) { |
| 45 | + e = SRM_LLIST_TAIL(l); |
| 46 | + } |
| 47 | + |
| 48 | + ne = (srm_llist_element *) malloc(sizeof(srm_llist_element)); |
| 49 | + ne->ptr = (void *) p; |
| 50 | + if (l->size == 0) { |
| 51 | + l->head = ne; |
| 52 | + l->head->prev = NULL; |
| 53 | + l->head->next = NULL; |
| 54 | + l->tail = ne; |
| 55 | + } else { |
| 56 | + ne->next = e->next; |
| 57 | + ne->prev = e; |
| 58 | + if (e->next) { |
| 59 | + e->next->prev = ne; |
| 60 | + } else { |
| 61 | + l->tail = ne; |
| 62 | + } |
| 63 | + e->next = ne; |
| 64 | + } |
| 65 | + |
| 66 | + ++l->size; |
| 67 | + |
| 68 | + return 1; |
| 69 | +} |
| 70 | + |
| 71 | +int srm_llist_insert_prev(srm_llist *l, srm_llist_element *e, const void *p) |
| 72 | +{ |
| 73 | + srm_llist_element *ne; |
| 74 | + |
| 75 | + if (!e) { |
| 76 | + e = SRM_LLIST_HEAD(l); |
| 77 | + } |
| 78 | + |
| 79 | + ne = (srm_llist_element *) malloc(sizeof(srm_llist_element)); |
| 80 | + ne->ptr = (void *) p; |
| 81 | + if (l->size == 0) { |
| 82 | + l->head = ne; |
| 83 | + l->head->prev = NULL; |
| 84 | + l->head->next = NULL; |
| 85 | + l->tail = ne; |
| 86 | + } else { |
| 87 | + ne->next = e; |
| 88 | + ne->prev = e->prev; |
| 89 | + if (e->prev) |
| 90 | + e->prev->next = ne; |
| 91 | + else |
| 92 | + l->head = ne; |
| 93 | + e->prev = ne; |
| 94 | + } |
| 95 | + |
| 96 | + ++l->size; |
| 97 | + |
| 98 | + return 0; |
| 99 | +} |
| 100 | + |
| 101 | +int srm_llist_remove(srm_llist *l, srm_llist_element *e, void *user) |
| 102 | +{ |
| 103 | + if (e == NULL || l->size == 0) |
| 104 | + return 0; |
| 105 | + |
| 106 | + if (e == l->head) { |
| 107 | + l->head = e->next; |
| 108 | + |
| 109 | + if (l->head == NULL) |
| 110 | + l->tail = NULL; |
| 111 | + else |
| 112 | + e->next->prev = NULL; |
| 113 | + } else { |
| 114 | + e->prev->next = e->next; |
| 115 | + if (!e->next) |
| 116 | + l->tail = e->prev; |
| 117 | + else |
| 118 | + e->next->prev = e->prev; |
| 119 | + } |
| 120 | + |
| 121 | + l->dtor(user, e->ptr); |
| 122 | + free(e); |
| 123 | + --l->size; |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +int srm_llist_remove_next(srm_llist *l, srm_llist_element *e, void *user) |
| 129 | +{ |
| 130 | + return srm_llist_remove(l, e->next, user); |
| 131 | +} |
| 132 | + |
| 133 | +int srm_llist_remove_prev(srm_llist *l, srm_llist_element *e, void *user) |
| 134 | +{ |
| 135 | + return srm_llist_remove(l, e->prev, user); |
| 136 | +} |
| 137 | + |
| 138 | +srm_llist_element *srm_llist_jump(srm_llist *l, int where, int pos) |
| 139 | +{ |
| 140 | + srm_llist_element *e=NULL; |
| 141 | + int i; |
| 142 | + |
| 143 | + if (where == LIST_HEAD) { |
| 144 | + e = SRM_LLIST_HEAD(l); |
| 145 | + for (i = 0; i < pos; ++i) { |
| 146 | + e = SRM_LLIST_NEXT(e); |
| 147 | + } |
| 148 | + } |
| 149 | + else if (where == LIST_TAIL) { |
| 150 | + e = SRM_LLIST_TAIL(l); |
| 151 | + for (i = 0; i < pos; ++i) { |
| 152 | + e = SRM_LLIST_PREV(e); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return e; |
| 157 | +} |
| 158 | + |
| 159 | +size_t srm_llist_count(srm_llist *l) |
| 160 | +{ |
| 161 | + return l->size; |
| 162 | +} |
| 163 | + |
| 164 | +void srm_llist_destroy(srm_llist *l, void *user) |
| 165 | +{ |
| 166 | + while (srm_llist_count(l) > 0) { |
| 167 | + srm_llist_remove(l, SRM_LLIST_TAIL(l), user); |
| 168 | + } |
| 169 | + |
| 170 | + free (l); |
| 171 | + l = NULL; |
| 172 | +} |
| 173 | + |
| 174 | +/* |
| 175 | + * Local Variables: |
| 176 | + * c-basic-offset: 4 |
| 177 | + * tab-width: 4 |
| 178 | + * End: |
| 179 | + * vim600: fdm=marker |
| 180 | + * vim: noet sw=4 ts=4 |
| 181 | + */ |
0 commit comments