xref: /onnv-gate/usr/src/lib/libc/port/gen/tsearch.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Tree search algorithm, generalized from Knuth (6.2.2) Algorithm T.
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  *
360Sstevel@tonic-gate  * The NODE * arguments are declared in the lint files as char *,
370Sstevel@tonic-gate  * because the definition of NODE isn't available to the user.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
40*6812Sraf #pragma weak _tdelete = tdelete
41*6812Sraf #pragma weak _tsearch = tsearch
42*6812Sraf #pragma weak _twalk = twalk
430Sstevel@tonic-gate 
44*6812Sraf #include "lint.h"
450Sstevel@tonic-gate #include "mtlib.h"
460Sstevel@tonic-gate #include "libc.h"
470Sstevel@tonic-gate #include <sys/types.h>
480Sstevel@tonic-gate #include <search.h>
490Sstevel@tonic-gate #include <stdlib.h>
500Sstevel@tonic-gate #include <thread.h>
510Sstevel@tonic-gate #include <synch.h>
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 
540Sstevel@tonic-gate 
550Sstevel@tonic-gate typedef struct node { char *key; struct node *llink, *rlink; } NODE;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate static void __twalk(NODE *, void (*)(const void *, VISIT, int), int);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* Find or insert key into search tree */
610Sstevel@tonic-gate void *
tsearch(const void * ky,void ** rtp,int (* compar)())620Sstevel@tonic-gate tsearch(const void *ky, void **rtp, int (*compar)())
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	char *key = (char *)ky;
650Sstevel@tonic-gate 	NODE **rootp = (NODE **)rtp;
660Sstevel@tonic-gate 	NODE *q;	/* New node if key not found */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (rootp == NULL)
690Sstevel@tonic-gate 		return (NULL);
700Sstevel@tonic-gate 	while (*rootp != NULL) {			/* T1: */
710Sstevel@tonic-gate 		int r = (*compar)(key, (*rootp)->key);	/* T2: */
720Sstevel@tonic-gate 		if (r == 0)
730Sstevel@tonic-gate 			return ((void *)*rootp);	/* Key found */
740Sstevel@tonic-gate 		rootp = (r < 0) ?
750Sstevel@tonic-gate 		    &(*rootp)->llink :		/* T3: Take left branch */
760Sstevel@tonic-gate 		    &(*rootp)->rlink;		/* T4: Take right branch */
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate 	q = lmalloc(sizeof (NODE));		/* T5: Not found */
790Sstevel@tonic-gate 	if (q != NULL) {			/* Allocate new node */
800Sstevel@tonic-gate 		*rootp = q;			/* Link new node to old */
810Sstevel@tonic-gate 		q->key = key;			/* Initialize new node */
820Sstevel@tonic-gate 		q->llink = q->rlink = NULL;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	return ((void *)q);
850Sstevel@tonic-gate }
860Sstevel@tonic-gate 
870Sstevel@tonic-gate /* Delete node with key key */
880Sstevel@tonic-gate void *
tdelete(const void * ky,void ** rtp,int (* compar)())890Sstevel@tonic-gate tdelete(const void *ky, void **rtp, int (*compar)())
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	char *key = (char *)ky;
920Sstevel@tonic-gate 	NODE **rootp = (NODE **)rtp;
930Sstevel@tonic-gate 	NODE *p;		/* Parent of node to be deleted */
940Sstevel@tonic-gate 	NODE *q;	/* Successor node */
950Sstevel@tonic-gate 	NODE *r;	/* Right son node */
960Sstevel@tonic-gate 	int ans;		/* Result of comparison */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	if (rootp == NULL || (p = *rootp) == NULL)
990Sstevel@tonic-gate 		return (NULL);
1000Sstevel@tonic-gate 	while ((ans = (*compar)(key, (*rootp)->key)) != 0) {
1010Sstevel@tonic-gate 		p = *rootp;
1020Sstevel@tonic-gate 		rootp = (ans < 0) ?
1030Sstevel@tonic-gate 		    &(*rootp)->llink :		/* Take left branch */
1040Sstevel@tonic-gate 		    &(*rootp)->rlink;		/* Take right branch */
1050Sstevel@tonic-gate 		if (*rootp == NULL)
1060Sstevel@tonic-gate 			return (NULL);		/* Key not found */
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 	r = (*rootp)->rlink;			/* D1: */
1090Sstevel@tonic-gate 	if ((q = (*rootp)->llink) == NULL)	/* Llink NULL? */
1100Sstevel@tonic-gate 		q = r;
1110Sstevel@tonic-gate 	else if (r != NULL) {			/* Rlink NULL? */
1120Sstevel@tonic-gate 		if (r->llink == NULL) {		/* D2: Find successor */
1130Sstevel@tonic-gate 			r->llink = q;
1140Sstevel@tonic-gate 			q = r;
1150Sstevel@tonic-gate 		} else {			/* D3: Find NULL link */
1160Sstevel@tonic-gate 			for (q = r->llink; q->llink != NULL; q = r->llink)
1170Sstevel@tonic-gate 				r = q;
1180Sstevel@tonic-gate 			r->llink = q->rlink;
1190Sstevel@tonic-gate 			q->llink = (*rootp)->llink;
1200Sstevel@tonic-gate 			q->rlink = (*rootp)->rlink;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 	}
1230Sstevel@tonic-gate 	lfree(*rootp, sizeof (NODE));		/* D4: Free node */
1240Sstevel@tonic-gate 	*rootp = q;			/* Link parent to replacement */
1250Sstevel@tonic-gate 	return ((void *)p);
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate /* Walk the nodes of a tree */
1300Sstevel@tonic-gate void
twalk(const void * rt,void (* action)(const void *,VISIT,int))1310Sstevel@tonic-gate twalk(const void *rt,		/* Root of the tree to be walked */
1320Sstevel@tonic-gate 	void (*action)(const void *, VISIT, int))
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	NODE *root = (NODE *)rt;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	if (root != NULL && action != NULL)
1370Sstevel@tonic-gate 		__twalk(root, action, 0);
1380Sstevel@tonic-gate }
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /* Walk the nodes of a tree */
1420Sstevel@tonic-gate static void
__twalk(NODE * root,void (* action)(const void *,VISIT,int),int level)1430Sstevel@tonic-gate __twalk(NODE *root,		/* Root of the tree to be walked */
1440Sstevel@tonic-gate 	void (*action)(const void *, VISIT, int),
1450Sstevel@tonic-gate 	int level)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	if (root->llink == NULL && root->rlink == NULL)
1480Sstevel@tonic-gate 		(*action)(root, leaf, level);
1490Sstevel@tonic-gate 	else {
1500Sstevel@tonic-gate 		(*action)(root, preorder, level);
1510Sstevel@tonic-gate 		if (root->llink != NULL)
1520Sstevel@tonic-gate 			__twalk(root->llink, action, level + 1);
1530Sstevel@tonic-gate 		(*action)(root, postorder, level);
1540Sstevel@tonic-gate 		if (root->rlink != NULL)
1550Sstevel@tonic-gate 			__twalk(root->rlink, action, level + 1);
1560Sstevel@tonic-gate 		(*action)(root, endorder, level);
1570Sstevel@tonic-gate 	}
1580Sstevel@tonic-gate }
159