summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-09-16 09:05:06 +0200
committerTyge Løvset <[email protected]>2020-09-16 09:05:06 +0200
commit47b096d57f2d716db40a9ec6e724c2a76038778d (patch)
treeb6720b8cef663aaf0bdd9f92fbd892e98a80a318 /examples
parent0ce57669673a5a22d8207ec884bcebb97cb18448 (diff)
downloadSTC-modified-47b096d57f2d716db40a9ec6e724c2a76038778d.tar.gz
STC-modified-47b096d57f2d716db40a9ec6e724c2a76038778d.zip
Changed API to conform with std:: containers. cmap now only use first, second. for result and value types.
Diffstat (limited to 'examples')
-rw-r--r--examples/advanced.c10
-rw-r--r--examples/benchmark.c4
-rw-r--r--examples/birthday.c8
-rw-r--r--examples/complex.c4
-rw-r--r--examples/demos.c2
-rw-r--r--examples/ex_gaussian.c16
-rw-r--r--examples/geek1.c185
-rw-r--r--examples/geek2.c130
-rw-r--r--examples/geek3.c50
-rw-r--r--examples/geek4.c264
-rw-r--r--examples/geek5.c140
-rw-r--r--examples/geek6.c124
-rw-r--r--examples/geek7.c159
-rw-r--r--examples/inits.c12
-rw-r--r--examples/mapmap.c18
-rw-r--r--examples/phonebook.c2
-rw-r--r--examples/words.c12
17 files changed, 44 insertions, 1096 deletions
diff --git a/examples/advanced.c b/examples/advanced.c
index 1f4907c6..c48c25a2 100644
--- a/examples/advanced.c
+++ b/examples/advanced.c
@@ -5,7 +5,7 @@
* 2. A comparison function for equality.
* When your key type consists of several members, you will usually have the hash function
- * calculate hash values for the individual members, and then somehow combine them into one
+ * calculate hash values for the individual members, and then somehow combine them into one
* hash value for the entire object.
* In order to use Viking as a map key, it is smart to define a plain-old-data "view"
* of the Viking struct, to simplfy insert and lookup in the map.
@@ -51,7 +51,7 @@ Viking viking_fromVw(VikingVw vw) {
}
// Using the full c_cmap() macro to define [Viking -> int] hash map type:
-c_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash,
+c_cmap(vk, Viking, int, c_default_destroy, vikingvw_equals, vikingvw_hash,
viking_destroy, VikingVw, viking_toVw, viking_fromVw);
// cmap_vk uses vikingvw_hash() for hash value calculations, and vikingvw_equals() for equality test.
@@ -70,11 +70,11 @@ int main()
VikingVw einar = {"Einar", "Norway"};
cmap_vk_entry_t *e = cmap_vk_find(&vikings, einar);
- e->value += 5; // update
- cmap_vk_emplace(&vikings, einar, 0).first->value += 5; // again
+ e->second += 5; // update
+ cmap_vk_emplace(&vikings, einar, 0).first->second += 5; // again
c_foreach (k, cmap_vk, vikings) {
- printf("%s of %s has %d hp\n", k.get->key.name.str, k.get->key.country.str, k.get->value);
+ printf("%s of %s has %d hp\n", k.get->first.name.str, k.get->first.country.str, k.get->second);
}
cmap_vk_destroy(&vikings);
}
diff --git a/examples/benchmark.c b/examples/benchmark.c
index 55cae17f..0a492d3d 100644
--- a/examples/benchmark.c
+++ b/examples/benchmark.c
@@ -36,12 +36,12 @@ crand_rng64_t rng;
#define CMAP_SETUP(tt, Key, Value) cmap_##tt map = cmap_ini \
; cmap_##tt##_set_load_factors(&map, max_load_factor, 0.0)
-#define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val).first->value
+#define CMAP_PUT(tt, key, val) cmap_##tt##_put(&map, key, val).first->second
#define CMAP_EMPLACE(tt, key, val) cmap_##tt##_emplace(&map, key, val)
#define CMAP_ERASE(tt, key) cmap_##tt##_erase(&map, key)
#define CMAP_FIND(tt, key) (cmap_##tt##_find(map, key) != NULL)
#define CMAP_FOR(tt, i) c_foreach (i, cmap_##tt, map)
-#define CMAP_ITEM(tt, i) i.get->value
+#define CMAP_ITEM(tt, i) i.get->second
#define CMAP_SIZE(tt) cmap_size(map)
#define CMAP_BUCKETS(tt) cmap_bucket_count(map)
#define CMAP_CLEAR(tt) cmap_##tt##_clear(&map)
diff --git a/examples/birthday.c b/examples/birthday.c
index e2d2600a..1cd57066 100644
--- a/examples/birthday.c
+++ b/examples/birthday.c
@@ -21,7 +21,7 @@ void repeats(void)
clock_t now = clock();
for (size_t i = 0; i < N; ++i) {
uint64_t k = crand_i64(&rng) & mask;
- int v = ++cmap_ic_emplace(&m, k, 0).first->value;
+ int v = ++cmap_ic_emplace(&m, k, 0).first->second;
if (v > 1) printf("%zu: %llx - %d\n", i, k, v);
}
float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
@@ -40,16 +40,16 @@ void distribution(void)
clock_t now = clock();
crand_uniform_i32_t dist = crand_uniform_i32_init(0, M);
for (size_t i = 0; i < N; ++i) {
- ++cmap_x_emplace(&map, crand_uniform_i32(&rng, &dist), 0).first->value;
+ ++cmap_x_emplace(&map, crand_uniform_i32(&rng, &dist), 0).first->second;
}
float diff = (float) (clock() - now) / CLOCKS_PER_SEC;
uint64_t sum = 0;
- c_foreach (i, cmap_x, map) sum += i.get->value;
+ c_foreach (i, cmap_x, map) sum += i.get->second;
sum /= map.size;
c_foreach (i, cmap_x, map)
- printf("%u: %zu - %zu\n", i.get->key, i.get->value, sum);
+ printf("%u: %zu - %zu\n", i.get->first, i.get->second, sum);
printf("%.02f\n", diff);
}
diff --git a/examples/complex.c b/examples/complex.c
index d3f2cf4e..aa746b90 100644
--- a/examples/complex.c
+++ b/examples/complex.c
@@ -22,14 +22,14 @@ int main() {
clist_y tableList = clist_ini;
// Put in some data.
cmap_g listMap = cmap_ini;
-
+
*carray2f_at(&table, y, x) = 3.1415927f; // table[y][x]
clist_y_push_back(&tableList, table);
cmap_g_put(&listMap, tableKey, tableList);
cmap_s_put(&myMap, strKey, listMap);
}
{ // Access the data entry
- carray2f table = *clist_y_back(&cmap_g_find(&cmap_s_find(&myMap, strKey)->value, tableKey)->value);
+ carray2f table = *clist_y_back(&cmap_g_find(&cmap_s_find(&myMap, strKey)->second, tableKey)->second);
printf("value (%d, %d) is: %f\n", y, x, *carray2f_at(&table, y, x));
}
diff --git a/examples/demos.c b/examples/demos.c
index 78636864..0247945d 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -168,7 +168,7 @@ void mapdemo3()
printf("size %zu\n", cmap_size(table));
c_foreach (i, cmap_str, table)
printf("entry: %s: %s\n", i.get->first.str, i.get->second.str);
- cmap_str_destroy(&table); // frees key and value CStrs, and hash table (CVec).
+ cmap_str_destroy(&table); // frees key and value cstrs, and hash table.
}
diff --git a/examples/ex_gaussian.c b/examples/ex_gaussian.c
index 63e16f3c..559512ce 100644
--- a/examples/ex_gaussian.c
+++ b/examples/ex_gaussian.c
@@ -11,7 +11,7 @@ c_cmap(i, int, size_t);
// Declare int vector with map entries that can be sorted by map keys.
static int compare(cmap_i_entry_t *a, cmap_i_entry_t *b) {
- return c_default_compare(&a->key, &b->key);
+ return c_default_compare(&a->first, &b->first);
}
// Vector: typetag 'e' for (map) entry
c_cvec(e, cmap_i_entry_t, c_default_destroy, compare);
@@ -22,17 +22,17 @@ int main()
const double Mean = -12.0, StdDev = 8.0, Mag = 12000.0 / StdDev;
printf("Demo of gaussian / normal distribution of %d random samples\n", N);
-
+
// Setup random engine with normal distribution.
- uint64_t seed = time(NULL);
+ uint64_t seed = time(NULL);
crand_rng64_t rng = crand_rng64_init(seed);
crand_normal_f64_t dist = crand_normal_f64_init(Mean, StdDev);
-
+
// Create histogram map
cmap_i mhist = cmap_ini;
for (size_t i = 0; i < N; ++i) {
int index = (int) round( crand_normal_f64(&rng, &dist) );
- cmap_i_emplace(&mhist, index, 0).first->value += 1;
+ cmap_i_emplace(&mhist, index, 0).first->second += 1;
}
// Transfer map to vec and sort it by map keys.
@@ -40,15 +40,15 @@ int main()
c_foreach (i, cmap_i, mhist)
cvec_e_push_back(&vhist, *i.get);
cvec_e_sort(&vhist);
-
+
// Print the gaussian bar chart
cstr_t bar = cstr_ini;
c_foreach (i, cvec_e, vhist) {
- size_t n = (size_t) (i.get->value * Mag / N);
+ size_t n = (size_t) (i.get->second * Mag / N);
if (n > 0) {
// bar string: take ownership in new str after freeing current.
cstr_take(&bar, cstr_with_size(n, '*'));
- printf("%4d %s\n", i.get->key, bar.str);
+ printf("%4d %s\n", i.get->first, bar.str);
}
}
// Cleanup
diff --git a/examples/geek1.c b/examples/geek1.c
deleted file mode 100644
index 8da51f47..00000000
--- a/examples/geek1.c
+++ /dev/null
@@ -1,185 +0,0 @@
-// https://www.geeksforgeeks.org/maximize-the-number-of-sum-pairs-which-are-divisible-by-k/
-// Given an array of N integers and an integer K. The task is to print the maximum number
-// of pairs(a[i] + a[j]) possible which are divisible by K.
-
-
-int a[] = { 1, 2, 2, 3, 2, 4, 10 };
-//int a[] = { 1, 2, 2, 3, 2, 4, 10, 8, 9, 3, 2, 4 };
-
-
-#ifndef CXX
-// C program to implement the above approach
-#include <stdio.h>
-#include <stc/cmap.h>
-
-c_cmap(ii, int, int);
-
-// Function to maximize the number of pairs
-int findMaximumPairs(int a[], int n, int k)
-{
-
- // Hash-table
- cmap_ii hash = cmap_ini;
- for (int i = 0; i < n; i++) {
- cmap_ii_emplace(&hash, a[i] % k, 0).first->value++;
- }
-
- int count = 0;
-
- // Iterate for all numbers less than hash values
- c_foreach (it, cmap_ii, hash) {
-
- // If the number is 0
- if (it.get->key == 0) {
- // We take half since same number
- count += it.get->value / 2;
- cmap_ii_put(&hash, it.get->key, it.get->key & 1);
- } else {
- int one = it.get->key;
- int two = k - it.get->key;
-
- cmap_ii_entry_t *hf = cmap_ii_find(&hash, one),
- *hs = cmap_ii_emplace(&hash, two, 0).first;
- // Check for minimal occurrence
- if (hf->value < hs->value) {
- // Take the minimal
- count += hf->value;
-
- // Subtract the pairs used
- hs->value -= hf->value;
- hf->value = 0;
- }
- else if (hf->value > hs->value) {
- // Take the minimal
- count += hs->value;
-
- // Subtract the pairs used
- hf->value -= hs->value;
- hs->value = 0;
- }
- else {
- // Check if numbers are same
- if (one == two) {
-
- // If same then number of pairs will be half
- count += it.get->value / 2;
-
- // Check for remaining
- hf->value = (it.get->key & 1);
- }
- else {
- // Store the number of pairs
- count += hf->value;
- hf->value = 0;
- hs->value = 0;
- }
- }
- }
- }
-
- return count;
-}
-
-// Driver code
-int main()
-{
- int n = sizeof(a) / sizeof(a[0]);
- int k = 2;
- printf("C : %d\n", findMaximumPairs(a, n, k));
-
- return 0;
-}
-
-
-#else
-
-
-// C++ program to implement the above
-// approach
-#include <bits/stdc++.h>
-using namespace std;
-
-// Function to maximize the number of pairs
-int findMaximumPairs(int a[], int n, int k)
-{
-
- // Hash-table
- unordered_map<int, int> hash;
- for (int i = 0; i < n; i++)
- hash[a[i] % k]++;
-
- int count = 0;
-
- // Iterate for all numbers less than hash values
- for (auto it : hash) {
-
- // If the number is 0
- if (it.first == 0) {
-
- // We take half since same number
- count += it.second / 2;
- if (it.first % 2 == 0)
- hash[it.first] = 0;
- else
- hash[it.first] = 1;
- }
- else {
-
- int first = it.first;
- int second = k - it.first;
-
- // Check for minimal occurrence
- if (hash[first] < hash[second]) {
- // Take the minimal
- count += hash[first];
-
- // Subtract the pairs used
- hash[second] -= hash[first];
- hash[first] = 0;
- }
- else if (hash[first] > hash[second]) {
- // Take the minimal
- count += hash[second];
-
- // Subtract the pairs used
- hash[first] -= hash[second];
- hash[second] = 0;
- }
- else {
- // Check if numbers are same
- if (first == second) {
-
- // If same then number of pairs will be half
- count += it.second / 2;
-
- // Check for remaining
- if (it.first % 2 == 0)
- hash[it.first] = 0;
- else
- hash[it.first] = 1;
- }
- else {
-
- // Store the number of pairs
- count += hash[first];
- hash[first] = 0;
- hash[second] = 0;
- }
- }
- }
- }
-
- return count;
-}
-
-// Driver code
-int main()
-{
- int n = sizeof(a) / sizeof(a[0]);
- int k = 2;
- cout << "C++ : " << findMaximumPairs(a, n, k);
-
- return 0;
-}
-
-#endif \ No newline at end of file
diff --git a/examples/geek2.c b/examples/geek2.c
deleted file mode 100644
index 778960f3..00000000
--- a/examples/geek2.c
+++ /dev/null
@@ -1,130 +0,0 @@
-#ifndef RUST
-
-#include <stc/cmap.h>
-#include <stc/cstr.h>
-
-c_cmap_str();
-c_cset_str();
-
-int main()
-{
- // Lets use an explicit type signature (which would
- // be `cmap<String, String>` in this example).
- cmap_str book_reviews = cmap_ini;
- cset_str set = cset_ini;
- cset_str_insert(&set, "Hello");
- cset_str_insert(&set, "You");
- cset_str_insert(&set, "Tube");
- c_foreach (i, cset_str, set)
- printf("%s ", i.get->key.str);
- puts("");
-
- // Review some books.
- c_push(&book_reviews, cmap_str, {
- {"Adventures of Huckleberry Finn", "My favorite book."},
- {"Grimms' Fairy Tales", "Masterpiece."},
- {"Pride and Prejudice", "Very enjoyable."},
- {"The Adventures of Sherlock Holmes", "Eye lyked it alot."},
- });
-/*
- cmap_str_emplace(&book_reviews,
- "Adventures of Huckleberry Finn",
- "My favorite book."
- );
- cmap_str_emplace(&book_reviews,
- "Grimms' Fairy Tales",
- "Masterpiece."
- );
- cmap_str_emplace(&book_reviews,
- "Pride and Prejudice",
- "Very enjoyable."
- );
- cmap_str_emplace(&book_reviews,
- "The Adventures of Sherlock Holmes",
- "Eye lyked it alot."
- );
-*/
- // Check for a specific one.
- // When collections store owned values (String), they can still be
- // queried using references (&str).
- if (! cmap_str_find(&book_reviews, "Les Misérables")) {
- printf("We've got %zu reviews, but Les Misérables ain't one.\n",
- cmap_size(book_reviews));
- }
-
- // oops, this review has a lot of spelling mistakes, let's delete it.
- cmap_str_erase(&book_reviews, "The Adventures of Sherlock Holmes");
-
- // Look up the values associated with some keys.
- const char* to_find[] = {"Pride and Prejudice", "Alice's Adventure in Wonderland", NULL};
- for (const char** book = to_find; *book; ++book) {
- cmap_str_entry_t *review = cmap_str_find(&book_reviews, *book);
- if (review) printf("%s: %s\n", *book, review->value.str);
- else printf("%s is unreviewed.\n", *book);
- }
-
- // Look up the value for a key (will panic if the key is not found).
- printf("Review for Jane: %s\n", cmap_str_find(&book_reviews, "Pride and Prejudice")->value.str);
-
- // Iterate over everything.
- c_foreach (i, cmap_str, book_reviews) {
- printf("- %s: \"%s\"\n", i.get->key.str, i.get->value.str);
- }
- cmap_str_destroy(&book_reviews);
-}
-
-#else // ======================================================
-
-use std::collections::HashMap;
-
-// Type inference lets us omit an explicit type signature (which
-// would be `HashMap<String, String>` in this example).
-let mut book_reviews = HashMap::new();
-
-// Review some books.
-book_reviews.insert(
- "Adventures of Huckleberry Finn".to_str(),
- "My favorite book.".to_str(),
-);
-book_reviews.insert(
- "Grimms' Fairy Tales".to_str(),
- "Masterpiece.".to_str(),
-);
-book_reviews.insert(
- "Pride and Prejudice".to_str(),
- "Very enjoyable.".to_str(),
-);
-book_reviews.insert(
- "The Adventures of Sherlock Holmes".to_str(),
- "Eye lyked it alot.".to_str(),
-);
-
-// Check for a specific one.
-// When collections store owned values (String), they can still be
-// queried using references (&str).
-if !book_reviews.contains_key("Les Misérables") {
- println!("We've got {} reviews, but Les Misérables ain't one.",
- book_reviews.len());
-}
-
-// oops, this review has a lot of spelling mistakes, let's delete it.
-book_reviews.remove("The Adventures of Sherlock Holmes");
-
-// Look up the values associated with some keys.
-let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
-for &book in &to_find {
- match book_reviews.get(book) {
- Some(review) => println!("{}: {}", book, review),
- None => println!("{} is unreviewed.", book)
- }
-}
-
-// Look up the value for a key (will panic if the key is not found).
-println!("Review for Jane: {}", book_reviews["Pride and Prejudice"]);
-
-// Iterate over everything.
-for (book, review) in &book_reviews {
- println!("{}: \"{}\"", book, review);
-}
-
-#endif \ No newline at end of file
diff --git a/examples/geek3.c b/examples/geek3.c
deleted file mode 100644
index a3b41ee1..00000000
--- a/examples/geek3.c
+++ /dev/null
@@ -1,50 +0,0 @@
-// xx3.c
-
-#include <stc/cmap.h>
-#include <stc/cstr.h>
-
-c_cmap_strkey(si, int);
-c_cmap_strkey(ss, cstr_t, cstr_destroy);
-
-int main ()
-{
- cmap_si mymap = cmap_ini;
- cmap_si_put(&mymap, "Mars", 3000);
- cmap_si_put(&mymap, "Saturn", 60000);
- cmap_si_put(&mymap, "Jupiter", 70000);
-
- // ...
- cmap_si_put(&mymap, "Mars", 3396);
- cmap_si_emplace(&mymap, "Saturn", 0).first->value += 272;
- cmap_si_put(&mymap, "Jupiter", cmap_si_emplace(&mymap, "Saturn", 0).first->value + 9638);
- cmap_si_emplace(&mymap, "Sun", 0).first->value += 1000;
- cmap_si_emplace(&mymap, "Sun", 0).first->value += 1000;
-
- c_foreach (x, cmap_si, mymap) {
- printf("%s: %d\n", x.get->key.str, x.get->value);
- }
- cmap_si_destroy(&mymap);
-
- puts("------------------------");
-
- // Create an unordered_map of three strings (that map to strings)
- cmap_ss u = cmap_ini;
- cmap_ss_put(&u, "RED", cstr("#FF0000"));
- cmap_ss_put(&u, "GREEN", cstr("#00FF00"));
- cmap_ss_put(&u, "BLUE", cstr("#0000FF"));
-
- // Iterate and print keys and values of unordered_map
- c_foreach (n, cmap_ss, u) {
- printf("%s: %s\n", n.get->key.str, n.get->value.str);
- }
-
- // Add two new entries to the unordered_map
- cmap_ss_put(&u, "BLACK", cstr("#000000"));
- cmap_ss_put(&u, "WHITE", cstr("#FFFFFF"));
-
- // Output values by key
- printf("The HEX of color RED is:[%s]\n", cmap_ss_find(&u, "RED")->value.str);
- printf("The HEX of color BLACK is:[%s]\n", cmap_ss_find(&u, "BLACK")->value.str);
- cmap_ss_destroy(&u);
- return 0;
-} \ No newline at end of file
diff --git a/examples/geek4.c b/examples/geek4.c
deleted file mode 100644
index 91fb122d..00000000
--- a/examples/geek4.c
+++ /dev/null
@@ -1,264 +0,0 @@
-// https://www.geeksforgeeks.org/count-of-words-that-are-present-in-all-the-given-sentences/
-// https://www.geeksforgeeks.org/tag/cpp-unordered_map/
-/*
-Count of words that are present in all the given sentences
-Given n sentences. The task is to count the number of words that appear in all of these
-sentences. Note that every word consists of only lowercase English alphabets.
-
-Examples:
-
-Input: arr[] = {
- "there is a cow",
- "cow is our mother",
- "cow gives us milk and milk is sweet",
- "there is a boy who loves cow"}
-Output: 2
-Only the words "is" and "cow" appear in all of the sentences.
-
-Input: arr[] = {
- "abc aac abcd ccc",
- "ac aa abc cca",
- "abca aaac abcd ccc"}
-Output: 0
-
-Naive Approach: The naive approach is to take every word of the first sentence and compare it
- with rest of the lines if it is also present in all lines then increment the count.
-
-
-Efficient Approach: For all the words of the first sentence, we can check if it is also present in
- all the other sentences in constant time by using a map. Store all the words
- of the first sentence in a map and check how many of these stored words are
- present in all of the other sentences.
-*/
-#ifndef CXX
-// C implementation of the approach
-
-#include <stc/cmap.h>
-#include <stc/cvec.h>
-#include <stc/cstr.h>
-
-c_cvec_str();
-c_cmap_strkey(sb, bool);
-c_cvec(sb, cmap_sb_entry_t, cmap_sb_entry_destroy, c_no_compare);
-
-// Function to return the count of common words
-// in all the sentences
-int commonWords(cvec_str S)
-{
- int m, n, i, j;
-
-
- // To store all the words of first string
- cvec_sb ans = cvec_ini;
-
- // m will store number of strings in given vector
- m = cvec_size(S);
-
- i = 0;
-
- // Extract all words of first string and store it in ans
- while (i < cstr_size(S.data[0])) {
- // To store separate words
- cstr_t word = cstr_ini;
- cmap_sb_entry_t tmp = {cstr_ini, false};
-
- while (i < cstr_size(S.data[0]) && S.data[0].str[i] != ' ') {
- cstr_push_back(&word, S.data[0].str[i]);
- i++;
- }
-
- // Increase i to get at starting index
- // of the next word
- i++;
-
- // If str is not empty store it in map
- if (!cstr_empty(word)) {
- tmp.key = cstr_move(&word);
- tmp.value = true;
- cvec_sb_push_back(&ans, tmp);
- }
- }
-
- // Start from 2nd line check if any word of
- // the first string did not match with
- // some word in the current line
- for (j = 1; j < m; j++) {
- // It will be used to check if a word is present
- // in a particuler string
- cmap_sb has = cmap_ini;
- i = 0;
-
- while (i < cstr_size(S.data[j])) {
- cstr_t word = cstr_ini;
- while (i < cstr_size(S.data[j]) && S.data[j].str[i] != ' ') {
- cstr_push_back(&word, S.data[j].str[i]);
- i++;
- }
- i++;
- if (!cstr_empty(word)) {
- cmap_sb_put(&has, word.str, true);
- }
- cstr_destroy(&word);
- }
-
- // Check all words of this vector
- // if it is not present in current line
- // make it false
- for (int k = 0; k < cvec_size(ans); k++) {
- if (ans.data[k].value != false
- && cmap_sb_emplace(&has, ans.data[k].key.str, false).first->value == false) {
- ans.data[k].value = false;
- }
-
- // This line is used to consider only distinct words
- else if (ans.data[k].value != false
- && cmap_sb_emplace(&has, ans.data[k].key.str, false).first->value == true) {
- cmap_sb_put(&has, ans.data[k].key.str, false);
- }
- }
- cmap_sb_destroy(&has);
- }
-
- // This function will print
- // the count of common words
- int cnt = 0;
-
- // If current word appears in all the sentences
- for (int k = 0; k < cvec_size(ans); k++) {
- if (ans.data[k].value == true)
- cnt++;
- }
-
- cvec_sb_destroy(&ans);
- return cnt;
-}
-
-// Driver code
-int main()
-{
- cvec_str S = cvec_ini;
- cvec_str_emplace_back(&S, "there is a cow");
- cvec_str_emplace_back(&S, "cow is our mother");
- cvec_str_emplace_back(&S, "cow gives us milk and milk is sweet");
- cvec_str_emplace_back(&S, "there is a boy who loves cow");
-
- printf("%d\n", commonWords(S));
- cvec_str_destroy(&S);
- return 0;
-}
-
-
-#else // ========================= c++ =======================
-
-
-// C++ implementation of the approach
-#include <unordered_map>
-#include <vector>
-#include <iostream>
-using namespace std;
-
-// Function to return the count of common words
-// in all the sentences
-int commonWords(vector<string> S)
-{
- int m, n, i, j;
-
- // To store separate words
- string word;
-
- // It will be used to check if a word is present
- // in a particuler string
- unordered_map<string, bool> has;
-
- // To store all the words of first string
- vector<pair<string, bool> > ans;
-
- pair<string, bool> tmp;
-
- // m will store number of strings in given vector
- m = S.size();
-
- i = 0;
-
- // Extract all words of first string and store it in ans
- while (i < S[0].size()) {
- word = "";
- while (i < S[0].size() && S[0][i] != ' ') {
- word += S[0][i];
- i++;
- }
-
- // Increase i to get at starting index
- // of the next word
- i++;
-
- // If word is not empty store it in map
- if (word != "") {
- tmp = make_pair(word, true);
- ans.push_back(tmp);
- }
- }
-
- // Start from 2nd line check if any word of
- // the first string did not match with
- // some word in the current line
- for (j = 1; j < m; j++) {
- has.clear();
- i = 0;
-
- while (i < S[j].size()) {
- word = "";
- while (i < S[j].size() && S[j][i] != ' ') {
- word += S[j][i];
- i++;
- }
- i++;
- if (word != "") {
- has[word] = true;
- }
- }
-
- // Check all words of this vector
- // if it is not present in current line
- // make it false
- for (int k = 0; k < ans.size(); k++) {
- if (ans[k].second != false
- && has[ans[k].first] == false) {
- ans[k].second = false;
- }
-
- // This line is used to consider only distinct words
- else if (ans[k].second != false
- && has[ans[k].first] == true) {
- has[ans[k].first] = false;
- }
- }
- }
-
- // This function will print
- // the count of common words
- int cnt = 0;
-
- // If current word appears in all the sentences
- for (int k = 0; k < ans.size(); k++) {
- if (ans[k].second == true)
- cnt++;
- }
-
- return cnt;
-}
-
-// Driver code
-int main()
-{
- vector<string> S;
- S.push_back("there is a cow");
- S.push_back("cow is our mother");
- S.push_back("cow gives us milk and milk is sweet");
- S.push_back("there is a boy who loves cow");
-
- cout << commonWords(S);
-
- return 0;
-}
-#endif \ No newline at end of file
diff --git a/examples/geek5.c b/examples/geek5.c
deleted file mode 100644
index f19edf8c..00000000
--- a/examples/geek5.c
+++ /dev/null
@@ -1,140 +0,0 @@
-/*
-https://www.geeksforgeeks.org/number-of-times-the-given-string-occurs-in-the-array-in-the-range-l-r/
-
-Number of times the given string occurs in the array in the range [l, r]
-Given an array of strings arr[] and two integers l and r, the task is to find the number of times
-the given string str occurs in the array in the range [l, r] (1-based indexing). Note that the
-strings contain only lowercase letters.
-
-Examples:
-
-Input: arr[] = {"abc", "def", "abc"}, L = 1, R = 2, str = "abc"
-Output: 1
-
-Input: arr[] = {"abc", "def", "abc"}, L = 1, R = 3, str = "ghf"
-Output: 0
-*/
-
-#ifndef CXX
-
-#include <stc/cmap.h>
-#include <stc/cvec.h>
-#include <stc/cstr.h>
-
-c_cvec(i, int);
-c_cmap_strkey(sv, cvec_i, cvec_i_destroy);
-
-
-// Function to return the number of occurrences of
-int NumOccurrences(const char* arr[], int n, const char* str, int L, int R)
-{
- // To store the indices of strings in the array
- cmap_sv M = cmap_ini;
- for (int i = 0; i < n; i++) {
- const char* temp = arr[i];
- cmap_sv_entry_t *it = cmap_sv_find(&M, temp);
-
- // If current string doesn't
- // have an entry in the map
- // then create the entry
- if (it == NULL) {
- cvec_i A = cvec_ini;
- cvec_i_push_back(&A, i + 1);
- cmap_sv_put(&M, temp, A);
- }
- else {
- cvec_i_push_back(&it->value, i + 1);
- }
- }
-
- cmap_sv_entry_t *it = cmap_sv_find(&M, str);
-
- // If the given string is not
- // present in the array
- if (it == NULL)
- return 0;
-
- // If the given string is present
- // in the array
- cvec_i A = it->value;
- int y = 0, x = 0;
- for (; y < cvec_size(A); ++y) if (A.data[y] > R) break;
- for (; x < cvec_size(A); ++x) if (A.data[x] > L - 1) break;
-
- cmap_sv_destroy(&M);
- return (y - x);
-}
-
-// Driver code
-int main()
-{
- const char* arr[] = { "abc", "abcabc", "abc" };
- int n = sizeof(arr) / sizeof(*arr);
- int L = 1;
- int R = 3;
- const char* str = "abc";
-
- printf("%d\n", NumOccurrences(arr, n, str, L, R));
-
- return 0;
-}
-
-#else // =============================== c++ ==================================
-
-// C++ implementation of the approach
-#include <bits/stdc++.h>
-using namespace std;
-
-// Function to return the number of occurrences of
-int NumOccurrences(string arr[], int n, string str, int L, int R)
-{
- // To store the indices of strings in the array
- unordered_map<string, vector<int> > M;
- for (int i = 0; i < n; i++) {
- string temp = arr[i];
- auto it = M.find(temp);
-
- // If current string doesn't
- // have an entry in the map
- // then create the entry
- if (it == M.end()) {
- vector<int> A;
- A.push_back(i + 1);
- M.insert(make_pair(temp, A));
- }
- else {
- it->second.push_back(i + 1);
- }
- }
-
- auto it = M.find(str);
-
- // If the given string is not
- // present in the array
- if (it == M.end())
- return 0;
-
- // If the given string is present
- // in the array
- vector<int> A = it->second;
- int y = upper_bound(A.begin(), A.end(), R) - A.begin();
- int x = upper_bound(A.begin(), A.end(), L - 1) - A.begin();
-
- return (y - x);
-}
-
-// Driver code
-int main()
-{
- string arr[] = { "abc", "abcabc", "abc" };
- int n = sizeof(arr) / sizeof(string);
- int L = 1;
- int R = 3;
- string str = "abc";
-
- cout << NumOccurrences(arr, n, str, L, R);
-
- return 0;
-}
-
-#endif \ No newline at end of file
diff --git a/examples/geek6.c b/examples/geek6.c
deleted file mode 100644
index a973312c..00000000
--- a/examples/geek6.c
+++ /dev/null
@@ -1,124 +0,0 @@
-// https://www.geeksforgeeks.org/find-the-smallest-positive-number-missing-from-an-unsorted-array-hashing-implementation/
-/*
-Find the smallest positive number missing from an unsorted array : Hashing Implementation
-Given an unsorted array with both positive and negative elements including 0. The task is
-to find the smallest positive number missing from the array in O(N) time.
-
-Examples:
-
-Input: arr[] = {-5, 2, 0, -1, -10, 15}
-Output: 1
-
-Input: arr[] = {0, 1, 2, 3, 4, 5}
-Output: 6
-
-Input: arr[] = {1, 1, 1, 0, -1, -2}
-Output: 2
-
-We can use hashing to solve this problem. The idea is to build a hash table of all positive
-elements in the given array. Once the hash table is built. We can look in the hash table for
-all positive integers, starting from 1. As soon as we find a number which is not there in the
-hash table, we return it.
-We can use the unordered_map in C++ to implement the hash which allow to perform look up
-operation in almost O(1) time complexity.
-*/
-
-#ifndef CXX
-
-// C program to find the smallest
-// positive missing number
-
-#include <stdio.h>
-#include <stc/cmap.h>
-c_cset(i, int);
-
-// Function to find the smallest positive
-// missing number
-int missingNumber(int a[], int n)
-{
- // Declaring an unordered_map
- cset_i mp = cset_ini;
-
- // if array value is positive
- // store it in map
- for (int i = 0; i < n; i++) {
- if (a[i] > 0)
- cset_i_insert(&mp, a[i]);
- }
-
- // index value set to 1
- int index = 1;
-
- // Return the first value starting
- // from 1 which does not exists in map
- while (1) {
- if (cset_i_find(&mp, index) == NULL) {
- return index;
- }
-
- index++;
- }
- cset_i_destroy(&mp);
-}
-
-// Driver code
-int main()
-{
- int a[] = { 1, 1, 1, 0, -1, -2 };
- int size = sizeof(a) / sizeof(a[0]);
-
- printf("Smallest positive missing number is : %d\n",
- missingNumber(a, size));
-
- return 0;
-}
-
-#else
-
-// C++ program to find the smallest
-// positive missing number
-
-#include <bits/stdc++.h>
-using namespace std;
-
-// Function to find the smallest positive
-// missing number
-int missingNumber(int a[], int n)
-{
- // Declaring an unordered_map
- unordered_map<int, int> mp;
-
- // if array value is positive
- // store it in map
- for (int i = 0; i < n; i++) {
- if (a[i] > 0)
- mp[a[i]]++;
- }
-
- // index value set to 1
- int index = 1;
-
- // Return the first value starting
- // from 1 which does not exists in map
- while (1) {
- if (mp.find(index) == mp.end()) {
- return index;
- }
-
- index++;
- }
-}
-
-// Driver code
-int main()
-{
- int a[] = { 1, 1, 1, 0, -1, -2 };
- int size = sizeof(a) / sizeof(a[0]);
-
- cout << "Smallest positive missing number is : "
- << missingNumber(a, size) << endl;
-
- return 0;
-}
-
-#endif \ No newline at end of file
diff --git a/examples/geek7.c b/examples/geek7.c
deleted file mode 100644
index eb78b43b..00000000
--- a/examples/geek7.c
+++ /dev/null
@@ -1,159 +0,0 @@
-/*
-https://www.geeksforgeeks.org/find-the-k-smallest-numbers-after-deleting-given-elements/
-
-Find the k smallest numbers after deleting given elements
-Given an array of integers, find the k smallest numbers after deleting given elements. In case of repeating elements delete only one instance in the given array for every instance of element present in the array containing the elements to be deleted.
-Assume that there are at least k elements left in the array after making n deletions.
-Examples:
-
-Input : array[] = { 5, 12, 33, 4, 56, 12, 20 }, del[] = { 12, 56, 5 }, k = 3
-Output : 4 12 20
-Explanation : After deletions { 33, 4, 12, 20 } will be left. Print top 3 smallest elements from it.
-
-Approach :
-Insert all the numbers in the hash map which are to be deleted from the array, so that we can check if the element in the array is also present in the Delete-array in O(1) time.
-Traverse through the array. Check if the element is present in the hash map.
-If present, erase it from the hash map. Else, insert it into a Min heap.
-After inserting all the elements excluding the ones which are to be deleted, Pop out k elements from the Min heap.
-*/
-
-#ifndef CXX
-
-// C implementation of the approach
-
-#include <stdio.h>
-#include <stc/clist.h>
-#include <stc/cmap.h>
-#include <stc/cvec.h>
-#include <stc/cpqueue.h>
-
-c_cmap(ii, int, int);
-c_cvec(i, int);
-c_cpqueue(i, cvec_i, >);
-
-// Find k minimum element from arr[0..m-1] after deleting
-// elements from del[0..n-1]
-void findElementsAfterDel(int arr[], int m, int del[],
- int n, int k)
-{
- // Hash Map of the numbers to be deleted
- cmap_ii mp = cmap_ini;
- for (int i = 0; i < n; ++i) {
-
- // Increment the count of del[i]
- cmap_ii_emplace(&mp, del[i], 0).first->value++;
- }
-
- cpqueue_i heap = cpqueue_i_init();
-
- for (int i = 0; i < m; ++i) {
-
- // Search if the element is present
- cmap_ii_entry_t *e = cmap_ii_find(&mp, arr[i]);
- if (e != NULL) {
-
- // Decrement its frequency
- e->value--;
-
- // If the frequency becomes 0,
- // erase it from the map
- if (e->value == 0)
- cmap_ii_erase_entry(&mp, e);
- }
-
- // Else push it in the min heap
- else
- cpqueue_i_push(&heap, arr[i]);
- }
-
- // Print top k elements in the min heap
- for (int i = 0; i < k; ++i) {
- printf("%d ", *cpqueue_i_top(&heap));
-
- // Pop the top element
- cpqueue_i_pop(&heap);
- }
- cpqueue_i_destroy(&heap);
-}
-
-int main()
-{
- int array[] = { 5, 12, 33, 4, 56, 12, 20 };
- int m = sizeof(array) / sizeof(array[0]);
-
- int del[] = { 12, 56, 5 };
- int n = sizeof(del) / sizeof(del[0]);
-
- int k = 3;
-
- findElementsAfterDel(array, m, del, n, k);
- return 0;
-}
-
-#else // =====================================================
-// C++ implementation of the approach
-
-#include <iostream>
-#include <queue>
-#include <unordered_map>
-#include <vector>
-using namespace std;
-
-// Find k minimum element from arr[0..m-1] after deleting
-// elements from del[0..n-1]
-void findElementsAfterDel(int arr[], int m, int del[],
- int n, int k)
-{
- // Hash Map of the numbers to be deleted
- unordered_map<int, int> mp;
- for (int i = 0; i < n; ++i) {
-
- // Increment the count of del[i]
- mp[del[i]]++;
- }
-
- priority_queue<int, vector<int>, greater<int> > heap;
-
- for (int i = 0; i < m; ++i) {
-
- // Search if the element is present
- if (mp.find(arr[i]) != mp.end()) {
-
- // Decrement its frequency
- mp[arr[i]]--;
-
- // If the frequency becomes 0,
- // erase it from the map
- if (mp[arr[i]] == 0)
- mp.erase(arr[i]);
- }
-
- // Else push it in the min heap
- else
- heap.push(arr[i]);
- }
-
- // Print top k elements in the min heap
- for (int i = 0; i < k; ++i) {
- cout << heap.top() << " ";
-
- // Pop the top element
- heap.pop();
- }
-}
-
-int main()
-{
- int array[] = { 5, 12, 33, 4, 56, 12, 20 };
- int m = sizeof(array) / sizeof(array[0]);
-
- int del[] = { 12, 56, 5 };
- int n = sizeof(del) / sizeof(del[0]);
-
- int k = 3;
-
- findElementsAfterDel(array, m, del, n, k);
- return 0;
-}
-
-#endif \ No newline at end of file
diff --git a/examples/inits.c b/examples/inits.c
index 5fdb5c30..1bd6e5f4 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -54,7 +54,7 @@ int main(void) {
});
c_foreach (i, cmap_id, idnames)
- printf("%d: %s\n", i.get->key, i.get->value.str);
+ printf("%d: %s\n", i.get->first, i.get->second.str);
puts("");
cmap_id_destroy(&idnames);
@@ -71,13 +71,13 @@ int main(void) {
{"Spain", 10},
{"France", 10},
});
- cmap_cnt_emplace(&countries, "Greenland", 0).first->value += 20;
- cmap_cnt_emplace(&countries, "Sweden", 0).first->value += 20;
- cmap_cnt_emplace(&countries, "Norway", 0).first->value += 20;
- cmap_cnt_emplace(&countries, "Finland", 0).first->value += 20;
+ cmap_cnt_emplace(&countries, "Greenland", 0).first->second += 20;
+ cmap_cnt_emplace(&countries, "Sweden", 0).first->second += 20;
+ cmap_cnt_emplace(&countries, "Norway", 0).first->second += 20;
+ cmap_cnt_emplace(&countries, "Finland", 0).first->second += 20;
c_foreach (i, cmap_cnt, countries)
- printf("%s: %d\n", i.get->key.str, i.get->value);
+ printf("%s: %d\n", i.get->first.str, i.get->second);
puts("");
cmap_cnt_destroy(&countries);
diff --git a/examples/mapmap.c b/examples/mapmap.c
index 94f4139e..50806e3d 100644
--- a/examples/mapmap.c
+++ b/examples/mapmap.c
@@ -9,18 +9,18 @@ c_cmap_strkey(cfg, cmap_str, cmap_str_destroy);
int main(void) {
cmap_cfg config = cmap_ini;
cmap_str init = cmap_ini;
- cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->value, "name", "Joe");
- cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->value, "groups", "proj1,proj3");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj1", "Energy");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj2", "Windy");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj3", "Oil");
- cmap_str_put(&cmap_cfg_emplace(&config, "admin", init).first->value, "employees", "2302");
+ cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "name", "Joe");
+ cmap_str_put(&cmap_cfg_emplace(&config, "user", init).first->second, "groups", "proj1,proj3");
+ cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj1", "Energy");
+ cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj2", "Windy");
+ cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj3", "Oil");
+ cmap_str_put(&cmap_cfg_emplace(&config, "admin", init).first->second, "employees", "2302");
- cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->value, "proj2", "Wind"); // Update
+ cmap_str_put(&cmap_cfg_emplace(&config, "group", init).first->second, "proj2", "Wind"); // Update
c_foreach (i, cmap_cfg, config)
- c_foreach (j, cmap_str, i.get->value)
- printf("%s: %s - %s (%u)\n", i.get->key.str, j.get->key.str, j.get->value.str, i.get->value.bucket_count);
+ c_foreach (j, cmap_str, i.get->second)
+ printf("%s: %s - %s (%u)\n", i.get->first.str, j.get->first.str, j.get->second.str, i.get->second.bucket_count);
cmap_cfg_destroy(&config);
} \ No newline at end of file
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 04ba2094..9cc9245f 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -30,7 +30,7 @@ c_cmap_str();
void print_phone_book(cmap_str phone_book)
{
c_foreach (i, cmap_str, phone_book)
- printf("%s\t- %s\n", i.get->key.str, i.get->value.str);
+ printf("%s\t- %s\n", i.get->first.str, i.get->second.str);
}
int main(int argc, char **argv)
diff --git a/examples/words.c b/examples/words.c
index 65e7e02d..d720fa0e 100644
--- a/examples/words.c
+++ b/examples/words.c
@@ -29,12 +29,12 @@ int main1()
cmap_si word_map = cmap_ini;
c_foreach (w, cvec_str, words)
- cmap_si_emplace(&word_map, w.get->str, 0).first->value += 1;
+ cmap_si_emplace(&word_map, w.get->str, 0).first->second += 1;
c_foreach (pair, cmap_si, word_map) {
printf("%d occurrences of word '%s'\n",
- pair.get->value,
- pair.get->key.str);
+ pair.get->second,
+ pair.get->first.str);
}
cmap_si_destroy(&word_map);
@@ -50,9 +50,9 @@ int main1()
int main2()
{
- std::vector<std::string> words = {
+ std::vector<std::string> words = {
"this", "sentence", "is", "not", "a", "sentence",
- "this", "sentence", "is", "a", "hoax"
+ "this", "sentence", "is", "a", "hoax"
};
std::unordered_map<std::string, size_t> word_map;
@@ -77,4 +77,4 @@ int main() {
int main() {
main1();
}
-#endif \ No newline at end of file
+#endif \ No newline at end of file