1 | #include "dthdr.h" |
---|---|
2 | |
3 | /* Restore dictionary from given tree or list of elements. |
4 | ** There are two cases. If called from within, list is nil. |
5 | ** From without, list is not nil and data->size must be 0. |
6 | ** |
7 | ** Written by Kiem-Phong Vo (5/25/96) |
8 | */ |
9 | |
10 | int dtrestore(reg Dt_t* dt, reg Dtlink_t* list) |
11 | { |
12 | reg Dtlink_t *t, **s, **ends; |
13 | reg int type; |
14 | reg Dtsearch_f searchf = dt->meth->searchf; |
15 | |
16 | type = dt->data->type&DT_FLATTEN; |
17 | if(!list) /* restoring a flattened dictionary */ |
18 | { if(!type) |
19 | return -1; |
20 | list = dt->data->here; |
21 | } |
22 | else /* restoring an extracted list of elements */ |
23 | { if(dt->data->size != 0) |
24 | return -1; |
25 | type = 0; |
26 | } |
27 | dt->data->type &= ~DT_FLATTEN; |
28 | |
29 | if(dt->data->type&(DT_SET|DT_BAG)) |
30 | { dt->data->here = NIL(Dtlink_t*); |
31 | if(type) /* restoring a flattened dictionary */ |
32 | { for(ends = (s = dt->data->htab) + dt->data->ntab; s < ends; ++s) |
33 | { if((t = *s) ) |
34 | { *s = list; |
35 | list = t->right; |
36 | t->right = NIL(Dtlink_t*); |
37 | } |
38 | } |
39 | } |
40 | else /* restoring an extracted list of elements */ |
41 | { dt->data->size = 0; |
42 | while(list) |
43 | { t = list->right; |
44 | (*searchf)(dt,(void*)list,DT_RENEW); |
45 | list = t; |
46 | } |
47 | } |
48 | } |
49 | else |
50 | { if(dt->data->type&(DT_OSET|DT_OBAG)) |
51 | dt->data->here = list; |
52 | else /*if(dt->data->type&(DT_LIST|DT_STACK|DT_QUEUE))*/ |
53 | { dt->data->here = NIL(Dtlink_t*); |
54 | dt->data->head = list; |
55 | } |
56 | if(!type) |
57 | dt->data->size = -1; |
58 | } |
59 | |
60 | return 0; |
61 | } |
62 |