//! Exercise 10.37: //! Given a vector that has ten elements, copy the elements from positions //! 3 through 7 in reverse order to a list. inline void vec2list_3_7_reverse(const std::vector<int>& v, std::list<int>& l) { //! 1 2 3 4 5 6 7 8 9 10 //! ^ ^^ //! rend rbegin std::copy(v.crbegin() + 3, v.crbegin() + 8, std::back_inserter(l)); //! ^ //! @note: std::copy copies the range [first,last) into result. //! hence, the arguments here denote: //! [7 6 5 4 3 2) //! ^ this one is specified but not included. //! @note: also const vecrions if functions can be used. v.crbegin() and //! v.crbegin() }
std::string to_string() const { if (neg) { return "-" + (-(*this)).to_string(); } std::vector<int> decs = { 0 }; for (auto p = digits.crbegin(); p != digits.crend(); ++p) { int carryover = 0; for (auto pdec = decs.begin(); pdec != decs.end(); ++pdec) { carryover += *pdec * radix + *p; *pdec = carryover % 10; carryover /= 10; } while (carryover != 0) { decs.push_back(carryover % 10); carryover /= 10; } } std::string s; std::transform(decs.crbegin(), decs.crend(), std::back_inserter(s), [](int i){ return '0' + i; }); return s; }
//! Exercise 10.34 inline void r_print(const std::vector<std::string> &v) { std::for_each(v.crbegin(), v.crend(), [](const std::string &s) { std::cout << s << " "; }); }
inline void insertCandidate(std::vector<Candidate<SIZE>>& candidates, const Candidate<SIZE>& candidate) { // this should be fairly efficient, sorting the data to leave the lowest // cost elements at the end of the list means that we'll be shifting as // few elements as possible, since we'll consume the lowest cost values // and insert new candidates based on those, which will also be estimated // at a cost near to the last consumed value, which will in turn cause them // to be inserted near the end of the sorted list. // find the right place auto i = candidates.crbegin(); while (i != candidates.crend() and candidate > *i) { ++i; } auto index = candidates.size() - (i - candidates.crbegin()); // insert the data candidates.insert(candidates.begin()+index, candidate); }
std::size_t indexset::pos_greater_less_loc(const std::vector<irange> & rgs, std::size_t location) { std::vector<irange>::const_reverse_iterator it; for (it = rgs.crbegin(); it != rgs.crend(); ++it) { std::size_t idx = static_cast<std::size_t>( std::distance(it, rgs.crend() -1) ); if (rgs[idx].location() <= location) { return idx; } } return NotFound; }
std::vector<std::unique_ptr<std::vector<Node> > > scc(Graph<Node> &graph, std::vector<Node>& leaders){ std::set<Node> visited; std::vector< std::unique_ptr<std::vector<Node>> > scc; for(auto it=leaders.crbegin(); it!=leaders.crend(); ++it){ const auto node = *it; if(visited.find(node) == visited.end()){ scc.push_back(std::make_unique<std::vector<Node> >()); DFS2(graph, node, visited, *(scc.back())); } } return scc; }
void printWay(const std::vector<State>& way, std::ostream& s) { using namespace std; if (way.empty()) { s << "no way\n"; return; } int n = 0; for (auto p = way.crbegin(); p != way.crend(); ++p) { s << "pos " << n++ << '\n'; s << *p << '\n'; } }
//TODO: make this function use file hashes instead std::vector<fs::path> get_dupes_from_files(const std::vector<fs::path> &files) { std::vector<fs::path> dupes; uintmax_t prev = 0, curr = 0; std::string file_exten_prev, file_exten_curr; for (auto it = files.crbegin(); it != files.crend(); ++it) //loops in reverse alphabetical order { file_exten_curr = it->extension().string(); curr = fs::file_size(*it); //we assume a file is a duplicate if the file is next to a file alphabetically with the same file size if (file_exten_curr == file_exten_prev && prev == curr && prev != 0) { std::cout << it->filename().string() << std::endl; dupes.push_back(*it); } prev = curr; file_exten_prev = file_exten_curr; } return dupes; }
const_reverse_iterator crbegin() const { return _vector.crbegin(); }
void ClientConsole::Draw( void ) { if ( !isVisible || !view ) { return; } view->Bind(); const vector4 consoleColour{ 0.0f, 0.0f, 0.0f, 0.5f }; Renderer::DrawQuad( 0, 0, view->width, view->height / 2, 0.0f, 0.0f, 1.0f, 1.0f, consoleColour, nullptr ); Resize(); const uint16_t fontSize = static_cast<uint16_t>( con_fontSize->GetInt32() ); const real32_t x = 0.0f; vector2 pos = { x, (view->height / 2.0f) - font->lineHeight[fontSize] }; // draw the input line font->Draw( pos, String::Format( ">%s", input->GetLine() ), fontSize ); // and now the cursor const char *line = input->GetLine(); real32_t savedX = pos[0]; pos[0] = font->GetGlyphWidth( '>', fontSize ); for ( const char *p = line; *p; p++ ) { if ( p - line >= static_cast<int32_t>( input->GetCursorPos() ) ) { break; } pos[0] += font->GetGlyphWidth( *p, fontSize ); } //TODO: overstrike mode if ( static_cast<uint32_t>( GetElapsedTime() ) & 256 ) { // flash every 250ms font->Draw( pos, "_", fontSize ); } pos[0] = savedX; // draw version information static const std::vector<std::string> helpInfoList { PRODUCT_NAME " on " OS_STRING " (" ARCH_STRING ")", PRODUCT_VERSION, }; const uint16_t helpFontSize = 12u; uint32_t numHelpLinesDrawn = 0u; for ( auto helpInfo = helpInfoList.crbegin(); helpInfo != helpInfoList.crend(); ++helpInfo ) { real32_t helpWidth = 0u; for ( const char *p = (*helpInfo).c_str(); *p; p++ ) { helpWidth += font->GetGlyphWidth( *p, helpFontSize ); } const vector2 helpPos = { view->width - helpWidth, (view->height / 2) - (font->lineHeight[helpFontSize] * numHelpLinesDrawn) - font->lineHeight[fontSize] - 4u }; font->Draw( helpPos, *helpInfo, helpFontSize ); numHelpLinesDrawn++; } // grab a subset of the console buffer that we may want to draw - we do word-wrapping in realtime // line breaks and carriage returns are preprocessed in the Console const uint32_t numLines = console->buffer->size(); const uint32_t start = std::max( 0, static_cast<int32_t>( numLines ) - static_cast<int32_t>( scrollAmount ) - static_cast<int32_t>( lineCount ) ); const uint32_t begin = std::min( numLines, start ); const uint32_t end = std::min( begin + lineCount, numLines ); std::vector<std::string> lines( console->buffer->begin() + begin, console->buffer->begin() + end ); uint32_t drawn = 0u; for ( auto line = lines.crbegin(); line != lines.crend(); ++line ) { // substring of renderable characters std::vector<std::string> subsets; Split( *line, subsets ); for ( auto subset = subsets.crbegin(); subset != subsets.crend(); ++subset ) { const uint32_t subsetLineCount = font->GetTextLineCount( pos, *subset, fontSize ); drawn += subsetLineCount; if ( drawn > lineCount ) { break; } pos[1] -= font->lineHeight[fontSize] * subsetLineCount; font->Draw( pos, *subset, fontSize, &colourTable[ColourIndex( COLOUR_WHITE )] ); } } }