forked from freewilll/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
67 lines (53 loc) · 1.48 KB
/
list.c
File metadata and controls
67 lines (53 loc) · 1.48 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdlib.h>
#include "wcc.h"
void free_circular_linked_list(CircularLinkedList *cll) {
if (!cll) return;
CircularLinkedList *head = cll->next;
CircularLinkedList *l = head;
do {
CircularLinkedList *next = l->next;
wfree(l);
l = next;
} while (l != head);
}
#define MIN_SIZE 0
static void resize_list(List* l, int new_length);
List *new_list(int initial_allocation) {
List *l = wmalloc(sizeof(List));
l->length = 0;
l->allocated = 0;
l->elements = NULL;
if (initial_allocation) {
l->allocated = initial_allocation;
l->elements = wmalloc(sizeof(void *) * initial_allocation);
}
return l;
}
void free_list(List *l) {
wfree(l->elements);
wfree(l);
}
static int round_up(int length) {
if (length == 0) return 0;
int result = 1;
while (result < length) result *= 2;
return result;
}
static void resize_list(List* l, int new_length) {
if (new_length < l->allocated) {
l->length = new_length;
return;
}
int new_allocated = round_up(new_length);
if (new_allocated < MIN_SIZE) new_allocated = MIN_SIZE;
if (new_length > l->length) {
l->elements = wrealloc(l->elements, sizeof(void *) * new_allocated);
if (!l->elements) panic("Realloc failed");
}
l->length = new_length;
l->allocated = new_allocated;
}
void append_to_list(List *l, void *element) {
resize_list(l, l->length + 1);
l->elements[l->length - 1] = element;
}