summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-01-04 11:18:15 +0100
committerGitHub <[email protected]>2021-01-04 11:18:15 +0100
commit884c017ad973d413f182b17f36864242a0478e0b (patch)
treeb332cbdceaf380a21bc111cc6ea7f4633b17100c
parent361ecbcdad468c64eebc13c6770a8ec8d681169a (diff)
downloadSTC-modified-884c017ad973d413f182b17f36864242a0478e0b.tar.gz
STC-modified-884c017ad973d413f182b17f36864242a0478e0b.zip
Update cmap_api.md
-rw-r--r--docs/cmap_api.md24
1 files changed, 13 insertions, 11 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 36dc829a..ae0a5285 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -204,17 +204,17 @@ Output:
```
### Example 3
-Demonstrate cmap with plain-old-data struct key type Vec3i: cmap<Vec3i, int>:
+Demonstrate cmap with plain-old-data key type Vec3i and int as mapped type: cmap<Vec3i, int>.
```c
#include "stc/cmap.h"
#include <stdio.h>
typedef struct { int x, y, z; } Vec3i;
-using_cmap(v3, Vec3i, int, c_default_del,
- c_default_clone,
- c_mem_equals,
- c_default_hash32);
+using_cmap(v3, Vec3i, int, c_default_del, // mapped: empty int destroy func
+ c_default_clone, // mapped: clone int by "memcpy"
+ c_mem_equals, // key: compare Vec3i bit-by-bit
+ c_default_hash32); // key: hash Vec3i in 32-bits word-by-word.
int main()
{
@@ -280,8 +280,8 @@ Demonstrate a complex key type.
#include <stc/cstr.h>
typedef struct Viking {
- cstr_t name;
- cstr_t country;
+ cstr name;
+ cstr country;
} Viking;
void viking_del(Viking* vk) {
@@ -319,19 +319,20 @@ using_cmap(vk, Viking, int, c_default_del, c_default_clone,
vikingraw_equals, vikingraw_hash,
viking_del, viking_fromRaw, viking_toRaw, VikingRaw);
-
-int main() {
+int main()
+{
cmap_vk vikings = cmap_vk_init();
c_push_items(&vikings, cmap_vk, {
{ {"Einar", "Norway"}, 20},
{ {"Olaf", "Denmark"}, 24},
{ {"Harald", "Iceland"}, 12},
});
-
+ cmap_vk_put(&vikings, (VikingRaw){"Bjorn", "Sweden"}, 10);
+
VikingRaw lookup = {"Einar", "Norway"};
cmap_vk_entry_t *e = cmap_vk_find(&vikings, lookup);
- e->second += 3; // add 3 hp points
+ e->second += 3; // add 3 hp points to Einar
cmap_vk_emplace(&vikings, lookup, 0).first->second += 5; // add 5 more to Einar
c_foreach (k, cmap_vk, vikings) {
@@ -343,6 +344,7 @@ int main() {
Output:
```
Olaf of Denmark has 24 hp
+Bjorn of Sweden has 10 hp
Einar of Norway has 28 hp
Harald of Iceland has 12 hp
```