1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate */
26*7c478bd9Sstevel@tonic-gate
27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*7c478bd9Sstevel@tonic-gate
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate * Binary tree access. The routines contained in this file are:
31*7c478bd9Sstevel@tonic-gate * slp_tsearch
32*7c478bd9Sstevel@tonic-gate * slp_tfind
33*7c478bd9Sstevel@tonic-gate * slp_twalk
34*7c478bd9Sstevel@tonic-gate *
35*7c478bd9Sstevel@tonic-gate * These have the same interfaces as tsearch(3C), tfind(3C), and
36*7c478bd9Sstevel@tonic-gate * twalk(3C), with two important distinctions:
37*7c478bd9Sstevel@tonic-gate * - libc twalk is inadequate, since it doesn't allow the caller to pass
38*7c478bd9Sstevel@tonic-gate * cookies into the action function (prohibiting thread-safe usage).
39*7c478bd9Sstevel@tonic-gate * slp_twalk allows cookies.
40*7c478bd9Sstevel@tonic-gate * - libc tsearch and tfind *always* lock access to the tree. This can
41*7c478bd9Sstevel@tonic-gate * be inefficient when it isn't necessary to lock the tree in order
42*7c478bd9Sstevel@tonic-gate * to ensure thread-safety. It is the responsibility of the caller to
43*7c478bd9Sstevel@tonic-gate * ensure that slp_tsearch and slp_tfind are safe for concurrent access.
44*7c478bd9Sstevel@tonic-gate *
45*7c478bd9Sstevel@tonic-gate * It is possible for this implementation to degenerate into a
46*7c478bd9Sstevel@tonic-gate * linked-list algorithm with certain inputs. If this proves to be
47*7c478bd9Sstevel@tonic-gate * a problem in practice, these routines can be optimized by balancing
48*7c478bd9Sstevel@tonic-gate * the trees.
49*7c478bd9Sstevel@tonic-gate */
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate #include <stdio.h>
52*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
53*7c478bd9Sstevel@tonic-gate #include <slp-internal.h>
54*7c478bd9Sstevel@tonic-gate
55*7c478bd9Sstevel@tonic-gate struct node { char *key; struct node *llink, *rlink; };
56*7c478bd9Sstevel@tonic-gate typedef struct node NODE;
57*7c478bd9Sstevel@tonic-gate
slp_twalk(void * r,void (* action)(void *,VISIT,int,void *),int level,void * cookie)58*7c478bd9Sstevel@tonic-gate void slp_twalk(void *r,
59*7c478bd9Sstevel@tonic-gate void (*action)(void *, VISIT, int, void *),
60*7c478bd9Sstevel@tonic-gate int level, void *cookie) {
61*7c478bd9Sstevel@tonic-gate NODE *root = (NODE *) r;
62*7c478bd9Sstevel@tonic-gate if (root->llink == NULL && root->rlink == NULL)
63*7c478bd9Sstevel@tonic-gate (*action)(root, leaf, level, cookie);
64*7c478bd9Sstevel@tonic-gate else {
65*7c478bd9Sstevel@tonic-gate (*action)(root, preorder, level, cookie);
66*7c478bd9Sstevel@tonic-gate if (root->llink != NULL)
67*7c478bd9Sstevel@tonic-gate slp_twalk(root->llink, action, level + 1, cookie);
68*7c478bd9Sstevel@tonic-gate (*action)(root, postorder, level, cookie);
69*7c478bd9Sstevel@tonic-gate if (root->rlink != NULL)
70*7c478bd9Sstevel@tonic-gate slp_twalk(root->rlink, action, level + 1, cookie);
71*7c478bd9Sstevel@tonic-gate (*action)(root, endorder, level, cookie);
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate }
74*7c478bd9Sstevel@tonic-gate
75*7c478bd9Sstevel@tonic-gate /* Find or insert key into search tree */
slp_tsearch(const void * ky,void ** rtp,int (* compar)())76*7c478bd9Sstevel@tonic-gate void *slp_tsearch(const void *ky, void **rtp, int (* compar)()) {
77*7c478bd9Sstevel@tonic-gate char *key = (char *)ky;
78*7c478bd9Sstevel@tonic-gate NODE **rootp = (NODE **)rtp;
79*7c478bd9Sstevel@tonic-gate NODE *q; /* New node if key not found */
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate if (rootp == NULL)
82*7c478bd9Sstevel@tonic-gate return (NULL);
83*7c478bd9Sstevel@tonic-gate while (*rootp != NULL) { /* T1: */
84*7c478bd9Sstevel@tonic-gate int r = (*compar)(key, (*rootp)->key); /* T2: */
85*7c478bd9Sstevel@tonic-gate if (r == 0)
86*7c478bd9Sstevel@tonic-gate return ((void *)*rootp); /* Key found */
87*7c478bd9Sstevel@tonic-gate rootp = (r < 0) ?
88*7c478bd9Sstevel@tonic-gate &(*rootp)->llink : /* T3: Take left branch */
89*7c478bd9Sstevel@tonic-gate &(*rootp)->rlink; /* T4: Take right branch */
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate q = (NODE *) malloc(sizeof (NODE)); /* T5: Not found */
92*7c478bd9Sstevel@tonic-gate if (q != NULL) { /* Allocate new node */
93*7c478bd9Sstevel@tonic-gate *rootp = q; /* Link new node to old */
94*7c478bd9Sstevel@tonic-gate q->key = key; /* Initialize new node */
95*7c478bd9Sstevel@tonic-gate q->llink = q->rlink = NULL;
96*7c478bd9Sstevel@tonic-gate }
97*7c478bd9Sstevel@tonic-gate return ((void *)q);
98*7c478bd9Sstevel@tonic-gate }
99*7c478bd9Sstevel@tonic-gate
slp_tfind(const void * ky,void * const * rtp,int (* compar)(const void *,const void *))100*7c478bd9Sstevel@tonic-gate void *slp_tfind(const void *ky, void *const *rtp,
101*7c478bd9Sstevel@tonic-gate int (*compar)(const void *, const void *)) {
102*7c478bd9Sstevel@tonic-gate void *key = (char *)ky;
103*7c478bd9Sstevel@tonic-gate NODE **rootp = (NODE **)rtp;
104*7c478bd9Sstevel@tonic-gate if (rootp == NULL)
105*7c478bd9Sstevel@tonic-gate return (NULL);
106*7c478bd9Sstevel@tonic-gate while (*rootp != NULL) { /* T1: */
107*7c478bd9Sstevel@tonic-gate int r = (*compar)(key, (*rootp)->key); /* T2: */
108*7c478bd9Sstevel@tonic-gate if (r == 0)
109*7c478bd9Sstevel@tonic-gate return ((void *)*rootp); /* Key found */
110*7c478bd9Sstevel@tonic-gate rootp = (r < 0) ?
111*7c478bd9Sstevel@tonic-gate &(*rootp)->llink : /* T3: Take left branch */
112*7c478bd9Sstevel@tonic-gate &(*rootp)->rlink; /* T4: Take right branch */
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate return (NULL);
115*7c478bd9Sstevel@tonic-gate }
116