diff options
| author | Tyge Løvset <[email protected]> | 2020-04-26 23:38:02 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-04-26 23:38:02 +0200 |
| commit | 749ec1f9265ea3c02084a876c5f7c9651242c9f8 (patch) | |
| tree | d9bd0c62b40b805cbc5b643de8d3eed6160f9e47 | |
| parent | 03dec0353d78959700936eb3a041795b1bacaa8e (diff) | |
| download | STC-modified-749ec1f9265ea3c02084a876c5f7c9651242c9f8.tar.gz STC-modified-749ec1f9265ea3c02084a876c5f7c9651242c9f8.zip | |
Update advanced_example.md
| -rw-r--r-- | advanced_example.md | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/advanced_example.md b/advanced_example.md index 7c924a1c..7771b3c9 100644 --- a/advanced_example.md +++ b/advanced_example.md @@ -1,4 +1,4 @@ -To be able to use CMap (or one of the other unordered associative containers) with a user-defined key-type, you need to define two things:
+To be able to use CMap with a user-defined key-type, you need to define two things:
1. A hash function; this must be a function that calculates the hash value given an object of the key-type.
@@ -12,8 +12,7 @@ Assuming a key-type like this, and want string as value, we define the functions #include <stc/cmap.h>
#include <stc/cstring.h>
-struct Person
-{
+struct Person {
CString name;
CString surname;
int age;
@@ -52,9 +51,7 @@ int personview_compare(const struct PersonView* x, const struct PersonView* y) { And a hash function that combines the three member's hashes:
```
size_t personview_hash(const struct PersonView* pv, size_t ignore) {
- // Compute individual hash values for name, surname and age
// http://stackoverflow.com/a/1646913/126995
-
size_t res = 17;
res = res * 31 + c_defaultHash(pv->name, strlen(pv->name));
res = res * 31 + c_defaultHash(pv->surname, strlen(pv->surname));
@@ -70,7 +67,7 @@ declare_CMap(ex, struct Person, int, c_noDestroy, ```
Note we use struct PersonView to put keys in the map, but keys are stored as struct Person with proper dynamically allocated CStrings to store name and surname.
-````
+```
int main()
{
CMap_ex m6 = cmap_init;
@@ -87,6 +84,5 @@ int main() cmap_ex_destroy(&m6);
}
```
-CMap uses personview_hash() for hash value calculations, and the personview_compare() for equality checks.
-The cmap_ex_destroy() function will free CStrings name, surname and the value for each item in the map, in addition to the CMap hash table itself.
+CMap uses personview_hash() for hash value calculations, and the personview_compare() for equality checks. The cmap_ex_destroy() function will free CStrings name, surname and the value for each item in the map, in addition to the CMap hash table itself.
|
