-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayList.h
443 lines (379 loc) · 12.5 KB
/
arrayList.h
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^{ BEGINNING OF FILE }^^^^^^^^^^^^^^^^^^^^^^^^^^//
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdbool.h>
typedef enum Type
{
INT = sizeof(int),
FLOAT = sizeof(float),
DOUBLE = sizeof(double),
CHAR = sizeof(char)
} Type;
typedef struct arrayList
{
int size;
int index;
int elements;
int avaliableMemory;
void *array;
Type dataType;
} arrayList;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Intialisation of arrayList
void intialise_arrayList(struct arrayList *a1, int arraySize, Type dataType)
{
a1->array = calloc(arraySize, dataType); // 8 bytes
if (a1->array == NULL)
{
exit(1);
}
a1->size = arraySize; //How many elements can be stored in the array
a1->index = 0; // The current index
a1->avaliableMemory = arraySize * dataType; // The amount of memory the array has
a1->elements = 0;
a1->dataType = dataType; // For reference of the type employed
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Add elemet to arrayList
void add_element(struct arrayList *a1, int element)
{
void *status = NULL;
// No longer any space in the arrayList
if (a1->index == a1->size)
{
// Reallocates memory (no cast as realloc returns void pointer by default)
status = realloc(a1->array, a1->size * a1->dataType);
//Checks for failure in memory allocation
if (status == NULL)
{
exit(1);
}
// Inserts specified element
switch (a1->dataType)
{
case INT:
((int *)(a1->array))[a1->index++] = element;
break;
case DOUBLE:
((double *)(a1-> array))[a1->index++] = (double) element;
break;
default:
break;
}
a1->size = a1->size * 2;
a1->avaliableMemory = a1->avaliableMemory * 2;
}
else
{
// Here was the malevolent little bug, some values did not display because it was casting the pointer to a different data type
// E.g. ((int *)(a1->array))[a1->index++] = *((double *)element);
// Ensure that both data types are correct and it is smooth sailing
switch (a1->dataType)
{
case INT:
((int *)(a1->array))[a1->index++] = element;
break;
case DOUBLE:
((double *)(a1-> array))[a1->index++] = (double) element;
break;
default:
break;
}
}
a1->elements++;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Insert element at specific index shifting other elements
// Have to use a void pointer for the element
void arraylist_insert(struct arrayList *a1, int element, int index)
{
void *pointer = NULL;
pointer = malloc(a1->size * a1->dataType);
if (pointer == NULL)
{
exit(1);
}
switch (a1->dataType)
{
case INT:
for (int i = 0; i < index; i++)
{
// Inserts the values before the specified index into new memory block
((int *) pointer)[i] = ((int *)(a1->array))[i];
}
// Inserts the value into specified index of new memory block
((int *)pointer)[index] = element;
// ((int *)pointer)[index + 1] = ((int *)a1->array)[index];
// Increments number of elements to account for the new size due to shifting
a1->elements++;
// FIRST IDEATION: Inserts subsquent elements from previous memory address into the new one.
for (int i = index + 1, x = index; i < a1->elements; i++ , x++)
{
((int *)pointer)[i] = ((int *)(a1->array))[x];
}
break;
case DOUBLE:
for (int i = 0; i < index; i++)
{
((double *) pointer)[i] = ((double *)(a1->array))[i];
}
a1->elements++;
// Insert the double element
((double *)pointer)[index] = (double) element;
for (int i = index + 1, x = index; i < a1->elements + 1; i++, x++)
{
((double *)pointer)[i] = ((double *)(a1->array))[x];
}
break;
default:
break;
}
// Releases the memory of the previous arrayList
free(a1->array);
// Assigns the new pointer
a1->array = pointer;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Removes element at specific index and shifts elements down
void arraylist_remove(struct arrayList *a1, int element, int index)
{
int *pointer = NULL;
pointer = (int *) malloc(a1->size * sizeof(int));
if (pointer == NULL)
{
exit(1);
}
for (int i = 0; i < index; i++)
{
pointer[i] = a1->array[i];
}
pointer[index] = element;
for (int i = index + 1; i < a1->size; i++)
{
pointer[i] = a1->array[i];
}
free(a1->array);
a1->array = pointer;
a1->elements--;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Pops/removes the last element of the array
void arraylist_pop(struct arrayList *a1)
{
a1->array[a1->index - 1] = 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Cleares all the elements but keeps the memory allocated for array
void arraylist_clear(struct arrayList *a1)
{
int *end = a1->array + a1->index;
//Add a counter for the number of user added elements in the struct otherwise this will add unnecessary addtional operations (setting values to zero that are zero)
for (; a1->array < end; a1->array++)
{
a1->array = 0;
}
a1->elements = 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Returns elements at specifc index (maybe use void pointer so it is not type specifc)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Return the first element of the array
int arraylist_front(struct arrayList *a1)
{
return *(a1->array);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Return the last element of the array
// Requires a counter to return a user inserted value otherwise 0 place holder will be returned
int arraylist_back(struct arrayList *a1)
{
return *(a1->array + a1->elements - 1);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Return the size of the array
int arraylist_size(struct arrayList *a1)
{
//Needs counter to get the number of user inserted elements.
return a1->size;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Returns the number of bytes allocated to the arrayList
int arraylist_bytes(struct arrayList *a1)
{
return a1->avaliableMemory;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Return the current allocated capacity of the array (number of elements)
int arraylist_capacity(struct arrayList *a1)
{
return a1->size;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Return if array is empty (boolean value)
bool arraylist_is_empty(struct arrayList *a1)
{
return a1->index > 0;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Shrink the array to fit the existing elements
void arraylist_shrink_to_fit(struct arrayList *a1)
{
int* pointer = NULL;
pointer = (int *) malloc(a1->elements * sizeof(int));
if (pointer == NULL)
{
exit(1);
}
for (int i = 0; i < 8; i++)
{
pointer[i] = a1->array[i];
}
free(a1->array);
a1->array = pointer;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Searches the array for a specific index and returns false if not found
bool arraylist_find(struct arrayList *a1, int index)
{
if (a1->array[index])
{
return true;
}
return false;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Bubble Sorts the array in ascending order
void arraylist_sort_ascending(struct arrayList *a1)
{
for (int x = 0; x < a1->elements; x++)
{
for (int i = 0; i < a1->elements - 1; i++)
{
if (a1->array[i] > a1->array[i + 1])
{
int temp = a1->array[i];
a1->array[i] = a1->array[i + 1];
a1->array[i + 1] = temp;
}
}
}
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Bubble Sorts the array in descending order
void arraylist_sort_descending(struct arrayList *a1)
{
for (int x = 0; x < a1->elements; x++)
{
for (int i = 0; i < a1->elements - 1; i++)
{
if (a1->array[i] < a1->array[i + 1])
{
int temp = a1->array[i];
a1->array[i] = a1->array[i + 1];
a1->array[i + 1] = temp;
}
}
}
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Swaps two elements in the arraylist
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Reverses the order of elements within the array (NOTE: IMPROVE THE READABILITY)
void arraylist"_reverse(struct arrayList *a1)
{
int *pointer = NULL;
pointer = (int *) malloc(a1->size * sizeof(int));
if (pointer == NULL)
{
exit(1);
}
for (int i = a1->size, x = 0; i > 0; i--, x++)
{
pointer[x]= a1->array[i];
}
free(a1->array);
a1->array = pointer;
free(pointer);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Maps a function to each array element (Add a struct within the struct that acts as a map for each element)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Filter and create new array list with elements that have satisfied a condition
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Iteratres over the elements applying a function to it (for loop)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Creates a deep copy of the arraylist
arrayList deep_copy(struct arrayList *a1)
{
arrayList copy;
copy = *a1;
return copy;
}"
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Custom comparators sets a custom comparison
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Resizes the arrayList
void resizeArray(struct arrayList *a1)
{
//Check for zero value
int *pointer = NULL;
pointer = (int*) malloc(a1->size * sizeof(int));
for (int i = 0; i < a1->size - 1; i++)
{
pointer[i] = a1->array[i];
}
free((a1->array));
a1->array = (int*) malloc((a1->size - 1) * sizeof(int));
for (int i = 0; i < a1->size - 1; i++)
{
a1->array[i] = *pointer;
++pointer;
}
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Removes element from arrayList
void removeElement(struct arrayList *a1, int elementIndex)
{
a1->array[elementIndex] = 0;
//Add resize array function
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Displays all elements in the arrayList
// Add a switch statement to display different types
void displayElements(struct arrayList *a1)
{
switch (a1->dataType)
{
case INT:
//use a counter in the struct to determine the correct number of elements ot print instead of filtering by zero (which maybe an actual element)
for (int i = 0; i < a1->elements; i++)
{
// if (a1->array[i] != 0)
// {
// printf("%d ", a1->array[i]);
// }
printf("%d ", ((int *)a1->array)[i]);
}
break;
case DOUBLE:
for (int i = 0; i < a1->elements; i++)
{
// if (a1->array[i] != 0)
// {
// printf("%d ", a1->array[i]);
// }
printf("%f ", ((double *)a1->array)[i]);
}
break;
default:
break;
}
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//
// Displays element at specified index in the arrayList
void displayElement(struct arrayList *a1, int index)
{
printf("\nElement at index %d : %d", index, a1->array[index]);
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^{ END OF FILE }^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//"