Derive-C
Loading...
Searching...
No Matches
vector.c File Reference
#include <assert.h>
#include <stdint.h>
#include <derive-c/macros/iterators.h>
#include <derive-c/structures/vector/template.h>
Include dependency graph for vector.c:

Go to the source code of this file.

Classes

struct  complex
 

Macros

#define T   int32_t
 
#define SELF   vec_ints
 
#define T   struct complex
 
#define T_DELETE   complex_delete
 
#define SELF   vec_complex
 
#define T   char
 
#define SELF   char_vec
 

Functions

void ints_example ()
 
void complex_delete (struct complex *self)
 
void complex_example ()
 
void iterate_example ()
 
int main ()
 

Macro Definition Documentation

◆ SELF [1/3]

#define SELF   vec_ints

Definition at line 11 of file vector.c.

◆ SELF [2/3]

#define SELF   vec_complex

Definition at line 11 of file vector.c.

◆ SELF [3/3]

#define SELF   char_vec

Definition at line 11 of file vector.c.

◆ T [1/3]

#define T   int32_t

Definition at line 10 of file vector.c.

◆ T [2/3]

#define T   struct complex

Definition at line 10 of file vector.c.

◆ T [3/3]

#define T   char

Definition at line 10 of file vector.c.

◆ T_DELETE

#define T_DELETE   complex_delete

Definition at line 48 of file vector.c.

Function Documentation

◆ complex_delete()

void complex_delete ( struct complex * self)
Examples
structures/vector.c.

Definition at line 45 of file vector.c.

45{ free(self->description); }
char * description
Definition vector.c:41
Here is the caller graph for this function:

◆ complex_example()

void complex_example ( )
Examples
structures/vector.c.

Definition at line 52 of file vector.c.

52 {
53 vec_complex vec = vec_complex_new_with_capacity(5);
54 size_t entries = 5;
55 for (size_t i = 0; i < entries; i++) {
56 struct complex item = {.description = strdup("Complex item"), .score = i * 10};
57 vec_complex_push(&vec, item);
58 }
59
60 assert(vec_complex_size(&vec) == entries);
61
62 struct complex* first_item = vec_complex_write(&vec, 0);
63 first_item->score += 5;
64
65 assert(vec_complex_read(&vec, 0)->score == 5);
66
67 struct complex popped = vec_complex_pop(&vec);
68 assert(popped.score == 40); // Last item's score should be 40
69
70 vec_complex_delete(&vec);
71 complex_delete(&popped);
72}
size_t score
Definition vector.c:42
void complex_delete(struct complex *self)
Definition vector.c:45
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ints_example()

void ints_example ( )
Examples
structures/vector.c.

Definition at line 14 of file vector.c.

14 {
15 vec_ints ints = vec_ints_new_with_capacity(10);
16 const int32_t upto = 100;
17
18 for (int32_t i = 0; i < upto; i++) {
19 vec_ints_push(&ints, i);
20 }
21 assert(vec_ints_size(&ints) == upto);
22
23 for (int32_t i = 0; i < upto; i++) {
24 int* value = vec_ints_write(&ints, i);
25 *value += 1; // Increment each value by 1
26 }
27
28 for (int32_t i = 0; i < upto; i++) {
29 assert(*vec_ints_read(&ints, i) == i + 1);
30 }
31
32 // Pop the last value
33 int32_t last_value = vec_ints_pop(&ints);
34 assert(last_value == upto); // Last value should be 99 + 1
35 assert(vec_ints_size(&ints) == upto - 1);
36
37 vec_ints_delete(&ints);
38}
Here is the caller graph for this function:

◆ iterate_example()

void iterate_example ( )
Examples
structures/vector.c.

Definition at line 78 of file vector.c.

78 {
79 char_vec vec = char_vec_new();
80 char_vec_push(&vec, 'H');
81 char_vec_push(&vec, 'e');
82 char_vec_push(&vec, 'l');
83 char_vec_push(&vec, 'l');
84 char_vec_push(&vec, 'o');
85 char_vec_push(&vec, ' ');
86 char_vec_push(&vec, 'W');
87 char_vec_push(&vec, 'o');
88 char_vec_push(&vec, 'r');
89 char_vec_push(&vec, 'l');
90 char_vec_push(&vec, 'd');
91
92 assert(char_vec_size(&vec) == 11);
93 {
94 // Iterate over the vector and print the items
95 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
96 char const* item = NULL;
97 while (item = char_vec_iter_const_next(&iter), item != NULL) {
98 printf("%c", *item);
99 }
100 printf("\n");
101 }
102
103 {
104 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
105 ITER_LOOP(char_vec_iter_const, iter, char const*, foo) { printf("%c", *foo); }
106 printf("\n");
107 }
108
109 char_vec_delete(&vec);
110}
#define ITER_LOOP(ITER_TYPE, ITER_NAME, VALUE_TYPE, VALUE_NAME)
Definition iterators.h:2
Definition arena.c:39
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 112 of file vector.c.

112 {
113 ints_example();
116}
void ints_example()
Definition vector.c:14
void complex_example()
Definition vector.c:52
void iterate_example()
Definition vector.c:78
Here is the call graph for this function: