Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: php-4.0.5RC6
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 295 lines (232 sloc) 6.685 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2001 Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 0.92 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.zend.com/license/0_92.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/


#include "zend.h"
#include "zend_llist.h"

ZEND_API void zend_llist_init(zend_llist *l, size_t size, void (*dtor)(void *data), unsigned char persistent)
{
l->head = NULL;
l->tail = NULL;
l->size = size;
l->dtor = dtor;
l->persistent = persistent;
}


ZEND_API void zend_llist_add_element(zend_llist *l, void *element)
{
zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);

tmp->prev = l->tail;
tmp->next = NULL;
if (l->tail) {
l->tail->next = tmp;
} else {
l->head = tmp;
}
l->tail = tmp;
memcpy(tmp->data, element, l->size);
}


ZEND_API void zend_llist_prepend_element(zend_llist *l, void *element)
{
zend_llist_element *tmp = pemalloc(sizeof(zend_llist_element)+l->size-1, l->persistent);

tmp->next = l->head;
tmp->prev = NULL;
if (l->head) {
l->head->prev = tmp;
} else {
l->tail = tmp;
}
l->head = tmp;
memcpy(tmp->data, element, l->size);
}


#define DEL_LLIST_ELEMENT(current,l) \
if (current->prev) {\
current->prev->next = current->next;\
} else {\
l->head = current->next;\
}\
if (current->next) {\
current->next->prev = current->prev;\
} else {\
l->tail = current->prev;\
}\
if (l->dtor) {\
l->dtor(current->data);\
pefree(current, l->persistent);\
}


ZEND_API void zend_llist_del_element(zend_llist *l, void *element, int (*compare)(void *element1, void *element2))
{
zend_llist_element *current=l->head;

while (current) {
if (compare(current->data, element)) {
DEL_LLIST_ELEMENT(current,l);
break;
}
current = current->next;
}
}


ZEND_API void zend_llist_destroy(zend_llist *l)
{
zend_llist_element *current=l->head, *next;

while (current) {
next = current->next;
if (l->dtor) {
l->dtor(current->data);
}
pefree(current, l->persistent);
current = next;
}
}


ZEND_API void zend_llist_clean(zend_llist *l)
{
zend_llist_destroy(l);
l->head = l->tail = NULL;
}


ZEND_API void zend_llist_remove_tail(zend_llist *l)
{
zend_llist_element *old_tail;

if ((old_tail = l->tail)) {
if (l->tail->prev) {
l->tail->prev->next = NULL;
}
l->tail = l->tail->prev;
pefree(old_tail, l->persistent);
}
}


ZEND_API void zend_llist_copy(zend_llist *dst, zend_llist *src)
{
zend_llist_element *ptr;

zend_llist_init(dst, src->size, src->dtor, src->persistent);
ptr = src->head;
while (ptr) {
zend_llist_add_element(dst, ptr->data);
ptr = ptr->next;
}
}


ZEND_API void zend_llist_apply_with_del(zend_llist *l, int (*func)(void *data))
{
zend_llist_element *element, *next;

element=l->head;
while (element) {
next = element->next;
if (func(element->data)) {
DEL_LLIST_ELEMENT(element,l);
}
element = next;
}
}


ZEND_API void zend_llist_apply(zend_llist *l, void (*func)(void *data))
{
zend_llist_element *element;

for (element=l->head; element; element=element->next) {
func(element->data);
}
}

ZEND_API void zend_llist_sort(zend_llist *l, llist_compare_func_t comp_func)
{
int list_size=0, i;

zend_llist_element **elements;
zend_llist_element *element, **ptr;

for (element=l->head; element; element=element->next) {
list_size++;
}

if (list_size == 0) {
return;
}

elements = (zend_llist_element **) emalloc(list_size*sizeof(zend_llist_element *));

ptr = &elements[0];

for (element=l->head; element; element=element->next) {
*ptr++ = element;
}

qsort(elements, list_size, sizeof(zend_llist_element *), (int (*)(const void *, const void *)) comp_func);

l->head = elements[0];
elements[0]->prev = NULL;

for (i=1; i<list_size; i++) {
elements[i]->prev = elements[i-1];
elements[i-1]->next = elements[i];
}
elements[i-1]->next = NULL;
l->tail = elements[i-1];
efree(elements);
}


ZEND_API void zend_llist_apply_with_argument(zend_llist *l, void (*func)(void *data, void *arg), void *arg)
{
zend_llist_element *element;

for (element=l->head; element; element=element->next) {
func(element->data, arg);
}
}


ZEND_API void zend_llist_apply_with_arguments(zend_llist *l, llist_apply_with_args_func_t func, int num_args, ...)
{
zend_llist_element *element;
va_list args;

va_start(args, num_args);
for (element=l->head; element; element=element->next) {
func(element->data, num_args, args);
}
va_end(args);
}


ZEND_API int zend_llist_count(zend_llist *l)
{
zend_llist_element *element;
int element_count=0;

for (element=l->head; element; element=element->next) {
element_count++;
}
return element_count;
}


ZEND_API void *zend_llist_get_first_ex(zend_llist *l, zend_llist_position *pos)
{
zend_llist_position *current = pos ? pos : &l->traverse_ptr;

*current = l->head;
if (*current) {
return (*current)->data;
} else {
return NULL;
}
}


ZEND_API void *zend_llist_get_last_ex(zend_llist *l, zend_llist_position *pos)
{
zend_llist_position *current = pos ? pos : &l->traverse_ptr;

*current = l->tail;
if (*current) {
return (*current)->data;
} else {
return NULL;
}
}


ZEND_API void *zend_llist_get_next_ex(zend_llist *l, zend_llist_position *pos)
{
zend_llist_position *current = pos ? pos : &l->traverse_ptr;

if (*current) {
*current = (*current)->next;
if (*current) {
return (*current)->data;
}
}
return NULL;
}


ZEND_API void *zend_llist_get_prev_ex(zend_llist *l, zend_llist_position *pos)
{
zend_llist_position *current = pos ? pos : &l->traverse_ptr;

if (*current) {
*current = (*current)->prev;
if (*current) {
return (*current)->data;
}
}
return NULL;
}
Something went wrong with that request. Please try again.