erplt.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // table operations:
  2. unsigned symbols_are_equal (int* le, int* ri) {
  3. if (le[0] != ri[0])
  4. return 0;
  5. for (int a = 1; a <= le[0]; a++) {
  6. if (le[a] != ri[a])
  7. return 0;
  8. }
  9. return 1;
  10. }
  11. Table* table_lookup (Table* tab, int* key) {
  12. while (tab) {
  13. if (symbols_are_equal(tab->key, key))
  14. return tab;
  15. tab = tab->next;
  16. }
  17. return NULL;
  18. }
  19. Table* table_set (Table** tab, int* key, Atom* value) {
  20. Table *nt = table_lookup(*tab, key);
  21. if (!nt) {
  22. nt = (Table*)ALLOC(sizeof(Table));
  23. nt->key = duplicate_symbol(key);
  24. nt->next = *tab;
  25. *tab = nt;
  26. }
  27. nt->value = duplicate_atom(value);
  28. return nt;
  29. }
  30. void table_union (Table* start, Table** finish) {
  31. if (!start || !finish)
  32. _error("TABLE_UNION: Empty table");
  33. Table *start_p = start;
  34. while (start->next) {
  35. start = start->next;
  36. }
  37. start->next = *finish;
  38. *finish = start_p;
  39. }
  40. unsigned table_unset (Table** tab, int* key) {
  41. Table *prev = NULL;
  42. Table *nav = *tab;
  43. while (nav) {
  44. if (symbols_are_equal(nav->key, key)) {
  45. if (prev == NULL) {
  46. *tab = nav->next;
  47. } else {
  48. prev->next = nav->next;
  49. }
  50. nav->next = NULL;
  51. free_table(nav);
  52. return 1;
  53. }
  54. prev = nav;
  55. nav = nav->next;
  56. }
  57. return 0;
  58. }
  59. int table_size (Table* tab) {
  60. int size = 0;
  61. while (tab) {
  62. size ++;
  63. tab = tab->next;
  64. }
  65. return size;
  66. }