mirror of
https://github.com/swaywm/sway.git
synced 2026-04-29 06:46:22 -04:00
New list interface.
This commit is contained in:
parent
ee81b1aecb
commit
d404c4ab28
3 changed files with 242 additions and 118 deletions
121
include/list.h
121
include/list.h
|
|
@ -1,27 +1,108 @@
|
|||
#ifndef _SWAY_LIST_H
|
||||
#define _SWAY_LIST_H
|
||||
|
||||
#include <stdalign.h>
|
||||
#include <stddef.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
typedef struct {
|
||||
int capacity;
|
||||
int length;
|
||||
void **items;
|
||||
size_t capacity;
|
||||
size_t length;
|
||||
size_t memb_size;
|
||||
alignas(max_align_t) unsigned char data[];
|
||||
} list_t;
|
||||
|
||||
list_t *create_list(void);
|
||||
void list_free(list_t *list);
|
||||
void list_foreach(list_t *list, void (*callback)(void* item));
|
||||
void list_add(list_t *list, void *item);
|
||||
void list_insert(list_t *list, int index, void *item);
|
||||
void list_del(list_t *list, int index);
|
||||
void list_cat(list_t *list, list_t *source);
|
||||
// See qsort. Remember to use *_qsort functions as compare functions,
|
||||
// because they dereference the left and right arguments first!
|
||||
void list_qsort(list_t *list, int compare(const void *left, const void *right));
|
||||
// Return index for first item in list that returns 0 for given compare
|
||||
// function or -1 if none matches.
|
||||
int list_seq_find(list_t *list, int compare(const void *item, const void *cmp_to), const void *cmp_to);
|
||||
// stable sort since qsort is not guaranteed to be stable
|
||||
void list_stable_sort(list_t *list, int compare(const void *a, const void *b));
|
||||
// swap two elements in a list
|
||||
void list_swap(list_t *list, int src, int dest);
|
||||
/*
|
||||
* Creates a new list with an inital capacity.
|
||||
* If capacity is zero, some default value is used instead.
|
||||
*
|
||||
* memb_size must be the size of the type stored in this list.
|
||||
* If an element is added to this list which is not the same type
|
||||
* as the elements of the list, the behavior is undefined.
|
||||
*
|
||||
* This list should be freed with the stdlib free() function.
|
||||
*/
|
||||
list_t *list_new(size_t memb_size, size_t capacity);
|
||||
|
||||
/*
|
||||
* Adds an element to the end of the list.
|
||||
*/
|
||||
void list_add(list_t **list, const void *data);
|
||||
|
||||
/*
|
||||
* Adds an element at an arbitrary position in the list, moving
|
||||
* the elements past index to make space.
|
||||
* index must be less than or equal to the length of the list.
|
||||
*/
|
||||
void list_insert(list_t **list, size_t index, const void *data);
|
||||
|
||||
/*
|
||||
* Deletes the element at an arbitrary position in the list,
|
||||
* moving the elements past index into the space.
|
||||
* index must be less than the length of the list.
|
||||
*/
|
||||
void list_delete(list_t *list, size_t index);
|
||||
|
||||
/*
|
||||
* Swaps the elements at i1 and i2.
|
||||
* i1 and i2 must both be less than the length of the list.
|
||||
*/
|
||||
void list_swap(list_t *list, size_t i1, size_t i2);
|
||||
|
||||
/*
|
||||
* Gets the element of the list at the index.
|
||||
* index must be less than the length of the list.
|
||||
*/
|
||||
void *list_get(list_t *list, size_t index);
|
||||
|
||||
/*
|
||||
* Sorts the list using the stdlib qsort() function.
|
||||
*/
|
||||
void list_qsort(list_t *list, int compare(const void *, const void *));
|
||||
|
||||
/*
|
||||
* Sorts the list using insertion sort.
|
||||
* This should be used if you need a stable sort, and your list is
|
||||
* short and/or nearly sorted.
|
||||
*/
|
||||
void list_isort(list_t *list, int compare(const void *, const void *));
|
||||
|
||||
/*
|
||||
* Returns the index of key in the list, using the stdlib bsearch() function,
|
||||
* or -1 if it was not found. If ret is not null, the found element will be
|
||||
* copied into it.
|
||||
* The list must be sorted.
|
||||
*/
|
||||
ssize_t list_bsearch(const list_t *list, int compare(const void *, const void *),
|
||||
const void *key, void *ret);
|
||||
|
||||
/*
|
||||
* Returns the index of the key in the list, using a linear search,
|
||||
* or -1 if it was not found. If ret is not null, the found element will be
|
||||
* copied into it.
|
||||
*/
|
||||
ssize_t list_lsearch(const list_t *list, int compare(const void *, const void *),
|
||||
const void *key, void *ret);
|
||||
|
||||
/*
|
||||
* Calls a function on every item in the list.
|
||||
*/
|
||||
void list_foreach_cb(list_t *list, void callback(void *));
|
||||
|
||||
/*
|
||||
* This is for loop helper, iterating over every element in list.
|
||||
* iter must be a pointer to the list element's type.
|
||||
* You should not add, remove, or sort elements from the list while inside this loop.
|
||||
*
|
||||
* Example:
|
||||
* char **ptr;
|
||||
* list_foreach(list_of_strings, ptr) {
|
||||
* printf("%s\n", *ptr);
|
||||
* }
|
||||
*/
|
||||
#define list_foreach(list, iter) \
|
||||
for (iter = (void *)list->data; \
|
||||
(unsigned char *)iter < list->data + list->memb_size * list->length; \
|
||||
++iter)
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue