// Candidate function for interventions static void onCubeTouch(void* ctxt, unsigned cid) { cond.next_condition(); LOG("Condition %i\n", cond.get_condition_number()); for(CubeID cid : CubeSet::connected()) { vbuf[cid].attach(cid); activateCube(cid); } }
// When pressed, move to the next condition void onTouch(void* ctxt, unsigned id) { CubeID cube(id); // On touchout (similar to mouseout) if (cube.isTouching() == 0) { cond.next_condition(); LOG("Condition %i\n", cond.get_condition_number()); for(CubeID cid : CubeSet::connected()) { vbuf[cid].attach(cid); activateCube(cid); } } }
void PrintConditions(int source, int dest, const Conditions & conditions) { printf("------------------------------约束条件如下:\n"); printf("起点: 「%d」, 终点: 「%d」\n", source, dest); printf("必须经过的点:"); bool firstBlood = false; for(Conditions::const_iterator iter = conditions.begin(); iter != conditions.end(); ++iter) { if(firstBlood) printf("|"); printf("%d", *iter); firstBlood = true; } printf("\n"); }
// RIDMap map<string, RIDList> // RIDTree map<string, ValueMap> bool Domain::Indices::Lookup(const set<string>& dimensions, const Conditions& conditions, Data::RIDTree& results) { ValuesMap values; if(!this->meta->index->OpenReader()) { return false; } if(!this->meta->index->Lookup(dimensions, values)) { this->meta->index->Close(); return false; } Data::RIDMap records; ValuesMap::iterator vlist; for(vlist = values.begin(); vlist != values.end(); vlist++) { sort(vlist->second.begin(), vlist->second.end()); conditions.Apply(vlist->first, vlist->second); if(!this->meta->index->Lookup(vlist->first, vlist->second, records)) { this->meta->index->Close(); return false; } else { results[vlist->first] = records; } } this->meta->index->Close(); return true; }
uint KSpread::qHash(const Conditions &c) { uint res = 0; foreach (const Conditional& co, c.conditionList()) { res ^= qHash(co); } return res; }
void ReadConditionsData(char *conditionsStream, int & source, int & dest, Conditions & conditions) { int i = 0; source = ReadANumberFromStr(conditionsStream, i); dest = ReadANumberFromStr(conditionsStream, i); while(conditionsStream[i] != '\0') { conditions.insert(ReadANumberFromStr(conditionsStream, i)); } }
//--------------------------------------------------------------------------------------------------------赛题入口 void search_route(char *graphStream[5000], int edge_num, char *conditionsStream) { Graph graph; EdgeInfoDict edgeInfoDict; int source; int dest; Conditions conditions; ShortestPathDict pathDict; SK66_D_dict ddict; SK66_F_dict fdict; ReadGraphData(graphStream, graph, edgeInfoDict); // read a.csv ReadConditionsData(conditionsStream, source, dest, conditions); // read b.csv // std::set<int> without; // without.insert(1); // without.insert(2); SK66(source, source, dest, conditions.size(), graph, edgeInfoDict, conditions, ddict, fdict, pathDict); std::pair<std::pair<int, int>, int> key; key.first.first = source; key.first.second = dest; key.second = conditions.size(); Path ansPath = fdict[key]; int ansCost = ansPath.first; std::vector<int> & pointPath = ansPath.second.first; std::vector<int> & edgePath = ansPath.second.second; printf("路径花费 == %d\n", ansCost); printf("共经过了「%d」个结点: ",pointPath.size()); for(std::vector<int>::const_iterator iter = pointPath.begin(); iter != pointPath.end(); ++iter) printf("%d|", (*iter)); printf("\n"); printf("共经过了「%d」条边: ", edgePath.size()); for(std::vector<int>::const_iterator iter = edgePath.begin(); iter != edgePath.end(); ++iter) printf("%d|", (*iter)); printf("\n"); }
void commit() { for ( Condition & condition : conditions ) { std::for_each( condition.macros.begin(), condition.macros.end(), macroUsedCallback_ ); } conditions.clear(); std::for_each( macros.begin(), macros.end(), macroUsedCallback_ ); macros.clear(); }
void Clear() { with_limit = false; limit_offset = 0; limit_count = -1; with_desc_asc = false; is_desc = false; with_alpha = false; aggregate = AGGREGATE_EMPTY; conds.clear(); names.clear(); orderby = Slice(); }
static bool hideSideBar(CubeID cid, Side s) { // If cid is showing a bar on side s, hide it and check if the // blicket detector should turn off. ASSERT(activeCubes.test(cid)); if (!vbuf[cid].sprites[s].isHidden()) { vbuf[cid].sprites[s].hide(); if (barSpriteCount(cid) == 0 && cid == 0) { vbuf[cid].bg0.image(vec(0,0), Backgrounds, 0); } else { vbuf[cid].bg0.image(vec(0,0), cond.get_condition(), cid - 1); } return true; } else { return false; } }
static bool showSideBar(CubeID cid, Side s) { // If cid is not showing a bar on side s, show it and check if the // blicket detector shold go off. ASSERT(activeCubes.test(cid)); if (vbuf[cid].sprites[s].isHidden()) { vbuf[cid].sprites[s].setImage(Bars[s]); vbuf[cid].sprites[s].move(getRestPosition(s)); if (barSpriteCount(cid) == 1 && cid == 0) { vbuf[cid].bg0.image(vec(0,0), Backgrounds, 1); } else { vbuf[cid].bg0.image(vec(0,0), cond.get_condition(), cid - 1); } return true; } else { return false; } }
static void activateCube(CubeID cid) { // Mark cube as active and render its canvas // activeCubes.mark(cid); vbuf[cid].initMode(BG0_SPR_BG1); if (cid == 0) { vbuf[cid].bg0.image(vec(0,0), Backgrounds, 0); } else { vbuf[cid].bg0.image(vec(0,0), cond.get_condition(), cid - 1); } auto neighbors = vbuf[cid].physicalNeighbors(); for(int side=0; side<4; ++side) { if (neighbors.hasNeighborAt(Side(side))) { showSideBar(cid, Side(side)); } else { hideSideBar(cid, Side(side)); } } }
void Conditions::add(const Conditions& cdts) { std::for_each(cdts.conditionlist().begin(), cdts.conditionlist().end(), AddCondition(*this)); }
void SK66( int node, int source, int dest, int iterCount, const Graph & graph, const EdgeInfoDict & edgeInfoDict, const Conditions & conditions, SK66_D_dict & ddict, SK66_F_dict & fdict, ShortestPathDict & pathDict) { // 当迭代次数为0时, 直接计算node->dest单源最短路径,存入结果字典里 if(iterCount == 0) { std::pair<int, int> pathToBeSolve(node, dest); if(!pathDict.count(pathToBeSolve)) Dijkstra(graph, edgeInfoDict, node, pathDict); if(!pathDict.count(pathToBeSolve)) { std::pair<std::pair<int, int>, int> key; key.first = pathToBeSolve; key.second = 0; Path path; path.first = 0xffffff; // 六个f, 足够表示无穷大了, 防止在后续加法中溢出 fdict[key] = path; } else { std::pair<std::pair<int, int>, int> key; key.first = pathToBeSolve; key.second = 0; ShortestPathDict::const_iterator PPath = pathDict.find(pathToBeSolve); Path path = PPath->second; fdict[key] = path; } } else { // 当迭代次数大于0的时候 std::pair<std::pair<int, int>, int> key; key.first = std::pair<int, int>(node, dest); key.second = iterCount; Path minCostPath; minCostPath.first = 0x7fffffff; for(Conditions::const_iterator iter = conditions.begin(); iter != conditions.end(); ++iter) { if(*iter == node) continue; // 计算D(v_i, v_l) std::pair<int, int> leftHalfPathToBeSolve(node, *iter); if(!pathDict.count(leftHalfPathToBeSolve)) { Dijkstra(graph, edgeInfoDict, node, pathDict); } if(!pathDict.count(leftHalfPathToBeSolve)) { Path leftHalfPath; leftHalfPath.first = 0xffffff; ddict[leftHalfPathToBeSolve] = leftHalfPath; } else { ShortestPathDict::const_iterator PPath = pathDict.find(leftHalfPathToBeSolve); Path leftHalfPath = PPath->second; ddict[leftHalfPathToBeSolve] = leftHalfPath; } // 计算F(v_l, t) // 筛选出最小值 std::pair<std::pair<int, int>, int> rightHalfPathToBeSolve; rightHalfPathToBeSolve.first.first = *iter; rightHalfPathToBeSolve.first.second = dest; rightHalfPathToBeSolve.second = iterCount-1; if(!fdict.count(rightHalfPathToBeSolve)) { SK66(*iter, source, dest, iterCount-1, graph, edgeInfoDict, conditions, ddict, fdict, pathDict); } if(ddict[leftHalfPathToBeSolve].first + fdict[rightHalfPathToBeSolve].first < minCostPath.first) { minCostPath.first = ddict[leftHalfPathToBeSolve].first + fdict[rightHalfPathToBeSolve].first; minCostPath.second.first.clear(); minCostPath.second.second.clear(); minCostPath.second.first.insert(minCostPath.second.first.end(), ddict[leftHalfPathToBeSolve].second.first.begin(), ddict[leftHalfPathToBeSolve].second.first.end()); minCostPath.second.first.insert(minCostPath.second.first.end(), fdict[rightHalfPathToBeSolve].second.first.begin(), fdict[rightHalfPathToBeSolve].second.first.end()); minCostPath.second.second.insert( minCostPath.second.second.end(), ddict[leftHalfPathToBeSolve].second.second.begin(), ddict[leftHalfPathToBeSolve].second.second.end()); minCostPath.second.second.insert( minCostPath.second.second.end(), fdict[rightHalfPathToBeSolve].second.second.begin(), fdict[rightHalfPathToBeSolve].second.second.end()); } } fdict[key] = minCostPath; } }
ActionConditions Planner::findActions(Actions actions, Conditions preConditions){ ActionConditions result; for(auto& a: actions){ Conditions postA = a->getPreConditions(); if(satisfies(postA, preConditions)){ Conditions yetToSatisfy; std::set_difference(preConditions.begin(),preConditions.end(), postA.begin(), postA.end(), std::inserter(yetToSatisfy, yetToSatisfy.begin()) ); Conditions combined; Conditions preA = a->getPostConditions(); std::set_union(preA.begin(), preA.end(), yetToSatisfy.begin(), yetToSatisfy.end(), std::inserter(combined, combined.begin())); bool c = false; for(auto& m : mutexes) if(satisfies(combined, m)){ c=true;break; } if(!c) result.insert({combined,a}); } } return result; }
Condition & condition() { assert( !empty() ); return conditions.back(); }
bool empty() const { return conditions.empty(); }