Graphviz  2.41.20171026.1811
dtdisc.c
Go to the documentation of this file.
1 #include "dthdr.h"
2 
3 /* Change discipline.
4 ** dt : dictionary
5 ** disc : discipline
6 **
7 ** Written by Kiem-Phong Vo (5/26/96)
8 */
9 
10 static void* dtmemory(Dt_t* dt,void* addr,size_t size,Dtdisc_t* disc)
11 {
12  if(addr)
13  { if(size == 0)
14  { free(addr);
15  return NIL(void*);
16  }
17  else return realloc(addr,size);
18  }
19  else return size > 0 ? malloc(size) : NIL(void*);
20 }
21 
22 Dtdisc_t* dtdisc(Dt_t* dt, Dtdisc_t* disc, int type)
23 {
24  reg Dtsearch_f searchf;
25  reg Dtlink_t *r, *t;
26  reg char* k;
27  reg Dtdisc_t* old;
28 
29  if(!(old = dt->disc) ) /* initialization call from dtopen() */
30  { dt->disc = disc;
31  if(!(dt->memoryf = disc->memoryf) )
32  dt->memoryf = dtmemory;
33  return disc;
34  }
35 
36  if(!disc) /* only want to know current discipline */
37  return old;
38 
39  searchf = dt->meth->searchf;
40 
41  UNFLATTEN(dt);
42 
43  if(old->eventf && (*old->eventf)(dt,DT_DISC,(void*)disc,old) < 0)
44  return NIL(Dtdisc_t*);
45 
46  dt->disc = disc;
47  if(!(dt->memoryf = disc->memoryf) )
48  dt->memoryf = dtmemory;
49 
50  if(dt->data->type&(DT_STACK|DT_QUEUE|DT_LIST))
51  goto done;
52  else if(dt->data->type&DT_BAG)
53  { if(type&DT_SAMEHASH)
54  goto done;
55  else goto dt_renew;
56  }
57  else if(dt->data->type&(DT_SET|DT_BAG))
58  { if((type&DT_SAMEHASH) && (type&DT_SAMECMP))
59  goto done;
60  else goto dt_renew;
61  }
62  else /*if(dt->data->type&(DT_OSET|DT_OBAG))*/
63  { if(type&DT_SAMECMP)
64  goto done;
65  dt_renew:
66  r = dtflatten(dt);
67  dt->data->type &= ~DT_FLATTEN;
68  dt->data->here = NIL(Dtlink_t*);
69  dt->data->size = 0;
70 
71  if(dt->data->type&(DT_SET|DT_BAG))
72  { reg Dtlink_t **s, **ends;
73  ends = (s = dt->data->htab) + dt->data->ntab;
74  while(s < ends)
75  *s++ = NIL(Dtlink_t*);
76  }
77 
78  /* reinsert them */
79  while(r)
80  { t = r->right;
81  if(!(type&DT_SAMEHASH)) /* new hash value */
82  { k = (char*)_DTOBJ(r,disc->link);
83  k = _DTKEY((void*)k,disc->key,disc->size);
84  r->hash = _DTHSH(dt,k,disc,disc->size);
85  }
86  (void)(*searchf)(dt,(void*)r,DT_RENEW);
87  r = t;
88  }
89  }
90 
91 done:
92  return old;
93 }
Dtdisc_t * disc
Definition: cdt.h:101
int link
Definition: cdt.h:83
int key
Definition: cdt.h:81
CDT_API Dtlink_t * dtflatten(Dt_t *)
Definition: dtflatten.c:9
Dtmemory_f memoryf
Definition: cdt.h:88
#define reg
Definition: dthdr.h:14
CDT_API Dtdisc_t * dtdisc(Dt_t *dt, Dtdisc_t *, int)
Definition: dtdisc.c:22
Definition: cdt.h:80
#define DT_SET
Definition: cdt.h:125
#define DT_BAG
Definition: cdt.h:126
#define DT_QUEUE
Definition: cdt.h:131
int size
Definition: cdt.h:73
#define NIL(t)
Definition: dthdr.h:13
#define _DTHSH(dt, ky, dc, sz)
Definition: cdt.h:214
#define DT_DISC
Definition: cdt.h:158
int size
Definition: cdt.h:82
#define DT_STACK
Definition: cdt.h:130
Definition: grammar.c:79
void *(* Dtsearch_f)(Dt_t *, void *, int)
Definition: cdt.h:37
#define DT_LIST
Definition: cdt.h:129
Dtsearch_f searchf
Definition: cdt.h:60
#define DT_SAMECMP
Definition: cdt.h:136
#define UNFLATTEN(dt)
Definition: dthdr.h:38
Dtlink_t * here
Definition: cdt.h:67
Dtmethod_t * meth
Definition: cdt.h:104
#define DT_FLATTEN
Definition: dthdr.h:22
int ntab
Definition: cdt.h:72
Definition: cdt.h:99
Dtdata_t * data
Definition: cdt.h:102
#define _DTKEY(o, ky, sz)
Definition: cdt.h:209
int type
Definition: cdt.h:66
#define _DTOBJ(e, lk)
Definition: cdt.h:208
#define DT_SAMEHASH
Definition: cdt.h:137
Dtmemory_f memoryf
Definition: cdt.h:103
#define DT_RENEW
Definition: cdt.h:145