root/xdebug_llist.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. xdebug_llist_alloc
  2. xdebug_llist_init
  3. xdebug_llist_insert_next
  4. xdebug_llist_insert_prev
  5. xdebug_llist_remove
  6. xdebug_llist_remove_next
  7. xdebug_llist_remove_prev
  8. xdebug_llist_jump
  9. xdebug_llist_count
  10. xdebug_llist_empty
  11. xdebug_llist_destroy

/* $Id: xdebug_llist.c,v 1.4 2003/02/20 14:30:54 derick Exp $ */

/* The contents of this file are subject to the Vulcan Logic Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.vl-srm.net/vlpl/
 *
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 *
 * The Original Code is vl-srm.net code.
 *
 * The Initial Developer of the Original Code is the Vulcan Logic 
 * Group.  Portions created by Vulcan Logic Group are Copyright (C) 
 * 2000, 2001, 2002 Vulcan Logic Group. All Rights Reserved.
 *
 * Author(s): Sterling Hughes <sterling@php.net> 
 */

#include <stdlib.h>
#include <string.h>

#include "xdebug_llist.h"

xdebug_llist *xdebug_llist_alloc(xdebug_llist_dtor dtor)
{
        xdebug_llist *l;

        l = malloc(sizeof(xdebug_llist));
        xdebug_llist_init(l, dtor);

        return l;
}

void xdebug_llist_init(xdebug_llist *l, xdebug_llist_dtor dtor)
{
        l->size = 0;
        l->dtor = dtor;
        l->head = NULL;
        l->tail = NULL;
}

int xdebug_llist_insert_next(xdebug_llist *l, xdebug_llist_element *e, const void *p)
{
        xdebug_llist_element  *ne;

        if (!e) {
                e = XDEBUG_LLIST_TAIL(l);
        }

        ne = (xdebug_llist_element *) malloc(sizeof(xdebug_llist_element));
        ne->ptr = (void *) p;
        if (l->size == 0) {
                l->head = ne;
                l->head->prev = NULL;
                l->head->next = NULL;
                l->tail = ne;
        } else {
                ne->next = e->next;
                ne->prev = e;
                if (e->next) {
                        e->next->prev = ne;
                } else {
                        l->tail = ne;
                }
                e->next = ne;
        }

        ++l->size;

        return 1;
}

int xdebug_llist_insert_prev(xdebug_llist *l, xdebug_llist_element *e, const void *p)
{
        xdebug_llist_element *ne;

        if (!e) {
                e = XDEBUG_LLIST_HEAD(l);
        }

        ne = (xdebug_llist_element *) malloc(sizeof(xdebug_llist_element));
        ne->ptr = (void *) p;
        if (l->size == 0) {
                l->head = ne;
                l->head->prev = NULL;
                l->head->next = NULL;
                l->tail = ne;
        } else {
                ne->next = e;
                ne->prev = e->prev;
                if (e->prev)
                        e->prev->next = ne;
                else
                        l->head = ne;
                e->prev = ne;
        }

        ++l->size;

        return 0;
}

int xdebug_llist_remove(xdebug_llist *l, xdebug_llist_element *e, void *user)
{
        if (e == NULL || l->size == 0)
                return 0;

        if (e == l->head) {
                l->head = e->next;

                if (l->head == NULL)
                        l->tail = NULL;
                else
                        e->next->prev = NULL;
        } else {
                e->prev->next = e->next;
                if (!e->next)
                        l->tail = e->prev;
                else
                        e->next->prev = e->prev;
        }

        if (l->dtor) {
                l->dtor(user, e->ptr);
        }
        free(e);
        --l->size;

        return 0;
}

int xdebug_llist_remove_next(xdebug_llist *l, xdebug_llist_element *e, void *user)
{
        return xdebug_llist_remove(l, e->next, user);
}

int xdebug_llist_remove_prev(xdebug_llist *l, xdebug_llist_element *e, void *user)
{
        return xdebug_llist_remove(l, e->prev, user);
}

xdebug_llist_element *xdebug_llist_jump(xdebug_llist *l, int where, int pos)
{
    xdebug_llist_element *e=NULL;
    int i;

    if (where == LIST_HEAD) {
        e = XDEBUG_LLIST_HEAD(l);
        for (i = 0; i < pos; ++i) {
            e = XDEBUG_LLIST_NEXT(e);
        }
    }
    else if (where == LIST_TAIL) {
        e = XDEBUG_LLIST_TAIL(l);
        for (i = 0; i < pos; ++i) {
            e = XDEBUG_LLIST_PREV(e);
        }
    }

    return e;
}

size_t xdebug_llist_count(xdebug_llist *l)
{
        return l->size;
}

void xdebug_llist_empty(xdebug_llist *l, void *user)
{
        while (xdebug_llist_count(l) > 0) {
                xdebug_llist_remove(l, XDEBUG_LLIST_TAIL(l), user);
        }
}

void xdebug_llist_destroy(xdebug_llist *l, void *user)
{
        xdebug_llist_empty(l, user);

        free (l);
        l = NULL;
}

/*
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 4
 * End:
 * vim600: fdm=marker
 * vim: noet sw=4 ts=4
 */

/* [<][>][^][v][top][bottom][index][help] */