forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvector.c
110 lines (97 loc) · 2.29 KB
/
vector.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
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ13.
*
* Codename:
* INI: Ni Interpreter
*
* Description:
* https://wis.fit.vutbr.cz/FIT/st/course-files-st.php/course/IFJ-IT/projects/ifj2013.pdf
*
* Project's GitHub repository:
* https://github.com/metthal/IFJ-Projekt
*
* Team:
* Marek Milkovič (xmilko01)
* Lukáš Vrabec (xvrabe07)
* Ján Spišiak (xspisi03)
* Ivan Ševčík (xsevci50)
* Marek Bertovič (xberto00)
*/
#include "vector.h"
#include "nierr.h"
#include <stdlib.h>
#include <string.h>
void vectorReserve(Vector *vec, uint32_t capacity)
{
// Would require deallocation of lost members
if (capacity <= vec->capacity)
return;
void *tmp;
if (vec->capacity > 0)
tmp = realloc(vec->data, vec->itemSize * capacity);
else
tmp = malloc(vec->itemSize * capacity);
if (tmp == NULL) {
setError(ERR_Allocation);
return;
}
vec->data = tmp;
vec->end = vec->data + vec->size * vec->itemSize;
vec->capacity = capacity;
memset(vec->end, 0, (vec->capacity - vec->size) * vec->itemSize);
}
void vectorShrinkToFit(Vector *vec)
{
if (vec->size == 0) {
free(vec->data);
vec->end = vec->data = NULL;
vec->capacity = 0;
}
else {
void *tmp = realloc(vec->data, vec->itemSize * vec->size);
if (tmp == NULL) {
setError(ERR_Allocation);
return;
}
vec->data = tmp;
vec->end = vec->data + vec->size * vec->itemSize;
vec->capacity = vec->size;
}
}
uint32_t vectorCapacity(const Vector *vec)
{
return vec->capacity;
}
void* vectorAt(Vector *vec, uint32_t index)
{
if (vec->size <= index) {
setError(ERR_Range);
return NULL;
}
return vec->data + vec->itemSize * index;
}
const void* vectorAtConst(const Vector *vec, uint32_t index)
{
if (vec->size <= index) {
setError(ERR_Range);
return NULL;
}
return vec->data + vec->itemSize * index;
}
void* vectorFront(Vector *vec)
{
if (vec->size == 0) {
setError(ERR_Range);
return NULL;
}
return vec->data;
}
void* vectorBack(Vector *vec)
{
if (vec->size == 0) {
setError(ERR_Range);
return NULL;
}
return vec->end - vec->itemSize;
}