summaryrefslogtreecommitdiffhomepage
path: root/docs/cmap_api.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-09-17 20:49:01 +0200
committerTyge Løvset <[email protected]>2021-09-17 20:49:01 +0200
commit3dffc2feff5b193fc31e552507bdf8694d966a9f (patch)
tree417e7889c40622b76c67fab1f71c0dbd67fa6443 /docs/cmap_api.md
parentf8673fa5250d47789c3d66b52aacd1be65ced619 (diff)
downloadSTC-modified-3dffc2feff5b193fc31e552507bdf8694d966a9f.tar.gz
STC-modified-3dffc2feff5b193fc31e552507bdf8694d966a9f.zip
Fixed a doc bug and improved example.
Diffstat (limited to 'docs/cmap_api.md')
-rw-r--r--docs/cmap_api.md62
1 files changed, 33 insertions, 29 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index f84a4aef..f72ee280 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -114,6 +114,7 @@ void c_default_del(Type* val); // doe
#include <stc/cstr.h>
#define i_key_str
+#define i_val_str
#include <stc/cmap.h>
int main()
@@ -318,8 +319,9 @@ Harald of Iceland has 12 hp
```
### Example 6
-Advanced 2: In example 5 we needed to construct a lookup key which allocated strings, and then had to free it after. In this example we use
-rawtype feature to make it even simpler to use. Note that we must use the emplace() methods to add "raw" type entries (otherwise compile error):
+Advanced 2: In example 5 we needed to construct a lookup key which allocated strings, and then had to free it after.
+In this example we use rawtype feature to make it even simpler to use. Note that we must use the emplace() methods
+to add "raw" type entries (otherwise compile error):
```c
#include <stc/cstr.h>
@@ -332,23 +334,25 @@ static void Viking_del(Viking* v) {
c_del(cstr, &v->name, &v->country);
}
-// Define a "raw" type with equals, hash, fromraw, toraw functions:
+// Define a "raw" type that need no allocations,
+// and define equals, hash, fromraw, toraw functions:
typedef struct {
const char* name;
const char* country;
} RViking;
-static int RViking_equals(const RViking* r1, const RViking* r2) {
- return !strcmp(r1->name, r2->name) && !strcmp(r1->country, r2->country);
-}
+static int RViking_equals(const RViking* r1, const RViking* r2)
+ { return !strcmp(r1->name, r2->name) && !strcmp(r1->country, r2->country); }
-static uint32_t RViking_hash(const RViking* r, int ignored) {
- return c_rawstr_hash(&r->name) ^ (c_rawstr_hash(&r->country) >> 15);
-}
+static uint32_t RViking_hash(const RViking* r, int ignored)
+ { return c_rawstr_hash(&r->name) ^ (c_rawstr_hash(&r->country) >> 15); }
+
+static Viking Viking_fromR(RViking r)
+ { return (Viking){cstr_from(r.name), cstr_from(r.country)}; }
-static Viking Viking_fromR(RViking r) {return (Viking){cstr_from(r.name), cstr_from(r.country)};}
-static RViking Viking_toR(const Viking* v) {return (RViking){v->name.str, v->country.str};}
+static RViking Viking_toR(const Viking* v)
+ { return (RViking){v->name.str, v->country.str}; }
#define i_tag vk
#define i_key Viking
@@ -363,24 +367,24 @@ static RViking Viking_toR(const Viking* v) {return (RViking){v->name.str, v->cou
int main()
{
- // Use a HashMap to store the vikings' health points.
- cmap_vk vikings = cmap_vk_init();
-
- // insert works as before, takes a constructed Viking object
- cmap_vk_insert(&vikings, (Viking){cstr_from("Einar"), cstr_from("Norway")}, 25);
- cmap_vk_insert(&vikings, (Viking){cstr_from("Olaf"), cstr_from("Denmark")}, 24);
-
- // but emplace is simpler to use now.
- cmap_vk_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12);
- cmap_vk_emplace(&vikings, (RViking){"Einar", "Denmark"}, 21);
-
- // and lookup uses "raw" key type, so no need construct/destruct key:
- printf("Lookup: Einar of Norway has %d hp\n\n", *cmap_vk_at(&vikings, (RViking){"Einar", "Norway"}));
-
- // print the status of the vikings.
- c_foreach (i, cmap_vk, vikings) {
- printf("%s of %s has %d hp\n", i.ref->first.name.str, i.ref->first.country.str, i.ref->second);
+ c_forauto (cmap_vk, vikings) // RAII
+ {
+ // Insert works as before, takes a constructed Viking object
+ cmap_vk_insert(&vikings, (Viking){cstr_from("Einar"), cstr_from("Norway")}, 25);
+ cmap_vk_insert(&vikings, (Viking){cstr_from("Olaf"), cstr_from("Denmark")}, 24);
+
+ // But emplace is simpler to use now - takes raw key argument
+ cmap_vk_emplace(&vikings, (RViking){"Harald", "Iceland"}, 12);
+ cmap_vk_emplace(&vikings, (RViking){"Einar", "Denmark"}, 21);
+
+ // Lookup also uses raw key type, so no need construct/destruct key:
+ printf("Lookup: Einar of Norway has %d hp\n\n", *cmap_vk_at(&vikings, (RViking){"Einar", "Norway"}));
+
+ // Print the status of the vikings.
+ c_foreach (i, cmap_vk, vikings) {
+ printf("%s of %s has %d hp\n", i.ref->first.name.str,
+ i.ref->first.country.str, i.ref->second);
+ }
}
- cmap_vk_del(&vikings);
}
```