Derive-C
Loading...
Searching...
No Matches
vector.c File Reference
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <derive-c/allocs/std.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_data

Macros

#define T   int32_t
#define SELF   vec_ints
#define T   struct complex_data
#define T_DELETE   complex_data_delete
#define SELF   vec_complex_data
#define T   char
#define SELF   char_vec

Functions

void ints_example ()
void complex_data_delete (struct complex_data *self)
void complex_data_example ()
void iterate_example ()
int main ()

Macro Definition Documentation

◆ SELF [1/3]

#define SELF   vec_ints

Definition at line 15 of file vector.c.

◆ SELF [2/3]

#define SELF   vec_complex_data

Definition at line 15 of file vector.c.

◆ SELF [3/3]

#define SELF   char_vec

Definition at line 15 of file vector.c.

◆ T [1/3]

#define T   int32_t

Definition at line 14 of file vector.c.

◆ T [2/3]

#define T   struct complex_data

Definition at line 14 of file vector.c.

◆ T [3/3]

#define T   char

Definition at line 14 of file vector.c.

◆ T_DELETE

#define T_DELETE   complex_data_delete

Definition at line 52 of file vector.c.

Function Documentation

◆ complex_data_delete()

void complex_data_delete ( struct complex_data * self)

Definition at line 49 of file vector.c.

49{ free(self->description); }
static void free(SELF *self, void *ptr)
Definition template.h:62
char * description
Definition option.c:12
Here is the call graph for this function:
Here is the caller graph for this function:

◆ complex_data_example()

void complex_data_example ( )
Examples
structures/vector.c.

Definition at line 56 of file vector.c.

56 {
57 vec_complex_data vec = vec_complex_data_new_with_capacity(5, stdalloc_get());
58 size_t entries = 5;
59 for (size_t i = 0; i < entries; i++) {
60 struct complex_data item = {.description = strdup("Complex item"), .score = i * 10};
61 vec_complex_data_push(&vec, item);
62 }
63
64 assert(vec_complex_data_size(&vec) == entries);
65
66 struct complex_data* first_item = vec_complex_data_write(&vec, 0);
67 first_item->score += 5;
68
69 assert(vec_complex_data_read(&vec, 0)->score == 5);
70
71 struct complex_data popped = vec_complex_data_pop(&vec);
72 assert(popped.score == 40); // Last item's score should be 40
73
74 vec_complex_data_delete(&vec);
75 complex_data_delete(&popped);
76}
size_t score
Definition vector.c:46
void complex_data_delete(struct complex_data *self)
Definition vector.c:49
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 18 of file vector.c.

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

◆ iterate_example()

void iterate_example ( )
Examples
structures/vector.c.

Definition at line 82 of file vector.c.

82 {
83 char_vec vec = char_vec_new(stdalloc_get());
84 char_vec_push(&vec, 'H');
85 char_vec_push(&vec, 'e');
86 char_vec_push(&vec, 'l');
87 char_vec_push(&vec, 'l');
88 char_vec_push(&vec, 'o');
89 char_vec_push(&vec, ' ');
90 char_vec_push(&vec, 'W');
91 char_vec_push(&vec, 'o');
92 char_vec_push(&vec, 'r');
93 char_vec_push(&vec, 'l');
94 char_vec_push(&vec, 'd');
95
96 assert(char_vec_size(&vec) == 11);
97 {
98 // Iterate over the vector and print the items
99 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
100 char const* item = NULL;
101 while (item = char_vec_iter_const_next(&iter), item != NULL) {
102 printf("%c", *item);
103 }
104 printf("\n");
105 }
106
107 {
108 char_vec_iter_const iter = char_vec_get_iter_const(&vec);
109
110 char const* c = NULL;
111 size_t index = 0;
112 while ((c = char_vec_iter_const_next(&iter))) {
113 printf("entry for '%c' at index %zu\n", *c, index);
114 index++;
115 }
116 }
117
118 char_vec_delete(&vec);
119}
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 121 of file vector.c.

121 {
122 ints_example();
125}
void ints_example()
Definition vector.c:18
void complex_data_example()
Definition vector.c:56
void iterate_example()
Definition vector.c:82
Here is the call graph for this function: