/** * Return a pointer to the element associated with the given handle, * or a null pointer if this Map does not hold the handle. * * Note that you will also get NULL as result if the given handle has * previously been associated with NULL. */ Handled* find(uint32_t handle) { bool handleFound = content.count(handle); if(handleFound) { return &(content[handle]); } else { return 0; } }
static void del(map_type & x, const key_type & v) { if (x.count(v) != 0) { x.erase(v); } else { PyErr_SetString(PyExc_IndexError, "Index out of range"); boost::python::throw_error_already_set(); } }
static void set(map_type & x, const key_type & v, mapped_type & m) { if (x.count(v) == 0) { x.insert(std::make_pair(v, m)); } else { PyErr_SetString(PyExc_IndexError, "Value already set"); boost::python::throw_error_already_set(); } }
static mapped_type get(map_type & x, const key_type & v) { if (x.count(v) != 0) { return x.at(v); } else { PyErr_SetString(PyExc_IndexError, "Index out of range"); boost::python::throw_error_already_set(); } }
static void scan_and_count(void const * area, size_t size, map_type const & m, map2_type & m2) { unsigned char const * p = static_cast<unsigned char const *>(area); for(size_t n = 0; n + sizeof(shared_ptr_layout) <= size; p += pointer_align, n += pointer_align) { shared_ptr_layout const * q = reinterpret_cast<shared_ptr_layout const *>(p); if(q->pn.id == abt_boost::detail::shared_count_id && q->pn.pi != 0 && m.count(q->pn.pi) != 0) { ++m2[q->pn.pi]; } } }
size_t listen(dht::DhtRunner& node, std::string chain, map_type& map) { std::future<size_t> token = node.listen(chain, [&map](const std::vector<std::shared_ptr<dht::Value>>& values) { // For every value found... for (const auto& value : values) { // Unpack then register and display it, if it's a new value. std::string content = value->unpack<std::string>(); if(!map.count(content)) { map.insert(std::make_pair(content, get_timestamp())); disp(content); } } // Continue lookup until no values are left. return true; }); if(token.wait_for(std::chrono::seconds(1)) != std::future_status::ready) { verbose("Warning: Could not create a listener since 1000ms."); verbose(" Trying again for 30s..."); if(token.wait_for(std::chrono::seconds(30)) != std::future_status::ready) verbose("Error: Failure."); else verbose(" Done."); } auto v = token.get(); verbose("Starting listening to " + chain + " with token " + std::to_string(v) + "."); return v; }
void strategy_c1(graph_type& graph, const string &q_entity, vec &f_q, map_type &index_of_kb, mat & Ws, vector<pair<double, AnswerInfo>> &answers){ if (index_of_kb.count(q_entity) == 0){ cout << "key error: can't find " << q_entity << "in index_of_kb" << endl; } static uniform_real_distribution<double> distribution(0.0, 1.0); static default_random_engine generator; unsigned start_index = index_of_kb.size(); for (auto &item1 : graph[q_entity]){ for (auto &candidate : item1.second){ vector<unsigned> answer_vec; answer_vec.push_back(index_of_kb[q_entity]); answer_vec.push_back(index_of_kb[item1.first]); // add relation in path answer_vec.push_back(index_of_kb[candidate]); double prob = min(1.0, 100.0 / graph[candidate].size()); unordered_set<unsigned> subgraph_set; // add subgraph for (auto & item2 : graph[candidate]){ if (distribution(generator) <= prob){ subgraph_set.insert(start_index + index_of_kb[item2.first]); for (auto & obj : item2.second){ subgraph_set.insert(start_index + index_of_kb[obj]); } } } //cout << "Element number of subgraph = " << subgraph_set.size() << endl; copy(subgraph_set.begin(), subgraph_set.end(), back_inserter(answer_vec)); auto score = scoreFunc(f_q, answer_vec, Ws); answers.push_back(std::move(make_pair(score * 1.5, std::move(AnswerInfo{ candidate, q_entity, 1 })))); } } //sort(answers.begin(), answers.end(), [](const pair<double, string> &lhs, const pair<double, string>&rhs){return lhs.first > rhs.first; }); }
void beam_search_c2(graph_type& graph, const string &q_entity, vec &f_q, map_type &index_of_kb, mat & Ws, vector<pair<double, AnswerInfo>> &answers){ priority_queue<triple_type, vector<triple_type>, Cmp> pq; static uniform_real_distribution<double> distribution(0.0, 1.0); static default_random_engine generator; if (index_of_kb.count(q_entity) == 0){ cout << "key error: can't find " << q_entity << "in index_of_kb" << endl; } for (auto &item1 : graph[q_entity]){ auto rel1 = item1.first; for (auto m : item1.second){ // mediator entity for (auto item2 : graph[m]){ auto rel2 = item2.first; // the 2nd hop relation vector<unsigned> answer_vec; answer_vec.push_back(index_of_kb[q_entity]); answer_vec.push_back(index_of_kb[rel1]); answer_vec.push_back(index_of_kb[rel2]); double score = scoreFunc(f_q, answer_vec, Ws); if (pq.empty() < 10){ pq.push(std::move(make_tuple(score, rel1, rel2))); } else if (get<0>(pq.top()) < score){ pq.pop(); pq.push(std::move(make_tuple(score, rel1, rel2))); } } } } unsigned start_index = index_of_kb.size(); while (!pq.empty()){ auto e = pq.top(); pq.pop(); string rel1 = get<1>(e); string rel2 = get<2>(e); for (auto & meditor_node : graph[q_entity][rel1]){ for (auto answer_node : graph[meditor_node][rel2]){ vector<unsigned> answer_vec; answer_vec.push_back(index_of_kb[q_entity]); answer_vec.push_back(index_of_kb[rel1]); answer_vec.push_back(index_of_kb[rel2]); answer_vec.push_back(index_of_kb[answer_node]); // add subgraph unordered_set<unsigned> subgraph_set; for (auto & item : graph[answer_node]){ double prob = min(1.0, 100.0 / graph[answer_node].size()); if (distribution(generator) <= prob){ subgraph_set.insert(start_index + index_of_kb[item.first]); // relation linking to answer node for (auto & o : item.second) subgraph_set.insert(start_index + index_of_kb[o]); } } //cout << "Element number of subgraph = " << subgraph_set.size() << endl; copy(subgraph_set.begin(), subgraph_set.end(), back_inserter(answer_vec)); auto score = scoreFunc(f_q, answer_vec, Ws); answers.push_back(std::move(make_pair(score, std::move(AnswerInfo{ answer_node, q_entity, 2})))); } } } //sort(answers.begin(), answers.end(), [](const pair<double, string> &lhs, const pair<double, string>&rhs){return lhs.first > rhs.first; }); }
bool has(const std::string &id) const { return _objects.count(id) > 0; }