ListEntry.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * category: [data structure]
  3. * apply status:
  4. * edit status:
  5. * build status:
  6. * description:
  7. */
  8. #ifndef LISTENTRY_H
  9. #define LISTENTRY_H
  10. #pragma once
  11. static __inline void ListEntry_InitNode(PLIST_ENTRY node)
  12. {
  13. node->Flink = node->Blink = NULL;
  14. }
  15. static __inline void ListEntry_InitHead(PLIST_ENTRY head)
  16. {
  17. head->Blink = head;
  18. head->Flink = head;
  19. }
  20. static __inline void ListEntry_DeleteNode(PLIST_ENTRY node)
  21. {
  22. PLIST_ENTRY prev = node->Blink;
  23. PLIST_ENTRY next = node->Flink;
  24. prev->Flink = next;
  25. next->Blink = prev;
  26. node->Flink = node->Blink = NULL;
  27. }
  28. static __inline void ListEntry_Insert(PLIST_ENTRY prev, PLIST_ENTRY next, PLIST_ENTRY node)
  29. {
  30. prev->Flink = node;
  31. node->Blink = prev;
  32. node->Flink = next;
  33. next->Blink = node;
  34. }
  35. static __inline void ListEntry_InsertBefore(PLIST_ENTRY pos, PLIST_ENTRY node)
  36. {
  37. ListEntry_Insert(pos->Blink, pos, node);
  38. }
  39. static __inline void ListEntry_InsertAfter(PLIST_ENTRY pos, PLIST_ENTRY node)
  40. {
  41. ListEntry_Insert(pos, pos->Flink, node);
  42. }
  43. static __inline void ListEntry_AddTail(PLIST_ENTRY head, PLIST_ENTRY node)
  44. {
  45. ListEntry_Insert(head->Blink, head, node);
  46. }
  47. static __inline void ListEntry_AddHead(PLIST_ENTRY head, PLIST_ENTRY node)
  48. {
  49. ListEntry_Insert(head, head->Flink, node);
  50. }
  51. static __inline PLIST_ENTRY ListEntry_RemoveListHead(PLIST_ENTRY head)
  52. {
  53. PLIST_ENTRY t = head->Flink;
  54. ListEntry_DeleteNode(t);
  55. return t;
  56. }
  57. static __inline PLIST_ENTRY ListEntry_RemoveListTail(PLIST_ENTRY head)
  58. {
  59. PLIST_ENTRY t = head->Blink;
  60. ListEntry_DeleteNode(t);
  61. return t;
  62. }
  63. static __inline int ListEntry_IsEmpty(PLIST_ENTRY head)
  64. {
  65. return head->Blink == head;
  66. }
  67. static __inline PLIST_ENTRY ListEntry_GetHead(PLIST_ENTRY head)
  68. {
  69. return head->Flink;
  70. }
  71. static __inline PLIST_ENTRY ListEntry_GetTail(PLIST_ENTRY head)
  72. {
  73. return head->Blink;
  74. }
  75. #define ListEntry_ForEachSafe(pos, n, head, type, member) \
  76. for (pos = CONTAINING_RECORD((head)->Flink, type, member), \
  77. n = CONTAINING_RECORD(pos->member.Flink, type, member); \
  78. &pos->member != (head); \
  79. pos = n, n = CONTAINING_RECORD(n->member.Flink, type, member))
  80. #define ListEntry_ForEach(pos, head, type, member) \
  81. for (pos = CONTAINING_RECORD((head)->Flink, type, member); \
  82. &pos->member != head; \
  83. pos = CONTAINING_RECORD(pos->member.Flink, type, member))
  84. #endif // LISTENTRY_H