forked from metthal/IFJ-Projekt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.h
120 lines (107 loc) · 3.02 KB
/
vector.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
/*
* 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)
*/
/**
* @file vector.h
* @brief Declares generic vector operations.
*/
#ifndef VECTOR_H
#define VECTOR_H
#include <stdint.h>
#include <stdlib.h>
/** Structure to hold data about Vector. */
typedef struct
{
uint32_t size; ///< Used size of data container.
uint32_t capacity; ///< Real size of data container.
size_t itemSize; ///< Size of single item in bytes.
uint8_t *data; ///< Data container.
uint8_t *end; ///< Access optimization member.
} Vector;
/**
* Reserves specified capacity.
* Useful when final number of items is known to prevent
* multiple expensive reallocations.
* Sets ERR_Allocation if requested space
* couldn't be allocated and preserves previous capacity
* and data content.
* Note: Supports only increase in capacity.
* @param vec Vector to reserve memory in.
* @param capacity New capacity of vector.
*/
void vectorReserve(Vector *vec, uint32_t capacity);
/**
* Shrinks vector capacity to accommodate only
* current size.
* Useful after finalizing vector changes to
* free unused space, but requires reallocation.
* Sets ERR_Allocation if reallocation to new block
* failed and preserves previous capacity and data content.
* @param vec Vector to shrink.
*/
void vectorShrinkToFit(Vector *vec);
/**
* Returns current vector capacity.
* @param vec Vector to query.
* @return Capacity of vector.
*/
uint32_t vectorCapacity(const Vector *vec);
/**
* Returns current vector size.
* @param vec Vector to query.
* @return Size of vector.
*/
static inline uint32_t vectorSize(const Vector *vec)
{
return vec->size;
}
/**
* Returns pointer to item at specified index.
* When index is out of vector range, ERR_Range
* error is set.
* @param vec Vector to operate on.
* @param index Index of item in vector.
* @return Pointer to indexed item.
*/
void* vectorAt(Vector *vec, uint32_t index);
/**
* Returns constant pointer to item at specified index.
* When index is out of vector range, ERR_Range
* error is set.
* @param vec Constant vector to operate on.
* @param index Index of item in vector.
* @return Constant pointer to indexed item.
*/
const void* vectorAtConst(const Vector *vec, uint32_t index);
/**
* Returns pointer to first item in vector.
* When vector is empty, ERR_Range error is set.
* @param vec Vector to operate on.
* @return Pointer to first item.
*/
void* vectorFront(Vector *vec);
/**
* Returns pointer to last item in vector.
* When vector is empty, ERR_Range error is set.
* @param vec Vector to operate on.
* @return Pointer to last item.
*/
void* vectorBack(Vector *vec);
#endif