Skip to content

Commit fa0368d

Browse files
committed
More docs/godbolt links.
1 parent 72c01ed commit fa0368d

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

docs/algorithm_api.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ descriptive and reduces chances of making mistakes. It is generally easier to re
2323
| `c_foreach_n (it, ctype, container, n)`| Iteratate `n` first elements. Index variable is `{it}_index`. |
2424
| `c_foreach_kv (key, val, ctype, container)` | Iterate maps with "structured binding" |
2525
<!--{%raw%}-->
26+
[ [Run this code](https://godbolt.org/z/r8eWG4Txa) ]
2627
```c++
2728
#define i_type IMap, int, int
2829
#include "stc/smap.h"
@@ -133,6 +134,8 @@ crange& c_iota(start, stop); // l-value; otherwise like crange_ma
133134
crange& c_iota(start, stop, step); // l-value; otherwise like crange_make(start, stop, step)
134135
```
135136
The **crange_value** type is *isize*. Variables *start*, *stop*, and *step* are of type *crange_value*.
137+
138+
[ [Run this code](https://godbolt.org/z/6aaq6qTro) ]
136139
```c++
137140
// 1. All primes less than 32: See below for c_filter() and is_prime()
138141
crange r1 = crange_make(3, 32, 2);
@@ -145,10 +148,10 @@ c_filter(crange, r1, true
145148

146149
// 2. The first 11 primes:
147150
// c_iota() can be used as argument to c_filter.
148-
printf("2");
151+
printf("2"); // first prime
149152
c_filter(crange, c_iota(3), true
150153
&& is_prime(*value)
151-
&& (printf(" %zi", *value), c_flt_take(10))
154+
&& (c_flt_take(10), printf(" %zi", *value))
152155
);
153156
// 2 3 5 7 11 13 17 19 23 29 31
154157
```
@@ -365,6 +368,7 @@ int main(void) {
365368
A macro for conveniently defining functions with multiple return values. This is for encouraging
366369
to write functions that returns extra error context when error occurs, or just multiple return values.
367370

371+
[ [Run this code](https://godbolt.org/z/MsYG75Eae) ]
368372
```c++
369373
Vec get_data(void) {
370374
return c_make(Vec, {1, 2, 3, 4, 5, 6});
@@ -475,6 +479,8 @@ Append linearily in containers using a predicate. `value` is a pointer to each e
475479
Erase linearily in containers using a predicate. `value` is a pointer to each element in predicate.
476480
- `c_erase_if(CntType, cnt_ptr, pred)`. Use with **list**, **hmap**, **hset**, **smap**, and **sset**.
477481
- `c_eraseremove_if(CntType, cnt_ptr, pred)`. Use with **stack**, **vec**, **deque**, and **queue** only.
482+
483+
[ [Run this code](https://godbolt.org/z/5WWGf4dbd) ]
478484
<!--{%raw%}-->
479485
```c++
480486
#include <stdio.h>
@@ -483,7 +489,7 @@ Erase linearily in containers using a predicate. `value` is a pointer to each el
483489

484490
#define i_type Vec, int
485491
#define i_use_cmp
486-
#include "stc/vec.h"
492+
#include "stc/stack.h"
487493

488494
#define i_type List, int
489495
#define i_use_cmp
@@ -506,7 +512,6 @@ int main(void)
506512

507513
// Search vec for first value > 20.
508514
Vec_iter result;
509-
510515
c_find_if(Vec, vec, &result, *value > 20);
511516
if (result.ref) printf("found %d\n", *result.ref);
512517

@@ -589,18 +594,19 @@ The *X_sort()*, *X_sort_lowhigh()* functions are about twice as fast as *qsort()
589594
speed with *std::sort()**. Both *X_binary_seach()* and *X_lower_bound()* are about 30% faster than
590595
c++ *std::lower_bound()*.
591596
##### Usage examples
597+
598+
[ [Run this code](https://godbolt.org/z/rr1xvjcGG) ]
592599
```c++
593600
#define i_key int // sort a regular c-array of ints
594601
#include "stc/sort.h"
595602
#include <stdio.h>
596603
597604
int main(void) {
598-
int nums[] = {5, 3, 5, 9, 7, 4, 7, 2, 4, 9, 3, 1, 2, 6, 4};
599-
ints_sort(nums, c_arraylen(nums)); // `ints` derived from the `i_key` name
605+
int arr[] = {5, 3, 5, 9, 7, 4, 7, 2, 4, 9, 3, 1, 2, 6, 4};
606+
ints_sort(arr, c_arraylen(arr)); // `ints` derived from the `i_key` name
600607
c_forrange (i, c_arraylen(arr)) printf(" %d", arr[i]);
601608
}
602609
```
603-
604610
```c++
605611
#define i_type MyDeq, int
606612
#define i_use_cmp // enable sorting

docs/hmap_api.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ int main(void)
221221
### Example 4: Advanced
222222
Key type is struct. Based on https://doc.rust-lang.org/std/collections/struct.HashMap.html
223223
224-
[ [Run this code](https://godbolt.org/z/GhYKP9fzo) ]
224+
[ [Run this code](https://godbolt.org/z/K3MG6cn37) ]
225225
```c++
226226
#include <stc/cstr.h>
227227
@@ -230,25 +230,25 @@ typedef struct {
230230
cstr country;
231231
} Viking;
232232
233-
static inline Viking Viking_make(cstr_raw name, cstr_raw country) {
233+
Viking Viking_make(cstr_raw name, cstr_raw country) {
234234
return (Viking){.name = cstr_from(name), .country = cstr_from(country)};
235235
}
236236
237-
static inline bool Viking_eq(const Viking* a, const Viking* b) {
237+
bool Viking_eq(const Viking* a, const Viking* b) {
238238
return cstr_eq(&a->name, &b->name) && cstr_eq(&a->country, &b->country);
239239
}
240240
241-
static inline size_t Viking_hash(const Viking* a) {
241+
size_t Viking_hash(const Viking* a) {
242242
return cstr_hash(&a->name) ^ cstr_hash(&a->country);
243243
}
244244
245-
static inline Viking Viking_clone(Viking v) {
245+
Viking Viking_clone(Viking v) {
246246
v.name = cstr_clone(v.name);
247247
v.country = cstr_clone(v.country);
248248
return v;
249249
}
250250
251-
static inline void Viking_drop(Viking* vp) {
251+
void Viking_drop(Viking* vp) {
252252
cstr_drop(&vp->name);
253253
cstr_drop(&vp->country);
254254
}
@@ -285,7 +285,7 @@ In example 4 we needed to construct a lookup key which may allocate strings, and
285285
In this example we use keyraw feature to make it simpler to use and avoids the creation of a Viking object
286286
entirely when doing lookup.
287287

288-
[ [Run this code](https://godbolt.org/z/Yx1Ybhxqv) ]
288+
[ [Run this code](https://godbolt.org/z/zY3j5zGo9) ]
289289
<!--{%raw%}-->
290290
```c++
291291
#include "stc/cstr.h"

0 commit comments

Comments
 (0)