xref: /titanic_50/usr/src/lib/libast/common/cdt/dtmethod.c (revision 9584cebb1c69707f4c67306b661c2ed47d8676f1)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"dthdr.h"
23 
24 /*	Change search method.
25 **
26 **	Written by Kiem-Phong Vo (05/25/96)
27 */
28 
29 #if __STD_C
30 Dtmethod_t* dtmethod(Dt_t* dt, Dtmethod_t* meth)
31 #else
32 Dtmethod_t* dtmethod(dt, meth)
33 Dt_t*		dt;
34 Dtmethod_t*	meth;
35 #endif
36 {
37 	reg Dtlink_t	*list, *r;
38 	reg Dtdisc_t*	disc = dt->disc;
39 	reg Dtmethod_t*	oldmeth = dt->meth;
40 
41 	if(!meth || meth->type == oldmeth->type)
42 		return oldmeth;
43 
44 	if(disc->eventf &&
45 	   (*disc->eventf)(dt,DT_METH,(Void_t*)meth,disc) < 0)
46 		return NIL(Dtmethod_t*);
47 
48 	dt->data->minp = 0;
49 
50 	/* get the list of elements */
51 	list = dtflatten(dt);
52 
53 	if(dt->data->type&(DT_LIST|DT_STACK|DT_QUEUE) )
54 		dt->data->head = NIL(Dtlink_t*);
55 	else if(dt->data->type&(DT_SET|DT_BAG) )
56 	{	if(dt->data->ntab > 0)
57 			(*dt->memoryf)(dt,(Void_t*)dt->data->htab,0,disc);
58 		dt->data->ntab = 0;
59 		dt->data->htab = NIL(Dtlink_t**);
60 	}
61 
62 	dt->data->here = NIL(Dtlink_t*);
63 	dt->data->type = (dt->data->type&~(DT_METHODS|DT_FLATTEN)) | meth->type;
64 	dt->meth = meth;
65 	if(dt->searchf == oldmeth->searchf)
66 		dt->searchf = meth->searchf;
67 
68 	if(meth->type&(DT_LIST|DT_STACK|DT_QUEUE) )
69 	{	if(!(oldmeth->type&(DT_LIST|DT_STACK|DT_QUEUE)) )
70 		{	if((r = list) )
71 			{	reg Dtlink_t*	t;
72 				for(t = r->right; t; r = t, t = t->right )
73 					t->left = r;
74 				list->left = r;
75 			}
76 		}
77 		dt->data->head = list;
78 	}
79 	else if(meth->type&(DT_OSET|DT_OBAG))
80 	{	dt->data->size = 0;
81 		while(list)
82 		{	r = list->right;
83 			(*meth->searchf)(dt,(Void_t*)list,DT_RENEW);
84 			list = r;
85 		}
86 	}
87 	else if(!((meth->type&DT_BAG) && (oldmeth->type&DT_SET)) )
88 	{	int	rehash;
89 		if((meth->type&(DT_SET|DT_BAG)) && !(oldmeth->type&(DT_SET|DT_BAG)))
90 			rehash = 1;
91 		else	rehash = 0;
92 
93 		dt->data->size = dt->data->loop = 0;
94 		while(list)
95 		{	r = list->right;
96 			if(rehash)
97 			{	reg Void_t* key = _DTOBJ(list,disc->link);
98 				key = _DTKEY(key,disc->key,disc->size);
99 				list->hash = _DTHSH(dt,key,disc,disc->size);
100 			}
101 			(void)(*meth->searchf)(dt,(Void_t*)list,DT_RENEW);
102 			list = r;
103 		}
104 	}
105 
106 	return oldmeth;
107 }
108