node_list.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DTC_NODE_LIST_H
  17. #define __DTC_NODE_LIST_H
  18. #include "namespace.h"
  19. #include "global.h"
  20. #include "node.h"
  21. DTC_BEGIN_NAMESPACE
  22. #define INIT_NODE_LIST_HEAD(node, id) \
  23. do { \
  24. node.lru_prev() = id; \
  25. node.lru_next() = id; \
  26. } while (0)
  27. inline void __NODE_LIST_ADD(Node p, Node prev, Node next)
  28. {
  29. next.lru_prev() = p.node_id();
  30. p.lru_next() = next.node_id();
  31. p.lru_prev() = prev.node_id();
  32. prev.lru_next() = p.node_id();
  33. }
  34. inline void NODE_LIST_ADD(Node p, Node head)
  35. {
  36. __NODE_LIST_ADD(p, head, head.Next());
  37. }
  38. inline void NODE_LIST_ADD_TAIL(Node p, Node head)
  39. {
  40. __NODE_LIST_ADD(p, head.Prev(), head);
  41. }
  42. inline void __NODE_LIST_DEL(Node prev, Node next)
  43. {
  44. next.lru_prev() = prev.node_id();
  45. prev.lru_next() = next.node_id();
  46. }
  47. inline void NODE_LIST_DEL(Node p)
  48. {
  49. __NODE_LIST_DEL(p.Prev(), p.Next());
  50. p.lru_prev() = p.node_id();
  51. p.lru_next() = p.node_id();
  52. }
  53. inline void NODE_LIST_MOVE(Node p, Node head)
  54. {
  55. __NODE_LIST_DEL(p.Prev(), p.Next());
  56. NODE_LIST_ADD(p, head);
  57. }
  58. inline void NODE_LIST_MOVE_TAIL(Node p, Node head)
  59. {
  60. __NODE_LIST_DEL(p.Prev(), p.Next());
  61. NODE_LIST_ADD_TAIL(p, head);
  62. }
  63. inline int NODE_LIST_EMPTY(Node head)
  64. {
  65. return head.lru_next() == head.node_id();
  66. }
  67. /*正向遍历*/
  68. #define NODE_LIST_FOR_EACH(pos, head) \
  69. for (pos = head.Next(); pos != head; pos = pos.Next())
  70. /*反向遍历*/
  71. #define NODE_LIST_FOR_EACH_RVS(pos, head) \
  72. for (pos = head.Prev(); pos != head; pos = pos.Prev())
  73. DTC_END_NAMESPACE
  74. #endif