xdelta3-list.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* xdelta3 - delta compression tools and library
  2. Copyright 2016 Joshua MacDonald
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __XDELTA3_LIST__
  14. #define __XDELTA3_LIST__
  15. #define XD3_MAKELIST(LTYPE,ETYPE,LNAME) \
  16. \
  17. static inline ETYPE* \
  18. LTYPE ## _entry (LTYPE* l) \
  19. { \
  20. return (ETYPE*) ((char*) l - (ptrdiff_t) &((ETYPE*) 0)->LNAME); \
  21. } \
  22. \
  23. static inline void \
  24. LTYPE ## _init (LTYPE *l) \
  25. { \
  26. l->next = l; \
  27. l->prev = l; \
  28. } \
  29. \
  30. static inline void \
  31. LTYPE ## _add (LTYPE *prev, LTYPE *next, LTYPE *ins) \
  32. { \
  33. next->prev = ins; \
  34. prev->next = ins; \
  35. ins->next = next; \
  36. ins->prev = prev; \
  37. } \
  38. \
  39. static inline void \
  40. LTYPE ## _push_back (LTYPE *l, ETYPE *i) \
  41. { \
  42. LTYPE ## _add (l->prev, l, & i->LNAME); \
  43. } \
  44. \
  45. static inline void \
  46. LTYPE ## _del (LTYPE *next, \
  47. LTYPE *prev) \
  48. { \
  49. next->prev = prev; \
  50. prev->next = next; \
  51. } \
  52. \
  53. static inline ETYPE* \
  54. LTYPE ## _remove (ETYPE *f) \
  55. { \
  56. LTYPE *i = f->LNAME.next; \
  57. LTYPE ## _del (f->LNAME.next, f->LNAME.prev); \
  58. return LTYPE ## _entry (i); \
  59. } \
  60. \
  61. static inline ETYPE* \
  62. LTYPE ## _pop_back (LTYPE *l) \
  63. { \
  64. LTYPE *i = l->prev; \
  65. LTYPE ## _del (i->next, i->prev); \
  66. return LTYPE ## _entry (i); \
  67. } \
  68. \
  69. static inline ETYPE* \
  70. LTYPE ## _pop_front (LTYPE *l) \
  71. { \
  72. LTYPE *i = l->next; \
  73. LTYPE ## _del (i->next, i->prev); \
  74. return LTYPE ## _entry (i); \
  75. } \
  76. \
  77. static inline int \
  78. LTYPE ## _empty (LTYPE *l) \
  79. { \
  80. return l == l->next; \
  81. } \
  82. \
  83. static inline ETYPE* \
  84. LTYPE ## _front (LTYPE *f) \
  85. { \
  86. return LTYPE ## _entry (f->next); \
  87. } \
  88. \
  89. static inline ETYPE* \
  90. LTYPE ## _back (LTYPE *f) \
  91. { \
  92. return LTYPE ## _entry (f->prev); \
  93. } \
  94. \
  95. static inline int \
  96. LTYPE ## _end (LTYPE *f, ETYPE *i) \
  97. { \
  98. return f == & i->LNAME; \
  99. } \
  100. \
  101. static inline ETYPE* \
  102. LTYPE ## _next (ETYPE *f) \
  103. { \
  104. return LTYPE ## _entry (f->LNAME.next); \
  105. } \
  106. \
  107. static inline usize_t \
  108. LTYPE ## _length (LTYPE *l) \
  109. { \
  110. LTYPE *p; \
  111. usize_t c = 0; \
  112. \
  113. for (p = l->next; p != l; p = p->next) \
  114. { \
  115. c += 1; \
  116. } \
  117. \
  118. return c; \
  119. } \
  120. \
  121. typedef int unused_ ## LTYPE
  122. #endif