summaryrefslogtreecommitdiff
path: root/graphs/piscine/vector/vector.h
blob: 5afada7f1705967b645173e082dd4da8d2beefb7 (plain)
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
#ifndef VECTOR_H
#define VECTOR_H

#include <stddef.h>

struct vector
{
    // The number of elements in the vector
    size_t size;
    // The maximum number of elements in the vector
    size_t capacity;
    // The elements themselves
    int *data;
};

/*
** Initialize the vector with `n` capacity.
** Returns `NULL` if an error occured.
*/
struct vector *vector_init(size_t n);

/*
** Release the memory used by the vector.
** Does nothing if `v` is `NULL`.
*/
void vector_destroy(struct vector *v);

/*
** Resize the vector to `n` capacity.
** Returns `NULL` if an error occured.
*/
struct vector *vector_resize(struct vector *v, size_t n);

/*
** Append an element to the end of the vector. Expand the vector if needed.
** Returns `NULL` if an error occured.
*/
struct vector *vector_append(struct vector *v, int elt);

/*
** Display the vector contents on `stdout`.
** Displays `\n` if `v` is `NULL`.
*/
void vector_print(const struct vector *v);

/*
** Remove all the elements of the vector, and resize it to `n` capacity.
** Returns `NULL` if an error occured.
*/
struct vector *vector_reset(struct vector *v, size_t n);

/*
** Insert `n` at the index `i`. Expand the vector if needed.
** Returns `NULL` if an error occured.
*/
struct vector *vector_insert(struct vector *v, size_t i, int elt);

/*
** Remove the element at the index `i`.
** Returns `NULL` if an error occured.
*/
struct vector *vector_remove(struct vector *v, size_t i);

#endif /* !VECTOR_H */