From 749ec1f9265ea3c02084a876c5f7c9651242c9f8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Sun, 26 Apr 2020 23:38:02 +0200 Subject: Update advanced_example.md --- advanced_example.md | 12 ++++-------- 1 file 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 #include -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. -- cgit v1.2.3