-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrayList.c
104 lines (96 loc) · 3.12 KB
/
arrayList.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
#include <stdlib.h>
#include <stdio.h>
#include "arrayList.h"
//getSize not accessed outside of this file so declare it to be static
//static essentially makes it private to this file
static int getSize(primitiveType type);
/*
* Initializes the arrayList. Sets the size to 4,
* the number of elements to 0, the type to the parameter
* type, and allocates space for the array
*/
arrayList * initialize(primitiveType type)
{
arrayList * newList = malloc(sizeof(arrayList));
newList->numElements = 0;
newList->arraySize = 4;
newList->type = type;
newList->elementSize = getSize(type);
newList->array = malloc(sizeof(getSize(type)*4));
return newList;
}
/*
* Returns the size (in bytes) of the given primitive type
*/
int getSize(primitiveType type)
{
if (type == charType)
return sizeof(char);
else if (type == shortType)
return sizeof(short);
else
return sizeof(int);
}
/*
* Adds an element to the end of the list.
*
* If the list is full, the list will be doubled in size before
* adding the new element
*/
void addElement(arrayList * arrListPtr, void * element)
{
if (arrListPtr->numElements == arrListPtr->arraySize) { //if array is full, resize and copy
void * newArray = malloc((arrListPtr->arraySize)*(arrListPtr->elementSize)*2);
int i;
for (i = 0; i < ((arrListPtr->numElements))*(arrListPtr->elementSize); i++) {
((char *)newArray)[i] = ((char *)(arrListPtr->array))[i];
}
free(arrListPtr->array);
arrListPtr->array = newArray;
arrListPtr->arraySize *= 2;
}
int i;
for (i = 0; i < (arrListPtr->elementSize); i++) {
((char *)(arrListPtr->array))[i + ((arrListPtr->numElements)*(arrListPtr->elementSize))] = ((char * )(element))[i];
}
(arrListPtr->numElements)++;
}
/**
* Removes the element in the arrayList at the given index
*/
void removeElement(arrayList * arrListPtr, int index)
{
int i;
if (arrListPtr->type == charType) {
for (i = index; i < (arrListPtr->numElements) - 1; i++)
((char *)(arrListPtr->array))[i] = ((char *)(arrListPtr->array))[i+1];
}
else if (arrListPtr->type == shortType) {
for (i = index; i < (arrListPtr->numElements) - 1; i++)
((short *)(arrListPtr->array))[i] = ((short *)(arrListPtr->array))[i+1];
}
else { //if arrListPtr->type == intType
for (i = index; i < (arrListPtr->numElements) - 1; i++)
((int *)(arrListPtr->array))[i] = ((int *)(arrListPtr->array))[i+1];
}
(arrListPtr->numElements)--;
}
/**
* Prints the values in order in the given arrayList
*/
void printArray(arrayList * arylstP)
{
int i;
printf("array size: %d, num elements: %d contents: ",
arylstP->arraySize, arylstP->numElements);
for (i = 0; i < arylstP->numElements; i++)
{
if (arylstP->type == charType)
printf("%x ", ((char *)(arylstP->array))[i] & 0xff);
else if (arylstP->type == shortType)
printf("%04x ", ((short *)(arylstP->array))[i] & 0xffff);
else if (arylstP->type == intType)
printf("%08x ", ((int *)(arylstP->array))[i] & 0xffffffff);
}
printf("\n");
}