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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
# STC [ccommon](../include/stc/ccommon.h): Generic algorithms and macros
The following macros are recommended to use, and they safe/have no side-effects.
## Scope macros (RAII)
### c_AUTO, c_WITH, c_SCOPE, c_DEFER
General ***defer*** mechanics for resource acquisition. These macros allows you to specify the
freeing of the resources at the point where the acquisition takes place.
The **checkauto** utility described below, ensures that the `c_AUTO*` macros are used correctly.
| Usage | Description |
|:---------------------------------------|:----------------------------------------------------------|
| `c_WITH (Type var=init, drop)` | Declare `var`. Defer `drop...` to end of scope |
| `c_WITH (Type var=init, pred, drop)` | Adds a predicate in order to exit early if init failed |
| `c_AUTO (Type, var1,...,var4)` | `c_WITH (Type var1=Type_init(), Type_drop(&var1))` ... |
| `c_SCOPE (init, drop)` | Execute `init` and defer `drop` to end of scope |
| `c_DEFER (drop...)` | Defer `drop...` to end of scope |
| `continue` | Exit a block above without memory leaks |
For multiple variables, use either multiple **c_WITH** in sequence, or declare variable outside
scope and use **c_SCOPE**. For convenience, **c_AUTO** support up to 4 variables.
```c
// `c_WITH` is similar to python `with`: it declares and can drop a variable after going out of scope.
bool ok = false;
c_WITH (uint8_t* buf = malloc(BUF_SIZE), buf != NULL, free(buf))
c_WITH (FILE* fp = fopen(fname, "rb"), fp != NULL, fclose(fp))
{
int n = fread(buf, 1, BUF_SIZE, fp);
if (n <= 0) continue; // auto cleanup! NB do not break or return here.
...
ok = true;
}
return ok;
// `c_AUTO` automatically initialize and destruct up to 4 variables, like c_WITH.
c_AUTO (cstr, s1, s2)
{
cstr_append(&s1, "Hello");
cstr_append(&s1, " world");
cstr_append(&s2, "Cool");
cstr_append(&s2, " stuff");
printf("%s %s\n", cstr_str(&s1), cstr_str(&s2));
}
c_WITH (cstr str = cstr_lit("Hello"), cstr_drop(&str))
{
cstr_append(&str, " world");
printf("%s\n", cstr_str(&str));
}
// `c_SCOPE` is like `c_WITH` but works with an already declared variable.
static pthread_mutex_t mut;
c_SCOPE (pthread_mutex_lock(&mut), pthread_mutex_unlock(&mut))
{
/* Do syncronized work. */
}
// `c_DEFER` executes the expressions when leaving scope. Prefer c_WITH or c_SCOPE.
cstr s1 = cstr_lit("Hello"), s2 = cstr_lit("world");
c_DEFER (cstr_drop(&s1), cstr_drop(&s2))
{
printf("%s %s\n", cstr_str(&s1), cstr_str(&s2));
}
```
**Example**: Load each line of a text file into a vector of strings:
```c
#include <errno.h>
#include <stc/cstr.h>
#define i_val_str
#include <stc/cvec.h>
// receiver should check errno variable
cvec_str readFile(const char* name)
{
cvec_str vec = cvec_str_init(); // returned
c_WITH (FILE* fp = fopen(name, "r"), fp != NULL, fclose(fp))
c_WITH (cstr line = cstr_NULL, cstr_drop(&line))
while (cstr_getline(&line, fp))
cvec_str_emplace_back(&vec, cstr_str(&line));
return vec;
}
int main()
{
c_WITH (cvec_str x = readFile(__FILE__), cvec_str_drop(&x))
c_FOREACH (i, cvec_str, x)
printf("%s\n", cstr_str(i.ref));
}
```
### The **checkauto** utility program (for RAII)
The **checkauto** program will check the source code for any misuses of the `c_AUTO*` macros which
may lead to resource leakages. The `c_AUTO*`- macros are implemented as one-time executed **for-loops**,
so any `return` or `break` appearing within such a block will lead to resource leaks, as it will disable
the cleanup/drop method to be called. A `break` may originally be intended to break a loop or switch
outside the `c_AUTO` scope.
NOTE: One must always make sure to unwind temporary allocated resources before a `return` in C. However, by using `c_AUTO*`-macros,
- it is much easier to automatically detect misplaced return/break between resource acquisition and destruction.
- it prevents forgetting to call the destructor at the end.
The **checkauto** utility will report any misusages. The following example shows how to correctly break/return
from a `c_AUTO` scope:
```c
int flag = 0;
for (int i = 0; i<n; ++i) {
c_AUTO (cstr, text)
c_AUTO (List, list)
{
for (int j = 0; j<m; ++j) {
List_push_back(&list, i*j);
if (cond1())
break; // OK: breaks current for-loop only
}
// WRONG:
if (cond2())
break; // checkauto ERROR! break inside c_AUTO.
if (cond3())
return -1; // checkauto ERROR! return inside c_AUTO
// CORRECT:
if (cond2()) {
flag = 1; // flag to break outer for-loop
continue; // cleanup and leave c_AUTO block
}
if (cond3()) {
flag = -1; // return -1
continue; // cleanup and leave c_AUTO block
}
...
}
// do the return/break outside of c_AUTO
if (flag < 0) return flag;
else if (flag > 0) break;
...
} // for
```
## Loop abstraction macros
### c_FORLIST
Iterate compound literal array elements. Additional to `i.ref`, you can access `i.data`, `i.size`, and `i.index` of the input list/element.
```c
// apply multiple push_backs
c_FORLIST (i, int, {1, 2, 3})
cvec_i_push_back(&vec, *i.ref);
// insert in existing map
c_FORLIST (i, cmap_ii_raw, { {4, 5}, {6, 7} })
cmap_ii_insert(&map, i.ref->first, i.ref->second);
// string literals pushed to a stack of cstr:
c_FORLIST (i, const char*, {"Hello", "crazy", "world"})
cstack_str_emplace(&stk, *i.ref);
// reverse the list:
c_FORLIST (i, int, {1, 2, 3})
cvec_i_push_back(&vec, i.data[i.size - 1 - i.index]);
```
### c_FOREACH, c_FORPAIR
| Usage | Description |
|:-----------------------------------------|:--------------------------------|
| `c_FOREACH (it, ctype, container)` | Iteratate all elements |
| `c_FOREACH (it, ctype, it1, it2)` | Iterate the range [it1, it2) |
| `c_FORPAIR (key, val, ctype, container)` | Iterate with structured binding |
```c
#define i_key int
#define i_val int
#define i_tag ii
#include <stc/csmap.h>
...
c_FORLIST (i, csmap_ii_raw, { {23,1}, {3,2}, {7,3}, {5,4}, {12,5} })
csmap_ii_insert(&map, i.ref->first, i.ref->second);
c_FOREACH (i, csmap_ii, map)
printf(" %d", i.ref->first);
// 3 5 7 12 23
csmap_ii_iter it = csmap_ii_find(&map, 7);
c_FOREACH (i, csmap_ii, it, csmap_ii_end(&map))
printf(" %d", i.ref->first);
// 7 12 23
c_FORPAIR (id, count, csmap_ii, map)
printf(" (%d %d)", *_.id, *_.count);
// (3 2) (5 4) (7 3) (12 5) (23 1)
```
### c_FORRANGE
Abstraction for iterating sequence of numbers. Like python's **for** *i* **in** *range()* loop.
| Usage | Python equivalent |
|:--------------------------------------------|:-------------------------------------|
| `c_FORRANGE (stop)` | `for _ in range(stop):` |
| `c_FORRANGE (i, stop) // i type = long long` | `for i in range(stop):` |
| `c_FORRANGE (i, start, stop)` | `for i in range(start, stop):` |
| `c_FORRANGE (i, start, stop, step)` | `for i in range(start, stop, step):` |
```c
c_FORRANGE (5) printf("x");
// xxxxx
c_FORRANGE (i, 5) printf(" %lld", i);
// 0 1 2 3 4
c_FORRANGE (i, -3, 3) printf(" %lld", i);
// -3 -2 -1 0 1 2
c_FORRANGE (i, 30, 0, -5) printf(" %lld", i);
// 30 25 20 15 10 5
```
### c_FORWHILE, c_FORFILTER
Iterate containers with stop-criteria and chained range filtering.
| Usage | Description |
|:----------------------------------------------------|:---------------------------------------|
| `c_FORWHILE (it, ctype, start, pred)` | Iterate until pred is false |
| `c_FORFILTER (it, ctype, container, filter)` | Filter out items in chain with && |
| `c_FORFILTER (it, ctype, container, filter, pred)` | Filter and iterate until pred is false |
| Built-in filter | Description |
|:----------------------------------|:-------------------------------------|
| `c_flt_skip(it, numItems)` | Skip numItems |
| `c_flt_take(it, numItems)` | Take numItems |
| `c_flt_skipwhile(it, predicate)` | Skip items until predicate is false |
| `c_flt_takewhile(it, predicate)` | Take items until predicate is false |
`it.index` holds the index of the source item, and `it.count` the current number of items taken.
```c
#define i_type IVec
#define i_val int
#include <stc/cstack.h>
#include <stdio.h>
bool isPrime(int i) {
for (int j=2; j*j <= i; ++j) if (i % j == 0) return false;
return true;
}
#define isOdd(i) ((i) & 1)
int main() {
c_AUTO (IVec, vec) {
c_FORRANGE (i, 1000) IVec_push(&vec, 1000000 + i);
c_FORFILTER (i, IVec, vec,
isOdd(*i.ref)
&& c_flt_skip(i, 100) // built-in
&& isPrime(*i.ref)
, c_flt_take(i, 10)) { // breaks loop on false.
printf(" %d", *i.ref);
}
puts("");
}
}
// Out: 1000211 1000213 1000231 1000249 1000253 1000273 1000289 1000291 1000303 1000313
```
Note that `c_flt_take()` is given as an optional argument, which makes the loop stop when it becomes false (for efficiency). Chaining it after `isPrime()` instead will give same result, but the full input is processed.
### c_make, c_new, c_delete
- **c_make**: Make a container from a literal initializer list. Example:
```c
#define i_val_str // cstr value type
#include <stc/cset.h>
#define i_key int
#define i_val int
#include <stc/cmap.h>
...
cset_str myset = c_make(cset_str, {"This", "is", "the", "story"}); // note: const char* values given!
int x = 7, y = 8;
cmap_int mymap = c_make(cmap_int, { {1, 2}, {3, 4}, {5, 6}, {x, y} });
```
- **c_new(Type)**: Allocate *and init* a new object on the heap
- **c_delete(Type, ptr)**: Drop *and free* an object allocated on the heap
```c
#include <stc/cstr.h>
cstr *stringptr = c_new(cstr, cstr_from("Hello"));
printf("%s\n", cstr_str(stringptr));
c_delete(cstr, stringptr);
```
### crange
- **crange** is a number sequence generator type, similar to [boost::irange](https://www.boost.org/doc/libs/release/libs/range/doc/html/range/reference/ranges/irange.html). The **crange_value** type is `long long`. Below *start*, *stop*, and *step* are of type *crange_value*:
```c
crange& crange_obj(...) // create a compound literal crange object
crange crange_make(stop); // will generate 0, 1, ..., stop-1
crange crange_make(start, stop); // will generate start, start+1, ... stop-1
crange crange_make(start, stop, step); // will generate start, start+step, ... upto-not-including stop
// note that step may be negative.
crange_iter crange_begin(crange* self);
crange_iter crange_end(crange* self);
void crange_next(crange_iter* it);
// 1. All primes less than 32:
crange r1 = crange_make(3, 32, 2);
printf("2"); // first prime
c_FORFILTER (i, crange, r1
, isPrime(*i.ref))
printf(" %lld", *i.ref);
// 2 3 5 7 11 13 17 19 23 29 31
// 2. The 11 first primes:
printf("2");
c_FORFILTER (i, crange, crange_obj(3, INT64_MAX, 2)
, isPrime(*i.ref)
, c_flt_take(10))
printf(" %lld", *i.ref);
// 2 3 5 7 11 13 17 19 23 29 31
```
### c_find_if, c_erase_if, c_swap, c_drop
Find or erase linearily in containers using a predicate
```c
// Search vec for first value > 2:
cvec_i_iter i;
c_find_if(i, cvec_i, vec, *i.ref > 2);
if (i.ref) printf("%d\n", *i.ref);
// Search map for a string containing "hello" and erase it:
cmap_str_iter it, it1 = ..., it2 = ...;
c_find_if(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello"));
if (it.ref) cmap_str_erase_at(&map, it);
// Erase all strings containing "hello":
// Note 1: iter i need not be declared.
// Note 2: variables index and count can be accessed in predicate.
c_erase_if(i, csmap_str, map, cstr_contains(i.ref, "hello"));
// Safe macro for swapping internals of two objects of same type:
c_swap(cmap_int, &map1, &map2);
// Drop multiple containers of same type:
c_drop(cvec_i, &vec1, &vec2, &vec3);
```
### General predefined template parameter functions
```c
int c_default_cmp(const Type*, const Type*);
Type c_default_clone(Type val); // simple copy
Type c_default_toraw(const Type* val); // dereference val
void c_default_drop(Type* val); // does nothing
typedef const char* crawstr;
int crawstr_cmp(const crawstr* x, const crawstr* y);
bool crawstr_eq(const crawstr* x, const crawstr* y);
uint64_t crawstr_hash(const crawstr* x);
```
### c_malloc, c_calloc, c_realloc, c_free: customizable allocators
Memory allocator wrappers that uses signed sizes.
### c_arraylen
Return number of elements in an array. array must not be a pointer!
```c
int array[] = {1, 2, 3, 4};
intptr_t n = c_arraylen(array);
```
|