text
stringlengths
1
2.12k
source
dict
c++, strings, search On having Index as a class template and other functions not. […] Is it reasonable or there is a good reason to make them generic in advance? I think you can apply the YAGNI principle here: unless you are really going to need it, don't make Index a class template. The same goes for SubstringOccure...
{ "domain": "codereview.stackexchange", "id": 45449, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, strings, search", "url": null }
python, performance, python-3.x, pdf Title: Speed up search function for PDFs Question: This function takes a file path to a PDF file, and a search string(s). It spits out a count of the number of times the string(s) shows in the PDF. Any ideas how I can make it faster? It can be tested with any PDF (not password pro...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf I profiled it using a single string and this 74 page PDF https://thedocs.worldbank.org/en/doc/79bb914488308f1e75744fccc4e12cb3-0290032021/world-bank-notes-on-debarred-firms-and-individuals-pdf 7998 function calls (7989 primitive calls) in 0.182 seconds Ordered ...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf ncalls tottime percall cumtime percall filename:lineno(function) 453 0.000 0.000 0.000 0.000 {built-in method builtins.getattr} 444 0.000 0.000 0.000 0.000 fitz.py:621(__getitem__) 312 0.000 0.000 0.000 0.000 {built-in metho...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf 76 0.000 0.000 0.000 0.000 _weakrefset.py:27(__exit__) 76 0.000 0.000 0.000 0.000 weakref.py:121(_commit_removals) 76 0.000 0.000 0.000 0.000 _weakrefset.py:21(__enter__) 76 0.000 0.000 0.000 0.000 <frozen _collec...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf 74 0.000 0.000 0.000 0.000 fitz.py:6972(rotation) 74 0.000 0.000 0.000 0.000 fitz.py:5839(_forget_page) 74 0.000 0.000 0.005 0.000 fitz.py:9002(extractText) 74 0.000 0.000 0.000 0.000 fitz.py:8909(<lambda>) ...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf Some things I have tried # I tested using join rather than concatenating but found it to be slower. # for page in doc: # text += page.get_text() text = ' '.join(page.get_text() for page in doc) Answer: where did the time go? Thank you for including meas...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf That way for each .PDF file you can get_text just once, and report all the desired word counts as a single return value. An alternate approach would be to pass in a regex: pattern = re.compile(r'\b(apple|banana)\b', re.I) long identifiers def count_of_string_in_pdf_file_advanced_a...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
python, performance, python-3.x, pdf This looks like a setup for calling groupby() to obtain counts of every word in the document. But then we go on to do something else. Maybe it is leftover debug? This is a relatively inexpensive operation, but there's no need to do it so we may as well drop it. This codebase achie...
{ "domain": "codereview.stackexchange", "id": 45450, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, performance, python-3.x, pdf", "url": null }
c++, performance, collections, c++20, memory-optimization Title: A collection based on a bitset to store a set of unique integers from a given range (rev. 2)
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization Question: This is a continuation of this review. I applied most of the proposed changes. These changes were focused mostly on decoupling the class from the rest of the program and making its interface more like STL collections. Description of the code: This is ...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization Another example is the keyword const that not only prevents mistakes in the code but also it may improve optimization. I'll also gladly take hints on making the code faster. Actually, I'll take any sensible comments as long as they are not detrimental to the pe...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization #include <algorithm> #include <cassert> #include <cstddef> #include <cstdint> #include <functional> #include <iterator> #include <limits> #include <vector>
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization // This container can store unique numbers in range 0..capacity-1 using only about capacity/CHAR_BIT bytes of memory. // (The memory usage is the same regardles of how many numbers are stored.) template<typename T> class CompactSet { static_assert(!std::num...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization [[nodiscard]] const_iterator end() const { return {*this, bitset.size()}; } [[nodiscard]] const_iterator cbegin() const { return begin(); } [[nodiscard]] const_iterator cend() const { return end(); } #ifndef NDEBUG void validate() const; #endif...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization template<typename T> typename CompactSet<T>::const_iterator& CompactSet<T>::const_iterator::operator++() { for (++i; i != compactSet.bitset.size() && !compactSet.bitset[i]; ++i) { } return *this; } template<typename T> typename CompactSet<T>::const_ite...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization CompactSet.cc: #include "./CompactSet.hh" #ifndef NDEBUG template<typename T> void CompactSet<T>::validate() const { const std::size_t actualCount = std::ranges::count(bitset.cbegin(), bitset.cend(), true); assert(count == actualCount); } #endif #incl...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
c++, performance, collections, c++20, memory-optimization Whilst ++ operator is careful not to index the vector out of range, -- has no such check. I think we can reduce the size of iterator by wrapping std::vector::iterator rather than needing a reference to the container. But perhaps not, given the need to termina...
{ "domain": "codereview.stackexchange", "id": 45451, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, collections, c++20, memory-optimization", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe Title: Beginner's attempt at TicTacToe Question: This is my attempt at making a basic TicTacToe game to play against another human player or a computer controlled opponent. The project was a lot harder for me than I initially thought it would be! I have the feeli...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe while ((choice = showMenu(choice)) != '5'){ if (choice == '1' || choice == '2'){ three_row = gameLoop(grid, choice, difficulty, three_row); clearScreen(); drawGrid(grid); if(three_row == 1){ ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe // check diagonal left to right for (row = 0 ; row < 3; row++){ result = result + grid[row][row]; if (result == 237 || result == 264){ if (result == 237) { three_row = 1; } else{ ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe return_result: return three_row; } int checkTwo(char grid[][3], int move){ /* * This function checks if there are 2 XX's or OO's in a row, * column or one of the diagonals and if a certain counter move by * the CPU is needed. * * If needed it...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe resetVars(nX_ptr, nO_ptr, result_ptr); // check rows for (row = 0 ; row < 3; row++){ resetVars(nX_ptr, nO_ptr, result_ptr); for(column = 0 ; column < 3; column++){ if(grid[row][column] == 'X'){ nX++; ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe #if defined(_WIN32) || defined(_WIN64) system("cls"); #endif } int cpuMove(char grid[][3], int move, int turn, int difficulty){ char first_turn[5] = {'1','3', '5', '7', '9'}; char second_turn[4] = {'1','3', '7', '9'}; int n = 0; ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe int gameLoop(char grid[][3], int choice, int difficulty, int three_row){ int move = 0; int turn = 1; char mark = 'X'; mark = startingMark(mark, choice); clearScreen(); while(turn != 10) { clearScreen(); drawGrid(grid)...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe int pickCpuMove(int move, int location){ /* * Return a (random until empty spot is found) move from a * selection of moves based on where the sequence XX or OO was * found on the grid. */ int n = 0; char diag_LR[3] = {'1', '5', '9'}; // location ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe int setDifficulty(int difficulty){ int input = 0; clearScreen(); puts("\n***** SET DIFFICULTY *****\n"); if (difficulty == '1'){ puts("Current difficulty is EASY\n"); } else if (difficulty == '2'){ puts("Current difficulty is HA...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe int getMove(char grid[][3], int move, char mark, int choice, int turn, int difficulty){ bool move_ok = 0; // get Human player move if(choice == '2' || (choice == '1' && mark == 'X')){ while(move_ok != 1){ ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe bool checkThree(char grid[][3]) {//don't need three_row // left to right diagonal if (grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2]) { return true; } ... return false; // you don't treat 1 or 2 differently from this function this would require slightly d...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe clear names: Reading a variable should let future you/devs know what is stored in it. don't pass basic variables as parameters to edit in main, three_row has several issues. It isn't very clear what it stores. I'd change it winner and remove it as a parameter to ...
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
performance, algorithm, c, error-handling, tic-tac-toe All these together: https://gist.github.com/depperm/b28798d3730c0f8394fd1ed380fa95e5 (almost 100 lines shorter)
{ "domain": "codereview.stackexchange", "id": 45452, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "performance, algorithm, c, error-handling, tic-tac-toe", "url": null }
c++, bitset Title: A Dynamic Bitset that I wrote and will use in my compression algortithm. This will be used to store the chars as bits from fstream Question: I have currently implemented an increment method. A method to add bytes. And a method to print out the bitset. Main.cpp #include <iostream> #include "Dynami...
{ "domain": "codereview.stackexchange", "id": 45453, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, bitset", "url": null }
c++, bitset DynamicBitset::~DynamicBitset() { delete[] this->array; } void DynamicBitset::addByte(unsigned char byte) { unsigned char* tempArray = new unsigned char[this->arrayLength + 1]; memcpy(tempArray, this->array, this->arrayLength); tempArray[this->arrayLength] = byte; delete[] this->array;...
{ "domain": "codereview.stackexchange", "id": 45453, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, bitset", "url": null }
c++, bitset /* DynamicBitset& DynamicBitset::operator++() { return *this; } */ void DynamicBitset::printArray() { for (int i = 0; i < this->arrayLength; i++) { std::cout << (int) this->array[i] << ", "; } } For anyone who wants to see the current progress of the project here is the github rep...
{ "domain": "codereview.stackexchange", "id": 45453, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, bitset", "url": null }
c++, bitset (By the way, you had a bug there: this->arrayLength == bitLength / 8 should have used assignment, =, not equality comparison. Turn on all compiler warnings and pay attention to them, your compiler should be able to warn you about typos like this.) this-> You don't need to say this->array, unless there's an...
{ "domain": "codereview.stackexchange", "id": 45453, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, bitset", "url": null }
c++, bitset I recommend that you look at a reference like cppreference.com when overloading standard operators, to see if you have the right function signature. Don't use std::endl std::endl not only outputs a newline, it also flushes the output buffer. This can cause significant delays, and is typically unnecessary. ...
{ "domain": "codereview.stackexchange", "id": 45453, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, bitset", "url": null }
c++, c++11, pointers, optional Title: deep_ptr; a header-only, deep copying, value semantic smart pointer for optionally defined types Question: Edit: final revision here A couple days ago I posted a similar question here. Since then, I have refined the implementation a bit further, as the solution I had previousl...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional #include <cassert> // assert #include <memory> // std::unique_ptr namespace ptr { namespace detail { // dispatches delete to functor DeleteOp template <typename T, typename DeleteOp> struct delete_dispatcher { static void op( T* ptr ) { Delete...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional // returns function pointer to delete dispatcher static constexpr auto default_deleter() { return &detail::delete_dispatcher<element_type, deleter_type>::op; } // construct empty ptr deep_ptr( std::nullptr_t = nullptr ) : base{ nullptr, []( pointer ) { ...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional if ( this == &rhs ) return *this; // execute copy op if ( rhs ) { assert( rhs.get_copier() != nullptr ); *this = { rhs.get_copier()( *rhs.get() ) , rhs.get_deleter() , rhs.get_copier() ...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional auto defined5 = std::move( defined3 ); // move op assert( defined5->foo == 1 ); assert( defined5->bar == 2 ); assert( !defined3 ); // nullptr_t assignment defined5 = nullptr;// reset assert( !defined5 ); } // deep_ptr with functor deleter static bool mydeleter...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional // derived type custom copier { C c{}; c.foo = 1; c.bar = 2; c.baz = 3; deep_ptr<B> lambda{ new C{c} , deep_ptr<B>::default_deleter() , []( const B& what ) -> B* { // must return B* return new C{ static_cast<const C&>( what ) }; ...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional // returns stored value, UB if none value_type& operator*() { return *this->_get(); } const value_type& operator*() const { return *this->_get(); } // returns pointer to stored value, UB if none const value_type* operator->() const { return this->_get(); } // returns...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional auto myopt2 = myopt; // copy assert( myopt2.value().foo == 1 ); // test with derived class in Nullable<base> myopt = derived{ 8,9 }; assert( static_cast<const derived&>( myopt.value() ).bar == 9 ); auto myopt3 = myopt; // copy assert( static_cast<const derived&>( myopt3.value() )....
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, c++11, pointers, optional This smart pointer is less flexible than std::unique_ptr in terms of deleter. We only accept function pointers, but I think we should accept any function object, by inheriting from std::unique_ptr<T, DeleteOp>, and do away with the despatchers. Since copying a deep pointer has value se...
{ "domain": "codereview.stackexchange", "id": 45454, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, c++11, pointers, optional", "url": null }
c++, array, c++20 Title: Writing a multidimensional array view for contiguous arrays in C++20 Question: When creating little games or other programs I often need multidimensional arrays. Usually I just do the simple std::vector<std::vector<T>> thing for simplicity's sake. However, this runs way more allocations and t...
{ "domain": "codereview.stackexchange", "id": 45455, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, array, c++20", "url": null }
c++, array, c++20 template <typename T, std::size_t... strides> requires (sizeof...(strides) > 0) class MDSpan { public: constexpr MDSpan() = default; constexpr MDSpan(T* begin) { reset(begin); } constexpr void reset(T* begin) { m_begin = begin; } constexpr const T...
{ "domain": "codereview.stackexchange", "id": 45455, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, array, c++20", "url": null }
c++, array, c++20 constexpr bool operator==(const MDSpan& other) const { return m_begin == other.m_begin; } constexpr bool operator!=(const MDSpan& other) const { return !(*this == other); } private: constexpr static std::size_t getStridesProduct(std::size_t index) { std::size_...
{ "domain": "codereview.stackexchange", "id": 45455, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, array, c++20", "url": null }
c++, array, c++20 I would appreciate any kind of review and criticism, but have some specific questions, too: MDSpan::operator[] returning a sub-view could be marked as const, since it does not modify this in any way. However it still grants access to the underlying data. The same applies for MDSpan::data(), MDSpan::...
{ "domain": "codereview.stackexchange", "id": 45455, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, array, c++20", "url": null }
c++, array, c++20 The constructor default-initialises m_begin, then assigns. It's better to use a real initialiser: constexpr MDSpan(T* begin) m_begin{begin} { } *(m_begin + offset) is more conventionally written m_begin[offset]. We could really use a constructor for creating a MDSpan<const T> f...
{ "domain": "codereview.stackexchange", "id": 45455, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, array, c++20", "url": null }
c++, array, c++20 We could overload the operators for different number of dimensions, using constraints. As a user, I wouldn't expect to pay the cost of bounds checking unless I specifically ask for it. That's the main difference between at() and [] in standard collections, for example. I would throw std::out_of_ran...
{ "domain": "codereview.stackexchange", "id": 45455, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, array, c++20", "url": null }
c++ Title: Graphical Visualizer in terminal V[2]: using it visualizing: falling sand sim, sorting algorithms and the game of life Question: this is the second version of this project, you can check previous post here: Terminal Graphical Visualizer, using a queue of different matrices I implemented bare-bones version ...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ using frame_matrix = std::vector<std::vector<Pixel>>; class Frame { // consists of a pixel matrix, methods to manipulate and print the frame and consts for more flexebility private: void initialize_frame(); bool is_valid_input(std::string const &height_start, std::string const &height_length, std::string...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ Frame::Frame() { initialize_frame(); } // without parameters initializes the frame to an empty frame of FRAME::HEIGHT * FRAME::WIDTH filled with white FRAME::BACKGROUND characters Frame::Frame(const std::string &input) { // with input from user initilizes the frame and sets it to according to the input initi...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ std::string input_sections[AMOUNT_OF_INPUT_OPTIONS]; size_t input_sections_index = 0; while (std::getline(input_stream, input_sections[input_sections_index], INPUT_DELIMITER)) // inserts the input into sections of an array ++input_sections_index; if (!is_valid_input(input_sections[0], input_s...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ auto pos = colors.find(color); if (pos == colors.end()) return false; if (!is_a_number(height_start) || !is_a_number(height_start) || !is_a_number(height_start) || !is_a_number(height_start)) { // checks if all passed numbers are actuall numbers return false; } if (printable_char....
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ // prints the frames same as print_sequence but without using system("clear") insted providing the previous frame so it can just update the pixels that are different void GraphicalVisualizer::print_sequence_no_clear(const std::chrono::milliseconds millis) { std::queue<Frame> local_temp_queue = frame_queue; ...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ Frame get_field() const; }; falling_sand.cpp #include "falling_sand.hpp" #include <random> int generate_random_length() { std::random_device dev; std::mt19937 rng(dev()); std::uniform_int_distribution<std::mt19937::result_type> dist(MIN_SAND_LENGTH, MAX_SAND_LENGTH); // distribution in range [MIN_SA...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ while (!sand_blocks.empty()) { SandBlock current_block = sand_blocks.front(); bool did_move = true; while (did_move) { did_move = false; visualizer.add_frame(field); // adds the current state of frame to the visualizer queue frame_matrix new_frame = fie...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ for (size_t i = 0; i < current_block.links.size(); ++i) { std::pair<size_t, size_t> link = current_block.links[i]; // same as without diag fall but it does not stop when encounters anothe particle below if (link.first != Frame::FRAME_HEIGHT - 1 && field.get_current_f...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ int direction = generate_random_direction(); // checks if generated 1 in direction and also for bounds and posibility to fall diagonally to the left else checks right if (link.second != Frame::FRAME_WIDTH - 1 && direction == 1 && field.get_current_frame()[link.first + 1][li...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ class AlgorithmVisualizer { // consists of the visualizer to print the frames, a frame that is changing and different kind of sorting algos that works on grid private: GraphicalVisualizer visualizer; Frame to_sort; public: AlgorithmVisualizer(Frame to_sort, char symbol); std::vector<std::pair<int...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ change_frame_colors(to_sort, SAND_SHAPE); grid frame_to_vec = frame_matrix_to_num(to_sort.current_frame, symbol); // bubble_sort(frame_to_vec, symbol); // merge_sort(frame_to_vec, symbol); quick_sort(frame_to_vec, symbol, 0, frame_to_vec.size()); system("clear"); visualizer.print_sequence(...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ convert_to.push_back(line); } int matrix_index = 0; // places the shape and color to insert into the frame goes column by column for (auto height : convert_from) { if (height.first >= Frame::FRAME_HEIGHT) return {}; for (int j = 0; j < height.first; ++j) { c...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ size_t left_index = 0; size_t right_index = 0; while (vect_to_sort.size() < left.size() + right.size()) { if (left_index == left.size()) vect_to_sort.insert(vect_to_sort.end(), right.begin() + right_index, right.end()); else if (right_index == right.size()) vect_to_s...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ size_t left_index = 0; size_t right_index = 0; while (vect_to_sort.size() < left.size() + right.size()) { if (left_index == left.size()) vect_to_sort.insert(vect_to_sort.end(), right.begin() + right_index, right.end()); else if (right_index == right.size()) vect_to_s...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ if (is_bigger_found) { std::swap(vect_to_sort[pivot], vect_to_sort[first_bigger_than_pivot]); pivot = first_bigger_than_pivot; to_sort.set_current_frame(num_to_frame_matrix(vect_to_sort, symbol)); visualizer.add_frame(to_sort); } quick_sort(vect_to_sort, symbol, start, pivo...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ if (y > 0 && grid[y - 1][x].character == game_of_life::CELL_SHAPE) ++amount_of_neighbors; if (y < Frame::FRAME_HEIGHT - 1 && grid[y + 1][x].character == game_of_life::CELL_SHAPE) ++amount_of_neighbors; if (grid[y][x].character == game_of_life::CELL_SHAPE && amount_of_neighbors < 2) ...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ using namespace game_of_life; // left square grid[ref_point_y][ref_point_x] = {CELL_SHAPE, colors.at(CELL_COLOR)}; grid[ref_point_y + 1][ref_point_x] = {CELL_SHAPE, colors.at(CELL_COLOR)}; grid[ref_point_y][ref_point_x + 1] = {CELL_SHAPE, colors.at(CELL_COLOR)}; grid[ref_point_y + 1][ref_point_...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ // right part ref_point_x += 10; ref_point_y -= 2; grid[ref_point_y][ref_point_x] = {CELL_SHAPE, colors.at(CELL_COLOR)}; grid[ref_point_y + 1][ref_point_x] = {CELL_SHAPE, colors.at(CELL_COLOR)}; grid[ref_point_y + 2][ref_point_x] = {CELL_SHAPE, colors.at(CELL_COLOR)}; grid[ref_point_y][ref...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
c++ return 0; } couple of notes: in graphical_visualizer I tried adding another version of printing the sequence without using clear it semi-works and I still prefer the system("clear") because I got annoyed trying to solve stupid bugs with it. second in algorithm_visualizer I have 2 versions of merge sort, one is pr...
{ "domain": "codereview.stackexchange", "id": 45456, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++", "url": null }
rust Title: Handling default std::io::Error in Rust Question: I'm really new to Rust with less than a day of rusting. I'd like to know if the following code is ok or could it be written somehow better, and specifically, which would be the preferred way to handle the actual error case. The return type bool represents ...
{ "domain": "codereview.stackexchange", "id": 45457, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "rust", "url": null }
rust This has more duplicated Oks, but it is easy to see at a glance whether each case is either a success or a failure. As to the exact choice of error propagation operation Err(e) => Err(e): I generally prefer not to use the ? operator with a value known to be an error. In this case, it's simple to not early-return ...
{ "domain": "codereview.stackexchange", "id": 45457, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "rust", "url": null }
python, file-system, compression, file-structure Title: Looking for help simplifying a general purpose compression function Question: The following is a general purpose file compression function with the following features: It can take either a single file or directory as the source input It defaults to the source d...
{ "domain": "codereview.stackexchange", "id": 45458, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, file-system, compression, file-structure", "url": null }
python, file-system, compression, file-structure # Checks if the src is a file or a directory, if a directory we then group all files # with the extension given by the extension parameter. if os.path.isdir(src) is True: fnames = sorted(glob.glob(src + f'\*{extension}')) else: fnames = [src]...
{ "domain": "codereview.stackexchange", "id": 45458, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, file-system, compression, file-structure", "url": null }
python, file-system, compression, file-structure Obvious comments are more harmful than having no comment at all. Don't is True; use boolean variables as predicates directly. Suggested Only lightly tested - import glob import zipfile from pathlib import Path def _zip_and_delete_prot( src: Path, dst: Path | No...
{ "domain": "codereview.stackexchange", "id": 45458, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, file-system, compression, file-structure", "url": null }
python, file-system, compression, file-structure # Write to archive without directory structure included in the compression # If arcname is provided try to use the provided arcname if arcname is None: arcname_inner = file.name else: arcname_inner = arcname with ...
{ "domain": "codereview.stackexchange", "id": 45458, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "python, file-system, compression, file-structure", "url": null }
c++, linked-list, file Title: Count the frequencies of each word in a file Question: I have tried to implement a code that does the following: store words in an array (unordered) use function order_array() to place repetitions for the same word near (so the result, given a sample text file, could be: mom mom mom mom...
{ "domain": "codereview.stackexchange", "id": 45459, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, linked-list, file", "url": null }
c++, linked-list, file void upload_array(string &w, string a[]) { a[count] = w; count ++; } bool is_new_string(string &word, int position) //check better { bool repeated = false; for(int i = 0; i < position; i++) { if(word == list[i] && repeated == true) { ...
{ "domain": "codereview.stackexchange", "id": 45459, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, linked-list, file", "url": null }
c++, linked-list, file Answer: Kudos on writing a working program! You already sense that things can be improved, which is also great. Toby Speight already pointed out some issues in your code. You are a beginning C++ programmer, so it's normal that you don't write optimal code yet; you have just learned the basic too...
{ "domain": "codereview.stackexchange", "id": 45459, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, linked-list, file", "url": null }
c++, linked-list, file auto frequencies = count_frequencies(words); // We should have reached the end, if we didn't something went wrong if (!input.eof()) { std::cerr << "Error reading input\n"; return EXIT_FAILURE; } for (const auto& item: frequencies) { std::cout << std::for...
{ "domain": "codereview.stackexchange", "id": 45459, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, linked-list, file", "url": null }
c++, linked-list, file template so you can create a frequency counting function that can count any kind of input, not just an array of strings. A default comparison operator to make a struct comparable, which helps it sort later. The concept std::ranges::input_range to restrict the input of count_frequencies() to rang...
{ "domain": "codereview.stackexchange", "id": 45459, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, linked-list, file", "url": null }
java, object-oriented Title: Java Queue data structure Question: Could you review this implementation of a queue data structure according to the principles of Object Oriented Programming and Clean Code? import java.util.ArrayList; public class SecurityCheckQueue<E extends ByAir> { private ArrayList<E> queue; ...
{ "domain": "codereview.stackexchange", "id": 45460, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "java, object-oriented", "url": null }
java, object-oriented public void delay(String flight) { checkFlight(flight); validateLength(); ArrayList<E> elements = selectByFlight(flight); removeFromQueue(elements); queue.addAll(elements); } } Answer: Advice 1 - Put the class in a package It is customary in industrial...
{ "domain": "codereview.stackexchange", "id": 45460, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "java, object-oriented", "url": null }
java, object-oriented (Also note the curly braces around the throw statement; it's a Java custom, too. Advice 7 - checkFlight private void checkFlight(String flight) { if (flight == null) throw new IllegalArgumentException("Null"); } Why not this? private void checkFlight(String flight) { Objects.requ...
{ "domain": "codereview.stackexchange", "id": 45460, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "java, object-oriented", "url": null }
c++, performance, c++20 Title: Split command in C++20 Question: The Windows OS doesn't feature a Split command and although I do have a Linux partition, I mainly use Windows 11. As such, I implemented the Split command in C++20 so that I can use it on Windows. The program seems pretty fast; I used it with a large Lin...
{ "domain": "codereview.stackexchange", "id": 45461, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, c++20", "url": null }
c++, performance, c++20 int main(int argc, char* argv[]) { if (argc != 3 && argc != 4) { std::cerr << "Usage: " << argv[0] << " <source_file> <output_prefix> [chunk_size_in_bytes]" << std::endl; return 1; } std::string sourceFile = argv[1]; std::string outputPrefix = argv[2]; unsign...
{ "domain": "codereview.stackexchange", "id": 45461, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, c++20", "url": null }
c++, performance, c++20 That's a very small buffer size for copying file contents. Consider something much larger. inputFile.read(buffer, readSize); outputFile.write(buffer, readSize); if (!inputFile) { break; } We need to check that the read() was successful before writi...
{ "domain": "codereview.stackexchange", "id": 45461, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c++, performance, c++20", "url": null }
c, generics, c11 Title: Generic map-like function Question: This function mimics Python's map() function (No, it doesn't support generic return types for the function or multiple iterables) by applying the provided function func to each element of the array iter. It returns a new array containing the results which mu...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 gen_map(c, char) gen_map(uc, unsigned char) gen_map(si, short int) gen_map(usi, unsigned short int) gen_map(i, int) gen_map(ui, unsigned int) gen_map(li, long int) gen_map(uli, unsigned long int) gen_map(lli, long long int) gen_map(ulli, unsigned long long int) gen_map(f, float) gen_map(d, double) g...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 free(rev_strs); return EXIT_SUCCESS; } #endif /* TEST_MAIN */ This is what the preprocessor produced (after running gcc -E map.c -o map.i, removing irrelevant code, and formatting): static char *map_c(size_t len, const char iter[static len], char (*func)(char x)) { ...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 if (!out) { return ((void *) 0); } for (size_t i = 0; i < len; ++i) { out[i] = func(iter[i]); } return out; } static long int *map_li(size_t len, const long int iter[static len], long int (*func)(long int x)) { long int *out = malloc(sizeof *out * len); if (!o...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 if (!out) { return ((void *) 0); } for (size_t i = 0; i < len; ++i) { out[i] = func(iter[i]); } return out; } static long double *map_ld(size_t len, const long double iter[static len], long double (*func)(long double x)) { long double *out = malloc(sizeof *out * le...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 PS: Why do this? It served as a good exercise. Answer: This is Inflexible It always allocates an output buffer on the heap, which the caller must free(). It cannot write to a buffer allocated by the caller. It cannot use a map function whose output type is different than the input type. It does no...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 (Here, I made the source argument come before the destination, but perhaps I should have followed the K&R convention of destination before source.) Let’s test it with the following function that takes the square root of an unsigned int argument: (Technically, I should have declared the arguments as v...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
c, generics, c11 MSVC 19.38 with /std:c17 /arch:AVX2 /O2 /EHc /fp:fast compiles it to: $LL12@main: vcvtdq2pd ymm2, XMMWORD PTR xs$[rsp+rbx*4] vcmppd ymm1, ymm2, ymm5, 1 vandpd ymm0, ymm1, ymm4 vaddpd ymm2, ymm0, ymm2 vsqrtpd ymm3, ymm2 vmovupd YMMWORD PTR ys...
{ "domain": "codereview.stackexchange", "id": 45462, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, generics, c11", "url": null }
sorting, time-limit-exceeded, binary-tree, binary-search-tree Title: Sort array of numbers using tree sort - Leetcode 912 Question: Problem statement: Sort integer array nums in O(N log N) time, without relying on any library sort routine. I am trying to use a tree sort method (using the creation of a BST and then pe...
{ "domain": "codereview.stackexchange", "id": 45463, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "sorting, time-limit-exceeded, binary-tree, binary-search-tree", "url": null }
sorting, time-limit-exceeded, binary-tree, binary-search-tree Given zero elements, and asked to spit them back in ascending order, the proper response is zero elements. In contrast, your TreeNode(nums[0]) call catastrophically raises IndexError in the empty case. base case Your insert function is lovely. root ...
{ "domain": "codereview.stackexchange", "id": 45463, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "sorting, time-limit-exceeded, binary-tree, binary-search-tree", "url": null }
sorting, time-limit-exceeded, binary-tree, binary-search-tree list(range(n)) list(reversed(range(n))) Will insert run in logarithmic time? Heavy sigh! It needs O(N) linear time to chase through those unbalanced nodes. Total sort time is O(N²) quadratic. We are ruined! Fortunately this is an off-line (batch) sort -- y...
{ "domain": "codereview.stackexchange", "id": 45463, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "sorting, time-limit-exceeded, binary-tree, binary-search-tree", "url": null }
c, parsing, file Title: Advent of Code 2023 day 1: Trebuchet in C (part 2) Question: Problem description from the Advent of Code website: Part 1: The task involves analyzing a calibration document containing lines of text. Each line represents a calibration value that needs to be recovered by extracting the first and...
{ "domain": "codereview.stackexchange", "id": 45464, "lm_label": null, "lm_name": null, "lm_q1_score": null, "lm_q1q2_score": null, "lm_q2_score": null, "openwebmath_perplexity": null, "openwebmath_score": null, "tags": "c, parsing, file", "url": null }