-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.c
More file actions
152 lines (126 loc) · 3.52 KB
/
array.c
File metadata and controls
152 lines (126 loc) · 3.52 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
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
#include "array.h"
struct FixedArray *create_f_array(size_t capacity, enum TypeName fixed_type){
if(capacity < 0){
perror("Can't use a negtivie value as capacity.");
return;
}
// Allocate the array in the memory.
struct FixedArray *f_array = malloc(
sizeof(struct FixedArray));
// We are allocating the size of the array
// in the memory.
f_array->capacity = capacity;
f_array->data = malloc(sizeof(void *) * f_array->capacity);
f_array->fixed_type = fixed_type;
f_array->size = 0;
return f_array;
}
void append_to_f_array(struct FixedArray *f_array, void *element){
if(typename(element) != typename(f_array->fixed_type)){
perror("Can't accept the type of that value.");
}
if(f_array->size == f_array->capacity){
f_array->capacity *= 2;
f_array->data = realloc(
f_array->data, f_array->capacity * sizeof(void *)
);
}
f_array->data[f_array->size++] = element;
}
void *remove_f_array(struct FixedArray *f_array, int index){
if (index >= f_array->size) {
perror("Array is empty.");
}
// Shift all the elements after the index to the left.
void* element = f_array->data[index];
for (size_t i = index + 1; i < f_array->size; i++) {
f_array->data[i - 1] = f_array->data[i];
free(f_array->data[i - 1]);
}
// Decrement the size of the array.
f_array->size--;
return element;
}
void *pop_f_array(struct FixedArray *f_array){
if (f_array->size == 0) {
perror("Array is empty.");
}
void *element = f_array->data[f_array->size - 1];
free(f_array->data[f_array->size - 1]);
f_array->size--;
return element;
}
int count_f_array(struct FixedArray *f_array, void *element){
int count = 0;
for (int i = 0; i < f_array->size; i++) {
if(f_array->data[i] == element){
count += 1;
}
}
return count;
}
struct FixedArray *slice_f_array(struct FixedArray *f_array, int start, int end) {
if (start < 0 || end < 0 || end < start) {
if(start >= f_array->size || start > end){
perror("Check again man.");
return NULL;
}
}
struct FixedArray *new_f_array = create_f_array(
end - start, f_array->fixed_type
);
for (int i = start; i <= end; i++) {
new_f_array->data[i - start] = f_array->data[i];
}
return new_f_array;
}
void reverse_f_array(struct FixedArray *f_array){
if (f_array->size < 1) {
perror("Can't have a size less than 1");
return;
}
int start = 0;
int end = f_array->size-1;
while (end > start){
void *temp = f_array->data[start];
f_array->data[start] = f_array->data[end];
f_array->data[end] = temp;
start++; end--;
}
}
void sort_f_array(struct FixedArray *f_array){
if (f_array->size < 1) {
perror("Can't have a size less than 1");
return;
}
for(int i = 0; i < f_array->size - 1; i++){
for(int j=1; i < f_array->size - 1; i++){
if (f_array->data[j] > f_array->data[j + 1]) {
void *temp = f_array->data[j];
f_array->data[j] = f_array->data[j + 1];
f_array->data[j + 1] = temp;
}
}
}
}
void free_f_array(struct FixedArray *f_array){
for(int i = 0; i < f_array->size; i++){
free(f_array->data[i]);
}
free(f_array);
}
void print_f_array(struct FixedArray *f_array){
printf("[");
for (int i = 0; i < f_array->size; i++) {
if (f_array->fixed_type == PTR_T_STR) {
printf("\"%s\"", (char*)f_array->data[i]);
}
else if (f_array->fixed_type == PTR_T_INT) {
printf("%d", *(int*)f_array->data[i]);
}
if (i != f_array->size - 1) {
printf(",");
}
}
printf("]\n");
}