summaryrefslogtreecommitdiff
path: root/usr/src/lib/libast/common/comp/tsearch.c
blob: a47a14d9a7b94ca4cb4612e83748046e85baa561 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/***********************************************************************
*                                                                      *
*               This software is part of the ast package               *
*          Copyright (c) 1985-2009 AT&T Intellectual Property          *
*                      and is licensed under the                       *
*                  Common Public License, Version 1.0                  *
*                    by AT&T Intellectual Property                     *
*                                                                      *
*                A copy of the License is available at                 *
*            http://www.opensource.org/licenses/cpl1.0.txt             *
*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
*                                                                      *
*              Information and Software Systems Research               *
*                            AT&T Research                             *
*                           Florham Park NJ                            *
*                                                                      *
*                 Glenn Fowler <gsf@research.att.com>                  *
*                  David Korn <dgk@research.att.com>                   *
*                   Phong Vo <kpv@research.att.com>                    *
*                                                                      *
***********************************************************************/
/*
 * tsearch() for systems that have <search.h> but no tsearch()
 * why would such a system provide the interface but not the
 * implementation? that's what happens when one slimes their
 * way through standards compliance
 *
 * NOTE: please excuse the crude feature test
 */

#if !_UWIN

void _STUB_tsearch(){}

#else

#if _PACKAGE_ast
#include	<ast.h>
#endif

#define tdelete		______tdelete
#define tfind		______tfind
#define tsearch		______tsearch
#define twalk		______twalk

#include	<search.h>

#undef	tdelete
#undef	tfind
#undef	tsearch
#undef	twalk

#include	"dthdr.h"

/*	POSIX tsearch library based on libcdt
**	Written by Kiem-Phong Vo (AT&T Research, 07/19/95)
*/

typedef struct _tree_s
{	Dtlink_t	link;
	Void_t*		key;
} Tree_t;

typedef struct _treedisc_s
{	Dtdisc_t	disc;
	int(*		comparf)_ARG_((const Void_t*, const Void_t*));
} Treedisc_t;

#if defined(__EXPORT__)
#define extern	__EXPORT__
#endif

/* compare function */
#if __STD_C
static int treecompare(Dt_t* dt, char* one, char* two, Dtdisc_t* disc)
#else
static int treecompare(dt, one, two, disc)
Dt_t*		dt;
char*		one;
char*		two;
Dtdisc_t*	disc;
#endif
{
	return (*((Treedisc_t*)disc)->comparf)((Void_t*)one,(Void_t*)two);
}

static Treedisc_t	Treedisc =
{	{ sizeof(Dtlink_t), -1,	/* object is key		*/
	  0,
	  NIL(Dtmake_f), NIL(Dtfree_f),
	  treecompare,
	  NIL(Dthash_f),
	  NIL(Dtmemory_f),
	  NIL(Dtevent_f)
	},
	0
};

extern
#if __STD_C
Void_t* tsearch(const Void_t* key, Void_t** rootp,
		int(*comparf)(const Void_t*,const Void_t*) )
#else
Void_t* tsearch(key, rootp, comparf)
Void_t*		key;
Void_t**	rootp;
int(*		comparf)();
#endif
{
	reg Dt_t*	dt;
	reg Tree_t*	o;

	if(!rootp ||
	   (!(dt = *((Dt_t**)rootp)) && !(dt = dtopen((Dtdisc_t*)(&Treedisc),Dtorder))) )
		return NIL(Void_t*);

	/* dangerous to set comparf on each call but that's tsearch */
	Treedisc.comparf = comparf;

	if(!(o = (Tree_t*)dtmatch(dt,key)) )
	{	if(!(o = (Tree_t*)malloc(sizeof(Tree_t))) )
			return NIL(Void_t*);
		o->key = (Void_t*)key;
		dtinsert(dt,o);
	}

	if(o)
		*rootp = (Void_t*)dt;
	else if(*rootp == NIL(Void_t*) )
		dtclose(dt);

	return (Void_t*)(&o->key);
}

extern
#if __STD_C
Void_t* tfind(const Void_t* key, Void_t*const* rootp,
		int(*comparf)(const Void_t*, const Void_t*) )
#else
Void_t* tfind(key, rootp, comparf)
Void_t*		key;
Void_t**	rootp;
int(*		comparf)();
#endif
{
	reg Dt_t*	dt;
	reg Tree_t*	o;

	if(!rootp || !(dt = *((Dt_t**)rootp)) )
		return NIL(Void_t*);
	Treedisc.comparf = comparf;

	return (o = (Tree_t*)dtmatch(dt,key)) ? (Void_t*)(&o->key) : NIL(Void_t*);
}

/* the original tdelete() specifies that it will return the parent pointer
** in the tree if there is one. Since we are using a splay tree, a deleted
** node is always rotated to the root first. So this implementation always
** returns the key of the new root.
*/
extern
#if __STD_C
Void_t* tdelete(const Void_t* key, Void_t** rootp,
		int(*comparf)(const Void_t*, const Void_t*) )
#else
Void_t* tdelete(key, rootp, comparf)
Void_t*		key;
Void_t**	rootp;
int(*		comparf)();
#endif
{
	reg Dt_t*	dt;
	reg Tree_t*	o;
	Tree_t		obj;

	if(!rootp || !(dt = *((Dt_t**)rootp)) )
		return NIL(Void_t*);

	Treedisc.comparf = comparf;

	obj.key = (Void_t*)key;
	dtdelete(dt,&obj);

	if(!(o = dtfinger(dt)) )
	{	dtclose(dt);
		*rootp = NIL(Void_t*);
	}

	return o ? (Void_t*)(&o->key) : NIL(Void_t*);
}

/* the below routine assumes a particular layout of Dtlink_t.
** If this ever gets changed, this routine should be redone.
*/
#define lchild	link.hl._left
#define rchild	link.right

#if __STD_C
static void _twalk(Tree_t* obj, void(*action)(const Void_t*,VISIT,int), int level)
#else
static void _twalk(obj,action,level)
Tree_t*	obj;
void(*		action)();
int		level;
#endif
{	if(!obj->lchild && !obj->rchild)
		(*action)((Void_t*)obj,leaf,level);
	else
	{	(*action)((Void_t*)obj,preorder,level);
		if(obj->lchild)
			_twalk((Tree_t*)obj->lchild,action,level+1);
		(*action)((Void_t*)obj,postorder,level);
		if(obj->rchild)
			_twalk((Tree_t*)obj->rchild,action,level+1);
		(*action)((Void_t*)obj,endorder,level);
	}
}

/* the original twalk allows specifying arbitrary node to start traversal.
** Since our root is a dictionary structure, the search here will start
** at whichever node happens to be current root.
*/
extern
#if __STD_C
void twalk(const Void_t* root, void(*action)(const Void_t*,VISIT,int) )
#else
void twalk(root, action)
Void_t*	root;
void(*	action)();
#endif
{
	reg Tree_t*	o;

	if(root && (o = (Tree_t*)dtfinger((Dt_t*)root)) )
		_twalk(o,action,0);
}

#endif