SLinkedList_impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 SLinkedList_Init (SLinkedList *o)
  41. {
  42. o->first = NULL;
  43. #if SLINKEDLIST_PARAM_FEATURE_LAST
  44. o->last = NULL;
  45. #endif
  46. }
  47. static void SLinkedList_Prepend (SLinkedList *o, SLinkedListEntry *entry)
  48. {
  49. ASSERT(entry)
  50. SLinkedList_prev(entry) = NULL;
  51. SLinkedList_next(entry) = o->first;
  52. if (o->first) {
  53. SLinkedList_prev(o->first) = entry;
  54. } else {
  55. #if SLINKEDLIST_PARAM_FEATURE_LAST
  56. o->last = entry;
  57. #endif
  58. }
  59. o->first = entry;
  60. }
  61. #if SLINKEDLIST_PARAM_FEATURE_LAST
  62. static void SLinkedList_Append (SLinkedList *o, SLinkedListEntry *entry)
  63. {
  64. ASSERT(entry)
  65. SLinkedList_next(entry) = NULL;
  66. SLinkedList_prev(entry) = o->last;
  67. if (o->last) {
  68. SLinkedList_next(o->last) = entry;
  69. } else {
  70. o->first = entry;
  71. }
  72. o->last = entry;
  73. }
  74. #endif
  75. static void SLinkedList_InsertBefore (SLinkedList *o, SLinkedListEntry *entry, SLinkedListEntry *before_entry)
  76. {
  77. ASSERT(entry)
  78. ASSERT(before_entry)
  79. SLinkedList_prev(entry) = SLinkedList_prev(before_entry);
  80. SLinkedList_next(entry) = before_entry;
  81. if (SLinkedList_prev(before_entry)) {
  82. SLinkedList_next(SLinkedList_prev(before_entry)) = entry;
  83. } else {
  84. o->first = entry;
  85. }
  86. SLinkedList_prev(before_entry) = entry;
  87. }
  88. static void SLinkedList_InsertAfter (SLinkedList *o, SLinkedListEntry *entry, SLinkedListEntry *after_entry)
  89. {
  90. ASSERT(entry)
  91. ASSERT(after_entry)
  92. SLinkedList_next(entry) = SLinkedList_next(after_entry);
  93. SLinkedList_prev(entry) = after_entry;
  94. if (SLinkedList_next(after_entry)) {
  95. SLinkedList_prev(SLinkedList_next(after_entry)) = entry;
  96. } else {
  97. #if SLINKEDLIST_PARAM_FEATURE_LAST
  98. o->last = entry;
  99. #endif
  100. }
  101. SLinkedList_next(after_entry) = entry;
  102. }
  103. static void SLinkedList_Remove (SLinkedList *o, SLinkedListEntry *entry)
  104. {
  105. if (SLinkedList_prev(entry)) {
  106. SLinkedList_next(SLinkedList_prev(entry)) = SLinkedList_next(entry);
  107. } else {
  108. o->first = SLinkedList_next(entry);
  109. }
  110. if (SLinkedList_next(entry)) {
  111. SLinkedList_prev(SLinkedList_next(entry)) = SLinkedList_prev(entry);
  112. } else {
  113. #if SLINKEDLIST_PARAM_FEATURE_LAST
  114. o->last = SLinkedList_prev(entry);
  115. #endif
  116. }
  117. }
  118. static SLinkedListEntry * SLinkedList_First (const SLinkedList *o)
  119. {
  120. return o->first;
  121. }
  122. #if SLINKEDLIST_PARAM_FEATURE_LAST
  123. static SLinkedListEntry * SLinkedList_Last (const SLinkedList *o)
  124. {
  125. return o->last;
  126. }
  127. #endif
  128. static int SLinkedList_IsEmpty (const SLinkedList *o)
  129. {
  130. return !o->first;
  131. }
  132. #include "SLinkedList_footer.h"