int get(int key) { if(hashmap.count(key) == 0){ return -1; } store.splice(store.begin(), store, hashmap[key]); hashmap[key] = store.begin(); return (*hashmap[key]).second; }
/* * @param key: An integer * @return: An integer */ int get(int key) { // 1. access the cached items map // 2. update the key as the most recent used cache if (!cacheMap.count(key)) return -1; Node* item = cacheMap[key]; updateCache(item); return item->value; }
int Find(int k) { if (!parent_.count(k)) { parent_[k] = k; rank_[k] = 1; } return FindHelper(k); }
void inference_one(Blob &blob, unordered_map<string, unsigned> &index_of_word, unordered_map<string, unsigned> &index_of_kb, unordered_map <string, unordered_map<string, vector<string>>> &graph, mat &Ws, mat &Ww){ // Transform each word in question to corresponding index vector<string> words; vec f_q = zeros<vec>(Ww.n_rows); boost::split(words, blob.question, boost::is_any_of(" ")); for (auto &w : words) { if (index_of_word.count(w) > 0){ auto i = index_of_word[w]; f_q += Ww.col(i); } else{ cout << "cannot find " << w << " in index_of_word\n"; } } vector<pair<double, AnswerInfo>> answers; // Go over all candidate question topic entities for (auto &topic_e : blob.topic_entities){ strategy_c1(graph, topic_e, f_q, index_of_kb, Ws, answers); //beam_search_c2(graph, topic_e, f_q, index_of_kb, Ws, answers); } sort(answers.begin(), answers.end(), [](const pair<double, AnswerInfo> &lhs, const pair<double, AnswerInfo>&rhs){return lhs.first > rhs.first; }); std::ostringstream os; unordered_set<string> appeared; auto highest_score = answers[0].first; double threshold = 0.1; for (auto &a : answers){ AnswerInfo &info = a.second; // The candidates whose scores are not far from the best answer are regarded as predicated results. // The threshould is set to be same with the margin defined at training stage. //if (highest_score - threshold > a.first) break; if (appeared.count(a.second.answer) == 0){ os << info.answer << ":" << a.first << ":" << info.topic_entity << ":" << info.n_hop << " "; appeared.insert(info.answer); } } string answer_str = os.str(); // Extra space at last need to be removed if (answer_str.back() == ' ') answer_str.pop_back(); //static std::atomic<int> lno(0); //Ignore thread collision static int lno = 0; os.str(""); //os.clear(); os << blob.question << "\t" << blob.gold_answers_str << "\t" << answer_str << "\t" << blob.original_size_of_gold; blob.predicated = os.str(); lno++; cout << "Process to line " << lno << endl; }
bool check(string sub, vector<int> &cnt, unordered_map<string, pair<int, int> >& umap) { if (umap.count(sub)) { if (umap[sub].second > cnt[umap[sub].first]) { cnt[umap[sub].first]++; return true; } } return false; }
l get_index(unordered_map<string,l>& name_index, vector<player>& players, const string name) { if (name_index.count(name) == 0) { name_index[name] = players.size(); player p; p.name = name; players.emplace_back(p); } return name_index[name]; }
int solve(int x) { if (memo.count(x)) return memo[x]; if (x & 1) memo[x] = max(x, solve(3*x+1)); else memo[x] = max(x, solve(x >> 1)); return memo[x]; }
/* * @param key: An integer * @param value: An integer * @return: nothing */ void set(int key, int value) { if (m_capacity <= 0) return; if (cacheMap.count(key)) { cacheMap[key]->value = value; // update an existing cache also makes it most recently used updateCache(cacheMap[key]); return; } // 1. if LRUCache is full // 1.1 remove the least recent used cache - head // 1.2 insert key and value to the cache, make it the most recent used cache // 1.3 update the cache map // 2. if LRUCache is not full // 2.1 insert into the cache, and make it most recent used cache // 2.2 update the cache map if (isFull()) { Node* node = new Node(key, value); Node* temp = head; if (head == tail) { // single item, replace it with the newly created node head = node; tail = node; } else { head = head->next; head->prev = NULL; tail->next = node; node->prev = tail; tail = node; } cacheMap[key] = node; cacheMap.erase(temp->key); delete temp; } else { Node* node = new Node(key, value); if (cacheMap.empty()) { head = node; tail = node; } else { // make the node the most recent cache (tail) tail->next = node; node->prev = tail; tail = node; } cacheMap[key] = node; } }
ull nCr(int n, int k) { if (n == k) return 1; if (k <= 0) return 1; if (k == 1) return n; if (k == 2) return (0ull + n)*(n-1)/2; ull hash = 1267*n + k; if (myMap.count(hash)) return myMap[hash]; return myMap[hash] = nCr(n-1,k) + nCr(n-1,k-1); }
WordDistance(vector<string>& words) { db.reserve(words.size()); for(int i=0;i<words.size();i++){ if(db.count(words[i])) { db[words[i]].push_back(i); } else { db[words[i]] = vector<int>{i}; } } }
list<self_state> reconstruct_path(const unordered_map<self_state, self_state>& previous, const self_state& current) { list<self_state> path = {}; if (previous.count(current) > 0) { path = reconstruct_path(previous, const_cast<unordered_map<self_state, self_state> &>(previous)[current]); } path.push_back(current); return path; }
/* * Funció que afageix un regal a la llista, o incrementa les * unitats en el cas de que el regal ja existeixi. */ void afegirRegal(string nomRegal){ if(!regals.count(nomRegal)){ //cout << "El regal NO exixteix! Afegit!" << endl; regals[nomRegal] = 1; }else{ //cout << "El regal EXISTEIX! Increment!" << endl; regals[nomRegal]= regals[nomRegal]+1; } }
long double Factorial(int n) { if (n == 0) { return 1; } static unordered_map<int, long double> ans; if (ans.count(n) == 0) { ans[n] = Factorial(n - 1) * n; } return ans[n]; }
void insert_interaction(unordered_map<string,Interaction>& map,Interaction inter, vector<Ht_matrix> const& matrices,vector<double> const& theta, vector<Dataset> const& all_datasets){ string repres = inter.as_string(); if(map.count(repres) > 0) // element already in map, do nothing return; if(inter.check_for_map(matrices,theta,all_datasets)) map[repres] = inter; }
void erase(const string& idx) { if (index.empty() && count() > 0) { //...missing index, what to do? } if (index.count(idx) == 0) { //...throw } erase(index[idx]); }
void setInRange( uint16 id ) { if( um_units.count( id ) == 0 ) { // Insert Unit tmp( id ); um_units[ id ] = tmp; um_units[ id ].inrange = true; } else { um_units[ id ].inrange = true; } }
int find(int p){ if(!parent.count(p)) return -1; int root = p; while(root != parent[root]) root = parent[root]; while(root != p){ int nxt = parent[p]; parent[p] = root; p = nxt; } return p; }
/** * Prints heads of atribute updating functions * * * @param array of elements. * @param number of elements. * @param output file */ void printAttributeUpdatersHeaders(GUIElement * elements, int elementCount, FILE * file_class_source) { fprintf(file_class_source, "\n //printAttributeUpdatersHeaders() \n"); int currentId; currentId=0; for(int i=0;i<elementCount;i++) { for(int j=0;j<elements[i].attributeCount;j++) { GUIElementAttribute* att=&elements[i].attributes[j]; if(strcmp("id",att->name)==0) { continue; } ClassContainer * cont=elements[i].classContainer; PropertyAndType * prop=cont->CheckExistence(std::string(att->name)); if(prop!=0) { if(prop->cont) { if(att->name2==0) { fprintf(file_class_source, "static void Update_E%d_%s(CQMLGUI::CQML_Context *);\n",i,att->name); if(att->handler!=0) fprintf(file_class_source, "static %s Handler_E%d_%s(CQMLGUI::CQML_Context *);\n",prop->type.c_str(),i,att->name); } else { PropertyAndType * prop2=0; if(defaultClassMap.count(prop->type)>0) { prop2=defaultClasses[defaultClassMap[prop->type]]->CheckExistence(std::string(att->name2)); } if(prop2!=0) { fprintf(file_class_source, "static void Update_E%d_%s_%s(CQMLGUI::CQML_Context *);\n",i,att->name,att->name2); if(att->handler!=0) fprintf(file_class_source, "static %s Handler_E%d_%s_%s(CQMLGUI::CQML_Context *);\n",prop2->type.c_str(),i,att->name,att->name2); } } } } else { printf("nonexistent attribute: %s \nin %s\n",att->name,elements[i].name); exit(0); } } } }
double conversion(string a) { if (variables.count(a)) { //cout << "found " << a << endl; return stod(variables[a]); } else if (isalpha(a[0])) { exceptionThrown = true; errorMessage = "Undeclared-Variable"; return 1; } return stod(a); }
bool canWinNim(string s) { if (memo.count(s) > 0) { return memo[s]; } for (string s2 : generatePossibleNextMoves(s)) { if (!canWinNim(s2)) { return true; } } return memo[s] = false; }
int helper(TreeNode* node, int sum, unordered_map<int, int> &map, int prev) { if (!node) { return 0; } int val = prev + node->val; int res = ((val == sum) ? 1 : 0) + (map.count(val - sum) ? map[val - sum] : 0); map[val]++; res = res + helper(node->left, sum, map, val) + helper(node->right, sum, map, val); map[val]--; return res; }
iterator operator[](const string& idx) { if (index.empty() && count() > 0) { buildIndex(); } if (index.count(idx) == 0) { //...throw out_of_range --> index-key not found } return (*this)[index[idx]]; }
string find(string s) { if(parents.count(s) == 0) { return parents[s] = s; } else if(parents[s] == s) return s; else return find(parents[s]); }
int get_ans(long long s) { vst.clear(); if (dpth.count(s)) return dpth[s]; queue<long long> q; q.push(s); vst.insert(s); int level = 0, sz, i, j; while (!q.empty()) { sz = q.size(); for (i = 0; i < sz; ++i) { long long node = q.front(); q.pop(); long long move[] = {get_next(l, node, true), get_next(r, node, true), get_next(all, node, true), get_next(l, node, false), get_next(r, node, false), get_next(all, node, false)}; for (j = 0; j < 6; ++j) if (dpth.count(move[j])) return level + 1 + dpth[move[j]]; for (j = 0; j < 6; ++j) if (vst.find(move[j]) == vst.end()) vst.insert(move[j]), q.push(move[j]); } ++level; } }
ll product(unordered_map<ll,ll> const & xs, unordered_map<ll,ll> const & ys, ll acc, ll k) { ll cnt = 0; for (auto it : xs) { ll x, xcnt; tie(x, xcnt) = it; ll y = - (acc + x); y = (y % k + k) % k; if (ys.count(y)) { ll ycnt = ys.at(y); cnt += xcnt * ycnt; } } return cnt; }
void Lexer::get_identifier(string::const_iterator &it, Lexeme ¤t_lexeme) { if (isalpha(*it)) { string identifier; while (isalpha(*it) || isdigit(*it) || *it == '_') identifier += *(it++); --it; current_lexeme = map_identifier_types.count(identifier) != 0 ? map_identifier_types.find(identifier)->second : kId; current_lexeme.value = identifier; } }
void put(int key, int new_value) { if (key_to_value.count(key) > 0) { key_to_value[key] = new_value; refresh_time(key); } else { if (key_to_value.size() >= capacity) { erase(oldest_key()); } key_to_value[key] = new_value; refresh_time(key); } }
LL S(LL n) { if(n <= maxn) return mu[n]; if(Hash.count(n)) return Hash[n]; LL res = 0; for(LL i = 2, nex = 0; i <= n; i = nex+1) { nex = n / (n/i); res += S(n/i) * (nex - i + 1); } res = 1 - res; Hash[n] = res; return res; }
void insertToEnd(int key, int value){ if(isFull()||key2Node.count(key)!=0) return; ListNode* tmp=new ListNode(key,value); key2Node[key]=tmp; //insert to hash //insert yo linkedlist if(!head) head=tail=tmp; else{ tail->next=tmp; tmp->prev=tail; tail=tail->next; } }
void ReferencePage( deque<int>& Q, unordered_map<int,deque<int>::iterator>& M, int p ) { // if p is not in M, add p to both Q and M if( M.count(p)==0 ) { Enqueue(Q,p); M[p] = Q.begin(); } else { deque<int>::iterator it = M[p]; Q.erase(it); Enqueue(Q,p); } }