Vector 0.0.2
Loading...
Searching...
No Matches
create.c
1#include "vector.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <assert.h>
5
6void default_create(void);
7void explicit_opts_create(void);
8void extended_header(void);
9void cloning(void);
10
11int main(void)
12{
13 default_create();
14 explicit_opts_create();
15 extended_header();
16 cloning();
17
18 return 0;
19}
20
21void default_create(void)
22{
23 vector_t *vector = vector_create(.element_size = sizeof(int));
24
25 if (!vector)
26 {
27 perror("vector_create");
28 abort();
29 }
30
31 // ...
32
33 vector_destroy(vector);
34}
35
36void explicit_opts_create(void)
37{
38 vector_t *vector = vector_create (
39 .element_size = sizeof(float),
40 .initial_cap = 100
41 );
42
43 assert(vector && "Buy more RAM!");
44
45 // ...
46
47 vector_destroy(vector);
48}
49
50void extended_header(void)
51{
52 typedef struct
53 {
54 size_t meta;
55 // ...
56 // ...
57 // ...
58 }
59 ext_t;
60
61 vector_t *vector = vector_create (
62 .ext_header_size = sizeof(ext_t),
63 .element_size = sizeof(long)
64 );
65
66 // ...
67
68 ext_t *header = vector_get_ext_header(vector);
69 *header = (ext_t) {0};
70
71 // ...
72
73 vector_destroy(vector);
74}
75
76void cloning(void)
77{
78 vector_opts_t opts = {.element_size = sizeof(int)};
79 vector_t *vector = vector_create_(&opts);
80
81 vector_t *clone = vector_clone(vector);
82
83 vector_destroy(vector);
84 vector_destroy(clone);
85}
86
void * vector_get_ext_header(const vector_t *const vector)
Provides a location where user can put a header for the derived class.
Definition vector.c:162
#define vector_create(...)
Vector constructor.
Definition vector.h:162
vector_t * vector_clone(const vector_t *const vector)
Duplicates a vector.
Definition vector.c:119
void vector_destroy(vector_t *const vector)
Deallocates vector.
Definition vector.c:112
vector_t * vector_create_(const vector_opts_t *const opts)
Vector contructor.
Definition vector.c:82
Vector options.
Definition vector.h:52
size_t element_size
Size of the underling element type.
Definition vector.h:56
Vector control structure type.
Definition vector.c:24
Public interface of the vector.