dtrestore.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <cdt/dthdr.h>
  2. #include <stddef.h>
  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. int dtrestore(Dt_t* dt, Dtlink_t* list)
  10. {
  11. Dtlink_t *t, **s, **ends;
  12. int type;
  13. Dtsearch_f searchf = dt->meth->searchf;
  14. type = dt->data->type&DT_FLATTEN;
  15. if(!list) /* restoring a flattened dictionary */
  16. { if(!type)
  17. return -1;
  18. list = dt->data->here;
  19. }
  20. else /* restoring an extracted list of elements */
  21. { if(dt->data->size != 0)
  22. return -1;
  23. type = 0;
  24. }
  25. dt->data->type &= ~DT_FLATTEN;
  26. if(dt->data->type&DT_SET)
  27. { dt->data->here = NULL;
  28. if(type) /* restoring a flattened dictionary */
  29. { for(ends = (s = dt->data->htab) + dt->data->ntab; s < ends; ++s)
  30. { if((t = *s) )
  31. { *s = list;
  32. list = t->right;
  33. t->right = NULL;
  34. }
  35. }
  36. }
  37. else /* restoring an extracted list of elements */
  38. { dt->data->size = 0;
  39. while(list)
  40. { t = list->right;
  41. searchf(dt, list, DT_RENEW);
  42. list = t;
  43. }
  44. }
  45. }
  46. else
  47. { if(dt->data->type&(DT_OSET|DT_OBAG))
  48. dt->data->here = list;
  49. else /*if(dt->data->type&(DT_LIST|DT_STACK|DT_QUEUE))*/
  50. { dt->data->here = NULL;
  51. dt->data->head = list;
  52. }
  53. if(!type)
  54. dt->data->size = -1;
  55. }
  56. return 0;
  57. }