-
Notifications
You must be signed in to change notification settings - Fork 14
/
List.c
36 lines (30 loc) · 874 Bytes
/
List.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "List.h"
#include <string.h>
#include <stdlib.h>
void List_init(List *l, size_t itemsize) {
memset(l, 0, sizeof *l);
mem_init(&l->mem);
l->itemsize = itemsize;
}
void List_free(List *l) {
mem_free(&l->mem);
List_init(l, 0);
}
int List_add(List *l, void* item) {
int ret = mem_write(&l->mem, l->count * l->itemsize, item, l->itemsize);
if(ret) l->count++;
return ret;
}
int List_get(List *l, size_t index, void* item) {
if(index >= l->count) return 0;
void* src = mem_getptr(&l->mem, index * l->itemsize, l->itemsize);
if(!src) return 0;
memcpy(item, src, l->itemsize);
return 1;
}
void* List_getptr(List *l, size_t index) {
return mem_getptr(&l->mem, index * l->itemsize, l->itemsize);
}
void List_sort(List *l, int(*compar)(const void *, const void *)) {
qsort(mem_getptr(&l->mem, 0, l->itemsize * l->count), l->count, l->itemsize, compar);
}