wlroots/util/set.c

26 lines
497 B
C
Raw Normal View History

2022-08-19 18:19:30 +00:00
#include "util/set.h"
2022-08-19 18:20:43 +00:00
ssize_t set_add(uint32_t values[], size_t *len, size_t cap, uint32_t target) {
2022-08-19 18:19:30 +00:00
for (uint32_t i = 0; i < *len; ++i) {
if (values[i] == target) {
2022-08-19 18:20:43 +00:00
return i;
2022-08-19 18:19:30 +00:00
}
}
2022-08-19 18:20:43 +00:00
if (*len == cap) {
return -1;
}
values[*len] = target;
return (*len)++;
2022-08-19 18:19:30 +00:00
}
2022-08-19 18:20:43 +00:00
ssize_t set_remove(uint32_t values[], size_t *len, size_t cap, uint32_t target) {
2022-08-19 18:19:30 +00:00
for (uint32_t i = 0; i < *len; ++i) {
if (values[i] == target) {
2022-08-19 18:20:43 +00:00
--(*len);
values[i] = values[*len];
return i;
2022-08-19 18:19:30 +00:00
}
}
2022-08-19 18:20:43 +00:00
return -1;
2022-08-19 18:19:30 +00:00
}