list_common.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*!
  2. Temelia - List common interface.
  3. Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).
  4. @author Dascalu Laurentiu, Macovei Alexandru
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. /*
  18. * This file is used to define macros used in linked_list and
  19. * doubly_linked_list to avoid duplicating code.
  20. */
  21. /*
  22. * Confucius says:
  23. * He who wants to properly debug this mess must either copy the code over in
  24. * the source file to set breakpoints there, then come back here and redo the
  25. * macro, or, set breakpoints in the preprocessed code, then also come back
  26. * here and redo the macro.
  27. *
  28. * And Confucius may be wrong here.
  29. */
  30. #ifndef LISTCOMMON_H_
  31. #define LISTCOMMON_H_
  32. #include "common.h"
  33. #include "iterator.h"
  34. /*!
  35. * @param Type either linked_list or doubly_linked_list
  36. */
  37. #define LIST_NEW(type, L)\
  38. do\
  39. {\
  40. L = (struct _##type *) _new(sizeof(struct _##type));\
  41. _ASSERT(L, ==, NULL, NULL_POINTER, NULL);\
  42. L->size = 0;\
  43. L->begin = NULL;\
  44. L->end = NULL;\
  45. }while(0)\
  46. /*!
  47. * @param Type either linked_list or doubly_linked_list
  48. */
  49. #define LIST_DELETE(L, crt, prc, type)\
  50. do\
  51. {\
  52. _ASSERT(L, ==, NULL, NULL_POINTER,);\
  53. crt = L->begin;\
  54. while (crt != NULL)\
  55. {\
  56. prc = crt;\
  57. crt = type##_get_next(crt);\
  58. type##_delete(prc);\
  59. }\
  60. _delete(L);\
  61. }while(0)\
  62. /*!
  63. * @param Type either linked_list or doubly_linked_list
  64. */
  65. #define LIST_SORT(L, it, aux, compare, context, type)\
  66. do\
  67. {\
  68. int ordonat;\
  69. _ASSERT(L, ==, NULL, NULL_POINTER,);\
  70. _ASSERT(compare, ==, NULL, NULL_POINTER,);\
  71. do\
  72. {\
  73. ordonat = 0;\
  74. for (it = L->begin; type##_get_next(it) != NULL; it = type##_get_next(it))\
  75. {\
  76. if (compare(type##_get_key(it), type##_get_key(type##_get_next(it)), context) > 0)\
  77. {\
  78. aux = type##_get_key(it);\
  79. type##_set_key(it, type##_get_key(type##_get_next(it)));\
  80. type##_set_key(type##_get_next(it), aux);\
  81. ordonat = 1;\
  82. }\
  83. }\
  84. } while (ordonat);\
  85. } while(0)\
  86. #define LIST_MERGE(L3, it1, it2, key1, key2, type, compare, context)\
  87. do\
  88. {\
  89. for (; it1 && it2;)\
  90. {\
  91. key1 = type##_iterator_get_key(it1);\
  92. key2 = type##_iterator_get_key(it2);\
  93. \
  94. if (compare(key1, key2, context) < 0)\
  95. {\
  96. type##_push_back(L3, key1);\
  97. it1 = type##_iterator_get_next(it1);\
  98. }\
  99. else\
  100. {\
  101. type##_push_back(L3, key2);\
  102. it2 = type##_iterator_get_next(it2);\
  103. }\
  104. }\
  105. \
  106. for (; it1; it1 = type##_iterator_get_next(it1))\
  107. type##_push_back(L3, type##_iterator_get_key(it1));\
  108. \
  109. for (; it2; it2 = type##_iterator_get_next(it2))\
  110. type##_push_back(L3, type##_iterator_get_key(it2));\
  111. \
  112. }while(0)\
  113. #endif /* LISTCOMMON_H_ */