1da2e3ebchin/***********************************************************************
2da2e3ebchin*                                                                      *
3da2e3ebchin*               This software is part of the ast package               *
43e14f97Roger A. Faulkner*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebchin*                      and is licensed under the                       *
6da2e3ebchin*                  Common Public License, Version 1.0                  *
77c2fbfbApril Chin*                    by AT&T Intellectual Property                     *
8da2e3ebchin*                                                                      *
9da2e3ebchin*                A copy of the License is available at                 *
10da2e3ebchin*            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebchin*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebchin*                                                                      *
13da2e3ebchin*              Information and Software Systems Research               *
14da2e3ebchin*                            AT&T Research                             *
15da2e3ebchin*                           Florham Park NJ                            *
16da2e3ebchin*                                                                      *
17da2e3ebchin*                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebchin*                  David Korn <dgk@research.att.com>                   *
19da2e3ebchin*                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebchin*                                                                      *
21da2e3ebchin***********************************************************************/
22da2e3ebchin#include	"dthdr.h"
23da2e3ebchin
24da2e3ebchin/*	Change discipline.
25da2e3ebchin**	dt :	dictionary
26da2e3ebchin**	disc :	discipline
27da2e3ebchin**
28da2e3ebchin**	Written by Kiem-Phong Vo (5/26/96)
29da2e3ebchin*/
30da2e3ebchin
31da2e3ebchin#if __STD_C
32da2e3ebchinstatic Void_t* dtmemory(Dt_t* dt,Void_t* addr,size_t size,Dtdisc_t* disc)
33da2e3ebchin#else
34da2e3ebchinstatic Void_t* dtmemory(dt, addr, size, disc)
35da2e3ebchinDt_t* 		dt;	/* dictionary			*/
36da2e3ebchinVoid_t* 	addr;	/* address to be manipulate	*/
37da2e3ebchinsize_t		size;	/* size to obtain		*/
38da2e3ebchinDtdisc_t* 	disc;	/* discipline			*/
39da2e3ebchin#endif
40da2e3ebchin{
41da2e3ebchin	if(addr)
42da2e3ebchin	{	if(size == 0)
43da2e3ebchin		{	free(addr);
44da2e3ebchin			return NIL(Void_t*);
45da2e3ebchin		}
46da2e3ebchin		else	return realloc(addr,size);
47da2e3ebchin	}
48da2e3ebchin	else	return size > 0 ? malloc(size) : NIL(Void_t*);
49da2e3ebchin}
50da2e3ebchin
51da2e3ebchin#if __STD_C
52da2e3ebchinDtdisc_t* dtdisc(Dt_t* dt, Dtdisc_t* disc, int type)
53da2e3ebchin#else
54da2e3ebchinDtdisc_t* dtdisc(dt,disc,type)
55da2e3ebchinDt_t*		dt;
56da2e3ebchinDtdisc_t*	disc;
57da2e3ebchinint		type;
58da2e3ebchin#endif
59da2e3ebchin{
60da2e3ebchin	reg Dtsearch_f	searchf;
61da2e3ebchin	reg Dtlink_t	*r, *t;
62da2e3ebchin	reg char*	k;
63da2e3ebchin	reg Dtdisc_t*	old;
64da2e3ebchin
65da2e3ebchin	if(!(old = dt->disc) )	/* initialization call from dtopen() */
66da2e3ebchin	{	dt->disc = disc;
67da2e3ebchin		if(!(dt->memoryf = disc->memoryf) )
68da2e3ebchin			dt->memoryf = dtmemory;
69da2e3ebchin		return disc;
70da2e3ebchin	}
71da2e3ebchin
72da2e3ebchin	if(!disc)	/* only want to know current discipline */
73da2e3ebchin		return old;
74da2e3ebchin
75da2e3ebchin	searchf = dt->meth->searchf;
76da2e3ebchin
77da2e3ebchin	UNFLATTEN(dt);
78da2e3ebchin
79da2e3ebchin	if(old->eventf && (*old->eventf)(dt,DT_DISC,(Void_t*)disc,old) < 0)
80da2e3ebchin		return NIL(Dtdisc_t*);
81da2e3ebchin
82da2e3ebchin	dt->disc = disc;
83da2e3ebchin	if(!(dt->memoryf = disc->memoryf) )
84da2e3ebchin		dt->memoryf = dtmemory;
85da2e3ebchin
86da2e3ebchin	if(dt->data->type&(DT_STACK|DT_QUEUE|DT_LIST))
87da2e3ebchin		goto done;
88da2e3ebchin	else if(dt->data->type&DT_BAG)
89da2e3ebchin	{	if(type&DT_SAMEHASH)
90da2e3ebchin			goto done;
91da2e3ebchin		else	goto dt_renew;
92da2e3ebchin	}
93da2e3ebchin	else if(dt->data->type&(DT_SET|DT_BAG))
94da2e3ebchin	{	if((type&DT_SAMEHASH) && (type&DT_SAMECMP))
95da2e3ebchin			goto done;
96da2e3ebchin		else	goto dt_renew;
97da2e3ebchin	}
98da2e3ebchin	else /*if(dt->data->type&(DT_OSET|DT_OBAG))*/
99da2e3ebchin	{	if(type&DT_SAMECMP)
100da2e3ebchin			goto done;
101da2e3ebchin	dt_renew:
102da2e3ebchin		r = dtflatten(dt);
103da2e3ebchin		dt->data->type &= ~DT_FLATTEN;
104da2e3ebchin		dt->data->here = NIL(Dtlink_t*);
105da2e3ebchin		dt->data->size = 0;
106da2e3ebchin
107da2e3ebchin		if(dt->data->type&(DT_SET|DT_BAG))
108da2e3ebchin		{	reg Dtlink_t	**s, **ends;
109da2e3ebchin			ends = (s = dt->data->htab) + dt->data->ntab;
110da2e3ebchin			while(s < ends)
111da2e3ebchin				*s++ = NIL(Dtlink_t*);
112da2e3ebchin		}
113da2e3ebchin
114da2e3ebchin		/* reinsert them */
115da2e3ebchin		while(r)
116da2e3ebchin		{	t = r->right;
117da2e3ebchin			if(!(type&DT_SAMEHASH))	/* new hash value */
118da2e3ebchin			{	k = (char*)_DTOBJ(r,disc->link);
119da2e3ebchin				k = _DTKEY((Void_t*)k,disc->key,disc->size);
120da2e3ebchin				r->hash = _DTHSH(dt,k,disc,disc->size);
121da2e3ebchin			}
122da2e3ebchin			(void)(*searchf)(dt,(Void_t*)r,DT_RENEW);
123da2e3ebchin			r = t;
124da2e3ebchin		}
125da2e3ebchin	}
126da2e3ebchin
127da2e3ebchindone:
128da2e3ebchin	return old;
129da2e3ebchin}
130