summaryrefslogtreecommitdiffhomepage
path: root/arraylist.c
blob: 048299f8d90fcb207fb6be0fbfb6fb93d555c37a (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "arraylist.h"

#define PUBLIC /* nothing */
#define PRIVATE static

// size doubles whenever it is exceeded
#define ARRAYLIST_DEFAULT_SIZE 4
//#define ARRAYLIST_DEBUG

struct array_list_type {
	void* data; // the arraylist data
	int element_size;
	int max_num_elements;
	int num_elements;
};

PRIVATE bool al_grow(Arraylist* al) {
	void* result = realloc(
			al->data,
			al->max_num_elements * al->element_size * 2
			);
	if(result != NULL) {
		al->data = result;
		al->max_num_elements *= 2;
		return true;
	} else {
		return false;
	}
}

void al_delete_last(Arraylist *al) {
	if(al->num_elements > 0)
		al->num_elements -= 1;
}

bool al_delete_at(Arraylist *al, int position) {
	if(position == (al->num_elements - 1)) {
		al_delete_last(al);
		return true;
	}
	else if((position >= 0) && (position < al->num_elements)) {
		memcpy(
				al->data + (position * al->element_size),
				al->data + ((position + 1) * al->element_size),
				al->element_size * (al->num_elements - position)
			  );
		al->num_elements -= 1;
		return true;
	}
	printf("Error: deletion out of bounds\n");
	return false;
}

int al_len(Arraylist* al) {
	return al->num_elements;
}

bool al_allocate_at_least(Arraylist* al, int size) {
	if(al->max_num_elements >= size) return true;
	void* result = realloc(
			al->data,
			size * al->element_size
			);
	if(result != NULL) {
		al->data = result;
		al->max_num_elements = size;
#ifdef ARRAYLIST_DEBUG
		printf("Grew arraylist to %i\n", al->max_num_elements);
#endif
		return true;
	} else {
		return false;
	}
}


Arraylist* al_create(int type_size) {
	Arraylist* al = malloc(sizeof(Arraylist));

	al->data = malloc(sizeof(type_size) * ARRAYLIST_DEFAULT_SIZE);
	al->element_size = type_size;
	al->num_elements = 0;
	al->max_num_elements = ARRAYLIST_DEFAULT_SIZE;

	return al;
}

void al_free(Arraylist* al) {
	free(al->data);
	free(al);
}

bool al_push(Arraylist* al, void* item) {
	if(al->num_elements >= al->max_num_elements) {
		if(!al_grow(al)) {
			printf("Error: Failed to grow arraylist to %i\n", al->max_num_elements);
			return false;
		}
#ifdef ARRAYLIST_DEBUG
		printf("Grew arraylist to %i\n", al->max_num_elements);
#endif
	}
	memcpy(
			al->data + (al->num_elements * al->element_size),
			item,
			al->element_size
		  );
	al->num_elements += 1;
	return true;
}

void* al_access(Arraylist* al, int position) {
	if((position >= al->num_elements) || (position < 0))
		return NULL;
	return al->data + (al->element_size * position);
}

bool al_overwrite_and_delete(Arraylist* al, int source, int target) {
	if(source == target) return true;
	else if((source >= al->num_elements) || (target >= al->num_elements) || (source < 0) || (target < 0)) {
		printf("Error, out of bounds");
		return false;
	}
	memcpy(
			al->data + (target * al->element_size),
			al->data + (source * al->element_size),
			al->element_size
			);
	return al_delete_at(al, source);
}