SLinkedList_impl.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 void SLinkedListMarkRemoved (SLinkedListEntry *entry)
  31. {
  32. ASSERT(entry)
  33. SLinkedList_next(entry) = entry;
  34. }
  35. static int SLinkedListIsRemoved (SLinkedListEntry *entry)
  36. {
  37. ASSERT(entry)
  38. return (entry == SLinkedList_next(entry));
  39. }
  40. static void SLinkedList_Init (SLinkedList *o)
  41. {
  42. o->first = NULL;
  43. }
  44. static SLinkedListEntry * SLinkedList_Next (SLinkedList *o, SLinkedListEntry *entry)
  45. {
  46. ASSERT(entry)
  47. return SLinkedList_next(entry);
  48. }
  49. static SLinkedListEntry * SLinkedList_Prev (SLinkedList *o, SLinkedListEntry *entry)
  50. {
  51. ASSERT(entry)
  52. return (entry == o->first) ? NULL : SLinkedList_prev(entry);
  53. }
  54. static void SLinkedList_Prepend (SLinkedList *o, SLinkedListEntry *entry)
  55. {
  56. ASSERT(entry)
  57. SLinkedList_next(entry) = o->first;
  58. if (o->first) {
  59. SLinkedList_prev(o->first) = entry;
  60. } else {
  61. #if SLINKEDLIST_PARAM_FEATURE_LAST
  62. o->last = entry;
  63. #endif
  64. }
  65. o->first = entry;
  66. }
  67. #if SLINKEDLIST_PARAM_FEATURE_LAST
  68. static void SLinkedList_Append (SLinkedList *o, SLinkedListEntry *entry)
  69. {
  70. ASSERT(entry)
  71. SLinkedList_next(entry) = NULL;
  72. if (o->first) {
  73. SLinkedList_prev(entry) = o->last;
  74. SLinkedList_next(o->last) = entry;
  75. } else {
  76. o->first = entry;
  77. }
  78. o->last = entry;
  79. }
  80. #endif
  81. static void SLinkedList_InsertBefore (SLinkedList *o, SLinkedListEntry *entry, SLinkedListEntry *before_entry)
  82. {
  83. ASSERT(entry)
  84. ASSERT(before_entry)
  85. SLinkedList_next(entry) = before_entry;
  86. if (before_entry != o->first) {
  87. SLinkedList_prev(entry) = SLinkedList_prev(before_entry);
  88. SLinkedList_next(SLinkedList_prev(before_entry)) = entry;
  89. } else {
  90. o->first = entry;
  91. }
  92. SLinkedList_prev(before_entry) = entry;
  93. }
  94. static void SLinkedList_InsertAfter (SLinkedList *o, SLinkedListEntry *entry, SLinkedListEntry *after_entry)
  95. {
  96. ASSERT(entry)
  97. ASSERT(after_entry)
  98. SLinkedList_next(entry) = SLinkedList_next(after_entry);
  99. SLinkedList_prev(entry) = after_entry;
  100. if (SLinkedList_next(after_entry)) {
  101. SLinkedList_prev(SLinkedList_next(after_entry)) = entry;
  102. } else {
  103. #if SLINKEDLIST_PARAM_FEATURE_LAST
  104. o->last = entry;
  105. #endif
  106. }
  107. SLinkedList_next(after_entry) = entry;
  108. }
  109. static void SLinkedList_Remove (SLinkedList *o, SLinkedListEntry *entry)
  110. {
  111. if (entry != o->first) {
  112. SLinkedList_next(SLinkedList_prev(entry)) = SLinkedList_next(entry);
  113. if (SLinkedList_next(entry)) {
  114. SLinkedList_prev(SLinkedList_next(entry)) = SLinkedList_prev(entry);
  115. } else {
  116. #if SLINKEDLIST_PARAM_FEATURE_LAST
  117. o->last = SLinkedList_prev(entry);
  118. #endif
  119. }
  120. } else {
  121. o->first = SLinkedList_next(entry);
  122. }
  123. }
  124. static void SLinkedList_RemoveFirst (SLinkedList *o)
  125. {
  126. ASSERT(o->first)
  127. o->first = SLinkedList_next(o->first);
  128. }
  129. #if SLINKEDLIST_PARAM_FEATURE_LAST
  130. static void SLinkedList_RemoveLast (SLinkedList *o)
  131. {
  132. ASSERT(o->first)
  133. if (o->last == o->first) {
  134. o->first = NULL;
  135. } else {
  136. SLinkedList_next(SLinkedList_prev(o->last)) = NULL;
  137. o->last = SLinkedList_prev(o->last);
  138. }
  139. }
  140. #endif
  141. static SLinkedListEntry * SLinkedList_First (const SLinkedList *o)
  142. {
  143. return o->first;
  144. }
  145. #if SLINKEDLIST_PARAM_FEATURE_LAST
  146. static SLinkedListEntry * SLinkedList_Last (const SLinkedList *o)
  147. {
  148. return (!o->first ? NULL : o->last);
  149. }
  150. #endif
  151. static int SLinkedList_IsEmpty (const SLinkedList *o)
  152. {
  153. return !o->first;
  154. }
  155. #include "SLinkedList_footer.h"