2018-02-12 21:29:23 +01:00
|
|
|
#ifndef WLR_TYPES_WLR_LIST_H
|
|
|
|
#define WLR_TYPES_WLR_LIST_H
|
2017-04-25 21:06:58 +02:00
|
|
|
|
2017-11-19 00:17:40 +01:00
|
|
|
#include <stdbool.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <stddef.h>
|
2017-04-25 21:06:58 +02:00
|
|
|
|
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
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Initialize a list. Returns true on success, false on failure.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-11-19 00:17:40 +01:00
|
|
|
bool wlr_list_init(struct wlr_list *list);
|
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Deinitialize a list.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-11-19 00:17:40 +01:00
|
|
|
void wlr_list_finish(struct wlr_list *list);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes `callback` on each element in the list.
|
|
|
|
*/
|
|
|
|
void wlr_list_for_each(struct wlr_list *list, void (*callback)(void *item));
|
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
|
|
|
* Add `item` to the end of a list.
|
2017-11-19 00:17:40 +01:00
|
|
|
* Returns: new list length or `-1` on failure.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-11-19 00:17:40 +01:00
|
|
|
ssize_t wlr_list_push(struct wlr_list *list, void *item);
|
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Place `item` into index `index` in the list.
|
|
|
|
* Returns: new list length or `-1` on failure.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-11-19 00:17:40 +01:00
|
|
|
ssize_t wlr_list_insert(struct wlr_list *list, size_t index, void *item);
|
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Remove an item from the list.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
void wlr_list_del(struct wlr_list *list, size_t index);
|
2017-11-19 00:17:40 +01:00
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Remove and return an item from the end of the list.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
void *wlr_list_pop(struct wlr_list *list);
|
2017-11-19 00:17:40 +01:00
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Get a reference to the last item of a list without removal.
|
2017-08-16 09:23:21 +02:00
|
|
|
*/
|
2017-10-22 16:56:40 +02:00
|
|
|
void *wlr_list_peek(struct wlr_list *list);
|
2017-11-19 00:17:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Append each item in `source` to `list`.
|
|
|
|
* Does not modify `source`.
|
|
|
|
* Returns: new list length or `-1` on failure.
|
|
|
|
*/
|
|
|
|
ssize_t wlr_list_cat(struct wlr_list *list, const struct wlr_list *source);
|
|
|
|
|
2017-08-16 09:23:21 +02:00
|
|
|
/**
|
2017-11-19 00:17:40 +01:00
|
|
|
* Sort a list using `qsort`.
|
|
|
|
*/
|
|
|
|
void wlr_list_qsort(struct wlr_list *list,
|
|
|
|
int compare(const void *left, const void *right));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the index of the first item in the list that returns 0 for the given
|
|
|
|
* `compare` function, or -1 if none matches.
|
|
|
|
*/
|
|
|
|
ssize_t wlr_list_find(struct wlr_list *list,
|
|
|
|
int compare(const void *item, const void *cmp_to), const void *cmp_to);
|
2017-04-25 21:06:58 +02:00
|
|
|
|
|
|
|
#endif
|