T2 Prim<T1,T2>::min_spanning_tree(T1 source) { SetKeyVal setKeyVal; // initialize: insert all elements in the set V-X for (T1 vertex=0; vertex != graph_.nVertex(); ++vertex) { T2 val = std::numeric_limits<T2>::max(); if (vertex == source) { val = 0; } KeyVal keyVal(vertex, val); setKeyVal.insert(keyVal); // reset visited flag vecVisited_[vertex] = false; vecVal_[vertex] = val; } while (!setKeyVal.empty()) { SetKeyVal::iterator beginIt = setKeyVal.begin(); T1 curVertex = (*beginIt).key; vecVisited_[curVertex] = true; setKeyVal.erase(beginIt); // update val of the neighboring vertices std::vector<Edge<T1,T2>> vecOutEdges = graph_.outEdges(curVertex); for (std::vector<Edge<T1,T2>>::iterator edgeIt = vecOutEdges.begin(); edgeIt != vecOutEdges.end(); ++edgeIt) { T1 nextVertex = (*edgeIt).target(); if (vecVisited_[nextVertex]) { continue; } // If not visited, check if we need to update its val T2 weight = (*edgeIt).weight(); if (weight < vecVal_[nextVertex]) { //as an alternate to update we remove and insert back with updated weight SetKeyVal::iterator it = setKeyVal.find(KeyVal(nextVertex,vecVal_[nextVertex])); assert((it != setKeyVal.end()) && "element not present in set"); setKeyVal.erase(it); setKeyVal.insert(KeyVal(nextVertex,weight)); vecVal_[nextVertex] = weight; } } } // http://stackoverflow.com/questions/3221812/sum-of-elements-in-a-stdvector T2 sumVal = 0; for (std::vector<T2>::iterator valIt = vecVal_.begin(); valIt != vecVal_.end(); ++valIt) { sumVal += (*valIt); } return sumVal; }
void GameUpdate(float dt) { GetKeyboardState(keybuf); if (dbg_Pause) return; //------------------------------------------------------- // add forces //------------------------------------------------------- for(int i = 0; i < m_iNumBodies; i ++) { if (!m_pxBodies[i]) continue; if (dbg_UseGravity) { m_pxBodies[i]->AddImpulse(Vector(0, -25.0f * m_pxBodies[i]->GetMass()), dt); } } if(m_pxPlayer) { xPlayerImpulse.x = (KeyVal(VK_RIGHT) - KeyVal(VK_LEFT)) * 100.0f * m_pxPlayer->GetMass(); xPlayerImpulse.y = (KeyVal(VK_UP )) * 200.0f * m_pxPlayer->GetMass(); xPlayerImpulse -= m_pxPlayer->GetDisplacement() * 0.3f / dt; m_pxPlayer->AddImpulse(xPlayerImpulse, dt); } xPlayerImpulse = Vector(0, 0); //------------------------------------------------------- // test collisions //------------------------------------------------------- for(int i = 0; i < m_iNumBodies; i ++) { if (!m_pxBodies[i]) continue; for(int j = i+1; j < m_iNumBodies; j ++) { if (!m_pxBodies[j]) continue; m_pxBodies[i]->Collide(*(m_pxBodies[j])); } } //------------------------------------------------------- // Integrate //------------------------------------------------------- for(int i = 0; i < m_iNumBodies; i ++) { if (!m_pxBodies[i]) continue; m_pxBodies[i]->Update(dt); } }
void Main::ResetConfig() { mSettings.clear(); mSettings["Graphics"] = std::vector<KeyVal>(); mSettings["Graphics"].push_back(KeyVal("Renderer", "Direct3D9 Rendering Subsystem")); mSettings["Graphics"].push_back(KeyVal("ResolutionWidth", "1280")); mSettings["Graphics"].push_back(KeyVal("ResolutionHeight", "1024")); mSettings["Graphics"].push_back(KeyVal("AA", "0")); mSettings["Graphics"].push_back(KeyVal("Fullscreen", "No")); mSettings["Graphics"].push_back(KeyVal("VSync", "Yes")); mSettings["Graphics"].push_back(KeyVal("NVPerfHUD", "Yes")); }
void csNodeIterator::SkipWrongClassname () { if (Classname) while (Iterator->HasNext ()) { csRef<iKeyValuePair> KeyVal (CS::GetNamedChildObject<iKeyValuePair> ( CurrentNode->QueryObject (), "classname")); if (KeyVal) { bool done = !strcmp (KeyVal->GetValue (), Classname); if (done) return; } NextNode (); } }
void Main::GetConfig() { Ogre::ConfigFile cf; #if _DEBUG cf.load("BlackstarFramework_d.cfg"); #else cf.load("BlackstarFramework.cfg"); #endif Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator(); Ogre::String secName, typeName, archName; while (seci.hasMoreElements()) { secName = seci.peekNextKey(); Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext(); Ogre::ConfigFile::SettingsMultiMap::iterator i; if (mSettings.find(secName) == mSettings.end()) mSettings[secName] = std::vector<KeyVal>(); for (i = settings->begin(); i != settings->end(); i++) { bool added = false; if (secName == "Graphics") { for (auto x = mSettings[secName].begin(); x != mSettings[secName].end(); x++) { if (x->Key == i->first) { x->Val = i->second; added = true; break; } } } else mSettings[secName].push_back(KeyVal(i->first, i->second)); } } }
bool kv(std::vector<KeyVal>* items, std::set<std::string>& seen) { std::string key; nextword(key); int ch = next(); if (ch == '>' && key.empty()) { return false; } else if (ch == '#' && key.empty()) { comment(); return true; } else if (ch != '=') { throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")"); } std::string value; ch = next(); if (ch != '"') { throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">"); } while (1) { ch = next(); if (ch == '&' && !(flags & FLAG_USE_COMPAT)) { std::string varname; while (1) { ch = next(); if (isalnum(ch) || (varname.empty() && ch == '#')) varname.push_back(ch); else if (ch == ';') break; else { stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n" << "To include an ampersand or quote, use & or "\n"; throw CoreException("Parse error"); } } if (varname.empty()) throw CoreException("Empty XML entity reference"); else if (varname[0] == '#' && (varname.size() == 1 || (varname.size() == 2 && varname[1] == 'x'))) throw CoreException("Empty numeric character reference"); else if (varname[0] == '#') { const char* cvarname = varname.c_str(); char* endptr; unsigned long lvalue; if (cvarname[1] == 'x') lvalue = strtoul(cvarname + 2, &endptr, 16); else lvalue = strtoul(cvarname + 1, &endptr, 10); if (*endptr != '\0' || lvalue > 255) throw CoreException("Invalid numeric character reference '&" + varname + ";'"); value.push_back(static_cast<char>(lvalue)); } else { insp::flat_map<std::string, std::string>::iterator var = stack.vars.find(varname); if (var == stack.vars.end()) throw CoreException("Undefined XML entity reference '&" + varname + ";'"); value.append(var->second); } } else if (ch == '\\' && (flags & FLAG_USE_COMPAT)) { int esc = next(); if (esc == 'n') value.push_back('\n'); else if (isalpha(esc)) throw CoreException("Unknown escape character \\" + std::string(1, esc)); else value.push_back(esc); } else if (ch == '"') break; else if (ch != '\r') value.push_back(ch); } if (!seen.insert(key).second) throw CoreException("Duplicate key '" + key + "' found"); items->push_back(KeyVal(key, value)); return true; }
bool kv(std::vector<KeyVal>* items, std::set<std::string>& seen) { std::string key; nextword(key); int ch = next(); if (ch == '>' && key.empty()) { return false; } else if (ch == '#' && key.empty()) { comment(); return true; } else if (ch != '=') { throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")"); } std::string value; ch = next(); if (ch != '"') { throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">"); } while (1) { ch = next(); if (ch == '&' && !(flags & FLAG_USE_COMPAT)) { std::string varname; while (1) { ch = next(); if (isalnum(ch)) varname.push_back(ch); else if (ch == ';') break; else { stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n" << "To include an ampersand or quote, use & or "\n"; throw CoreException("Parse error"); } } std::map<std::string, std::string>::iterator var = stack.vars.find(varname); if (var == stack.vars.end()) throw CoreException("Undefined XML entity reference '&" + varname + ";'"); value.append(var->second); } else if (ch == '\\' && (flags & FLAG_USE_COMPAT)) { int esc = next(); if (esc == 'n') value.push_back('\n'); else if (isalpha(esc)) throw CoreException("Unknown escape character \\" + std::string(1, esc)); else value.push_back(esc); } else if (ch == '"') break; else value.push_back(ch); } if (!seen.insert(key).second) throw CoreException("Duplicate key '" + key + "' found"); items->push_back(KeyVal(key, value)); return true; }