int main() { vector<string> x; string start; while(cin >> name1 >> name2) { if(graph.empty()) { start = name1; } if(graph.count(name1) == 0) { if(graph.count(name2) == 0) { //neither are in graph.emplace(name1, x); color.emplace(name1, "WHITE"); d.emplace(name1, 0); graph.emplace(name2, x); color.emplace(name2, "WHITE"); d.emplace(name2, 0); graph[name1].push_back(name2); graph[name2].push_back(name1); } else { //name1 is not in, but name2 is graph.emplace(name1, x); color.emplace(name1, "WHITE"); d.emplace(name1, 0); graph[name1].push_back(name2); graph[name2].push_back(name1); } } else if(graph.count(name1) > 0) { if(graph.count(name2) == 0) { //name1 is in, but not name2 graph.emplace(name2, x); color.emplace(name2, "WHITE"); d.emplace(name2, 0); graph[name1].push_back(name2); graph[name2].push_back(name1); } else { //both are in graph[name1].push_back(name2); graph[name2].push_back(name1); } } } BFS(start); }
shared_ptr<Sort> SymbolStack::replace(shared_ptr<Sort> sort, unordered_map<string, shared_ptr<Sort>>& mapping) { if (mapping.empty()) return sort; if (!sort->hasArgs()) { if (mapping.find(sort->toString()) != mapping.end()) return mapping[sort->toString()]; else return sort; } else { vector<shared_ptr<Sort>> newargs; bool changed = false; vector<shared_ptr<Sort>> argSorts = sort->getArgs(); for (auto argIt = argSorts.begin(); argIt != argSorts.end(); argIt++) { shared_ptr<Sort> result = replace(*argIt, mapping); newargs.push_back(result); if (result.get() != (*argIt).get()) changed = true; } if (changed) { return make_shared<Sort>(sort->getIdentifier(), newargs); } else { return sort; } } }
void set(int key, int value) { if (cache.empty()){ o_key=key; y_key=key; cache[key].value=value; if (debug) print(); return; } if (cache.count(key)>0){ //already present cache[key].value=value; //update value bool temp_debug=debug; debug=0; get(key); debug=temp_debug; }else{ cache[key].value=value; //insert entry cache[key].older=y_key; cache[y_key].younger=key; y_key=key; if (cache.size()>capacity){ //delete the oldest one o_key=cache[o_key].younger; cache.erase(cache[o_key].older); } } if (debug) print(); }
string topoSort(unordered_map<char, unordered_set<char>>& graph, unordered_map<char, int>& mp) { // For chars without any incoming edges, we output it as the start of the string. string res = ""; while (!mp.empty()) { bool found = false; for (auto& pair : mp) { if (pair.second == 0) { res.push_back(pair.first); for (auto& nn : graph[pair.first]) { mp[nn]--; } graph.erase(pair.first); mp.erase(pair.first); found = true; break; } } if (!found) { res.clear(); return res; } } return res; }
/* * @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; } }
bool helper(string s, unordered_map<string, int> hash){ for(int i=0; i<s.size(); i+=wordLen){ string t = s.substr(i,wordLen); if(hash.find(t)==hash.end()) return false; hash[t]--; if(hash[t]<0) return false; if(hash[t]==0) hash.erase(t); } return hash.empty(); }
void erase(const string& idx) { if (index.empty() && count() > 0) { //...missing index, what to do? } if (index.count(idx) == 0) { //...throw } erase(index[idx]); }
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]]; }
void print(){ if (cache.empty()) cout<<"Empty!"<<endl; else{ int current_key=y_key; while (true){ cout<<current_key<<':'<<cache[current_key].value<<' '; if (current_key==o_key) break; current_key=cache[current_key].older; } cout<<endl; } }
//------------------------------------------------------------------------------ PD_Particles load_pd(string loadPath, unordered_map<string, double> particleParameters) { PD_Particles particles = load_pd(loadPath); if(!particleParameters.empty()) { for(auto pm:particleParameters) { particles.registerParameter(pm.first, pm.second); } } return particles; }
int main(int argc, char *argv[]) { char *progname = argv[0]; if (argc < 2) { usage(progname); exit(1); } read_parameters(argc, argv); if (options["help"]) { usage(progname); return 0; } string model_filename = positional[0]; if (options["verbose"]) { cout << ">> Reading file " << model_filename << " ..." << endl; } if (read_uai_model(model_filename, &model)) { return -1; } if (options["verbose"]) { cout << *model << endl; } if (positional.size() > 1) { string evidence_filename = positional[1]; if (options["verbose"]) { cout << ">> Reading file " << evidence_filename << " ..." << endl; } if (read_uai_evidence(evidence_filename, evidence)) { return -2; } if (options["verbose"] && !evidence.empty()) { cout << ">> Evidence:" << endl; for (auto it : evidence) { cout << "Variable = " << it.first << ",\tValue = " << it.second << endl; } cout << endl; } } execute_task(); delete model; return 0; }
//根据数据集构建FP树 void FPTree::create(const unordered_map<string, int> &data) { unordered_map<string, int> umkey_count;//记录所有的键,即每个记录项,以及计数 string k; for (auto iter1 = data.begin(); iter1 != data.end(); ++iter1) //第一次扫描整个数据集 {//每条记录都是字符串对象,采用空格分开 istringstream stream(iter1->first); //初始化一个字符串输入流 while (stream >> k) {//逐一获得记录项 if (umkey_count.find(k) == umkey_count.end()) //如果不存在于头指针列表,则插入 umkey_count.emplace(move(k), iter1->second); else //否则只需要累加计数值即可 umkey_count[k] += iter1->second; } } for (auto iter2 = umkey_count.begin(); iter2 != umkey_count.end(); ++iter2) {//扫描每个记录项 if (iter2->second >= eps) //把较高频数的记录项留下 { head.emplace(iter2->first, new headNode(iter2->second)); } } if (head.empty()) return; //如果没有出现次数超过阈值的记录项,则退出 for (auto iter3 = data.begin(); iter3 != data.end(); ++iter3) {//第二次扫描整个数据集 //对于每条记录,根据记录项和出现的总次数构造节点,然后插入到集合 vector<node> key_count; istringstream stream(iter3->first); while (stream >> k) {//对于该条记录中的每个记录项 if (head.find(k) != head.end()) //只有出现次数较高 key_count.emplace_back(node(k, head[k]->count)); //才会被插入到集合中 } if (!key_count.empty()) {//若最终该条记录中有出现次数较多的项 sort(key_count.begin(), key_count.end(), node::comparer::greater()); vector<string> order; //则按照出现次数从高到低的顺序提取出来 for_each(key_count.begin(), key_count.end(), [&order](const node &arg){order.emplace_back(arg.key); }); updateTree(order, 0, root, iter3->second);//用于更新树 } } }
GMsg VectorBuffer::EditFeaturesFromInteraction( const unordered_map<long, feature_ptr> &features, const string &proj) { if (features.empty()) return G_NOERR; Q_ASSERT(proj == _layer->proj()); _undoStack->beginMacro(features.size() == 1 ? "Edit feature" : "Edit features"); for (unordered_map<long, feature_ptr>::const_iterator it = features.begin(); it != features.end(); it++) { long fid = it->first; feature_ptr src = it->second; Q_ASSERT(_deletedFeatures.find(fid) == _deletedFeatures.end()); src->put<value_unicode_string>( _config_app().VIEW_VECTORBUFFER_FIELD_EDITED, transcoder("utf-8").transcode("edited")); _undoStack->push(new VBCommandEditFeature(this, fid, src)); } _undoStack->endMacro(); return G_NOERR; }
int MonteCarloNode::select(Board& board) { if (n_ < kN) { int winner = RandomPlayout::simulate(board); if (p_ == winner) { w_++; } n_++; return winner; } if (n_ == kN) { initNext(board); } if (next_.empty()) { int winner = board.getCurrentPlayer(); if (p_ == winner) { w_++; } n_++; return winner; } auto element = max_element(next_.begin(), next_.end(), [this](auto& a, auto& b){ return a.second->uct(n_) < b.second->uct(n_); }); board.move(element->first); int winner = element->second->select(board); if (winner == p_) { w_++; } n_++; return winner; }
/* hook DestroyInstance to remove tableInstanceMap entry */ VK_LAYER_EXPORT VKAPI_ATTR void VKAPI_CALL vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks *pAllocator) { dispatch_key key = get_dispatch_key(instance); layer_data *my_data = get_my_data_ptr(key, layer_data_map); VkLayerInstanceDispatchTable *pTable = my_data->instance_dispatch_table; pTable->DestroyInstance(instance, pAllocator); // Clean up logging callback, if any while (my_data->logging_callback.size() > 0) { VkDebugReportCallbackEXT callback = my_data->logging_callback.back(); layer_destroy_msg_callback(my_data->report_data, callback, pAllocator); my_data->logging_callback.pop_back(); } layer_debug_report_destroy_instance(my_data->report_data); delete my_data->instance_dispatch_table; layer_data_map.erase(key); if (layer_data_map.empty()) { // Release mutex when destroying last instance. loader_platform_thread_delete_mutex(&globalLock); globalLockInitialized = 0; } }
void execute_marginals() { double uptime; vector<const Factor*> marginals = model->marginals(evidence, options, uptime); cout << ">> Marginals:" << endl; for (auto pf : marginals) { cout << *pf << endl; delete pf; } if (options["verbose"] && !evidence.empty()) { cout << ">> Evidence:" << endl; for (auto it : evidence) { cout << "Variable = " << it.first << ", Value = " << it.second << endl; } cout << endl; } cout << ">> Executed in " << uptime << "ms." << endl << endl; }
// query the status of the download/upload files. void* status_thread(void *arg) { //initialize the prev size. pthread_mutex_lock(&download_mutex); //update the prev to the current in the download array. for(unordered_map<string, record>::iterator iter = download_array.begin(); iter != download_array.end(); ++ iter) iter->second.prev = iter->second.current; pthread_mutex_unlock(&download_mutex); //update the prev to the current in the upload array. pthread_mutex_lock(&upload_mutex); for(unordered_map<string, record>::iterator iter = upload_array.begin(); iter != upload_array.end(); ++ iter) iter->second.prev = iter->second.current; pthread_mutex_unlock(&upload_mutex); sleep_us(500000); bool empty = false; int move_up = 0; while(status_query) { for(int i = 0; i< move_up; ++i) { printf("\033[1A"); printf("\033[K"); } move_up = 0; string result; char temp[MAXLINE]; pthread_mutex_lock(&download_mutex); if(download_array.empty()) empty = true; // show the progress of downloading files. for(unordered_map<string, record>::iterator iter= download_array.begin(); iter != download_array.end(); ++ iter) { get_progress("downloading", iter->first.c_str(), iter->second.prev, iter->second.current, iter->second.sum, temp); iter->second.prev = iter->second.current; result += temp; ++move_up; } pthread_mutex_unlock(&download_mutex); pthread_mutex_lock(&upload_mutex); if(empty && upload_array.empty()) empty = true; else empty = false; // show the progress of uploading files. for(unordered_map<string, record>::iterator iter = upload_array.begin(); iter != upload_array.end(); ++ iter) { get_progress("uploading", iter->first.c_str(), iter->second.prev, iter->second.current, iter->second.sum, temp); iter->second.prev = iter->second.current; result += temp; ++move_up; } pthread_mutex_unlock(&upload_mutex); if(empty) break; printf("%s", result.c_str()); fflush(stdout); sleep_us(500000); } if(empty) { printf("no download/upload files"); status_query = false; } return NULL; }