summaryrefslogtreecommitdiff
path: root/list.c
blob: 600142400895af768887cef43c0284310d11fca5 (plain)
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
37
38
39
40
41
42
43
44
45
46
#include <err.h>
#include "list.h"

#define LIST_SIZE 50

struct long_list* long_list_init(size_t size) {
	struct long_list* list = malloc(sizeof(struct long_list));
	list->list = malloc(sizeof(long) * size);
	list->alloc_size = size;
	list->length = 0;
	return list;
}

void long_list_free(struct long_list* list) {
	if(list == NULL)
		return;

	if(list->list != NULL)
		free(list->list);

	free(list);
}

void long_list_add(struct long_list* list, long value) {
	// increase space if needed
	if(list->length >= list->alloc_size) {
		list->list = realloc(list->list, sizeof(long) * (list->alloc_size + LIST_SIZE));
		list->alloc_size += LIST_SIZE;
	}
	list->list[list->length] = value;
	list->length = list->length + 1;
}

long long_list_get(struct long_list* list, size_t index) {
	if(index >= list->length)
		err(1, "index %ld out of bounds (%ld)", index, list->length);

	return list->list[index];
}

void long_list_set(struct long_list* list, size_t index, long value) {
	if(index >= list->length)
		err(1, "index %ld out of bounds (%ld)", index, list->length);

	list->list[index] = value;
}