Maps in Cilk

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

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;
}

Explanation

To create an empty map in C++, you can use cilk::hashtable. Here’s the syntax:

cilk::reducer< cilk::HASHTABLE(std::string, int) > m;
  • Set key/value pairs:
m.view().insert("k1", 7);
m.view().insert("k2", 13);
  • Print all key/value pairs:
for(auto& kv : m.view()) {
    std::cout << kv.first << ":" << kv.second << " ";
}
  • Get a value for a key:
int v1 = m.view().find("k1")->second;
  • If the key doesn’t exist, return zero value:
int v3 = m.view().find("k3") == m.view().end() ? 0 : m.view().find("k3")->second;
  • Return the number of key/value pairs:
std::cout << "len: " << m.view().size() << std::endl;
  • Remove key/value pairs:
m.view().erase("k2");
  • Clear all key/value pairs:
m.view().clear();
  • Check if the key was present:
bool prs = m.view().find("k2") != m.view().end();
  • Declare and initialize a new map:
cilk::reducer< cilk::HASHTABLE(std::string, int) > n(cilk::HASHTABLE<std::string, int>::type{{"foo", 1}, {"bar", 2}});
  • Map equality check:
if (n.view() == n2.view()) {
    std::cout << "n == n2" << std::endl;
}