2017-04-25 21:06:58 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2017-08-16 09:23:21 +02:00
|
|
|
#include <stdbool.h>
|
2017-04-25 21:06:58 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
2017-10-22 04:00:04 +02:00
|
|
|
#include <wlr/types/wlr_list.h>
|
2017-04-25 21:06:58 +02:00
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
struct wlr_list *list_create(void) {
|
|
|
|
struct wlr_list *list = malloc(sizeof(struct wlr_list));
|
2017-08-15 07:56:47 +02:00
|
|
|
if (!list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-25 21:06:58 +02:00
|
|
|
list->capacity = 10;
|
|
|
|
list->length = 0;
|
|
|
|
list->items = malloc(sizeof(void*) * list->capacity);
|
2017-08-15 07:56:47 +02:00
|
|
|
if (!list->items) {
|
|
|
|
free(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-04-25 21:06:58 +02:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
static bool list_resize(struct wlr_list *list) {
|
2017-04-25 21:06:58 +02:00
|
|
|
if (list->length == list->capacity) {
|
2017-08-16 09:23:21 +02:00
|
|
|
void *new_items = realloc(list->items, sizeof(void*) * (list->capacity + 10));
|
|
|
|
if (!new_items) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-04-25 21:06:58 +02:00
|
|
|
list->capacity += 10;
|
2017-08-16 09:23:21 +02:00
|
|
|
list->items = new_items;
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
2017-08-16 09:23:21 +02:00
|
|
|
return true;
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
void list_free(struct wlr_list *list) {
|
2017-04-25 21:06:58 +02:00
|
|
|
if (list == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(list->items);
|
|
|
|
free(list);
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
void list_foreach(struct wlr_list *list, void (*callback)(void *item)) {
|
2017-04-25 21:06:58 +02:00
|
|
|
if (list == NULL || callback == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < list->length; i++) {
|
|
|
|
callback(list->items[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
int list_add(struct wlr_list *list, void *item) {
|
2017-08-16 09:23:21 +02:00
|
|
|
if (!list_resize(list)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-25 21:06:58 +02:00
|
|
|
list->items[list->length++] = item;
|
2017-08-16 09:23:21 +02:00
|
|
|
return list->length;
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
int list_push(struct wlr_list *list, void *item) {
|
2017-08-16 09:23:21 +02:00
|
|
|
return list_add(list, item);
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
int list_insert(struct wlr_list *list, size_t index, void *item) {
|
2017-08-16 09:23:21 +02:00
|
|
|
if (!list_resize(list)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-25 21:06:58 +02:00
|
|
|
memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index));
|
|
|
|
list->length++;
|
|
|
|
list->items[index] = item;
|
2017-08-16 09:23:21 +02:00
|
|
|
return list->length;
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
void list_del(struct wlr_list *list, size_t index) {
|
2017-04-25 21:06:58 +02:00
|
|
|
list->length--;
|
|
|
|
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index));
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
void *list_pop(struct wlr_list *list) {
|
2017-04-25 21:06:58 +02:00
|
|
|
void *_ = list->items[list->length - 1];
|
|
|
|
list_del(list, list->length - 1);
|
|
|
|
return _;
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
void *list_peek(struct wlr_list *list) {
|
2017-04-25 21:06:58 +02:00
|
|
|
return list->items[list->length - 1];
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
int list_cat(struct wlr_list *list, struct wlr_list *source) {
|
2017-08-16 09:23:21 +02:00
|
|
|
size_t old_len = list->length;
|
2017-04-25 21:06:58 +02:00
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < source->length; ++i) {
|
2017-08-16 09:23:21 +02:00
|
|
|
if (list_add(list, source->items[i]) == -1) {
|
|
|
|
list->length = old_len;
|
|
|
|
return -1;
|
|
|
|
}
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
2017-08-16 09:23:21 +02:00
|
|
|
return list->length;
|
2017-04-25 21:06:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
void list_qsort(struct wlr_list *list, int compare(const void *left, const void *right)) {
|
2017-04-25 21:06:58 +02:00
|
|
|
qsort(list->items, list->length, sizeof(void *), compare);
|
|
|
|
}
|
|
|
|
|
2017-10-22 04:00:04 +02:00
|
|
|
int list_seq_find(struct wlr_list *list,
|
2017-04-25 21:06:58 +02:00
|
|
|
int compare(const void *item, const void *data),
|
|
|
|
const void *data) {
|
|
|
|
for (size_t i = 0; i < list->length; i++) {
|
|
|
|
void *item = list->items[i];
|
|
|
|
if (compare(item, data) == 0) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|