1#define NONSTD_IMPLEMENTATION
2#include "../nonstd.h"
3
4#include <stdio.h>
5
6// Example struct to demonstrate arrays of complex types
7typedef struct {
8 int id;
9 const char *name;
10} Person;
11
12int main(void) {
13 // Example 1: Basic integer array
14 printf("Example 1: Basic integer array\n");
15 array(int) numbers;
16 array_init(numbers);
17
18 // Push some numbers
19 for (int i = 1; i <= 5; i++) {
20 array_push(numbers, i * 10);
21 }
22
23 printf(" Array length: %zu\n", numbers.length);
24 printf(" Array capacity: %zu\n", numbers.capacity);
25 printf(" Contents: ");
26 for (size_t i = 0; i < numbers.length; i++) {
27 printf("%d ", numbers.data[i]);
28 }
29 printf("\n\n");
30
31 // Example 2: Pop elements
32 printf("Example 2: Pop elements\n");
33 int last = array_pop(numbers);
34 printf(" Popped: %d\n", last);
35 printf(" After pop: ");
36 for (size_t i = 0; i < numbers.length; i++) {
37 printf("%d ", numbers.data[i]);
38 }
39 printf("\n\n");
40
41 // Example 3: Insert and remove
42 printf("Example 3: Insert and remove\n");
43 array_insert(numbers, 2, 999); // Insert 999 at index 2
44 printf(" After insert at index 2: ");
45 for (size_t i = 0; i < numbers.length; i++) {
46 printf("%d ", numbers.data[i]);
47 }
48 printf("\n");
49
50 array_remove(numbers, 1); // Remove element at index 1
51 printf(" After remove index 1: ");
52 for (size_t i = 0; i < numbers.length; i++) {
53 printf("%d ", numbers.data[i]);
54 }
55 printf("\n\n");
56
57 // Example 4: Get and set
58 printf("Example 4: Get and set\n");
59 int value = array_get(numbers, 0);
60 printf(" Value at index 0: %d\n", value);
61 array_set(numbers, 0, 777);
62 printf(" After setting index 0 to 777: ");
63 for (size_t i = 0; i < numbers.length; i++) {
64 printf("%d ", numbers.data[i]);
65 }
66 printf("\n\n");
67
68 array_free(numbers);
69
70 // Example 5: Array with initial capacity
71 printf("Example 5: Array with initial capacity\n");
72 array(int) preallocated;
73 array_init_cap(preallocated, 100);
74 printf(" Initial capacity: %zu\n", preallocated.capacity);
75 printf(" Initial length: %zu\n", preallocated.length);
76 array_free(preallocated);
77 printf("\n");
78
79 // Example 6: foreach iteration
80 printf("Example 6: foreach iteration\n");
81 array(int) values;
82 array_init(values);
83 for (int i = 0; i < 10; i++) {
84 array_push(values, i * i); // Push squares
85 }
86
87 printf(" Using array_foreach: ");
88 int val;
89 array_foreach(values, val) {
90 printf("%d ", val);
91 }
92 printf("\n");
93
94 printf(" Using array_foreach_idx: ");
95 array_foreach_idx(values, val, idx) {
96 printf("[%zu]=%d ", idx, val);
97 }
98 printf("\n\n");
99
100 array_free(values);
101
102 // Example 7: Array of strings
103 printf("Example 7: Array of strings\n");
104 array(const char *) words;
105 array_init(words);
106
107 array_push(words, "Hello");
108 array_push(words, "World");
109 array_push(words, "from");
110 array_push(words, "C");
111
112 printf(" Words: ");
113 const char *word;
114 array_foreach(words, word) {
115 printf("%s ", word);
116 }
117 printf("\n\n");
118
119 array_free(words);
120
121 // Example 8: Array of structs
122 printf("Example 8: Array of structs\n");
123 array(Person) people;
124 array_init(people);
125
126 array_push(people, ((Person){1, "Alice"}));
127 array_push(people, ((Person){2, "Bob"}));
128 array_push(people, ((Person){3, "Charlie"}));
129
130 printf(" People:\n");
131 Person person;
132 array_foreach(people, person) {
133 printf(" ID: %d, Name: %s\n", person.id, person.name);
134 }
135 printf("\n");
136
137 array_free(people);
138
139 // Example 9: Reserve capacity
140 printf("Example 9: Reserve capacity\n");
141 array(double) measurements;
142 array_init(measurements);
143 printf(" Initial capacity: %zu\n", measurements.capacity);
144
145 array_reserve(measurements, 1000);
146 printf(" After reserve(1000): %zu\n", measurements.capacity);
147
148 array_free(measurements);
149 printf("\n");
150
151 // Example 10: Clear array
152 printf("Example 10: Clear array\n");
153 array(int) temp;
154 array_init(temp);
155 for (int i = 0; i < 5; i++) {
156 array_push(temp, i);
157 }
158 printf(" Length before clear: %zu\n", temp.length);
159 array_clear(temp);
160 printf(" Length after clear: %zu\n", temp.length);
161 printf(" Capacity after clear: %zu\n", temp.capacity);
162
163 array_free(temp);
164
165 return 0;
166}