diff options
| author | Tyge Løvset <[email protected]> | 2020-07-14 12:11:33 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-07-14 12:11:33 +0200 |
| commit | df4f9c6aa33fd5f33ed09ad4f698a548b4f913b9 (patch) | |
| tree | 6187f4d80586320ba5f96e831be3bb83c5622049 | |
| parent | 5baa9566801e902afaad733b6a23e8cfb7e376ee (diff) | |
| parent | 575552e9a580a91c0d80e2deb4ccd02eaeceecc2 (diff) | |
| download | STC-modified-df4f9c6aa33fd5f33ed09ad4f698a548b4f913b9.tar.gz STC-modified-df4f9c6aa33fd5f33ed09ad4f698a548b4f913b9.zip | |
Merge branch 'master' of https://github.com/tylo-work/C99Containers
| -rw-r--r-- | README.md | 14 |
1 files changed, 10 insertions, 4 deletions
@@ -103,18 +103,24 @@ CHash and CVector discussion You may customize the destroy-, hash- and equals- function. It also supports a few other arguments in the declare-statement that allows to define a convertion from a raw/literal type to the key-type specified. This is handy when e.g. having CString as key, as it enables the usage of string literals as key in *put() and *get() functions, instead of requering a constructed CString. Without it, you would have to write:
```
+declare_CHash(si, MAP, CString, int);
+...
chash_si_put(&map, cstring_make("mykey"), 12);
```
-but the main problem is lookup:
+but the main incovenience is with lookup:
```
CString lookup = cstring_make("mykey");
int x = chash_si_get(&map, lookup)->value;
cstring_destroy(&lookup);
```
-The predefined shorthand macro *declare_CHash_string()* defines a CHash container with a CString as key, however this you may use it like:
+To avoid this, use *declare_CHash_string()*:
```
-chash_si_put(&map, "mykey", 12); // constructs a CString key from the char* internally.
-int x = chash_si_get(&map, "mykey")->value; // no allocation of string key happens here, which is good.
+declare_CHash_string(si, MAP, int);
+...
+CHash_si map = chash_init;
+chash_si_put(&map, "mykey", 12); // constructs a CString key from the const char* internally.
+int x = chash_si_get(&map, "mykey")->value; // no allocation of string key happens here.
+chash_si_destroy(&map);
```
An alternative would be to use *char* * as key type, but you would have to manage the memory of the hash char* keys yourself.
Note that this customization is also available for **CVector**, but only affects the *find()* function currently. See *declare_CVector_string()*.
|
