-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Copy pathvector.c
168 lines (153 loc) · 4.03 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @file
* @brief This is a vector implemenation in C. A vector is an expandable array.
* @details This vector implementation in C comes with some wrapper functions that lets the user work with data without having to worrying about memory.
*/
#include <stdio.h> /// for IO operations
#include <stdlib.h> /// for malloc() and free()
#include <assert.h> /// for testing using assert()
/** This is the struct that defines the vector. */
typedef struct {
int len; ///< contains the length of the vector
int current; ///< holds the current item
int* contents; ///< the internal array itself
} Vector;
/**
* This function initilaizes the vector and gives it a size of 1
* and initializes the first index to 0.
* @params Vector* (a pointer to the Vector struct)
* @params int (the actual data to be passed to the vector)
* @returns none
*/
void init(Vector* vec, int val) {
vec->contents = (int*)malloc(sizeof(int));
vec->contents[0] = val;
vec->current = 0;
vec->len = 1;
}
/**
* This function clears the heap memory allocated by the Vector.
* @params Vector* (a pointer to the Vector struct)
* @returns: none
*/
void delete(Vector* vec) {
free(vec->contents);
}
/**
* This function clears the contents of the Vector.
* @params Vector* (a pointer to the Vector struct)
* @returns: none
*/
void clear(Vector* vec) {
delete(vec);
init(vec, 0);
}
/**
* This function returns the length the Vector.
* @params Vector* (a pointer to the Vector struct)
* @returns: int
*/
int len(Vector* vec) {
return vec->len;
}
/**
* This function pushes a value to the end of the Vector.
* @params Vector* (a pointer to the Vector struct)
* @params int (the value to be pushed)
* @returns: none
*/
void push(Vector* vec, int val) {
vec->contents = realloc(vec->contents, (sizeof(int) * (vec->len + 1)));
vec->contents[vec->len] = val;
vec->len++;
}
/**
* This function get the item at the specified index of the Vector.
* @params Vector* (a pointer to the Vector struct)
* @params int (the index to get value from)
* @returns: int
*/
int get(Vector* vec, int index) {
if(index < vec->len) {
return vec->contents[index];
}
return -1;
}
/**
* This function sets an item at the specified index of the Vector.
* @params Vector* (a pointer to the Vector struct)
* @params int (the index to set value at)
* @returns: none
*/
void set(Vector* vec, int index, int val) {
if(index < vec->len) {
vec->contents[index] = val;
}
}
/**
* This function gets the next item from the Vector each time it's called.
* @params Vector* (a pointer to the Vector struct)
* @returns: int
*/
int next(Vector* vec) {
if(vec->current == vec->len) {
vec->current = 0;
}
int current_val = vec->contents[vec->current];
vec->current++;
return current_val;
}
/**
* This function returns the pointer to the begining of the Vector.
* @params Vector* (a pointer to the Vector struct)
* @returns: void*
*/
void* begin(Vector* vec) {
return (void*)vec->contents;
}
/**
* This function prints the entire Vector as a list.
* @params Vector* (a pointer to the Vector struct)
* @returns: none
*/
void print(Vector* vec) {
int size = vec->len;
printf("[ ");
for(int count = 0; count < size; count++) {
printf("%d ", vec->contents[count]);
}
printf("]\n");
}
/**
* This function tests the functions used to work with Vectors.
* @returns: none
*/
static void test() {
Vector vec;
init(&vec, 10);
assert(get(&vec, 0) == 10);
push(&vec, 20);
assert(get(&vec, 1) == 20);
set(&vec, 0, 11);
assert(get(&vec, 0) == 11);
assert(next(&vec) == 11);
set(&vec, 1, 22);
assert(get(&vec, 1) == 22);
assert(len(&vec) == 2);
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test();
Vector vec;
init(&vec, 10);
push(&vec, 20);
print(&vec);
set(&vec, 0, 11);
set(&vec, 1, 22);
print(&vec);
printf("Length: %d\n", len(&vec));
return 0;
}