2017-09-23 10:26:01 +02:00
|
|
|
#ifndef WLR_UTIL_LIST_H
|
|
|
|
#define WLR_UTIL_LIST_H
|
2017-04-25 21:06:58 +02:00
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
struct wlr_list {
|
2017-04-25 21:06:58 +02:00
|
|
|
size_t capacity;
|
|
|
|
size_t length;
|
|
|
|
void **items;
|
2017-10-22 04:00:04 +02:00
|
|
|
};
|
2017-04-25 21:06:58 +02:00
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Creates a new list, may return `NULL` on failure
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
struct wlr_list *wlr_list_create(void);
|
|
|
|
void wlr_list_free(struct wlr_list *list);
|
|
|
|
void wlr_list_foreach(struct wlr_list *list, void (*callback)(void *item));
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Add `item` to the end of a list.
|
|
|
|
* Returns: new list length or `-1` on failure
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
int wlr_list_add(struct wlr_list *list, void *item);
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Add `item` to the end of a list.
|
|
|
|
* Returns: new list length or `-1` on failure
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
int wlr_list_push(struct wlr_list *list, void *item);
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Place `item` into index `index` in the list
|
|
|
|
* Returns: new list length or `-1` on failure
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
int wlr_list_insert(struct wlr_list *list, size_t index, void *item);
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Remove an item from the list
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
void wlr_list_del(struct wlr_list *list, size_t index);
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Remove and return an item from the end of the list
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
void *wlr_list_pop(struct wlr_list *list);
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Get a reference to the last item of a list without removal
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
void *wlr_list_peek(struct wlr_list *list);
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Append each item in `source` to `list`
|
|
|
|
* Does not modify `source`
|
|
|
|
* Returns: new list length or `-1` on failure
|
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
int wlr_list_cat(struct wlr_list *list, struct wlr_list *source);
|
2017-04-25 21:06:58 +02:00
|
|
|
// See qsort. Remember to use *_qsort functions as compare functions,
|
|
|
|
// because they dereference the left and right arguments first!
|
2017-10-22 16:56:40 +02:00
|
|
|
void wlr_list_qsort(struct wlr_list *list, int compare(const void *left, const void *right));
|
2017-04-25 21:06:58 +02:00
|
|
|
// Return index for first item in list that returns 0 for given compare
|
|
|
|
// function or -1 if none matches.
|
2017-10-22 16:56:40 +02:00
|
|
|
int wlr_list_seq_find(struct wlr_list *list,
|
2017-04-25 21:06:58 +02:00
|
|
|
int compare(const void *item, const void *cmp_to),
|
|
|
|
const void *cmp_to);
|
|
|
|
|
|
|
|
#endif
|