2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Binary tree access. The routines contained in this file are:
2N/A * slp_tsearch
2N/A * slp_tfind
2N/A * slp_twalk
2N/A *
2N/A * These have the same interfaces as tsearch(3C), tfind(3C), and
2N/A * twalk(3C), with two important distinctions:
2N/A * - libc twalk is inadequate, since it doesn't allow the caller to pass
2N/A * cookies into the action function (prohibiting thread-safe usage).
2N/A * slp_twalk allows cookies.
2N/A * - libc tsearch and tfind *always* lock access to the tree. This can
2N/A * be inefficient when it isn't necessary to lock the tree in order
2N/A * to ensure thread-safety. It is the responsibility of the caller to
2N/A * ensure that slp_tsearch and slp_tfind are safe for concurrent access.
2N/A *
2N/A * It is possible for this implementation to degenerate into a
2N/A * linked-list algorithm with certain inputs. If this proves to be
2N/A * a problem in practice, these routines can be optimized by balancing
2N/A * the trees.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <slp-internal.h>
2N/A
2N/Astruct node { char *key; struct node *llink, *rlink; };
2N/Atypedef struct node NODE;
2N/A
2N/Avoid slp_twalk(void *r,
2N/A void (*action)(void *, VISIT, int, void *),
2N/A int level, void *cookie) {
2N/A NODE *root = (NODE *) r;
2N/A if (root->llink == NULL && root->rlink == NULL)
2N/A (*action)(root, leaf, level, cookie);
2N/A else {
2N/A (*action)(root, preorder, level, cookie);
2N/A if (root->llink != NULL)
2N/A slp_twalk(root->llink, action, level + 1, cookie);
2N/A (*action)(root, postorder, level, cookie);
2N/A if (root->rlink != NULL)
2N/A slp_twalk(root->rlink, action, level + 1, cookie);
2N/A (*action)(root, endorder, level, cookie);
2N/A }
2N/A}
2N/A
2N/A/* Find or insert key into search tree */
2N/Avoid *slp_tsearch(const void *ky, void **rtp, int (* compar)()) {
2N/A char *key = (char *)ky;
2N/A NODE **rootp = (NODE **)rtp;
2N/A NODE *q; /* New node if key not found */
2N/A
2N/A if (rootp == NULL)
2N/A return (NULL);
2N/A while (*rootp != NULL) { /* T1: */
2N/A int r = (*compar)(key, (*rootp)->key); /* T2: */
2N/A if (r == 0)
2N/A return ((void *)*rootp); /* Key found */
2N/A rootp = (r < 0) ?
2N/A &(*rootp)->llink : /* T3: Take left branch */
2N/A &(*rootp)->rlink; /* T4: Take right branch */
2N/A }
2N/A q = (NODE *) malloc(sizeof (NODE)); /* T5: Not found */
2N/A if (q != NULL) { /* Allocate new node */
2N/A *rootp = q; /* Link new node to old */
2N/A q->key = key; /* Initialize new node */
2N/A q->llink = q->rlink = NULL;
2N/A }
2N/A return ((void *)q);
2N/A}
2N/A
2N/Avoid *slp_tfind(const void *ky, void *const *rtp,
2N/A int (*compar)(const void *, const void *)) {
2N/A void *key = (char *)ky;
2N/A NODE **rootp = (NODE **)rtp;
2N/A if (rootp == NULL)
2N/A return (NULL);
2N/A while (*rootp != NULL) { /* T1: */
2N/A int r = (*compar)(key, (*rootp)->key); /* T2: */
2N/A if (r == 0)
2N/A return ((void *)*rootp); /* Key found */
2N/A rootp = (r < 0) ?
2N/A &(*rootp)->llink : /* T3: Take left branch */
2N/A &(*rootp)->rlink; /* T4: Take right branch */
2N/A }
2N/A return (NULL);
2N/A}