summaryrefslogtreecommitdiffhomepage
path: root/advanced_example.c
diff options
context:
space:
mode:
authorTylo <[email protected]>2020-06-20 20:47:56 +0200
committerTylo <[email protected]>2020-06-20 20:47:56 +0200
commit173d32f0eb194b30b40fb5eb6e708bc3c4f8f421 (patch)
treed8b5da2247b557dc7a1def7bcfc5edcbe6cd4224 /advanced_example.c
parent2c1382a7cefea370a321ba00e83bcf29df48e5fd (diff)
downloadSTC-modified-173d32f0eb194b30b40fb5eb6e708bc3c4f8f421.tar.gz
STC-modified-173d32f0eb194b30b40fb5eb6e708bc3c4f8f421.zip
Renamed CMap to CHash, to reflect that it now supports both (unordered) Map and Set. Second arg in declare_CHash() should be 'set' or 'map'.
Refactored copt.h to be more consistent with other types. Added cvector_make(size, fillvalue) constructor.
Diffstat (limited to 'advanced_example.c')
-rw-r--r--advanced_example.c22
1 files changed, 10 insertions, 12 deletions
diff --git a/advanced_example.c b/advanced_example.c
index 449bddb3..b85b433e 100644
--- a/advanced_example.c
+++ b/advanced_example.c
@@ -1,4 +1,4 @@
-/*To be able to use CMap with a user-defined key-type, you need to define two things:
+/*To be able to use CHash 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.
@@ -64,28 +64,26 @@ size_t personview_hash(const struct PersonView* pv, size_t ignore) {
/*```
With this in place, we can declare the map Person -> int:
```*/
-declare_CMap(ex, struct Person, int, c_noDestroy, person_destroy,
- struct PersonView, personview_hash, personview_compare,
- person_getView, person_fromView);
+declare_CHash(ex, map, struct Person, int, c_emptyDestroy, personview_hash, personview_compare,
+ struct PersonView, person_destroy, person_getView, person_fromView);
/*```
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;
- cmap_ex_put(&m6, (struct PersonView){"John", "Doe", 24}, 1001);
- cmap_ex_put(&m6, (struct PersonView){"Jane", "Doe", 21}, 1002);
- cmap_ex_put(&m6, (struct PersonView){"John", "Travolta", 66}, 1003);
+ CHash_ex m6 = chash_init;
+ chash_ex_put(&m6, (struct PersonView){"John", "Doe", 24}, 1001);
+ chash_ex_put(&m6, (struct PersonView){"Jane", "Doe", 21}, 1002);
+ chash_ex_put(&m6, (struct PersonView){"John", "Travolta", 66}, 1003);
- c_foreach (it, cmap_ex, m6) {
+ c_foreach (it, chash_ex, m6) {
if (cstring_equals(it.item->key.name, "John"))
printf("%s %s %d -> %d\n", it.item->key.name.str, it.item->key.surname.str, it.item->key.age,
it.item->value);
}
- cmap_ex_destroy(&m6);
+ chash_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.
+CHash uses personview_hash() for hash value calculations, and the personview_compare() for equality checks. The chash_ex_destroy() function will free CStrings name, surname and the value for each item in the map, in addition to the CHash hash table itself.
*/
-