-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
190 lines (159 loc) · 4.55 KB
/
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "list.h"
#include "interpreter.h"
#include "listwrapper.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//nitializes a list by allocating memory for the buffer to store elements. It sets initial values for the list
void listInit(list *self, size_t size_elem)
{
if (self == NULL) {
NULL_ERROR(append);
return;
}
self->empty = true; //ist is initially empty
self->num_elems = 0;
// initialize the list with INIT_BUF_SIZE elements worth of space self ->
self->size_elem = size_elem;
self->buf = calloc(INIT_BUF_SIZE, self->size_elem);
self->mem = INIT_BUF_SIZE * self->size_elem;
if ((self->buf) == NULL) {
fprintf(stderr, "Error: malloc() returned null in init()!\n");
return;
}
}
/*function appends an element to the end of the list, ensuring that the list buffer has sufficient memory to accommodate the new element. If the buffer is full, it automatically expands the list to allocate additional memory*/
void listAppend(list *self, void *data)
{
if (self == NULL) {
NULL_ERROR(append);
return;
}
//error handling
else if (data == NULL) {
fprintf(stderr, "Error: append() called with invalid data!\n");
return;
}
if (self->empty) {
self->empty = false; //list is no longer empty
}
void *start = self->buf + (self->size_elem * self->num_elems);
void *limit = self->buf + self->mem;
if (start >= limit) {
fprintf(stderr, "Error: insufficient memory to append element while "
"calling append()!\n");
return;
}
memmove(start, data, self->size_elem);
self->num_elems++;
listExpand(self);
}
/*inserts an element at the specified index in the list, shifting existing elements to make room*/
void listInsertAt(list *self, unsigned index, void *data)
{
if (self == NULL) {
NULL_ERROR(insertAt);
return;
}
// note that if index == num_elems, it's equivalent to appending
else if (index > self->num_elems) {
fprintf(stderr,
"Error: invalid index used while calling insertAt()!\n");
return;
}
if (self->empty) {
self->empty = false;
}
// move elements after index to make room for the element
void *before_move = self->buf + (self->size_elem * index);
void *after_move = before_move + self->size_elem;
memmove(after_move, before_move,
(self->num_elems - index) * self->size_elem);
// copy the elements data into the space we just made
memmove(before_move, data, self->size_elem);
self->num_elems++;
listExpand(self);
}
void listDeleteAt(list *self, unsigned index)
{
if (self == NULL) {
NULL_ERROR(deleteAt);
return;
}
//to ensure index is within the range of existing elements
else if (index >= self->num_elems) {
fprintf(stderr,
"Error: invalid index used while calling deleteAt()!\n");
return;
}
// perform the deletion by moving elements right after the index back by one
void *before_move = self->buf + (self->size_elem * (index + 1));
void *after_move = before_move - self->size_elem;
memmove(after_move, before_move,
(self->num_elems - index - 1) * self->size_elem);
self->num_elems--;
listShrink(self);
if (self->num_elems == 0) {
self->empty = true;
}
}
/*way to access elements in the list by returning a pointer to the element at the specified index*/
void *listAt(list *self, unsigned index)
{
if (self == NULL) {
NULL_ERROR(at);
return NULL;
}
else if (index >= self->num_elems) {
fprintf(stderr, "Error: invalid index used while calling at()!\n");
return NULL;
}
void *retval = self->buf + (self->size_elem * index);
return retval;
}
// used to maintain that the list has space for at least twice as many elements
// as the current number of elements
void listExpand(list *self)
{
if (self == NULL) {
NULL_ERROR(expand);
return;
}
// double the list memory if needed
if ((self->mem) < 2 * (self->num_elems) * (self->size_elem)) {
self->mem = 2 * self->mem;
self->buf = realloc(self->buf, self->mem);
}
if ((self->buf) == NULL) {
fprintf(stderr, "Error: malloc() returned null in expand()!\n");
return;
}
}
// shrink the array if it can store more than 4 times the number of elements
// as the current number of elements
void listShrink(list *self)
{
if (self == NULL) {
NULL_ERROR(shrink);
return;
}
if ((self->mem) > 4 * (self->num_elems) * (self->size_elem)) {
self->mem = self->mem / 2;
self->buf = realloc(self->buf, self->mem);
}
if ((self->buf) == NULL) {
fprintf(stderr, "Error: malloc() returned null in expand()!\n");
return;
}
}
void listDestroy(list *self)
{
if (self == NULL) {
NULL_ERROR(destroy);
return;
}
if (self->buf != NULL)
free(self->buf);
}
GENERATE_LIST_WRAPPER(User_Parameters);