summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-12-01 21:45:19 +0100
committerTyge Løvset <[email protected]>2020-12-01 21:45:19 +0100
commitae032cb11e32f08258dd78c23e7624663cd25ef6 (patch)
tree7f9657c6c8dff0ab274cb713d35fbf3e96835daf
parent65033a7da71abb322b3dacf3e4a160961972c39e (diff)
downloadSTC-modified-ae032cb11e32f08258dd78c23e7624663cd25ef6.tar.gz
STC-modified-ae032cb11e32f08258dd78c23e7624663cd25ef6.zip
Added some examples.
-rw-r--r--docs/cmap_api.md65
-rw-r--r--docs/cset_api.md50
-rw-r--r--docs/cvec_api.md32
-rw-r--r--stc/cmap.h3
4 files changed, 104 insertions, 46 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 9c90dbdb..1fb5994c 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -126,42 +126,45 @@ uint32_t c_default_hash16(const void *data, size_t len);
uint32_t c_default_hash32(const void* data, size_t len);
```
-Example:
+## Example
```c
#include <stdio.h>
-#include <stc/cmap.h>
#include <stc/cstr.h>
+#include <stc/cmap.h>
-using_cmap(ii, int, int);
using_cmap_str();
-int main() {
- // -- map of ints --
- cmap_ii nums = cmap_ii_init();
- cmap_ii_put(&nums, 8, 64); // similar to insert_or_assign()
- cmap_ii_emplace(&nums, 11, 121);
-
- printf("%d\n", cmap_ii_find(&nums, 8)->second);
- cmap_ii_del(&nums);
-
- // -- map of cstr_t --
- cmap_str strings = cmap_str_init();
- cmap_str_emplace(&strings, "Make", "my");
- cmap_str_emplace(&strings, "Rainy", "day");
- cmap_str_emplace(&strings, "Sunny", "afternoon");
- c_push_items(&strings, cmap_str, { {"Eleven", "XI"}, {"Six", "VI"} });
-
- printf("size = %zu\n", cmap_str_size(strings));
- c_foreach (i, cmap_str, strings)
- printf("%s: %s\n", i.val->first.str, i.val->second.str);
- cmap_str_del(&strings); // frees all strings and map.
+int main()
+{
+ // Create an unordered_map of three strings (that map to strings)
+ cmap_str u = cmap_inits;
+ c_push_items(&u, cmap_str, {
+ {"RED","#FF0000"},
+ {"GREEN","#00FF00"},
+ {"BLUE","#0000FF"}
+ });
+
+ // Iterate and print keys and values of unordered map
+ c_foreach (n, cmap_str, u) {
+ printf("Key:[%s] Value:[%s]\n", n.val->first.str, n.val->second.str);
+ }
+
+ // Add two new entries to the unordered map
+ cmap_str_put(&u, "BLACK", "#000000");
+ cmap_str_put(&u, "WHITE", "#FFFFFF");
+
+ // Output values by key
+ printf("The HEX of color RED is:[%s]\n", cmap_str_at(&u, "RED")->str);
+ printf("The HEX of color BLACK is:[%s]\n", cmap_str_at(&u, "BLACK")->str);
+
+ return 0;
}
-// Output:
-64
-size = 5
-Rainy: day
-Sunny: afternoon
-Six: VI
-Make: my
-Eleven: XI
+```
+Output:
+```
+Key:[RED] Value:[#FF0000]
+Key:[GREEN] Value:[#00FF00]
+Key:[BLUE] Value:[#0000FF]
+The HEX of color RED is:[#FF0000]
+The HEX of color BLACK is:[#000000]
```
diff --git a/docs/cset_api.md b/docs/cset_api.md
index 92d6afe4..f0c62c8a 100644
--- a/docs/cset_api.md
+++ b/docs/cset_api.md
@@ -104,25 +104,45 @@ uint32_t c_default_hash16(const void *data, size_t len);
uint32_t c_default_hash32(const void* data, size_t len);
```
-Example:
+## Example
```c
+#include <stdio.h>
#include <stc/cstr.h>
-#include <stc/cmap.h>
+#include <stc/cset.h>
using_cset_str();
-int main() {
- cset_str words = cset_str_init();
- cset_str_emplace(&words, "Hello");
- cset_str_emplace(&words, "Crazy");
- cset_str_emplace(&words, "World");
- cset_str_erase(&words, "Crazy");
-
- // iterate the set of cstr_t values:
- c_foreach (i, cset_str, words)
- printf("%s ", i.val->str);
- cset_str_del(&words);
+int main ()
+{
+ cset_str first = cset_inits; // empty
+ cset_str second = cset_inits; c_push_items(&second, cset_str, {"red","green","blue"}); // init list
+ cset_str third = cset_inits; c_push_items(&third, cset_str, {"orange","pink","yellow"}); // init list
+ cset_str fourth = cset_inits;
+ cset_str_emplace(&fourth, "potatoes");
+ cset_str_emplace(&fourth, "milk");
+ cset_str_emplace(&fourth, "flour");
+
+ cset_str fifth = cset_inits;
+ c_foreach (x, cset_str, second) cset_str_emplace(&fifth, x.val->str);
+ c_foreach (x, cset_str, third) cset_str_emplace(&fifth, x.val->str);
+ c_foreach (x, cset_str, fourth) cset_str_emplace(&fifth, x.val->str);
+
+ printf("fifth contains:\n-------------\n");
+ c_foreach (x, cset_str, fifth) printf("%s\n", x.val->str);
+
+ c_del(cset_str, &first, &second, &third, &fourth, &fifth);
+ return 0;
}
-// Output:
-Hello World
+```
+Output:
+```
+red
+green
+flour
+orange
+blue
+pink
+yellow
+milk
+potatoes
```
diff --git a/docs/cvec_api.md b/docs/cvec_api.md
index 996cdb16..1a51470e 100644
--- a/docs/cvec_api.md
+++ b/docs/cvec_api.md
@@ -107,3 +107,35 @@ cvec_X_iter_t cvec_X_end(const cvec_X* self);
void cvec_X_next(cvec_X_iter_t* it);
cvec_X_value_t* cvec_X_itval(cvec_X_iter_t it);
```
+
+## Example
+```c
+#include <stdio.h>
+#include <stc/cvec.h>
+using_cvec(i, int);
+
+int main()
+{
+ // Create a vector containing integers
+ cvec_i v = cvec_inits;
+ c_push_items(&v, cvec_i, {7, 5, 16, 8});
+
+ // Add two more integers to vector
+ cvec_i_push_back(&v, 25);
+ cvec_i_push_back(&v, 13);
+
+ // Iterate and print values of vector
+ c_foreach (n, cvec_i, v) {
+ printf("%d\n", *n.val);
+ }
+}
+```
+Output:
+```
+7
+5
+16
+8
+25
+13
+``` \ No newline at end of file
diff --git a/stc/cmap.h b/stc/cmap.h
index 561d70fd..6401151e 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -285,8 +285,11 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t;
ctype##_##X##_next(ctype##_##X##_iter_t* it) { \
while ((++it->val, *++it->_hx == 0)) ; \
} \
+\
CMAP_ONLY_##ctype( STC_INLINE ctype##_##X##_mapped_t* \
ctype##_##X##_itval(ctype##_##X##_iter_t it) {return &it.val->second;} ) \
+ CSET_ONLY_##ctype( STC_INLINE ctype##_##X##_value_t* \
+ ctype##_##X##_itval(ctype##_##X##_iter_t it) {return it.val;} ) \
\
STC_API void \
ctype##_##X##_erase_entry(ctype##_##X* self, ctype##_##X##_value_t* val); \