iterator.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*!
  2. Temelia - Iterator implementation source file.
  3. Copyright (C) 2008, 2009 Ceata (http://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. #include <stdlib.h>
  18. #include "include/iterator.h"
  19. #include "include/common.h"
  20. struct _linked_list_iterator_t
  21. {
  22. /*! user's key */
  23. void *key;
  24. /*! reference to the next node */
  25. struct _linked_list_iterator_t *next;
  26. };
  27. struct _doubly_linked_list_iterator_t
  28. {
  29. /*! user's key */
  30. void *key;
  31. /*! reference to the next node */
  32. struct _doubly_linked_list_iterator_t *next;
  33. /*! reference to the previous node */
  34. struct _doubly_linked_list_iterator_t *prev;
  35. };
  36. linked_list_iterator_t linked_list_iterator_new(void *default_key,
  37. linked_list_iterator_t default_next)
  38. {
  39. linked_list_iterator_t it;
  40. it = (linked_list_iterator_t) _new(sizeof(struct _linked_list_iterator_t));
  41. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  42. it->key = default_key;
  43. it->next = default_next;
  44. return it;
  45. }
  46. void linked_list_iterator_delete(linked_list_iterator_t it)
  47. {
  48. _delete(it);
  49. }
  50. void *linked_list_iterator_get_key(linked_list_iterator_t it)
  51. {
  52. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  53. return it->key;
  54. }
  55. linked_list_iterator_t linked_list_iterator_get_next(linked_list_iterator_t it)
  56. {
  57. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  58. return it->next;
  59. }
  60. void linked_list_iterator_set_key(linked_list_iterator_t it, void *new_key)
  61. {
  62. _ASSERT(it, ==, NULL, NULL_POINTER,);
  63. it->key = new_key;
  64. }
  65. void linked_list_iterator_set_next(linked_list_iterator_t it,
  66. linked_list_iterator_t new_next)
  67. {
  68. _ASSERT(it, ==, NULL, NULL_POINTER,);
  69. it->next = new_next;
  70. }
  71. void linked_list_iterator_join(linked_list_iterator_t left,
  72. linked_list_iterator_t middle, linked_list_iterator_t right)
  73. {
  74. if (left)
  75. left->next = middle;
  76. if (middle)
  77. middle->next = right;
  78. }
  79. void linked_list_iterator_split(linked_list_iterator_t left,
  80. linked_list_iterator_t middle, linked_list_iterator_t right)
  81. {
  82. if (left)
  83. left->next = NULL;
  84. if (right)
  85. middle->next = NULL;
  86. }
  87. doubly_linked_list_iterator_t doubly_linked_list_iterator_new(void *key,
  88. doubly_linked_list_iterator_t default_prev,
  89. doubly_linked_list_iterator_t default_next)
  90. {
  91. doubly_linked_list_iterator_t it;
  92. it = _new(sizeof(struct _doubly_linked_list_iterator_t));
  93. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  94. it->key = key;
  95. it->prev = default_prev;
  96. it->next = default_next;
  97. return it;
  98. }
  99. void doubly_linked_list_iterator_delete(doubly_linked_list_iterator_t it)
  100. {
  101. _delete(it);
  102. }
  103. void *doubly_linked_list_iterator_get_key(doubly_linked_list_iterator_t it)
  104. {
  105. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  106. return it->key;
  107. }
  108. doubly_linked_list_iterator_t doubly_linked_list_iterator_get_prev(
  109. doubly_linked_list_iterator_t it)
  110. {
  111. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  112. return it->prev;
  113. }
  114. doubly_linked_list_iterator_t doubly_linked_list_iterator_get_next(
  115. doubly_linked_list_iterator_t it)
  116. {
  117. _ASSERT(it, ==, NULL, NULL_POINTER, NULL);
  118. return it->next;
  119. }
  120. void doubly_linked_list_iterator_set_key(doubly_linked_list_iterator_t it,
  121. void *key)
  122. {
  123. _ASSERT(it, ==, NULL, NULL_POINTER,);
  124. it->key = key;
  125. }
  126. void doubly_linked_list_iterator_set_prev(doubly_linked_list_iterator_t it,
  127. doubly_linked_list_iterator_t prev)
  128. {
  129. _ASSERT(it, ==, NULL, NULL_POINTER,);
  130. it->prev = prev;
  131. }
  132. void doubly_linked_list_iterator_set_next(doubly_linked_list_iterator_t it,
  133. doubly_linked_list_iterator_t next)
  134. {
  135. _ASSERT(it, ==, NULL, NULL_POINTER,);
  136. it->next = next;
  137. }
  138. void doubly_linked_list_iterator_join(doubly_linked_list_iterator_t left,
  139. doubly_linked_list_iterator_t middle,
  140. doubly_linked_list_iterator_t right)
  141. {
  142. if (left)
  143. left->next = middle;
  144. if (middle)
  145. {
  146. middle->prev = left;
  147. middle->next = right;
  148. }
  149. if (right)
  150. right->prev = middle;
  151. }
  152. void doubly_linked_list_iterator_spit(doubly_linked_list_iterator_t left,
  153. doubly_linked_list_iterator_t middle,
  154. doubly_linked_list_iterator_t right)
  155. {
  156. if (left)
  157. left->next = NULL;
  158. if (middle)
  159. middle->prev = middle->next = NULL;
  160. if (right)
  161. right->prev = NULL;
  162. }