xref: /linux/tools/perf/util/intlist.h (revision ffe0fb769a6db3b6027d9228b6fecb6b352e4834)
170b40c4aSDavid Ahern #ifndef __PERF_INTLIST_H
270b40c4aSDavid Ahern #define __PERF_INTLIST_H
370b40c4aSDavid Ahern 
470b40c4aSDavid Ahern #include <linux/rbtree.h>
570b40c4aSDavid Ahern #include <stdbool.h>
670b40c4aSDavid Ahern 
770b40c4aSDavid Ahern #include "rblist.h"
870b40c4aSDavid Ahern 
970b40c4aSDavid Ahern struct int_node {
1070b40c4aSDavid Ahern 	struct rb_node rb_node;
1170b40c4aSDavid Ahern 	int i;
1270b40c4aSDavid Ahern };
1370b40c4aSDavid Ahern 
1470b40c4aSDavid Ahern struct intlist {
1570b40c4aSDavid Ahern 	struct rblist rblist;
1670b40c4aSDavid Ahern };
1770b40c4aSDavid Ahern 
18*ffe0fb76SArnaldo Carvalho de Melo struct intlist *intlist__new(const char *slist);
1970b40c4aSDavid Ahern void intlist__delete(struct intlist *ilist);
2070b40c4aSDavid Ahern 
2170b40c4aSDavid Ahern void intlist__remove(struct intlist *ilist, struct int_node *in);
2270b40c4aSDavid Ahern int intlist__add(struct intlist *ilist, int i);
2370b40c4aSDavid Ahern 
2470b40c4aSDavid Ahern struct int_node *intlist__entry(const struct intlist *ilist, unsigned int idx);
2570b40c4aSDavid Ahern struct int_node *intlist__find(struct intlist *ilist, int i);
2670b40c4aSDavid Ahern 
2770b40c4aSDavid Ahern static inline bool intlist__has_entry(struct intlist *ilist, int i)
2870b40c4aSDavid Ahern {
2970b40c4aSDavid Ahern 	return intlist__find(ilist, i) != NULL;
3070b40c4aSDavid Ahern }
3170b40c4aSDavid Ahern 
3270b40c4aSDavid Ahern static inline bool intlist__empty(const struct intlist *ilist)
3370b40c4aSDavid Ahern {
3470b40c4aSDavid Ahern 	return rblist__empty(&ilist->rblist);
3570b40c4aSDavid Ahern }
3670b40c4aSDavid Ahern 
3770b40c4aSDavid Ahern static inline unsigned int intlist__nr_entries(const struct intlist *ilist)
3870b40c4aSDavid Ahern {
3970b40c4aSDavid Ahern 	return rblist__nr_entries(&ilist->rblist);
4070b40c4aSDavid Ahern }
4170b40c4aSDavid Ahern 
4270b40c4aSDavid Ahern /* For intlist iteration */
4370b40c4aSDavid Ahern static inline struct int_node *intlist__first(struct intlist *ilist)
4470b40c4aSDavid Ahern {
4570b40c4aSDavid Ahern 	struct rb_node *rn = rb_first(&ilist->rblist.entries);
4670b40c4aSDavid Ahern 	return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
4770b40c4aSDavid Ahern }
4870b40c4aSDavid Ahern static inline struct int_node *intlist__next(struct int_node *in)
4970b40c4aSDavid Ahern {
5070b40c4aSDavid Ahern 	struct rb_node *rn;
5170b40c4aSDavid Ahern 	if (!in)
5270b40c4aSDavid Ahern 		return NULL;
5370b40c4aSDavid Ahern 	rn = rb_next(&in->rb_node);
5470b40c4aSDavid Ahern 	return rn ? rb_entry(rn, struct int_node, rb_node) : NULL;
5570b40c4aSDavid Ahern }
5670b40c4aSDavid Ahern 
5770b40c4aSDavid Ahern /**
5870b40c4aSDavid Ahern  * intlist_for_each      - iterate over a intlist
5970b40c4aSDavid Ahern  * @pos:	the &struct int_node to use as a loop cursor.
6070b40c4aSDavid Ahern  * @ilist:	the &struct intlist for loop.
6170b40c4aSDavid Ahern  */
6270b40c4aSDavid Ahern #define intlist__for_each(pos, ilist)	\
6370b40c4aSDavid Ahern 	for (pos = intlist__first(ilist); pos; pos = intlist__next(pos))
6470b40c4aSDavid Ahern 
6570b40c4aSDavid Ahern /**
6670b40c4aSDavid Ahern  * intlist_for_each_safe - iterate over a intlist safe against removal of
6770b40c4aSDavid Ahern  *                         int_node
6870b40c4aSDavid Ahern  * @pos:	the &struct int_node to use as a loop cursor.
6970b40c4aSDavid Ahern  * @n:		another &struct int_node to use as temporary storage.
7070b40c4aSDavid Ahern  * @ilist:	the &struct intlist for loop.
7170b40c4aSDavid Ahern  */
7270b40c4aSDavid Ahern #define intlist__for_each_safe(pos, n, ilist)	\
7370b40c4aSDavid Ahern 	for (pos = intlist__first(ilist), n = intlist__next(pos); pos;\
7470b40c4aSDavid Ahern 	     pos = n, n = intlist__next(n))
7570b40c4aSDavid Ahern #endif /* __PERF_INTLIST_H */
76