-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.c
More file actions
137 lines (113 loc) · 3.11 KB
/
source.c
File metadata and controls
137 lines (113 loc) · 3.11 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
#include <stdio.h>
#include <stdlib.h>
#include "header.h"
void init_array(Vector* vector){
vector->size = 0;
vector->capacity = 1;
vector->m_array = (int *)calloc(vector->capacity, sizeof(int));
}
void resize(size_t new_size, Vector* vector){
int* tmp = (int*)malloc((new_size * sizeof(int)));
if (tmp == NULL){
printf("Allocation failed");
exit(1);
}
//Copy elements to the new array
//size_t copy_size = (vector->capacity < new_size) ? vector->capacity : new_size;
for (int i = 0; i < vector->capacity; ++i) {
tmp[i] = vector->m_array[i];
}
//Free the old array
free(vector->m_array);
//Update the vector with new array and size
vector->m_array = tmp;
tmp = NULL;
}
void push_back(int val, Vector* vector){
if (vector->size >= vector->capacity){
vector->capacity *= 2;
resize(vector->capacity, vector);
}
vector->m_array[vector->size++] = val;
}
void pop_back(Vector* vector){
if (vector->size <= 0) { printf("Undefined behaviour"); }
vector->size--;
}
//return functions
int at(Vector* vector, int pos){
int position = -1;
for (int i = 0; i < vector->size; ++i){
if (pos == vector->m_array[i]){
position = i;
break;
}
}
if (position == -1) { printf("There is no element with the given position in the vector\n"); }
return position;
}
int empty(Vector* vector){
if (vector->size > 0) { return 1;}
return 0;
}
int front(Vector* vector){
if (vector->size >= 1)
return vector->m_array[0];
return 0;
}
int back(Vector* vector){
if (vector->size >= 1)
return vector->m_array[vector->size];
return 0;
}
int size(Vector* vector){
return (int) vector->size+1;
}
int capacity(Vector* vector){
return (int) vector->capacity;
}
void insert(Vector* vector, int pos, int val){
if (pos < 0 || pos > vector->size){
printf("Invalid position");
return;
}
// checking new element will fit in capacity if won't multiply capacity by 2
if (vector->size == vector->capacity) {
vector->capacity *= 2;
resize(vector->capacity, vector);
}
for(int i = (int) vector->size; i > pos; --i){
vector->m_array[i] = vector->m_array[i-1];
}
vector->size++;
vector->m_array[pos] = val;
}
void erase(Vector* vector, int pos){
if (pos < 0 || pos >= vector->size){
printf("Invalid position");
return;
}
for(int i = pos; i < vector->size - 1; ++i){
vector->m_array[i] = vector->m_array[i+1];
}
vector->size--;
}
void shrink_to_fit(Vector* vector) {
if ( vector->size == vector->capacity) {
printf("You can't change vector's capacity");
return;
}
vector->capacity = vector->size;
resize((int) vector->capacity, vector);
}
// destructor
void clear(Vector* vector){
vector->size = 0;
vector->capacity = 1;
if (vector->m_array){
free(vector->m_array);
vector->m_array = NULL;
}
vector->m_array = (int *)calloc(vector->capacity, sizeof(int));
}
void destruct(Vector* vector) { clear(vector); }