void compile_only() { const int N = 3; concurrency::index<N> idx1(10, 11, 12); // Create a new index using copy contructor concurrency::index<N+1> idx2(idx1); }
/*---------------- Test on Host ------------------ */ bool CopyConstructWithIndexOnHost() { Log()<< "Testing copy construct index with another index on host" << std::endl; index<RANK> idx1(0, 1, 2); index<RANK> idx2(idx1); // copy construct return IsIndexSetToSequence<RANK>(idx2); }
void LOCA::MultiContinuation::ConstrainedGroup::fillB( NOX::Abstract::MultiVector& B) const { std::string callingFunction = "LOCA::MultiContinuation::ConstrainedGroup::fillB"; bool isZeroB = constraintsPtr->isDXZero(); Teuchos::RCP<const NOX::Abstract::MultiVector> my_B; if (!isZeroB) { Teuchos::RCP<const LOCA::MultiContinuation::ConstraintInterfaceMVDX> constraints_mvdx = Teuchos::rcp_dynamic_cast<const LOCA::MultiContinuation::ConstraintInterfaceMVDX>(constraintsPtr); if (constraints_mvdx == Teuchos::null) globalData->locaErrorCheck->throwError( callingFunction, std::string("Constraints object must be of type") + std::string("ConstraintInterfaceMVDX")); my_B = Teuchos::rcp(constraints_mvdx->getDX(),false); } // If the underlying system isn't bordered, we're done if (!isBordered) { if (isZeroB) B.init(0.0); else B = *my_B; return; } // Create views for underlying group int w = bordered_grp->getBorderedWidth(); std::vector<int> idx1(w); for (int i=0; i<w; i++) idx1[i] = i; Teuchos::RCP<NOX::Abstract::MultiVector> underlyingB = B.subView(idx1); // Combine blocks in underlying group bordered_grp->fillB(*underlyingB); // Create views for my blocks std::vector<int> idx2(numParams); for (int i=0; i<numParams; i++) idx2[i] = w+i; Teuchos::RCP<NOX::Abstract::MultiVector> my_B_x = B.subView(idx2); // Extract solution component from my_B and store in B if (isZeroB) my_B_x->init(0.0); else bordered_grp->extractSolutionComponent(*my_B, *my_B_x); }
/*---------------- Test on Host ------------------ */ runall_result TestOnHost() { runall_result result; Log() << "Testing constructor that takes individual co-ordinates on host" << std::endl; index<1> idx1(0); index<2> idx2(0, 1); index<3> idx3(0, 1, 2); result &= REPORT_RESULT(IsIndexSetToSequence<1>(idx1)); result &= REPORT_RESULT(IsIndexSetToSequence<2>(idx2)); result &= REPORT_RESULT(IsIndexSetToSequence<3>(idx3)); return result; }
Lexer::Token I1::parseAttributes( Lexer* lexer ) { Lexer::Token tok( document->getNextToken() ); while( tok != Lexer::TAGEND ) { if( tok == Lexer::ATTRIBUTE ) { std::wstring key; std::wstring value; splitAttribute( lexer->text(), key, value ); if( key == L"id" ) { id = value; try { document->addIndexId( id, this ); } catch( Class3Error& e ) { document->printError( e.code ); } } else if( key == L"roots" ) { std::wstring::size_type idx1( 0 ); std::wstring::size_type idx2( value.find( L' ' ) ); while( idx1 != std::wstring::npos ) { //split value on ' ' synRoots.push_back( value.substr( idx1, idx2 - idx1 ) ); idx1 = idx2 == std::wstring::npos ? std::wstring::npos : idx2 + 1; idx2 = value.find( L' ', idx1 ); } } else if( key == L"sortkey" ) primary->setSortKey( value ); else document->printError( ERR1_ATTRNOTDEF ); } else if( tok == Lexer::FLAG ) { if( lexer->text() == L"global" ) primary->setGlobal(); else document->printError( ERR1_ATTRNOTDEF ); } else if( tok == Lexer::ERROR_TAG ) throw FatalError( ERR_SYNTAX ); else if( tok == Lexer::END ) throw FatalError( ERR_EOF ); else document->printError( ERR1_TAGSYNTAX ); tok = document->getNextToken(); } return document->getNextToken(); //consume TAGEND }
std::string canonicalPath( char* arg ) { std::auto_ptr< char > cwd( ::getcwd( 0, 0 ) ); std::string fullpath( cwd.get() ); std::string inFile( arg ); #if defined( __UNIX__ ) || defined( __APPLE__ ) const char* srchstr = "../"; char sep = '/'; #else const char* srchstr = "..\\"; char sep = '\\'; #endif std::string::size_type idx1( inFile.find( srchstr ) ); if( idx1 == 0 ) { while( idx1 == 0 ) { //must be at start of line std::string::size_type idx2( fullpath.rfind( sep ) ); if( idx2 != std::string::npos ) { fullpath.erase( idx2 ); inFile.erase( idx1, 3 ); } else if( !fullpath.empty() ) { #if defined( __UNIX__ ) || defined( __APPLE__ ) idx2 = 0; #else idx2 = fullpath.find( ':' ); //don't kill drive if( idx2 != std::string::npos ) ++idx2; #endif fullpath.erase( idx2 ); inFile.erase( 0, 3 ); break; } idx1 = inFile.find( srchstr ); } fullpath += sep; fullpath += inFile; } else fullpath = inFile; #if !defined( __UNIX__ ) && !defined( __APPLE__ ) if( fullpath.size() > PATH_MAX ) throw FatalError( ERR_PATH_MAX ); #endif return fullpath; }
void LOCA::Homotopy::DeflatedGroup:: fillB(NOX::Abstract::MultiVector& B) const { string callingFunction = "LOCA::Homotopy::DeflatedGroup::fillB"; Teuchos::RCP<const NOX::Abstract::MultiVector> my_B = totalDistMultiVec; // If the underlying system isn't bordered, we're done if (!isBordered) { B = *my_B; return; } // Create views for underlying group int w = bordered_grp->getBorderedWidth(); std::vector<int> idx1(w); for (int i=0; i<w; i++) idx1[i] = i; Teuchos::RCP<NOX::Abstract::MultiVector> underlyingB = B.subView(idx1); // Combine blocks in underlying group bordered_grp->fillB(*underlyingB); // Create views for my blocks std::vector<int> idx2(2); for (int i=0; i<1; i++) idx2[i] = w+i; Teuchos::RCP<NOX::Abstract::MultiVector> my_B_x = B.subView(idx2); // Extract solution component from my_B and store in B bordered_grp->extractSolutionComponent(*my_B, *my_B_x); }
void LOCA::Homotopy::DeflatedGroup:: fillA(NOX::Abstract::MultiVector& A) const { string callingFunction = "LOCA::Homotopy::DeflatedGroup::fillA"; Teuchos::RCP<const NOX::Abstract::MultiVector> my_A = underlyingF; // If the underlying system isn't bordered, we're done if (!isBordered) { A = *my_A; return; } // Create views for underlying group int w = bordered_grp->getBorderedWidth(); std::vector<int> idx1(w); for (int i=0; i<w; i++) idx1[i] = i; Teuchos::RCP<NOX::Abstract::MultiVector> underlyingA = A.subView(idx1); // Fill A block in underlying group bordered_grp->fillA(*underlyingA); // Create views for my blocks std::vector<int> idx2(1); for (int i=0; i<1; i++) idx2[i] = w+i; Teuchos::RCP<NOX::Abstract::MultiVector> my_A_x = A.subView(idx2); // Extract solution component from my_A and store in A bordered_grp->extractSolutionComponent(*my_A, *my_A_x); }
// Method to store conf data in this class' data map void ConfDataReader::loadData(void) throw(ConfigurationException) { // By default, section name is "DEFAULT" std::string sectionName("DEFAULT"); // Do this until end-of-file reached or something else happens while(1) { try { std::string line; std::string variable; std::string value; formattedGetLine(line, true); // If line is too long, we throw an exception if (line.size()>255) { ConfigurationException e( "Line too long in configuration file '" + filename + "'." ); GPSTK_THROW(e); } // Skip the blank line if(line.size()<1) continue; // Let's find and strip comment lines if( (StringUtils::firstWord(line)[0] == '#') || (StringUtils::firstWord(line)[0] == ';') ) { formattedGetLine(line, true); } // Let's strip comments at the end of lines std::string::size_type idx(line.find('#')); if( !(idx == std::string::npos) ) { line = line.substr(0, idx); } idx = line.find(';'); if( !(idx == std::string::npos) ) { line = line.substr(0, idx); } // Remove trailing and leading blanks line = StringUtils::strip(line); // Skip blank lines if (line.size()==0) { continue; } // Let's start to get data out of file // First, handle section names // Test if this line declares a new section. Check for '[' idx = line.find('['); if( !(idx == std::string::npos) ) { // Now, check if there is a closing ']' std::string::size_type idx2(line.find(']')); if( !(idx2 == std::string::npos) ) { // Extract name and remove trailing and leading blanks line = StringUtils::strip( line.substr(idx+1, idx2-idx-1) ); // Check if section name is appropriate if( checkName(line) ) { // Update 'sectionName': make it uppercase sectionName = StringUtils::upperCase(line); } else { // Throw an exception if section name isn't appropriate ConfigurationException e( "Section name '" + line + "' in configuration file '" + filename + "' does not comply with rules."); GPSTK_THROW(e); } // If this was a section line, continue with next line continue; } else { // Throw an exception if section line is not closed ConfigurationException e( "Section line '" + line + "' in configuration file '" + filename + "' was improperly closed" ); GPSTK_THROW(e); } } // Second, handle variables // Separate variable name from value. Look for separators idx = line.find('='); if( idx == std::string::npos ) { idx = line.find(':'); } // If we found a separator, keep processing if( !(idx == std::string::npos) ) { // Read variable and value variable = StringUtils::strip( line.substr(0, idx) ); value = StringUtils::strip( line.substr(idx+1) ); // Now separate comments // Work on 'variable' std::string varComment; idx = variable.find(','); if( !(idx == std::string::npos) ) { varComment = StringUtils::strip(variable.substr(idx+1)); variable = StringUtils::strip(variable.substr(0, idx)); } // Check if variable name is appropriate if( checkName(variable) ) { // Make 'variable' uppercase variable = StringUtils::upperCase(variable); } else { // Throw an exception if variable name isn't appropriate ConfigurationException e( "Variable name '" + variable + "' in configuration file '" + filename + "' does not comply with rules."); GPSTK_THROW(e); } // Now work on 'value' std::string valueComment; idx = value.find(','); if( !(idx == std::string::npos) ) { valueComment = StringUtils::strip(value.substr(idx+1)); value = StringUtils::strip(value.substr(0, idx)); } // Store configuration data variableData varData; varData.varComment = varComment; varData.value = value; varData.valueComment = valueComment; confData[sectionName][variable] = varData; } } // End of try block catch (ConfigurationException& e) { GPSTK_THROW(e); } catch (EndOfFile& e) { // Initialize itCurrentSection itCurrentSection = confData.begin(); return; } catch (...) { return; } } // End of 'while(1)' } // End of method 'ConfDataReader::loadData()'
void LOCA::BorderedSolver::Nested::setMatrixBlocks( const Teuchos::RCP<const LOCA::BorderedSolver::AbstractOperator>& oper, const Teuchos::RCP<const NOX::Abstract::MultiVector>& blockA, const Teuchos::RCP<const LOCA::MultiContinuation::ConstraintInterface>& blockB, const Teuchos::RCP<const NOX::Abstract::MultiVector::DenseMatrix>& blockC) { string callingFunction = "LOCA::BorderedSolver::Nested::setMatrixBlocks()"; // Cast oper to a bordered operator Teuchos::RCP<const LOCA::BorderedSolver::JacobianOperator> op = Teuchos::rcp_dynamic_cast<const LOCA::BorderedSolver::JacobianOperator>(oper); if (op == Teuchos::null) globalData->locaErrorCheck->throwError( callingFunction, string("Operaror must be of type LOCA::BorderedSolver::JacobianOperator") + string(" in order to use nested bordered solver strategy.")); // Get bordered group grp = Teuchos::rcp_dynamic_cast<const LOCA::BorderedSystem::AbstractGroup>(op->getGroup()); if (grp == Teuchos::null) globalData->locaErrorCheck->throwError( callingFunction, string("Group must be of type LOCA::BorderedSystem::AbstractGroup") + string(" in order to use nested bordered solver strategy.")); Teuchos::RCP<const LOCA::MultiContinuation::ConstraintInterfaceMVDX> con_mvdx = Teuchos::rcp_dynamic_cast<const LOCA::MultiContinuation::ConstraintInterfaceMVDX>(blockB); if (con_mvdx == Teuchos::null) globalData->locaErrorCheck->throwError( callingFunction, "Constraints object must be of type ConstraintInterfaceMVDX"); bool isZeroA = (blockA.get() == NULL); bool isZeroB = con_mvdx->isDXZero(); bool isZeroC = (blockC.get() == NULL); Teuchos::RCP<const NOX::Abstract::MultiVector> blockB_dx; if (!isZeroB) blockB_dx = Teuchos::rcp(con_mvdx->getDX(), false); // ensure blocks B and C are not both zero if (isZeroB && isZeroC) globalData->locaErrorCheck->throwError( callingFunction, "Blocks B and C cannot both be zero"); // ensure blocks A and C are not both zero if (isZeroA && isZeroC) globalData->locaErrorCheck->throwError( callingFunction, "Blocks A and C cannot both be zero"); // Get unbordered group unbordered_grp = grp->getUnborderedGroup(); // get number of outer constraints if (isZeroB) numConstraints = blockC->numRows(); else numConstraints = blockB_dx->numVectors(); // Get total bordered width underlyingWidth = grp->getBorderedWidth(); myWidth = underlyingWidth + numConstraints; // combine blocks bool isCombinedAZero = grp->isCombinedAZero(); bool isCombinedBZero = grp->isCombinedBZero(); bool isCombinedCZero = grp->isCombinedCZero(); Teuchos::RCP<NOX::Abstract::MultiVector> A; Teuchos::RCP<NOX::Abstract::MultiVector> B; Teuchos::RCP<NOX::Abstract::MultiVector::DenseMatrix> C; if (!isCombinedAZero || !isZeroA) { A = unbordered_grp->getX().createMultiVector(myWidth); A->init(0.0); } if (!isCombinedBZero || !isZeroB) { B = unbordered_grp->getX().createMultiVector(myWidth); B->init(0.0); } if (!isCombinedCZero || !isZeroC) { C = Teuchos::rcp(new NOX::Abstract::MultiVector::DenseMatrix(myWidth, myWidth)); C->putScalar(0.0); } std::vector<int> idx1(underlyingWidth); for (int i=0; i<underlyingWidth; i++) idx1[i] = i; if (!isCombinedAZero) { Teuchos::RCP<NOX::Abstract::MultiVector> underlyingA = A->subView(idx1); grp->fillA(*underlyingA); } if (!isCombinedBZero) { Teuchos::RCP<NOX::Abstract::MultiVector> underlyingB = B->subView(idx1); grp->fillB(*underlyingB); } if (!isCombinedCZero) { NOX::Abstract::MultiVector::DenseMatrix underlyingC(Teuchos::View, *C, underlyingWidth, underlyingWidth, 0, 0); grp->fillC(underlyingC); } std::vector<int> idx2(numConstraints); for (int i=0; i<numConstraints; i++) idx2[i] = underlyingWidth+i; if (!isZeroA) { Teuchos::RCP<NOX::Abstract::MultiVector> my_A_x = A->subView(idx2); NOX::Abstract::MultiVector::DenseMatrix my_A_p(Teuchos::View, *C, underlyingWidth, numConstraints, 0, underlyingWidth); grp->extractSolutionComponent(*blockA, *my_A_x); grp->extractParameterComponent(false, *blockA, my_A_p); } if (!isZeroB) { Teuchos::RCP<NOX::Abstract::MultiVector> my_B_x = B->subView(idx2); NOX::Abstract::MultiVector::DenseMatrix my_B_p(Teuchos::View, *C, numConstraints, underlyingWidth, underlyingWidth, 0); grp->extractSolutionComponent(*blockB_dx, *my_B_x); grp->extractParameterComponent(true, *blockB_dx, my_B_p); } if (!isZeroC) { NOX::Abstract::MultiVector::DenseMatrix my_CC(Teuchos::View, *C, numConstraints, numConstraints, underlyingWidth, underlyingWidth); my_CC.assign(*blockC); } // Create unbordered operator Teuchos::RCP<LOCA::BorderedSolver::AbstractOperator> unbordered_op = Teuchos::rcp(new LOCA::BorderedSolver::JacobianOperator(unbordered_grp)); // set blocks in solver solver->setMatrixBlocksMultiVecConstraint(unbordered_op, A, B, C); }
/** * wxTreeCtrlのインスタンスを受け取って共通の設定を行う * * @param wxTreeCtrl* treeCtrl 設定対象のツリー * @param const wxWindowID id 設定対象のGUIの部位を表すID */ void JaneCloneUiUtil::SetTreeCtrlCommonSetting(wxTreeCtrl* treeCtrl, const wxWindowID id) { // プロパティファイルにフォント設定/背景色があれば使用する wxString widgetsName = wxT("ID_TreeFontButton"); wxString widgetsInfo = wxEmptyString; JaneCloneUtil::GetJaneCloneProperties(widgetsName, &widgetsInfo); if (widgetsInfo != wxEmptyString) { wxFont font; bool ret = font.SetNativeFontInfoUserDesc(widgetsInfo); if(ret) treeCtrl->SetFont(font); } widgetsName = wxT("ID_BoardListBGColorButton"); widgetsInfo.Clear(); JaneCloneUtil::GetJaneCloneProperties(widgetsName, &widgetsInfo); if (widgetsInfo != wxEmptyString) { wxColour bgColor; bool ret = bgColor.Set(widgetsInfo); if(ret) treeCtrl->SetBackgroundColour(bgColor); } wxTreeItemData treeData; wxTreeItemId m_rootId; // イメージリストにアイコンを登録する #ifndef __WXMAC__ wxImageList* treeImage = new wxImageList(16, 16); wxBitmap idx1(wxT("rc/folder.png"), wxBITMAP_TYPE_PNG); treeImage->Add(idx1); wxBitmap idx2(wxT("rc/text-html.png"), wxBITMAP_TYPE_PNG); treeImage->Add(idx2); #else // Macの場合画像ファイル読み込みの場所が異なる wxImageList* treeImage = new wxImageList(16, 16); wxBitmap idx1(wxT("JaneClone.app/Contents/MacOS/rc/folder.png"), wxBITMAP_TYPE_PNG); treeImage->Add(idx1); wxBitmap idx2(wxT("JaneClone.app/Contents/MacOS/rc/text-html.png") , wxBITMAP_TYPE_PNG); treeImage->Add(idx2); #endif treeCtrl->AssignImageList(treeImage); // ツリー部分へのカーソル合わせが起きた場合のイベント通知 treeCtrl->Connect(id, wxEVT_ENTER_WINDOW, wxMouseEventHandler(JaneClone::OnEnterWindow), NULL, wxWindow::FindWindowById(ID_WxJaneClone)); switch (id) { case ID_BoardTreectrl: { treeCtrl->SetLabel(BOARD_TREE); wxTreeItemId rootTemp = treeCtrl->AddRoot(wxT("2ch板一覧")); treeCtrl->SetItemImage(rootTemp, 0, wxTreeItemIcon_Normal); } break; case ID_FavsTreectrl: { treeCtrl->SetLabel(FAVS_TREE); wxTreeItemId rootTemp = treeCtrl->AddRoot(wxT("お気に入り一覧")); treeCtrl->SetItemImage(rootTemp, 0, wxTreeItemIcon_Normal); } break; case ID_NowReadingTreectrl: { treeCtrl->SetLabel(NOW_READ_TREE); wxTreeItemId rootTemp = treeCtrl->AddRoot(wxT("閲覧中一覧")); treeCtrl->SetItemImage(rootTemp, 0, wxTreeItemIcon_Normal); } break; } };
Index *clone_Index(const Index *index) override { long n = sub_cloners.size(); if (n == 1) return sub_cloners[0].clone_Index(index); if(dynamic_cast<const IndexFlat *>(index) || dynamic_cast<const faiss::IndexIVFFlat *>(index) || dynamic_cast<const faiss::IndexIVFPQ *>(index)) { if(!shard) { IndexProxy * res = new IndexProxy(); for(auto & sub_cloner: sub_cloners) { res->addIndex(sub_cloner.clone_Index(index)); } res->own_fields = true; return res; } else { auto index_ivfpq = dynamic_cast<const faiss::IndexIVFPQ *>(index); auto index_ivfflat = dynamic_cast<const faiss::IndexIVFFlat *>(index); FAISS_ASSERT (index_ivfpq || index_ivfflat || !"IndexShards implemented only for " "IndexIVFFlat or IndexIVFPQ"); std::vector<faiss::Index*> shards(n); for(long i = 0; i < n; i++) { // make a shallow copy long i0 = i * index->ntotal / n; long i1 = (i + 1) * index->ntotal / n; if(verbose) printf("IndexShards shard %ld indices %ld:%ld\n", i, i0, i1); if(reserveVecs) sub_cloners[i].reserveVecs = (reserveVecs + n - 1) / n; if (index_ivfpq) { faiss::IndexIVFPQ idx2( index_ivfpq->quantizer, index_ivfpq->d, index_ivfpq->nlist, index_ivfpq->code_size, index_ivfpq->pq.nbits); idx2.pq = index_ivfpq->pq; idx2.use_precomputed_table = 0; idx2.is_trained = index->is_trained; index_ivfpq->copy_subset_to(idx2, 0, i0, i1); shards[i] = sub_cloners[i].clone_Index(&idx2); } else if (index_ivfflat) { faiss::IndexIVFFlat idx2( index_ivfflat->quantizer, index->d, index_ivfflat->nlist, index_ivfflat->metric_type); index_ivfflat->copy_subset_to(idx2, 0, i0, i1); shards[i] = sub_cloners[i].clone_Index(&idx2); } } faiss::IndexShards *res = new faiss::IndexShards(index->d, true, false); for (int i = 0; i < n; i++) { res->add_shard(shards[i]); } res->own_fields = true; assert(index->ntotal == res->ntotal); return res; } } else if(auto miq = dynamic_cast<const MultiIndexQuantizer *>(index)) { if (verbose) { printf("cloning MultiIndexQuantizer: " "will be valid only for search k=1\n"); } const ProductQuantizer & pq = miq->pq; IndexSplitVectors *splitv = new IndexSplitVectors(pq.d, true); splitv->own_fields = true; for (int m = 0; m < pq.M; m++) { // which GPU(s) will be assigned to this sub-quantizer long i0 = m * n / pq.M; long i1 = pq.M <= n ? (m + 1) * n / pq.M : i0 + 1; std::vector<ToGpuCloner> sub_cloners_2; sub_cloners_2.insert( sub_cloners_2.begin(), sub_cloners.begin() + i0, sub_cloners.begin() + i1); ToGpuClonerMultiple cm(sub_cloners_2, *this); IndexFlatL2 idxc (pq.dsub); idxc.add (pq.ksub, pq.centroids.data() + m * pq.d * pq.ksub); Index *idx2 = cm.clone_Index(&idxc); splitv->add_sub_index(idx2); } return splitv; } else { return Cloner::clone_Index(index); } }
void run(const P& p0, const P& p1, const bool* blocked, int* flood_buffer, const P& map_dims, std::vector<P>& out, const bool allow_diagonal, const bool randomize_steps) { out.clear(); if (p0 == p1) { // Origin and target is same cell return; } floodfill::run(p0, blocked, flood_buffer, map_dims, -1, p1, allow_diagonal); if (flood_buffer[idx2(p1, map_dims.y)] == 0) { // No path exists return; } const std::vector<P>& dirs = allow_diagonal ? dir_utils::dir_list : dir_utils::cardinal_list; const size_t nr_dirs = dirs.size(); // Corresponds to the elements in "dirs" std::vector<bool> valid_offsets(nr_dirs, false); // The path length will be equal to the flood value at the target cell, so // we can reserve that many elements beforehand. out.reserve(flood_buffer[idx2(p1, map_dims.y)]); P p(p1); out.push_back(p); const R map_r(P(0, 0), map_dims - 1); while (true) { const int val = flood_buffer[idx2(p, map_dims.y)]; P adj_p; // Find valid offsets, and check if origin is reached for (size_t i = 0; i < nr_dirs; ++i) { const P& d(dirs[i]); adj_p = p + d; if (adj_p == p0) { // Origin reached return; } // TODO: What is the purpose of this check? If the current value is // zero, doesn't that mean this cell is the target? Only the target // cell and unreachable cells should have values of zero(?) // Try removing the check and verify if the algorithm still works if (val != 0) { const bool is_inside_map = map_r.is_p_inside(adj_p); const int adj_val = is_inside_map ? flood_buffer[idx2(adj_p, map_dims.y)] : 0; // Mark this as a valid travel direction if it's fewer steps // from the target than the current cell valid_offsets[i] = adj_val < val; } } // Set the next position to one of the valid offsets - either pick one // randomly, or iterate over the list and pick the first valid choice. if (randomize_steps) { std::vector<P> adj_p_bucket; for (size_t i = 0; i < nr_dirs; ++i) { if (valid_offsets[i]) { adj_p_bucket.push_back(p + dirs[i]); } } ASSERT(!adj_p_bucket.empty()); adj_p = rnd::element(adj_p_bucket); } else // Do not randomize step choices - iterate over offset list { for (size_t i = 0; i < nr_dirs; ++i) { if (valid_offsets[i]) { adj_p = P(p + dirs[i]); break; } } } out.push_back(adj_p); p = adj_p; } //while }
void run(const P& p0, const bool* blocked, int* out, const P& map_dims, int travel_lmt, const P& p1, const bool allow_diagonal) { std::fill_n(out, map_dims.x * map_dims.y, 0); // List of positions to travel to std::vector<P> positions; // In the worst case we need to visit every position, reserve the elements positions.reserve(map_dims.x * map_dims.y); // Instead of removing evaluated positions from the vector, we track which // index to try next (cheaper than erasing front elements). size_t next_p_idx = 0; int val = 0; bool path_exists = true; bool is_at_tgt = false; bool is_stopping_at_tgt = p1.x != -1; const R bounds(P(1, 1), map_dims - 2); P p(p0); const auto& dirs = allow_diagonal ? dir_utils::dir_list : dir_utils::cardinal_list; bool done = false; while (!done) { // "Flood" around the current position, and add those to the list of // positions to travel to. for (const P& d : dirs) { const P new_p(p + d); if ( !blocked[idx2(new_p, map_dims.y)] && bounds.is_p_inside(new_p) && out[idx2(new_p, map_dims.y)] == 0 && new_p != p0) { val = out[idx2(p, map_dims.y)]; if (travel_lmt == -1 || val < travel_lmt) { out[idx2(new_p, map_dims.y)] = val + 1; } if (is_stopping_at_tgt && new_p == p1) { is_at_tgt = true; break; } if (!is_stopping_at_tgt || !is_at_tgt) { positions.push_back(new_p); } } } // Offset loop if (is_stopping_at_tgt) { if (positions.size() == next_p_idx) { path_exists = false; } if (is_at_tgt || !path_exists) { done = true; } } else if (positions.size() == next_p_idx) { done = true; } if (val == travel_lmt) { done = true; } if (!is_stopping_at_tgt || !is_at_tgt) { if (positions.size() == next_p_idx) { // No more positions to evaluate path_exists = false; } else // There are more positions to evaluate { p = positions[next_p_idx]; ++next_p_idx; } } } // while }
/***************************************************************************** * First character of command id should be second char of line * Lexer returns a full line of data, needs to be parsed if .ce * In version 1.0, valid commands are: * .* (comment) followed by text to '\n' * .br (new line), nothing else allowed on line * .im (include file) 'filename' * .nameit (define text macro) symbol=[a-zA-Z0-9]+ (10 max) text='text string' * text may contain entity references, nameit references and tags * .ce (center) no tags, but text and both entity types * Version 2.0 only supports .*, .br, .im */ Lexer::Token Document::processCommand( Lexer* lexer, Tag* parent ) { if( lexer->cmdId() == Lexer::COMMENT ) ;//do nothing else if( lexer->cmdId() == Lexer::BREAK ) parent->appendChild( new BrCmd( this, parent, dataName(), dataLine(), dataCol() ) ); else if( lexer->cmdId() == Lexer::CENTER ) { CeCmd* cecmd( new CeCmd( this, parent, dataName(), dataLine(), dataCol() ) ); parent->appendChild( cecmd ); return cecmd->parse( lexer ); } else if( lexer->cmdId() == Lexer::IMBED ) { std::string env( Environment.value( "IPFCIMBED" ) ); std::vector< std::wstring > paths; std::wstring cwd; //empty string for current directory paths.push_back( cwd ); #ifdef __UNIX__ std::string separators( ":;" ); char slash( '/' ); #else std::string separators( ";" ); char slash( '\\' ); #endif std::string::size_type idx1( 0 ); std::string::size_type idx2( env.find_first_of( separators, idx1 ) ); std::wstring fbuffer; mbtowstring( env.substr( idx1, idx2 - idx1 ), fbuffer ); paths.push_back( fbuffer ); while( idx2 != std::string::npos ) { idx1 = idx2 + 1; idx2 = env.find_first_of( separators, idx1 ); fbuffer.clear(); mbtowstring( env.substr( idx1, idx2 - idx1 ), fbuffer ); paths.push_back( fbuffer ); } for( size_t count = 0; count < paths.size(); ++count ) { std::wstring* fname( new std::wstring( paths[ count ] ) ); if( !fname->empty() ) *fname += slash; *fname += lexer->text(); #ifndef __UNIX__ if( fname->size() > PATH_MAX ) { throw FatalError( ERR_PATH_MAX ); } #endif try { IpfFile* ipff( new IpfFile( fname ) ); fname = addFileName( fname ); pushInput( ipff ); break; } catch( FatalError& e ) { delete fname; if( count == paths.size() - 1 ) throw e; } catch( FatalIOError& e ) { delete fname; if( count == paths.size() - 1 ) throw e; } } } else if( lexer->cmdId() == Lexer::NAMEIT ) { std::wstring::size_type idx1( lexer->text().find( L"symbol=" ) ); std::wstring::size_type idx2( lexer->text().find( L' ', idx1 ) ); std::wstring sym( lexer->text().substr( idx1 + 7, idx2 - idx1 - 7 ) ); killQuotes( sym ); sym.insert( sym.begin(), L'&' ); sym += L'.'; std::wstring::size_type idx3( lexer->text().find( L"text=" ) ); //check for single quotes std::wstring::size_type idx4( lexer->text()[ idx3 + 5 ] == L'\'' ? \ lexer->text().find( L'\'', idx3 + 6 ) : \ lexer->text().find( L' ', idx3 + 5 ) ); std::wstring txt( lexer->text().substr( idx3 + 5, idx4 - idx3 - 5 ) ); killQuotes( txt ); if( !nls->isEntity( sym ) && nameIts.find( sym ) == nameIts.end() ) //add it to the list nameIts.insert( std::map< std::wstring, std::wstring >::value_type( sym, txt ) ); else printError( ERR3_DUPSYMBOL ); } else printError( ERR1_CMDNOTDEF ); return getNextToken(); }
void Document::makeBitmaps() { if( !bitmapNames.empty() ) { //could use tmpfile... tmpName = Environment.value( "TMP" ); tmpName += std::tmpnam( NULL ); std::FILE* tmp( std::fopen( tmpName.c_str(), "wb" ) ); if( !tmp ) throw FatalIOError( ERR_OPEN, L"(temporary file for bitmaps)" ); //get IPFCARTWORK from env std::string env( Environment.value( "IPFCARTWORK" ) ); std::vector< std::string > paths; std::string cwd; //empty string for current directory paths.push_back( cwd ); #ifdef __UNIX__ std::string separators( ":;" ); char slash( '/' ); #else std::string separators( ";" ); char slash( '\\' ); #endif std::string::size_type idx1( 0 ); std::string::size_type idx2( env.find_first_of( separators, idx1 ) ); paths.push_back( env.substr( idx1, idx2 - idx1 ) ); while( idx2 != std::string::npos ) { idx1 = idx2 + 1; idx2 = env.find_first_of( separators, idx1 ); paths.push_back( env.substr( idx1, idx2 - idx1 ) ); } try { for( BitmapNameIter itr = bitmapNames.begin(); itr != bitmapNames.end(); ++itr ) { std::string fname; wtombstring( itr->first, fname ); for( size_t count = 0; count < paths.size(); ++count ) { std::string fullname( paths[ count ] ); if( !fullname.empty() ) fullname += slash; fullname += fname; #ifndef __UNIX__ if( fullname.size() > PATH_MAX ) { throw FatalError( ERR_PATH_MAX ); } #endif try { #ifdef CHECKCOMP std::printf( "Processing bitmap %s\n", fullname.c_str() ); #endif Bitmap bm( fullname ); itr->second = bm.write( tmp ); break; } catch( FatalError& e ) { if( count == paths.size() - 1 ) throw FatalIOError( e.code, itr->first ); } catch( Class1Error& e ) { printError( e.code, itr->first ); } } } } catch( FatalError& e ) { std::fclose( tmp ); std::remove( tmpName.c_str() ); throw e; } catch( FatalIOError& e ) { std::fclose( tmp ); std::remove( tmpName.c_str() ); throw e; } std::fclose( tmp ); } }