text
stringlengths
1
2.12k
source
dict
c++, algorithm, recursion, template, c++20 /* recursive_find_if template function implementation with unwrap level */ template<std::size_t unwrap_level, class T, class Proj = std::identity, recursive_projected_invocable<unwrap_level, Proj, T> UnaryPredicate> constexpr auto recursive_find_if(T&& value, Unary...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c++, algorithm, recursion, template, c++20 template<std::ranges::input_range T> constexpr auto recursive_print(const T& input, const int level = 0) { T output = input; std::cout << std::string(level, ' ') << "Level " << level << ":" << std::endl; std::transform(std::ranges::cbegin(input), std::ranges::cend...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c++, algorithm, recursion, template, c++20 // recursive_transform_reduce template function implementation with execution policy template<std::size_t unwrap_level, class ExecutionPolicy, class Input, class T, class UnaryOp = std::identity, class BinaryOp = std::plus<T>> requires(recursive_invocable<unwrap_level, Unary...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c++, algorithm, recursion, template, c++20 template<typename T> concept is_recursive_sizeable = requires(T x) { recursive_size(x); }; // Copy from https://stackoverflow.com/a/37264642/6667035 #ifndef NDEBUG # define M_Assert(Expr, Msg) \ __M_Assert(#Expr, Expr, __FILE__, __LINE__, Msg) #else # define M_A...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c++, algorithm, recursion, template, c++20 // test case with unary operation, binary operation and execution policy M_Assert(recursive_transform_reduce<1>( std::execution::par, test_vectors_1, 1, [&](auto&& element) { return element + 1; }, [&](auto&& element1, auto&& eleme...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c++, algorithm, recursion, template, c++20 std::cout << "All tests passed!\n"; return; } int main() { auto start = std::chrono::system_clock::now(); recursive_transform_reduce_tests(); auto end = std::chrono::system_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; st...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c++, algorithm, recursion, template, c++20 This will then result in the correct output for the example I gave. But it still results in the wrong output for more nested containers. Note that your test cases don't catch this because you yourself thought the wrong behavior was the correct one. One way that could maybe ha...
{ "domain": "codereview.stackexchange", "id": 45544, "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++, algorithm, recursion, template, c++20", "url": null }
c, portability, gcc, c-preprocessor, c23 Title: Consolidating GNU C's and C23's attributes Question: C23 has introduced attribute specifier sequences. Consequently, the header below attempts to conditionally define macros for these sequences (for my own use cases). In cases where a compiler does not yet support them ...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, portability, gcc, c-preprocessor, c23 #if defined(__has_c_attribute) /* Every standard attribute whose name is of form `attr` can be also * spelled as __attr__ and its meaning is not changed. All attributes * are specified with leading pairs of underscores, to avoid that they * macro-expand in c...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, portability, gcc, c-preprocessor, c23 #if __has_c_attribute(__reproducible__) #define ATTRIB_REPRODUCIBLE [[__reproducible__]] #endif /* __reproducible__*/ #elif defined(__GNUC__) || defined(__clang__) /* GNU C doesn't have a __maybe_unused__ attribute, but it has unused...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, portability, gcc, c-preprocessor, c23 #if defined(__GNUC__) || defined(__clang__) /* To indicate that an allocation function both satisfies the nonaliasing * property and has a deallocator associated with it, both the plain form * ot the attribute and the one with the deallocator must be used. *...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, portability, gcc, c-preprocessor, c23 #define ATTRIB_NOINLINE __attribute__((noinline)) #else #define ATTRIB_MALLOC /* If only. */ #define ATTRIB_MALLOCEX(...) /* If only. */ #define ATTRIB_PRINTF_LIKE(n, m) /* If only. */ #define A...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, portability, gcc, c-preprocessor, c23 #if defined(__GNUC__) || defined(__clang__) #define likely(x) __builtin_expect((!!x), 1) #define unlikely(x) __builtin_expect((!!x), 0) #else #define likely(x) (!!x) #define unlikely(x) (!!x) #endif /* __GNUC__ || __clang__ *...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, portability, gcc, c-preprocessor, c23 #if !defined(MUSTTAIL) # define MUSTTAIL /**/ #endif Which would be much nicer if compilers other than GCC supported #if defined(__has_c_attribute) && __has_c_attribute(foo) What you currently have leaves attributes undefined if an implementation defines __has_c_attribute, bu...
{ "domain": "codereview.stackexchange", "id": 45545, "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, portability, gcc, c-preprocessor, c23", "url": null }
c, strings, io, library, portability Title: A small header-only input output library Question: The library (inspired by stb libraries) attempts to provide some commonly used functions (reading a file into memory, determining the size of a file) that are missing from the C standard library portably (for my own use cas...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability /* * Reads the file pointed to by `stream` to a buffer and returns it. * The returned buffer is a null-terminated string. * If `nbytes` is not NULL, it shall hold the size of the file. * * Returns NULL on memory allocation failure. The caller is responsible for * freeing th...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability /* * Reads the next line from the stream pointed to by `stream`. The returned line * is null-terminated and does not contain a newline, if one was found. * * The memory pointed to by `size` shall contain the length of the * line (including the terminating null character). E...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability #endif /* IO_H */ #ifdef IO_IMPLEMENTATION #if defined(IO_MALLOC) && defined(IO_REALLOC) && defined(IO_FREE) // Ok. #elif !defined(IO_MALLOC) && !defined(IO_REALLOC) && !defined(IO_FREE) // Ok. #else #error "Must define all or none of IO_MALLOC, IO_REALL...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability if (ntokens) { *ntokens = token_count; } return tokens; } IO_DEF char **io_split_lines(char *s, size_t *nlines) { return io_split_by_delim(s, "\n", nlines); } IO_DEF char *io_read_line(FILE *stream, size_t *size) { const size_t page_size = BUFSIZ; siz...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability /* * Reasons to not use `fseek()` and `ftell()` to compute the size of the file: * * Subclause 7.12.9.2 of the C Standard [ISO/IEC 9899:2011] specifies the * following behavior when opening a binary file in binary mode: * * >> A binary stream need not meaningfully support ...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability * #elif defined(__unix__) || defined(__linux__) et cetera * struct stat st; * * if (fstat(fileno(stream), st) == 0) { * return st.size; * } * return -1? * * } * #else * Fall back to the default and read it in chunks. * #endi...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability while ((rcount = fread(chunk, 1, CHUNK_SIZE, stream)) > 0) { *size += rcount; } return !ferror(stream); } IO_DEF bool io_write_lines(FILE *stream, size_t nlines, char *lines[const static nlines]) { for (size_t i = 0; i < nlines; ++i) { if (fprintf(...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability Review Request: Are there any bugs or undefined/implementation-defined behavior in the code? Are there any edge cases where the functions would leak memory? Where would restrict be suitable to use? General coding comments, style, bad practices, et cetera. Answer: There is a lot to...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability Bug: Infinite loop in io_read_file()? When fread(content + len, 1, page_size - 1, stream); returns 0 due to end-of-file, for (size_t rcount = 1; rcount; len += rcount) iterates endlessly. Do not assume file error is false before io_read_file() Similar problem in io_read_line(). ...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
c, strings, io, library, portability // Consider a `rewind()` here // Depends on what OP wants should stream not be at the beginning. do { rcount = fread(chunk, 1, CHUNK_SIZE, stream); *size += rcount; } while (rcount == CHUNK_SIZE); return !ferror(stream); } Unsigned constants Since CHUNK...
{ "domain": "codereview.stackexchange", "id": 45546, "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, io, library, portability", "url": null }
python, python-3.x, classes Title: Dynamically mapping an API's setting IDs to instance methods using Descriptors Question: I'm working with an API, where the url can be constructed with setting_ids. To more clearly indicate the functionality of the setting ids I am mapping them to a WriterClass' methods, with releva...
{ "domain": "codereview.stackexchange", "id": 45547, "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, python-3.x, classes", "url": null }
python, python-3.x, classes class BoundMethod: def __init__(self, id, instance): self._id = id self._instance = instance async def __call__(self, value): # placeholder logic for actual API call. await asyncio.sleep(1) # Simulate work print( f"Method called ...
{ "domain": "codereview.stackexchange", "id": 45547, "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, python-3.x, classes", "url": null }
python, python-3.x, classes As expected. Answer: Use a More Meaningful Name For Your Descriptor Class? How about renaming your descriptor class from MethodDescriptor to SettingDescriptor? And consider renaming class WriterClass to SettingWriter. I would also use more descriptive names for the descriptor instances th...
{ "domain": "codereview.stackexchange", "id": 45547, "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, python-3.x, classes", "url": null }
python, python-3.x, classes async def __call__(self, value): # placeholder logic for actual API call. await asyncio.sleep(1) # Simulate work print( f"Method called with id={self._id}, {value=}, at url={self._instance.domain} for serial {self._instance.device_serial}") retur...
{ "domain": "codereview.stackexchange", "id": 45547, "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, python-3.x, classes", "url": null }
python, python-3.x, classes # Example usage async def main(): creds = Credentials("123AA", "API_TOKEN") # API token obtained elsewhere in real code. async with httpx.AsyncClient() as client: setter = SettingWriter(creds, client) task1 = asyncio.create_task(setter.set("c", "some_str")) ...
{ "domain": "codereview.stackexchange", "id": 45547, "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, python-3.x, classes", "url": null }
performance, sql, stackexchange Title: Show how close a user is to being unsung Question: So this morning, I decided to create a SEDE query that answers the age-old question "How close am I to being Unsung?" because I recently polished up my first SEDE query and I realized that I had enjoyed learning how to code in S...
{ "domain": "codereview.stackexchange", "id": 45548, "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, sql, stackexchange", "url": null }
performance, sql, stackexchange Clustered Index Scan [PostsWithDeleted].[UIX_PostsWithDe… Cost: 97% The estimated to actual row ratio is not too bad: 1716 / 264 == 6.5. So the planner manages to get it within an order-of-magnitude, about par for the course, sufficient for deciding which access path to choose. Estimat...
{ "domain": "codereview.stackexchange", "id": 45548, "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, sql, stackexchange", "url": null }
algorithm, datetime, rust, library, time Title: RANDEVU - Rust crate implementing a simple algorithm I invented Question: I've created a simple algorithm which I've implemented in Rust and published it as a crate on crates.io. https://crates.io/crates/randevu While I do think my code is pretty clean and idiomatic, I ...
{ "domain": "codereview.stackexchange", "id": 45549, "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": "algorithm, datetime, rust, library, time", "url": null }
algorithm, datetime, rust, library, time /// Returns RDV level `u32` for an OBJECT `&str` on a specific DATE `&str` /// /// **RDV = number of leading zero bits in blake3(blake3(OBJECT) || blake3(DATE))** pub fn rdv(object: &str, date: &str) -> u32 { let mut hasher = blake3::Hasher::new(); hasher.update(blake3:...
{ "domain": "codereview.stackexchange", "id": 45549, "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": "algorithm, datetime, rust, library, time", "url": null }
algorithm, datetime, rust, library, time #[test] fn test_rdv_4() { assert_eq!(rdv("RUST", "2024-01-26"), 0); } } Answer: Naming The function rdv has a name, that I'd never remember. It's described as number of leading zero bits, but it is never explained where the presumable abbreviation originates fr...
{ "domain": "codereview.stackexchange", "id": 45549, "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": "algorithm, datetime, rust, library, time", "url": null }
python, performance Title: What could be the more optimised way to get all unique subarray which equals to a target Question: I am trying to solve Combination Sum II on LeetCode. I was able to derive a recursive solution for the problem but it is not optimised as it only beats 10% of the solutions. Combination Sum I...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
python, performance if index == len(candidates) or target < curr_sum: return inner(terms + [candidates[index]], curr_sum + candidates[index], index + 1) while index + 1 < len(candidates) and candidates[index] == candidates[index+1]: index += 1 inner(terms, curr_sum, i...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
python, performance ... inner(terms, curr_sum, index + 1) def combination_sum_2(candidates: list[int], target: int) -> list[list[int]]: def inner(terms: list[int], curr_sum: int, index: int) -> None: if curr_sum == target: if terms not in res: res.append(terms) ret...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
python, performance I prefer using Pythons generator functions. from typing import Iterator def combination_sum_2(candidates: list[int], target: int) -> Iterator[tuple[int, ...]]: def inner(terms: tuple[int, ...], curr_sum: int, lo: int, hi: int) -> Iterator[tuple[int, ...]]: if curr_sum == target: ...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
python, performance Code to make graph: import bisect import collections from typing import Iterator def test_orig(candidates, target): res = [] def helper(arr,index,curr_sum,ans): if curr_sum == target: #ans = sorted(ans) if ans not in res: res.append(ans) ...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
python, performance def rstretch__index(values: list[int], length: int) -> list[int]: return [ bisect.bisect(values, i) - 1 for i in range(length) ] def nsum2(candidates: list[int], target: int) -> Iterator[tuple[int, ...]]: def inner(terms: tuple[int, ...], curr_sum: int, lo: int, hi: int...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
python, performance def test_peil3(candidates: list[int], target: int) -> list[tuple[int, ...]]: return list(nsum3(candidates, target)) import functools import random import matplotlib.pyplot import numpy import graphtimer random.seed(42401) @functools.cache def args_conv(size: int) -> tuple[list[int], int]: ...
{ "domain": "codereview.stackexchange", "id": 45550, "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", "url": null }
verilog Title: ALU design for a 16-bit microprocessor Question: I'm having a hard time figuring out if the code I wrote is purely combinatorial or sequential logic. I'm designing a simple 16-bit microprocessor (will be implemented on a Spartan 6), and I'm new to Verilog, HDL and FPGAs. The code for the microprocessor...
{ "domain": "codereview.stackexchange", "id": 45551, "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": "verilog", "url": null }
verilog // Outputs output wire z /* Zero Flag Register (Embedded in res_out) */, n /* Negative/Sign Flag Register */; output reg o /* Overflow/Underflow/Carry Flag Register */; output reg cond /* Conditional Flag Register */; output wire [width-1:0] d_out /* Data Output Port */; // Internals reg [1:0] chk_oflow /* ...
{ "domain": "codereview.stackexchange", "id": 45551, "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": "verilog", "url": null }
verilog always@(posedge clk) begin if(en && !rst) begin case(opcode) // synthesis parallel-case 5'b00000: begin res_out [width-1:0] <= a_in [width-1:0]; // A end 5'b00001: begin res_out [width-1:0] <= b_in [width-1:0]; // B end...
{ "domain": "codereview.stackexchange", "id": 45551, "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": "verilog", "url": null }
verilog end 5'b01101: begin if(a_in [width-1:0] == b_in [width-1:0]) cond <= 1'b1; // Compare A == B, set Conditional Register as result else cond <= 1'b0; end 5'b01110: begin if(a_in [width-1:0] < b_in [width-1:0]) cond <= 1'b1; // Co...
{ "domain": "codereview.stackexchange", "id": 45551, "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": "verilog", "url": null }
verilog end 5'b11010: begin res_out [width-1:0] <= {a_in [width-1], a_in [width-1:1]}; // Arithmetic Right Shift A end 5'b11011: begin res_out [width-1:0] <= {b_in [width-1], b_in [width-1:1]}; // Arithmetic Right Shift B end 5...
{ "domain": "codereview.stackexchange", "id": 45551, "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": "verilog", "url": null }
verilog endmodule There is a good chance I'll reduce the number of operations it performs to reduce the amount of unnecessary operations it does on both A and B, since less operations translates to better RISC performance. Would a blocking or non-blocking operator be appropriate in this case? Answer: non-blocking o...
{ "domain": "codereview.stackexchange", "id": 45551, "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": "verilog", "url": null }
c, generics, stack, library Title: Generic stack implementation (revision) Question: The below post is a follow-up of Generic stack implementation. Below follows a header-only implementation of a generic stack (inspired by stb-libraries, following these guidelines: stb-howto.txt). After incorporating @Coderodde's adv...
{ "domain": "codereview.stackexchange", "id": 45552, "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, stack, library", "url": null }
c, generics, stack, library typedef struct stack Stack; /* * Creates a stack with `cap` elements of size `memb_size`. * * The stack can only store one type of elements. It does not support * heterogeneuous types. * * Returns a pointer to the stack on success, or NULL on failure to allocate * memory. */ STA...
{ "domain": "codereview.stackexchange", "id": 45552, "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, stack, library", "url": null }
c, generics, stack, library /* * Destroys and frees all memory associated with the stack referenced by `s`. */ STACK_DEF void stack_destroy(Stack *s) ATTRIB_NONNULL(1); #endif /* STACK_H */ #ifdef STACK_IMPLEMENTATION #if defined(IO_MALLOC) != defined(IO_REALLOC) || defined(IO_REALLOC) !=...
{ "domain": "codereview.stackexchange", "id": 45552, "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, stack, library", "url": null }
c, generics, stack, library if (s->cap > SIZE_MAX / s->memb_size) { return false; } void *const tmp = STACK_REALLOC(s->data, s->cap * s->memb_size); if (!tmp) { return false; } s->data = tmp; } char *const target = (char *) s->data + (s->size...
{ "domain": "codereview.stackexchange", "id": 45552, "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, stack, library", "url": null }
c, generics, stack, library #endif /* TEST_MAIN */ Review Request: What I am mainly interested in are my new overflow checks and reallocation strategy, namely in stack_create(), stack_push(), and stack_pop(). Other comments are also welcome. Edit: This is how it can be used: #define GSTACK_IM...
{ "domain": "codereview.stackexchange", "id": 45552, "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, stack, library", "url": null }
c, generics, stack, library 1 .h file vs. a .c and .h file This .h file brings in various #include <....h> files that would not be needed with classic .c, .h file approach. #include <stdio.h> #include <string.h> #include <stdint.h> Unclear why code always includes <stdio.h>, <stdint.h>. [Edit] Tolerate stack_destroy...
{ "domain": "codereview.stackexchange", "id": 45552, "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, stack, library", "url": null }
java, stream, lazy Title: Implementation of java.util.stream.Stream (and friends) that reads lines from the internet without requiring you to manage the resources Question: This streams lines of information over the internet using BufferedReader::lines. However, what makes this special (and thus, extraordinarily comp...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy As a result, I would like this review to focus on making sure my claim is as bullet proof as I make it out to be. Priority #1 is to make sure that this implementation cannot leak resources. Other than that, I want the obvious things like correctness/efficiency/readability/maintainability/etc. One po...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public class StreamLinesOverInternet<T> implements Stream<T> { private final Supplier<Stream<T>> encapsulatedStream; public static void main(final String[] args) { final String url = //THIS IS A >5 GIGABYTE FILE "https://raw.githubusercontent.com/nytimes...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public static StreamLinesOverInternet<String> stream(final URI uri) { Objects.requireNonNull(uri); final URL url; try { url = uri.toURL(); } catch (final Exception exception) { throw new IllegalSta...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy System.out.println("CLOSED THE BUFFERED READER"); } catch (final Exception exception) { throw new Illegal...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy private static <A, B, C> C convertStream ( final Supplier<A> currentStreamSupplier, final Function<A, ? extends B> function, final Function<Supplier<B>, C> constructor ) { Objects.requireNonNull(...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy @Override public Optional<T> findAny() { return this.terminateWithValueSafely(Stream::findAny); } @Override public Optional<T> findFirst() { return this.terminateWithValueSafely(Stream::findFirst); } @Override public boolean noneMatch(final Pr...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy @Override public Optional<T> reduce(final BinaryOperator<T> accumulator) { return this.terminateWithValueSafely(stream -> stream.reduce(accumulator)); } @Override public T reduce(final T identity, final BinaryOperator<T> accumulator) { return this.terminate...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy @Override public DoubleVersion flatMapToDouble(final Function<? super T, ? extends DoubleStream> function) { return convertStream ( this.encapsulatedStream, stream -> stream.flatMapToDouble(function), DoubleVersion::new )...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy @Override public IntVersion mapToInt(final ToIntFunction<? super T> toIntFunction) { return convertStream ( this.encapsulatedStream, stream -> stream.mapToInt(toIntFunction), IntVersion::new ) ; } @Ove...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy @Override public Iterator<T> iterator() { final Spliterator<T> spliterator = this.spliterator(); return Spliterators.iterator(spliterator); }
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public static class IntVersion implements IntStream { private final Supplier<IntStream> encapsulatedStream; private IntVersion(final Supplier<IntStream> encapsulatedStream) { Objects.requireNonNull(encapsulatedStream); this.encaps...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } @Override public PrimitiveIterator.OfInt iterator() { final Spliterator.OfInt spliterator = this.spliterator(); return Spliterators.iterator(spliterator); } @Override public StreamLinesOverInternet.IntVersion parall...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } @Override public IntSummaryStatistics summaryStatistics() { return this.terminateWithValueSafely(IntStream::summaryStatistics); } @Override public OptionalDouble average() { return this.terminateWithValueSafel...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } @Override public int[] toArray() { return this.terminateWithValueSafely(IntStream::toArray); } @Override public void forEachOrdered(final IntConsumer intConsumer) { Objects.requireNonNull(intConsumer); ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy () -> { final IntStream currentStream = this.encapsulatedStream.get(); final DoubleStream nextStream = currentStream.asDoubleStream(); return nextStream; } ; ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy final LongStream nextStream = currentStream.mapToLong(intToLongFunction); return nextStream; } ; return new LongVersion(nextStreamSupplier); } @Override public <U> StreamLinesOverInternet<U>...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy return this.continueStreamSafely(IntStream::unordered); } @Override public boolean isParallel() { try ( final IntStream stream = this.encapsulatedStream.get() ) { return stream.isParallel(...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public static class LongVersion implements LongStream { private final Supplier<LongStream> encapsulatedStream; private LongVersion(final Supplier<LongStream> encapsulatedStream) { Objects.requireNonNull(encapsulatedStream); this.e...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } } @Override public Spliterator.OfLong spliterator() { final long[] array = this.terminateWithValueSafely(LongStream::toArray); return Arrays.spliterator(array); } @Override public PrimitiveIterator.OfLo...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } @Override public boolean allMatch(final LongPredicate longPredicate) { Objects.requireNonNull(longPredicate); return this.terminateWithValueSafely(longStream -> longStream.allMatch(longPredicate)); } @Override publi...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public OptionalLong reduce(final LongBinaryOperator longBinaryOperator) { Objects.requireNonNull(longBinaryOperator); return this.terminateWithValueSafely(longStream -> longStream.reduce(longBinaryOperator)); } @Override public long ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy return this.continueStreamSafely(LongStream::sorted); } @Override public LongVersion distinct() { return this.continueStreamSafely(LongStream::distinct); } @Override public LongVersion flatMap(final LongFunction<? extend...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy return convertStream ( this.encapsulatedStream, stream -> stream.mapToObj(longFunction), StreamLinesOverInternet<U>::new ) ; } @Override public LongVersion map(final LongUnaryOpe...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public static class DoubleVersion implements DoubleStream { private final Supplier<DoubleStream> encapsulatedStream; private DoubleVersion(final Supplier<DoubleStream> encapsulatedStream) { Objects.requireNonNull(encapsulatedStream); ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } @Override public PrimitiveIterator.OfDouble iterator() { final Spliterator.OfDouble spliterator = this.spliterator(); return Spliterators.iterator(spliterator); } @Override public StreamLinesOverInternet.DoubleVersi...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy { Objects.requireNonNull(doublePredicate); return this.terminateWithValueSafely(doubleStream -> doubleStream.anyMatch(doublePredicate)); } @Override public DoubleSummaryStatistics summaryStatistics() { return this.ter...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy { Objects.requireNonNull(doubleBinaryOperator); return this.terminateWithValueSafely(doubleStream -> doubleStream.reduce(identity, doubleBinaryOperator)); } @Override public double[] toArray() { return this.terminateW...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy { Objects.requireNonNull(doubleFunction); return this.continueStreamSafely(doubleStream -> doubleStream.flatMap(doubleFunction)); } @Override public LongVersion mapToLong(final DoubleToLongFunction doubleToLongFunction) { ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy { Objects.requireNonNull(doublePredicate); return this.continueStreamSafely(doubleStream -> doubleStream.filter(doublePredicate)); } @Override public void close() { this.terminateSafely(DoubleStream::close); ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy } ``` Answer: try-with-resources and exception handling are features, not bugs. The vastly less complicated approach is to use Java as Java was intended - write an AutoCloseable utility class. For a very typical example of a situation where the JDK itself operates this way, see DirectoryStream. Th...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy It's only safe to close the stream once we can guarantee that all threads are done (i.e. after a join). This can still use AutoCloseable. And what does it even mean to auto-detect when a stream isn't used any more? When it hits EOF? No: because then we break the case where a user wants to perform sp...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, stream, lazy public static void main(String[] args) { try (HTTPLineStream stream = new HTTPLineStream( "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv" )) { stream.lines() .limit(10) .forEach(System.out::println); ...
{ "domain": "codereview.stackexchange", "id": 45553, "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, stream, lazy", "url": null }
java, performance, algorithm, programming-challenge, time-limit-exceeded Title: Leetcode: Steps to Make Array Non-decreasing Question: I was trying out leetcode's Steps to Make Array Non-decreasing As per the challenge's description: You are given a 0-indexed integer array nums. In one step, remove all elements num...
{ "domain": "codereview.stackexchange", "id": 45554, "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, performance, algorithm, programming-challenge, time-limit-exceeded", "u...
java, performance, algorithm, programming-challenge, time-limit-exceeded // skip if already removed if (nums[i] == -1) continue; if (nums[i - 1] == -1) { // swap by moving removed (-1) nb to right swap(nums, i, i - 1); continue; } if (nu...
{ "domain": "codereview.stackexchange", "id": 45554, "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, performance, algorithm, programming-challenge, time-limit-exceeded", "u...
java, performance, algorithm, programming-challenge, time-limit-exceeded Step 2: ie when for loop cycle finishes and before relooping --> array [5, -1, -1, 4, 7, -1, -1, 11, 11, -1, -1] //ie [5,4,7,11,11] Step 3: ie when for loop cycle finishes and before relooping --> array [5, -1, -1, -1, 7, 11, -1, -1, 11, -1, -1]...
{ "domain": "codereview.stackexchange", "id": 45554, "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, performance, algorithm, programming-challenge, time-limit-exceeded", "u...
beginner, rust, unit-conversion Title: Yet another Fahrenheit and Celsius converter Question: Introduction I am recently learning Rust by reading The Rust Programming Language (a.k.a. the book). I have finished the first three chapters so far, and this is my attempt for the first exercise listed at the end of Chapte...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
beginner, rust, unit-conversion let command: u32 = match command.trim().parse() { Ok(num) => num, Err(_) => { println!("Invalid command."); continue; } }; match command { 0 => fahrenheit_to_celsius(), 1 => celsi...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
beginner, rust, unit-conversion Enter one of the following commands: - 0: convert from Fahrenheit to Celsius - 1: convert from Celsius to Fahrenheit - 2: quit 0 Enter the temperature in Fahrenheit. 200 200 Fahrenheit = 93.33333333333333 Celsius Enter one of the following commands: - 0: convert from Fahrenheit to ...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
beginner, rust, unit-conversion Code duplication You have a lot of duplicated code between the celsius_to_fahrenheit and fahrenheit_to_celsius functions for reading a String input and converting it to a f64. Let's combine them into one function that takes command as an argument: fn convert_temperature(command: u32) { ...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
beginner, rust, unit-conversion Same for the parsing of the temperature. Inline variables in formatted strings This makes it easier to read: println!("{} Fahrenheit = {} Celsius", temperature, converted); can be just: println!("{temperature} Fahrenheit = {converted} Celsius"); Putting it together so far use std::io;...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
beginner, rust, unit-conversion Reusability Right now, this code is coupled tightly with the input and output system. However, with some changes, it will become more modular and flexible so that other code can reuse its logic. First, let's add an enum for the two temperatures: enum Temperature { Celsius(f64), ...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
beginner, rust, unit-conversion fn main() { println!("This is a program that converts temperatures between Fahrenheit and Celsius."); loop { println!("\nEnter one of the following commands:"); println!(" - 0: convert from Fahrenheit to Celsius"); println!(" - 1: convert from Celsius to...
{ "domain": "codereview.stackexchange", "id": 45555, "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": "beginner, rust, unit-conversion", "url": null }
c#, performance Title: Autocorrelation of a vector series Question: Related: Autocorrelation of a vector series (2) The given C# source code computed autocorrelation of a vector series. The listing gives accurate results, but is horribly slow. How can I optimize it to make it fast? public class TimeSeriesAnalysisForV...
{ "domain": "codereview.stackexchange", "id": 45556, "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", "url": null }
c#, performance Throwing ArgumentException doesn't feel right. C is a private method, and you are in full control of how it is called. This exception may never happen, and checking a validity of t is a waste of cycles. cValue is never used. It is OK to drop it. Regarding performance. It is not possible to optimize t...
{ "domain": "codereview.stackexchange", "id": 45556, "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", "url": null }
python, beginner, graphics, turtle-graphics, raytracing Title: Basic Ray Tracer Question: This is my first self-guided programming project, and all the math used in the program is self-taught. I wanted to get a better understanding of how computer graphics worked, but I just feel more confused. Please tell me what I ...
{ "domain": "codereview.stackexchange", "id": 45557, "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, beginner, graphics, turtle-graphics, raytracing", "url": null }