SLinkedList_impl.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**
  2. * @file SLinkedList_impl.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include "SLinkedList_header.h"
  30. static SLinkedListEntry * SLinkedListNext (SLinkedListEntry *entry)
  31. {
  32. ASSERT(entry)
  33. return SLinkedList_next(entry);
  34. }
  35. static SLinkedListEntry * SLinkedListPrev (SLinkedListEntry *entry)
  36. {
  37. ASSERT(entry)
  38. return SLinkedList_prev(entry);
  39. }
  40. static void SLinkedListMarkRemoved (SLinkedListEntry *entry)
  41. {
  42. ASSERT(entry)
  43. SLinkedList_next(entry) = entry;
  44. }
  45. static int SLinkedListIsRemoved (SLinkedListEntry *entry)
  46. {
  47. ASSERT(entry)
  48. return (entry == SLinkedList_next(entry));
  49. }
  50. static void SLinkedList_Init (SLinkedList *o)
  51. {
  52. o->first = NULL;
  53. #if SLINKEDLIST_PARAM_FEATURE_LAST
  54. o->last = NULL;
  55. #endif
  56. }
  57. static void SLinkedList_Prepend (SLinkedList *o, SLinkedListEntry *entry)
  58. {
  59. ASSERT(entry)
  60. SLinkedList_prev(entry) = NULL;
  61. SLinkedList_next(entry) = o->first;
  62. if (o->first) {
  63. SLinkedList_prev(o->first) = entry;
  64. } else {
  65. #if SLINKEDLIST_PARAM_FEATURE_LAST
  66. o->last = entry;
  67. #endif
  68. }
  69. o->first = entry;
  70. }
  71. #if SLINKEDLIST_PARAM_FEATURE_LAST
  72. static void SLinkedList_Append (SLinkedList *o, SLinkedListEntry *entry)
  73. {
  74. ASSERT(entry)
  75. SLinkedList_next(entry) = NULL;
  76. SLinkedList_prev(entry) = o->last;
  77. if (o->last) {
  78. SLinkedList_next(o->last) = entry;
  79. } else {
  80. o->first = entry;
  81. }
  82. o->last = entry;
  83. }
  84. #endif
  85. static void SLinkedList_InsertBefore (SLinkedList *o, SLinkedListEntry *entry, SLinkedListEntry *before_entry)
  86. {
  87. ASSERT(entry)
  88. ASSERT(before_entry)
  89. SLinkedList_prev(entry) = SLinkedList_prev(before_entry);
  90. SLinkedList_next(entry) = before_entry;
  91. if (SLinkedList_prev(before_entry)) {
  92. SLinkedList_next(SLinkedList_prev(before_entry)) = entry;
  93. } else {
  94. o->first = entry;
  95. }
  96. SLinkedList_prev(before_entry) = entry;
  97. }
  98. static void SLinkedList_InsertAfter (SLinkedList *o, SLinkedListEntry *entry, SLinkedListEntry *after_entry)
  99. {
  100. ASSERT(entry)
  101. ASSERT(after_entry)
  102. SLinkedList_next(entry) = SLinkedList_next(after_entry);
  103. SLinkedList_prev(entry) = after_entry;
  104. if (SLinkedList_next(after_entry)) {
  105. SLinkedList_prev(SLinkedList_next(after_entry)) = entry;
  106. } else {
  107. #if SLINKEDLIST_PARAM_FEATURE_LAST
  108. o->last = entry;
  109. #endif
  110. }
  111. SLinkedList_next(after_entry) = entry;
  112. }
  113. static void SLinkedList_Remove (SLinkedList *o, SLinkedListEntry *entry)
  114. {
  115. if (SLinkedList_prev(entry)) {
  116. SLinkedList_next(SLinkedList_prev(entry)) = SLinkedList_next(entry);
  117. } else {
  118. o->first = SLinkedList_next(entry);
  119. }
  120. if (SLinkedList_next(entry)) {
  121. SLinkedList_prev(SLinkedList_next(entry)) = SLinkedList_prev(entry);
  122. } else {
  123. #if SLINKEDLIST_PARAM_FEATURE_LAST
  124. o->last = SLinkedList_prev(entry);
  125. #endif
  126. }
  127. }
  128. static void SLinkedList_RemoveFirst (SLinkedList *o)
  129. {
  130. ASSERT(o->first)
  131. o->first = SLinkedList_next(o->first);
  132. if (o->first) {
  133. SLinkedList_prev(o->first) = NULL;
  134. } else {
  135. #if SLINKEDLIST_PARAM_FEATURE_LAST
  136. o->last = NULL;
  137. #endif
  138. }
  139. }
  140. #if SLINKEDLIST_PARAM_FEATURE_LAST
  141. static void SLinkedList_RemoveLast (SLinkedList *o)
  142. {
  143. ASSERT(o->last)
  144. o->last = SLinkedList_prev(o->last);
  145. if (o->last) {
  146. SLinkedList_next(o->last) = NULL;
  147. } else {
  148. o->first = NULL;
  149. }
  150. }
  151. #endif
  152. static SLinkedListEntry * SLinkedList_First (const SLinkedList *o)
  153. {
  154. return o->first;
  155. }
  156. #if SLINKEDLIST_PARAM_FEATURE_LAST
  157. static SLinkedListEntry * SLinkedList_Last (const SLinkedList *o)
  158. {
  159. return o->last;
  160. }
  161. #endif
  162. static int SLinkedList_IsEmpty (const SLinkedList *o)
  163. {
  164. return !o->first;
  165. }
  166. #include "SLinkedList_footer.h"