OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libc/] [search/] [tdelete.c] - Diff between revs 207 and 345

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/*      $NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $        */
/*      $NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $        */
 
 
/*
/*
 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
 * the AT&T man page says.
 * the AT&T man page says.
 *
 *
 * The node_t structure is for internal use only, lint doesn't grok it.
 * The node_t structure is for internal use only, lint doesn't grok it.
 *
 *
 * Written by reading the System V Interface Definition, not the code.
 * Written by reading the System V Interface Definition, not the code.
 *
 *
 * Totally public domain.
 * Totally public domain.
 */
 */
 
 
#include <sys/cdefs.h>
#include <sys/cdefs.h>
#if 0
#if 0
#if defined(LIBC_SCCS) && !defined(lint)
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $");
__RCSID("$NetBSD: tdelete.c,v 1.2 1999/09/16 11:45:37 lukem Exp $");
#endif /* LIBC_SCCS and not lint */
#endif /* LIBC_SCCS and not lint */
#endif
#endif
 
 
#include <assert.h>
#include <assert.h>
#define _SEARCH_PRIVATE
#define _SEARCH_PRIVATE
#include <search.h>
#include <search.h>
#include <stdlib.h>
#include <stdlib.h>
 
 
 
 
/* delete node with given key */
/* delete node with given key */
void *
void *
_DEFUN(tdelete, (vkey, vrootp, compar),
_DEFUN(tdelete, (vkey, vrootp, compar),
        const void *vkey _AND   /* key to be deleted */
        const void *vkey _AND   /* key to be deleted */
        void      **vrootp _AND /* address of the root of tree */
        void      **vrootp _AND /* address of the root of tree */
        int       (*compar)(const void *, const void *))
        int       (*compar)(const void *, const void *))
{
{
        node_t **rootp = (node_t **)vrootp;
        node_t **rootp = (node_t **)vrootp;
        node_t *p, *q, *r;
        node_t *p, *q, *r;
        int  cmp;
        int  cmp;
 
 
        if (rootp == NULL || (p = *rootp) == NULL)
        if (rootp == NULL || (p = *rootp) == NULL)
                return NULL;
                return NULL;
 
 
        while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
        while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
                p = *rootp;
                p = *rootp;
                rootp = (cmp < 0) ?
                rootp = (cmp < 0) ?
                    &(*rootp)->llink :          /* follow llink branch */
                    &(*rootp)->llink :          /* follow llink branch */
                    &(*rootp)->rlink;           /* follow rlink branch */
                    &(*rootp)->rlink;           /* follow rlink branch */
                if (*rootp == NULL)
                if (*rootp == NULL)
                        return NULL;            /* key not found */
                        return NULL;            /* key not found */
        }
        }
        r = (*rootp)->rlink;                    /* D1: */
        r = (*rootp)->rlink;                    /* D1: */
        if ((q = (*rootp)->llink) == NULL)      /* Left NULL? */
        if ((q = (*rootp)->llink) == NULL)      /* Left NULL? */
                q = r;
                q = r;
        else if (r != NULL) {                   /* Right link is NULL? */
        else if (r != NULL) {                   /* Right link is NULL? */
                if (r->llink == NULL) {         /* D2: Find successor */
                if (r->llink == NULL) {         /* D2: Find successor */
                        r->llink = q;
                        r->llink = q;
                        q = r;
                        q = r;
                } else {                        /* D3: Find NULL link */
                } else {                        /* D3: Find NULL link */
                        for (q = r->llink; q->llink != NULL; q = r->llink)
                        for (q = r->llink; q->llink != NULL; q = r->llink)
                                r = q;
                                r = q;
                        r->llink = q->rlink;
                        r->llink = q->rlink;
                        q->llink = (*rootp)->llink;
                        q->llink = (*rootp)->llink;
                        q->rlink = (*rootp)->rlink;
                        q->rlink = (*rootp)->rlink;
                }
                }
        }
        }
        free(*rootp);                           /* D4: Free node */
        free(*rootp);                           /* D4: Free node */
        *rootp = q;                             /* link parent to new node */
        *rootp = q;                             /* link parent to new node */
        return p;
        return p;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.