summaryrefslogtreecommitdiffhomepage
path: root/examples/README.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-19 18:20:10 +0200
committerGitHub <[email protected]>2020-09-19 18:20:10 +0200
commit339bb8a0a791ece66c91caee2b610e17881674f0 (patch)
tree5dbd12243475c109d3e3e9e06647a5ae03434a0c /examples/README.md
parenta047bfa20b825ec67a901a81546601cb06d61f18 (diff)
downloadSTC-modified-339bb8a0a791ece66c91caee2b610e17881674f0.tar.gz
STC-modified-339bb8a0a791ece66c91caee2b610e17881674f0.zip
Update README.md
Diffstat (limited to 'examples/README.md')
-rw-r--r--examples/README.md63
1 files changed, 30 insertions, 33 deletions
diff --git a/examples/README.md b/examples/README.md
index 74ac4384..a30991b1 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -5,60 +5,57 @@ This folder contains various examples and benchmarks.
Custom key example
------------------
-This demonstrates how to customize **cmap** with a user-defined key-type. When your key type consists of several members, you will usually have the hash function calculate hash values for the individual members, and then somehow combine them into one hash value for the entire object. If your key-type stores dynamic memory (e.g. cstr_t, as we will use), it is recommended to define a "view/raw"-struct of the your data first. In addition, you must define two functions:
+This demonstrates how to customize **cmap** with a user-defined key-type. When your key type consists of several members, you will usually have the hash function calculate hash values for the individual members, and then somehow combine them into one hash value for the entire object. If your key-type stores dynamic memory (e.g. cstr_t, as we will use), it is recommended to define a "raw"-struct of the your data first. In addition, you must define two functions:
1. A hash function; calculates the hash value given an object of the key-type.
2. A comparison function for equality;
-```
+```C
#include <stdio.h>
#include <stc/cmap.h>
#include <stc/cstr.h>
-// Viking view struct
-
-typedef struct VikingVw {
- const char* name;
- const char* country;
-} VikingVw;
-
-
-uint32_t vikingvw_hash(const VikingVw* vw, size_t ignore) {
- uint32_t hash = c_string_hash(vw->name) ^ c_string_hash(w->country);
- return hash;
-}
-static inline int vikingvw_equals(const VikingVw* x, const VikingVw* y) {
- return strcmp(x->name, y->name) == 0 && strcmp(x->country, y->country) == 0;
-}
-```
-And the Viking data struct with destroy and convertion functions between VikingVw <-> Viking structs.
-```
typedef struct Viking {
cstr_t name;
cstr_t country;
} Viking;
-
void viking_del(Viking* vk) {
cstr_del(&vk->name);
cstr_del(&vk->country);
}
+```
+And the Viking data struct with destroy and convertion functions between VikingVw <-> Viking structs.
+```C
+// Viking raw struct
-static inline VikingVw viking_toVw(Viking* vk) {
- VikingVw vw = {vk->name.str, vk->country.str}; return vw;
+typedef struct VikingRaw {
+ const char* name;
+ const char* country;
+} VikingRaw;
+
+uint32_t vikingraw_hash(const VikingRaw* vw, size_t ignore) {
+ uint32_t hash = c_string_hash(vw->name) ^ c_string_hash(w->country);
+ return hash;
+}
+static inline int vikingraw_equals(const VikingRaw* x, const VikingRaw* y) {
+ return strcmp(x->name, y->name) == 0 && strcmp(x->country, y->country) == 0;
}
-static inline Viking viking_fromVw(VikingVw vw) { // note: parameter is by value
+
+static inline VikingRaw viking_toRaw(Viking* vk) {
+ VikingRaw vw = {vk->name.str, vk->country.str}; return vw;
+}
+static inline Viking viking_fromRaw(VikingRaw vw) { // note: parameter is by value
Viking vk = {cstr(vw.name), cstr(vw.country)}; return vk;
}
```
With this in place, we use the full using_cmap() macro to define {Viking -> int} hash map type:
```
-using_cmap(vk, Viking, int, c_default_del, vikingvw_equals, vikingvw_hash,
- viking_del, VikingVw, viking_toVw, viking_fromVw);
-```
-cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test. cmap_vk_del() will free all memory allocated for Viking keys and the hash table values.
-Finally, main which also demos the generic c_push_items() of multiple elements:
+using_cmap(vk, Viking, int, c_default_del, vikingraw_equals, vikingraw_hash,
+ viking_del, VikingRaw, viking_toRaw, viking_fromRaw);
```
+cmap_vk uses vikingraw_hash() for hash value calculations, and vikingraw_equals() for equality test. cmap_vk_del() will free all memory allocated for Viking keys and the hash table values. Finally, main which also demos the generic c_push_items() of multiple elements:
+```C
int main() {
cmap_vk vikings = cmap_INIT;
c_push_items(&vikings, cmap_vk, {
@@ -66,13 +63,13 @@ int main() {
{ {"Olaf", "Denmark"}, 24 },
{ {"Harald", "Iceland"}, 12 },
});
- VikingVw look = {"Einar", "Norway"};
+ VikingRaw look = {"Einar", "Norway"};
cmap_vk_entry_t *e = cmap_vk_find(&vikings, look);
- e->value += 5; // update
- cmap_vk_emplace(&vikings, look, 0)->value += 5; // again
+ e->second += 5; // update
+ cmap_vk_emplace(&vikings, look, 0)->second += 5; // again
c_foreach (k, cmap_vk, vikings) {
- printf("%s of %s has %d hp\n", k.get->key.name.str, k.get->key.country.str, k.get->value);
+ printf("%s of %s has %d hp\n", k.get->first.name.str, k.get->first.country.str, k.get->second);
}
cmap_vk_del(&vikings);
}