-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.h
More file actions
96 lines (82 loc) · 2.69 KB
/
vector.h
File metadata and controls
96 lines (82 loc) · 2.69 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
#ifndef VECTOR_H
#define VECTOR_H
/* VECTOR_TYPE
* the type stored in the vectors
*/
#ifndef VECTOR_TYPE
#define VECTOR_TYPE int
#endif
/* REALLOC_FACTOR
* how much to expand the vector by when it runs out of space
* the value must be a positive integer
*/
#ifndef REALLOC_FACTOR
#define REALLOC_FACTOR 2
#endif
/* vector
* @param len number of elements in the vector
* @param allocated number of elements that may currently be stored
* @param elements pointer to the array of elements
*/
typedef struct {
int len;
int allocated;
VECTOR_TYPE *elements;
} vector;
/* vec_new creates a new vector of the specified length
* @param len lenght of the vector
* @return a pointer to the vector if successfull, NULL otherwise
*/
vector *vec_new(int len);
/* vec_append appends to the end of the vector
* @param vec vector to append to
* @param element int to append
* @return 0 if successful, -1 otherwise
*/
int vec_append(vector *vec, VECTOR_TYPE element);
/* vec_get retrieves the element at the specified index
* @param vec vector to in question
* @param index the index of the element
* @param result pointer to store the retrieved value in
* @return 0 if successful, NULL otherwise
*/
int vec_get(vector *vec, int index, VECTOR_TYPE *result);
/* vec_set sets the element at the specified index to the specified value
* @param vec the vector
* @param index the index of the element
* @param value the value to set
* @return 0 if successful, -1 otherwise
*/
int vec_set(vector *vec, int index, VECTOR_TYPE value);
/* vec_insert shifts all elements starting from the specified index by 1 and sets the element at the specified index to the specified value
* @param vec the vector
* @param index the index of the element
* @param value the value to set
* @return 0 if successful, -1 otherwise
*/
int vec_insert(vector *vec, int index, VECTOR_TYPE value);
/* vec_insert shifts all elements starting from the specified index by -1 (essentially removing the element at the specified index)
* @param vec the vector
* @param index the index of the element to remove
* @return 0 if successful, -1 otherwise
*/
int vec_pop(vector *vec, int index);
/* vec_reverse reverses the order of the elements in the vector
* @param vec the vector
*/
int vec_reverse(vector *vec);
/* vec_print prints out all the elements of the vector
* @param vec the vector
* @return 0 if successful, -1 otherwise
*/
int vec_print(vector *vec);
/* vec_shrink shrinks the allocated memory to match the current length of the vector
* @param vec the vector
* @return 0 if successful, -1 otherwise
*/
int vec_shrink(vector *vec);
/* vec_free frees the memory
* @param vec the vector
*/
void vec_free(vector *vec);
#endif