| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include <cdt/dthdr.h> |
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
/* Change search method. |
| 7 |
** |
| 8 |
** Written by Kiem-Phong Vo (05/25/96) |
| 9 |
*/ |
| 10 |
|
| 11 |
Dtmethod_t* dtmethod(Dt_t* dt, Dtmethod_t* meth) |
| 12 |
{ |
| 13 |
Dtlink_t *list, *r; |
| 14 |
Dtdisc_t* disc = dt->disc; |
| 15 |
Dtmethod_t* oldmeth = dt->meth; |
| 16 |
|
| 17 |
if(!meth || meth->type == oldmeth->type) |
| 18 |
return oldmeth; |
| 19 |
|
| 20 |
/* get the list of elements */ |
| 21 |
list = dtflatten(dt); |
| 22 |
|
| 23 |
if (dt->data.type & DT_SET) |
| 24 |
{ if (dt->data.ntab > 0) |
| 25 |
free(dt->data.htab); |
| 26 |
dt->data.ntab = 0; |
| 27 |
dt->data.htab = NULL; |
| 28 |
} |
| 29 |
|
| 30 |
dt->data.here = NULL; |
| 31 |
dt->data.type = (dt->data.type & ~(DT_METHODS|DT_FLATTEN)) | meth->type; |
| 32 |
dt->meth = meth; |
| 33 |
if(dt->searchf == oldmeth->searchf) |
| 34 |
dt->searchf = meth->searchf; |
| 35 |
|
| 36 |
if(meth->type&(DT_OSET|DT_OBAG)) |
| 37 |
{ dt->data.size = 0; |
| 38 |
while(list) |
| 39 |
{ r = list->right; |
| 40 |
meth->searchf(dt, list, DT_RENEW); |
| 41 |
list = r; |
| 42 |
} |
| 43 |
} |
| 44 |
else if(oldmeth->type&DT_SET) |
| 45 |
{ int rehash; |
| 46 |
if((meth->type&DT_SET) && !(oldmeth->type&DT_SET)) |
| 47 |
rehash = 1; |
| 48 |
else rehash = 0; |
| 49 |
|
| 50 |
dt->data.size = 0; |
| 51 |
dt->data.loop = 0; |
| 52 |
while(list) |
| 53 |
{ r = list->right; |
| 54 |
if(rehash) |
| 55 |
{ void* key = _DTOBJ(list,disc->link); |
| 56 |
key = _DTKEY(key,disc->key,disc->size); |
| 57 |
list->hash = dtstrhash(key, disc->size); |
| 58 |
} |
| 59 |
(void)meth->searchf(dt, list, DT_RENEW); |
| 60 |
list = r; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return oldmeth; |
| 65 |
} |