intlist.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef __PERF_INTLIST_H
  2. #define __PERF_INTLIST_H
  3. #include <linux/rbtree.h>
  4. #include <stdbool.h>
  5. #include "rblist.h"
  6. struct int_node {
  7. struct rb_node rb_node;
  8. int i;
  9. };
  10. struct intlist {
  11. struct rblist rblist;
  12. };
  13. struct intlist *intlist__new(void);
  14. void intlist__delete(struct intlist *ilist);
  15. void intlist__remove(struct intlist *ilist, struct int_node *in);
  16. int intlist__add(struct intlist *ilist, int i);
  17. struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx);
  18. struct int_node *intlist__find(struct intlist *ilist, int i);
  19. static inline bool intlist__has_entry(struct intlist *ilist, int i)
  20. {
  21. return intlist__find(ilist, i) != NULL;
  22. }
  23. static inline bool intlist__empty(const struct intlist *ilist)
  24. {
  25. return rblist__empty(&ilist->rblist);
  26. }
  27. static inline unsigned int intlist__nr_entries(const struct intlist *ilist)
  28. {
  29. return rblist__nr_entries(&ilist->rblist);
  30. }
  31. /* For intlist iteration */
  32. static inline struct int_node *intlist__first(struct intlist *ilist)
  33. {
  34. struct rb_node *rn = rb_first(&ilist->rblist.entries);
  35. return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
  36. }
  37. static inline struct int_node *intlist__next(struct int_node *in)
  38. {
  39. struct rb_node *rn;
  40. if (!in)
  41. return NULL;
  42. rn = rb_next(&in->rb_node);
  43. return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
  44. }
  45. /**
  46. * intlist_for_each - iterate over a intlist
  47. * @pos: the &struct int_node to use as a loop cursor.
  48. * @ilist: the &struct intlist for loop.
  49. */
  50. #define intlist__for_each(pos, ilist) \
  51. for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
  52. /**
  53. * intlist_for_each_safe - iterate over a intlist safe against removal of
  54. * int_node
  55. * @pos: the &struct int_node to use as a loop cursor.
  56. * @n: another &struct int_node to use as temporary storage.
  57. * @ilist: the &struct intlist for loop.
  58. */
  59. #define intlist__for_each_safe(pos, n, ilist) \
  60. for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\
  61. pos = n, n = intlist__next(n))
  62. #endif /* __PERF_INTLIST_H */