Title here
Summary here
To create an empty map in Cilk Plus, you can use the cilk::hashtable
. Here’s how to translate the given Go code into Cilk Plus.
Maps are Cilk’s built-in associative data type (sometimes called hashes or dicts in other languages).
#include <iostream>
#include <cilk/cilk.h>
#include <cilk/reducer.h>
#include <cilk/reducer_hashtable.h>
int main() {
// To create an empty map, use the cilk::hashtable
cilk::reducer< cilk::HASHTABLE(std::string, int) > m;
// Set key/value pairs using typical name[key] = val syntax
m.view().insert("k1", 7);
m.view().insert("k2", 13);
std::cout << "map: ";
for(auto& kv : m.view()) {
std::cout << kv.first << ":" << kv.second << " ";
}
std::cout << std::endl;
// Get a value for a key with name[key]
int v1 = m.view().find("k1")->second;
std::cout << "v1: " << v1 << std::endl;
// If the key doesn’t exist, the zero value of the value type is returned
int v3 = m.view().find("k3") == m.view().end() ? 0 : m.view().find("k3")->second;
std::cout << "v3: " << v3 << std::endl;
// Return the number of key/value pairs when called on a map
std::cout << "len: " << m.view().size() << std::endl;
// Remove key/value pairs from a map
m.view().erase("k2");
std::cout << "map: ";
for(auto& kv : m.view()) {
std::cout << kv.first << ":" << kv.second << " ";
}
std::cout << std::endl;
// To remove all key/value pairs from a map, clear the map
m.view().clear();
std::cout << "map: ";
for(auto& kv : m.view()) {
std::cout << kv.first << ":" << kv.second << " ";
}
std::cout << std::endl;
// The second return value indicates if the key was present in the map
bool prs = m.view().find("k2") != m.view().end();
std::cout << "prs: " << prs << std::endl;
// You can also declare and initialize a new map in the same line with this syntax
cilk::reducer< cilk::HASHTABLE(std::string, int) > n(cilk::HASHTABLE<std::string, int>::type{{"foo", 1}, {"bar", 2}});
std::cout << "map: ";
for(auto& kv : n.view()) {
std::cout << kv.first << ":" << kv.second << " ";
}
std::cout << std::endl;
// Checking for map equality
cilk::reducer< cilk::HASHTABLE(std::string, int) > n2(cilk::HASHTABLE<std::string, int>::type{{"foo", 1}, {"bar", 2}});
if (n.view() == n2.view()) {
std::cout << "n == n2" << std::endl;
}
return 0;
}
To create an empty map in C++, you can use cilk::hashtable
. Here’s the syntax:
cilk::reducer< cilk::HASHTABLE(std::string, int) > m;
m.view().insert("k1", 7);
m.view().insert("k2", 13);
for(auto& kv : m.view()) {
std::cout << kv.first << ":" << kv.second << " ";
}
int v1 = m.view().find("k1")->second;
int v3 = m.view().find("k3") == m.view().end() ? 0 : m.view().find("k3")->second;
std::cout << "len: " << m.view().size() << std::endl;
m.view().erase("k2");
m.view().clear();
bool prs = m.view().find("k2") != m.view().end();
cilk::reducer< cilk::HASHTABLE(std::string, int) > n(cilk::HASHTABLE<std::string, int>::type{{"foo", 1}, {"bar", 2}});
if (n.view() == n2.view()) {
std::cout << "n == n2" << std::endl;
}