ListEntry.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * category: [data structure]
  3. * apply status:
  4. * edit status:
  5. * build status:
  6. * description: this file is conflict with Comm/ListEntry.h
  7. */
  8. #ifndef LISTENTRY_H
  9. #define LISTENTRY_H
  10. #pragma once
  11. #ifndef _WIN32
  12. #ifndef NULL
  13. #ifdef __cplusplus
  14. #define NULL 0
  15. #else /* __cplusplus */
  16. #define NULL ((void *)0)
  17. #endif /* __cplusplus */
  18. #endif /* NULL */
  19. typedef struct _LIST_ENTRY
  20. {
  21. struct _LIST_ENTRY* Flink;
  22. struct _LIST_ENTRY* Blink;
  23. } LIST_ENTRY, * PLIST_ENTRY, * __restrict PRLIST_ENTRY;
  24. //
  25. // Singly linked list structure. Can be used as either a list head, or
  26. // as link words.
  27. //
  28. typedef struct _SINGLE_LIST_ENTRY
  29. {
  30. struct _SINGLE_LIST_ENTRY* Next;
  31. } SINGLE_LIST_ENTRY, * PSINGLE_LIST_ENTRY;
  32. #endif //_WIN32
  33. static __inline void ListEntry_InitNode(PLIST_ENTRY node)
  34. {
  35. node->Flink = node->Blink = NULL;
  36. }
  37. static __inline void ListEntry_InitHead(PLIST_ENTRY head)
  38. {
  39. head->Blink = head;
  40. head->Flink = head;
  41. }
  42. static __inline void ListEntry_DeleteNode(PLIST_ENTRY node)
  43. {
  44. PLIST_ENTRY prev = node->Blink;
  45. PLIST_ENTRY next = node->Flink;
  46. prev->Flink = next;
  47. next->Blink = prev;
  48. node->Flink = node->Blink = NULL;
  49. }
  50. static __inline void ListEntry_Insert(PLIST_ENTRY prev, PLIST_ENTRY next, PLIST_ENTRY node)
  51. {
  52. prev->Flink = node;
  53. node->Blink = prev;
  54. node->Flink = next;
  55. next->Blink = node;
  56. }
  57. static __inline void ListEntry_InsertBefore(PLIST_ENTRY pos, PLIST_ENTRY node)
  58. {
  59. ListEntry_Insert(pos->Blink, pos, node);
  60. }
  61. static __inline void ListEntry_InsertAfter(PLIST_ENTRY pos, PLIST_ENTRY node)
  62. {
  63. ListEntry_Insert(pos, pos->Flink, node);
  64. }
  65. static __inline void ListEntry_AddTail(PLIST_ENTRY head, PLIST_ENTRY node)
  66. {
  67. ListEntry_Insert(head->Blink, head, node);
  68. }
  69. static __inline void ListEntry_AddHead(PLIST_ENTRY head, PLIST_ENTRY node)
  70. {
  71. ListEntry_Insert(head, head->Flink, node);
  72. }
  73. static __inline PLIST_ENTRY ListEntry_RemoveListHead(PLIST_ENTRY head)
  74. {
  75. PLIST_ENTRY t = head->Flink;
  76. ListEntry_DeleteNode(t);
  77. return t;
  78. }
  79. static __inline PLIST_ENTRY ListEntry_RemoveListTail(PLIST_ENTRY head)
  80. {
  81. PLIST_ENTRY t = head->Blink;
  82. ListEntry_DeleteNode(t);
  83. return t;
  84. }
  85. static __inline int ListEntry_IsEmpty(PLIST_ENTRY head)
  86. {
  87. return head->Blink == head;
  88. }
  89. static __inline PLIST_ENTRY ListEntry_GetHead(PLIST_ENTRY head)
  90. {
  91. return head->Flink;
  92. }
  93. static __inline PLIST_ENTRY ListEntry_GetTail(PLIST_ENTRY head)
  94. {
  95. return head->Blink;
  96. }
  97. #define ListEntry_ForEachSafe(pos, n, head, type, member) \
  98. for (pos = CONTAINING_RECORD((head)->Flink, type, member), \
  99. n = CONTAINING_RECORD(pos->member.Flink, type, member); \
  100. &pos->member != (head); \
  101. pos = n, n = CONTAINING_RECORD(n->member.Flink, type, member))
  102. #define ListEntry_ForEach(pos, head, type, member) \
  103. for (pos = CONTAINING_RECORD((head)->Flink, type, member); \
  104. &pos->member != head; \
  105. pos = CONTAINING_RECORD(pos->member.Flink, type, member))
  106. #endif // LISTENTRY_H