text stringlengths 1 2.12k | source dict |
|---|---|
c++, beginner, rock-paper-scissors
do {
if (std::cin >> matches) {
// Read worked.
}
else {
// If the failure was because of EOF.
// Then you can't get any more input and it
// simply fail again so exit.
if (std::cin.eof()) {
... | {
"domain": "codereview.stackexchange",
"id": 45368,
"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++, beginner, rock-paper-scissors",
"url": null
} |
c++, beginner, rock-paper-scissors
Also no need for a break after a return. I would write like this:
// PS: Why 1->3 we are programmers.
// Numbers start at zero!
switch (r)
{
case 1: return 'r';
case 2: return 'p';
case 3: return 's';
}
But I would use an array.
... | {
"domain": "codereview.stackexchange",
"id": 45368,
"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++, beginner, rock-paper-scissors",
"url": null
} |
python, python-3.x, classes, python-requests
Title: Mapping an API's setting IDs to setting names with class methods, enabling clearer utilization
Question: I'm trying to access an API using the requests library in as clean a manner as possible. The API is accessing various settings of various devices. I'd like to ke... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, python-3.x, classes, python-requests
domain = "https://my_dummy_api.com"
headers = {"Authorization": "my_token"}
reader = SettingReader(domain, headers, 'my_device_serial')
result_A = reader.read_setting_A()
result_B = reader.read_setting_B()
result_C = reader.read_setting_C()
For these relatively simple que... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, python-3.x, classes, python-requests
Answer: One can try the approach of creating the methods dynamically. The answer posted by Linny is one way of doing this. Linny's solution implements a __getattr__ that is called when an attribute such as read_setting_A is undefined when calling reader.read_setting_A() and... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, python-3.x, classes, python-requests
def create_read_setting_function(value):
"""Create a method that calls self._read_setting with the specified
value as the argument. Note that this function returns a closure."""
return lambda self: self._read_setting(value)
# Create a dictionary... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, python-3.x, classes, python-requests
A Second Approach: Use Descriptors
Descriptors are very powerful. Properties are implemented as descriptors and we will be creating specialized properties. Now a client will access property setting_A rather than calling method read_setting_A. If you do not mind the change ... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, python-3.x, classes, python-requests
Prints:
read_setting called with setting_id = 1 domain = https://my_dummy_api.com
read_setting called with setting_id = 2 domain = https://my_dummy_api.com
read_setting called with setting_id = 3 domain = https://my_dummy_api.com
Notes
In both approaches when a read-settin... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, python-3.x, classes, python-requests
def __init__(self, domain, headers, device_serial):
self.domain = domain
self.headers = headers
self.device_serial = device_serial
domain = "https://my_dummy_api.com"
headers = {"Authorization": "my_token"}
reader = SettingReader(domain, headers, '... | {
"domain": "codereview.stackexchange",
"id": 45369,
"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, python-requests",
"url": null
} |
python, game, numpy
Title: Text-mode 2048 in Python (using numpy)
Question: Good evening. I am studying mathematics at the moment, so I have little to no formal education in actual computer engineering. However, I am trying my hands at learning Python because I will need lots of it for my future career. I hope you ca... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
This module defines a class Game whose instances are 2048 games.
"""
# Import useful libraries:
# - numpy: the "2048" game is played on a numpy array
# - random: random handles
# - enum: the status of the "2048" game is memorized as an Enum
# - re: we use the sub function to print the game... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
self.insert_random_2()
def shift_left(self):
"""Shift the table left.
The same algorithm is applied to every row:
- compact the row to the left by removing empty space
- replace consecutive equal integers with a single integer equal to their sum
... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
def shift_right(self):
"""Shift the table right.
The same algorithm is applied to every row:
- compact the row to the right by removing empty space
- replace consecutive equal integers with a single integer equal to their sum
"""
# i -- row i... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
def shift_up(self):
"""Shift the table up.
The same algorithm is applied to every column:
- compact the column above by removing empty space
- replace consecutive equal integers with a single integer equal to their sum
"""
# i -- column index... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
def shift_down(self):
"""Shift the table down.
The same algorithm is applied to every column:
- compact the column below by removing empty space
- replace consecutive equal integers with a single integer equal to their sum
"""
# i -- column i... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
def update_status(self):
"""Check whether the game has been won or lost and update the self.status instance variable if necessary."""
# If there is at least one element in self.table >= self.winning_number, update the game status to WON.
if np.any(self.table >= self.winning_... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
def display(self):
"""Display the game table in a legible format."""
# We use the array_str numpy method to convert the table to a string, and then we use a regular expression to
# remove the brackets. The first bracket needs to be replaced by a space to preserve indentation... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
Any feedback and advice is welcome, regardless of how harsh. Thanks everyone and have nice day.
Answer: Yay! Modules have docstrings, excellent. And same for classes.
Recommend that you routinely include .idea/ in .gitignore,
so files peculiar to certain IDE version won't leak out
into the public ... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
Consider using import re rather than from re import sub,
so the re.sub( ... ) call will more clearly be about regular expressions.
update_status
Please
lint
your code every now and again.
We see this bit of lint advice:
E722 Do not use bare `except`
Never write except:, as it catches "too many" ex... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
No. (But sometimes yes, at least in a first draft.)
The four shift_DIRECTION() methods work and are a good first step.
You could certainly stop here.
There are several refactoring avenues for improvement.
If you begin following my suggestions and then lose interest,
that's fine, you can stop at any... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
python, game, numpy
We usually want just class + function definitions in a source file,
for the most part. Nothing with side effects, such as print().
That lets other modules, including unit tests, silently import
those definitions. The main guard handles the case where we
actually want to run the script, rather than ... | {
"domain": "codereview.stackexchange",
"id": 45370,
"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, game, numpy",
"url": null
} |
c, file-system, posix, portability, c89
Title: Portable old-school filesystem tool
Question: I recently made a tool called mkfh to create a FHS compliant filesystem structure.
I aimed to make it as portable as possible, so I wrote it in C89 and also tried to catch the vibe of old-school tools!
Did I achieve that? Is ... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
if(!options && ( !strcmp(root, "-h") || !strcmp(root, "-u") ))
options = root;
if(options != 0)
while(*options)
switch(*options++) {
case 'a': ask_optional = 1;
break;
case 'A': ask_optio... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
return r;
}
int acc(char c) {
return c == 'y'
|| c == 'Y';
}
int ask(char* c) {
if(ask_optional == 1) return USR_FAIL;
if(ask_optional == 2) return USR_SUCCESS;
fprintf(stderr,"Create %s [y/N]:",c);
return acc(cgetc());
}
int vout(char* c) {
i... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
struct dir tree(void) {
return
newdir(root, REQUIRED, dirlst(14,
newdir("bin", REQUIRED, NULL),
newdir("boot", REQUIRED, NULL),
newdir("dev", REQUIRED, NULL),
newdir("etc", REQUIRED,
dirlst(4,
... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
newdir("share", REQUIRED,
dirlst(15,
newdir("man", REQUIRED,
dirlst(8,
newdir("man1", OPTIONAL, NULL),
... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
newdir("html", OPTIONAL, NULL),
newdir("mathml", OPTIONAL, NULL)
)),
newdir("terminfo", OPTIONAL, NULL),
newdir("tmac", OPT... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
newdir("color", OPTIONAL,
dirlst(1,
newdir("icc", OPTIONAL, NULL)
)),
newdir("dict", OPTIONAL, NULL),
newdir("doc", OPTION... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
newdir("src", OPTIONAL, NULL)
)),
newdir("var", REQUIRED,
dirlst(14,
newdir("cache", REQUIRED,
dirlst(3,
newdir("fonts", OPTIONAL, NULL),
... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
newdir("home", OPTIONAL, NULL),
newdir("root", OPTIONAL, NULL),
newdir("lib64", OPTIONAL, NULL)
));
} | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
usr.c
#include <sys/stat.h>
#include <stdio.h>
#define USR_SUCCESS 1
#define USR_FAIL 0
/* define user functions here */
/* create a directory at relative path PATH */
int umkdir(const char* path) {
struct stat st = {0};
if(stat(path, &st) == -1)
return ... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
Answer: EXIT_SUCCESS and EXIT_FAILURE
The language spec promises that EXIT_SUCCESS and EXIT_FAILURE are defined by stdlib.h. Having included that header, you do not need to check whether they are defined. Moreover, the point of these is that the specific values (other than 0) ... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
Compound statements (blocks)
As a matter of good code style, use compound statements (brace-enclosed code blocks) in conjunction with conditional and looping statements, even when they contain only one simple statement. Especially do not use the comma operator to squeeze what w... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
if(argc == 2) root = argv[1];
else if(argc == 3) options = argv[1],
root = argv[2];
else usage();
should be
if (argc == 2) {
root = argv[1];
} else if (argc == 3) {
options = argv[1];
root = argv[2];
} else {
... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c, file-system, posix, portability, c89
the correct spelling is "separator" (not "seperator")
function cgetc() would be more efficiently written in terms of the fgets() function.
the return value of function vout() is not actually computed by the function, and it is never used anyway. This function probably should no... | {
"domain": "codereview.stackexchange",
"id": 45371,
"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, file-system, posix, portability, c89",
"url": null
} |
c++, error-handling, c++20, exception
Title: Wrap a noexcept C++ library method with a method throwing exceptions with usable explanatory strings to stay DRY
Question: In our apps we're using a shared inhouse library which provides filesystem functions. All the functions are noexcept.
In several apps i found that sim... | {
"domain": "codereview.stackexchange",
"id": 45372,
"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++, error-handling, c++20, exception",
"url": null
} |
c++, error-handling, c++20, exception
// is this good design?
// reimplement it per app?
// repeat it on every invocation and don't create a wrapper?
void createDirectory_CanThrow(std::string_view directory) {
switch (createDirectory(directory))
{
default:
assert... | {
"domain": "codereview.stackexchange",
"id": 45372,
"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++, error-handling, c++20, exception",
"url": null
} |
c++, error-handling, c++20, exception
Then in main() you can write:
auto result = myorg::filesystem::createDirectory("C:\\test");
if (result != CreateDirectoryResult::Ok) {
std::cerr << "Error creating directory: " << what(result) << "\n";
}
Or maybe you can add a check() function that does the printing, and ret... | {
"domain": "codereview.stackexchange",
"id": 45372,
"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++, error-handling, c++20, exception",
"url": null
} |
c++, error-handling, c++20, exception
This is a bit advanced, but it allows you to write:
auto createDirectory_CanThrow = MakeThrow<createDirectory>{};
It still needs you to write overloads for check() that handle all the possible return types that your non-throwing functions can return, but creating the wrapper func... | {
"domain": "codereview.stackexchange",
"id": 45372,
"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++, error-handling, c++20, exception",
"url": null
} |
java, performance, hash-map
Title: Map constructor utility class in Java 8
Question: In a Java 8 project, I'm using several maps, many of which contain static or default content. In Java 9 and newer, there's the convenient Map.of() method and I want something similar convenient. So, I came up with the following imple... | {
"domain": "codereview.stackexchange",
"id": 45373,
"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, hash-map",
"url": null
} |
java, performance, hash-map
/**
* Shortcut to create a map
* @param keyClass type class of keys
* @param valueClass type class of values
* @param keysAndValues array with keys and values (even indices: keys, odd indices: values)
* @param <KeyT> type of keys
* @param <ValueT... | {
"domain": "codereview.stackexchange",
"id": 45373,
"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, hash-map",
"url": null
} |
java, performance, hash-map
I did quite a lot of tinkering to get this implementation and I believe there's still room for improvement. For example, the first two arguments must be the key and value classes to get the types correctly, which Java 9's Map.of() doesn't require. Besides implementational improvements, I'm ... | {
"domain": "codereview.stackexchange",
"id": 45373,
"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, hash-map",
"url": null
} |
c++, performance, r, rcpp
Title: Acceleration of Hidden Markov Modell likelihood calculation in Rcpp
Question: I wrote the following code in Rcpp, i.e. it is C++ code that is compiled in R for making faster calculations. The code in Rcpp is intended to calculate in a recursive way the likelihood function for an Hidde... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
c++, performance, r, rcpp
// [[Rcpp::export]]
arma::vec dmvnrm_arma_mc(arma::mat const &x,
arma::rowvec const &mean,
arma::mat const &sigma,
bool const logd = false,
int const cores = 1) {
using arma::uword;
... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
c++, performance, r, rcpp
}
if(k==1){
q.slice(j).submat(0,t,n-1,t) = q.slice(0).submat(0,t-1,n-1,t-1) + log(PI(0,j)) +
dmvnrm_arma_mc(as<arma::mat>(Data[t]), mu.submat(j,0,j,num_feat-1),
Sigma.slice(j), true,4);
}
}
}
if(k>1){
cb = SummarizeLastCol... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
c++, performance, r, rcpp
I tried to optimize all the functions shown by avoiding using any cumbersome functions, however, the time needed for the function Like_Fun_acoustic_FAST to run is still too much. The first function log_sum_exp2_cpp_apply takes as input a matrix and calculates the log sum for each row. The fu... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
c++, performance, r, rcpp
Answer: I tried to look for performance issues, but unfortunately I can't, because your code has a major problem:
Naming things
Your code is very hard to read because of the inconsistent way you are naming things. Also, while you are apparently not afraid to use long names for certain things,... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
c++, performance, r, rcpp
Go over all the function names, make sure they use the same style of naming (I recommend using snake_case, as that is the most commonly used style for function and variable names in C++ code), and ensure the name clearly explains what it is doing.
Do the same for variable names: make sure the... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
c++, performance, r, rcpp
With:
for (int row = 0; row < num_rows; ++row) {
Sure, it's a bit more typing, but now I instantly know what is being looped over, whereas with j and n I have to look elsewhere to see what is actually stored in those variables.
Another thing you can do is imagine your are talking with someon... | {
"domain": "codereview.stackexchange",
"id": 45374,
"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, r, rcpp",
"url": null
} |
php, database, mysqli
Title: A PHP class for the common database operations?
Question: I'm a CS undergrad, so I don't have much experience. But while coding vanilla PHP projects, I found that I was repeating myself a lot with the CRUD operations. So overtime, I developed a single file called config.php that I use in ... | {
"domain": "codereview.stackexchange",
"id": 45375,
"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": "php, database, mysqli",
"url": null
} |
php, database, mysqli
if (!$result) {
return array();
}
return $result->fetch_all(MYSQLI_ASSOC);
} finally {
$stmt->close();
}
}
public function executeWriteQuery($query, $params = array())
{
$stmt = $this->prepareStatement($quer... | {
"domain": "codereview.stackexchange",
"id": 45375,
"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": "php, database, mysqli",
"url": null
} |
php, database, mysqli
$this->conn->rollback();
return false;
}
}
private function prepareStatement($query, $params)
{
$stmt = $this->conn->prepare($query);
if (!$stmt) {
return false;
}
if ($params) {
$types = $this->getBindType... | {
"domain": "codereview.stackexchange",
"id": 45375,
"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": "php, database, mysqli",
"url": null
} |
php, database, mysqli
Answer: Yes of course it's OK to have such a file. However, your Connection class is hardly related to actual CRUD operations. It's more a DAO, a database abstraction object.
Speaking of the code you provided, it's not a bad code for an undergraduate, though it can see a good rewrite. Better yet,... | {
"domain": "codereview.stackexchange",
"id": 45375,
"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": "php, database, mysqli",
"url": null
} |
c++, datetime
Title: Getting current time with milliseconds
Question: I am looking for a more efficient or shorter way to achieve the following output using the following code:
timeval curTime;
gettimeofday(&curTime, NULL);
int milli = curTime.tv_usec / 1000;
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
... | {
"domain": "codereview.stackexchange",
"id": 45376,
"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++, datetime",
"url": null
} |
performance, python-3.x, sorting
Title: Average Pair Sorting algorithm
Question: I had to relax a bit, so I wrote this sorting algorithm. By any means it isn't fast, but I really started to get interested in it, since I haven't seen similar approach yet.
Disclaimer
I do not intend to make something revolutionary, but... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
performance, python-3.x, sorting
The code [Python]
import random
class ValueNode: #ValueNode object storing single number or a pair of values
def __init__(self, numL=None, numR=None, num=None):
if num is None: #Initializing a pair of values
self.numL = numL
self.numR = numR
self.valL = nu... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
performance, python-3.x, sorting
if isSorted(current_arr):
if isComplete(current_arr):
return current_arr
else:
unpackedArray = []
for i in current_arr:
unpackedArray += i.unpack()
stack.append(unpackedArray)
else:
newArray = ... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
performance, python-3.x, sorting
Answer: naming convention
self.numL = numL
self.numR = numR
self.valL = ...
PEP-8
asks that you spell these num_l, val_l, new_array, and so on.
(Also, somehow your defs wound up exdented and in the left margin,
but that must be a stackexchange copy-n-paste issu... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
performance, python-3.x, sorting
Now, I understand that this code happens to never ask if one is less than another.
But still, it's odd that you didn't throw in a
@total_ordering
decorator.
It feels like a giant hole in the sidewalk,
just waiting for some hapless maintenance engineer
to fall into.
And at this point, I... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
performance, python-3.x, sorting
make sense?
Yes, sure, given that it eventually puts the inputs into proper order.
I assume you have an automated
test suite
that compares results with what sorted() says.
Given the ready availability of such an oracle,
consider letting the
hypothesis
package torture test this algorit... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
performance, python-3.x, sorting
As a separate matter, I have seen the standard library array
(or equivalently, np.array) make code run 3x faster
than a naïve list of numbers.
How does that work?
Part of it is due to being cache-friendly.
For N numbers we don't need to do random reads via N 64-bit pointers.
In your ca... | {
"domain": "codereview.stackexchange",
"id": 45377,
"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, python-3.x, sorting",
"url": null
} |
python-3.x, console
Title: Python3 Unix command_line_interface task organizer application using argparse
Question: Greetings.
Lately I've tried to use a branch of text_formatting rules to enhance my productivity while working with different integration tools such as Git, but this formatting convention went so
deep i... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python-3.x, console
def handle_filename_argument_action() -> str:
filename_signature = "--filename"
filename = ''.join([argument for argument in command_line_arguments if command_line_arguments[command_line_arguments.index(filename_signature) + 1] == argument])
return filename
def setup_filename_arg... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python-3.x, console
cmd_input("clear")
should_open_file = input("Do you want to open the file? ( Y OR N )?: ")
if should_open_file.lower() == "yes" or should_open_file.lower() == "y":
webbrowser.open(saved_file_path)
if __name__ == "__main__":
setup_help_arguments()
setup_element_arguments()
... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python-3.x, console
Thank you for the type annotation.
Prefer Path over str here.
It is more specific, so it more clearly communicates Author's Intent.
And there are lots of nice convenience methods hanging off of each Path object.
filename = ...
Yeah, you wrote some code there.
It extends way, waayyy beyond an 80-ch... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python-3.x, console
long function
I count 64 lines in main().
That's starting to get too long --
we need to vertically scroll in order to visually take it all in.
Consider breaking out one or more sections as
(individually testable!) helper functions.
Often for loops will be likely candidates.
Calling it main is not a... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python-3.x, console
That's a little weird.
Typical idiom would be to set a bool flag when we
encounter "--elements", and then just test the flag.
(And for the number on the left that we're comparing,
a for loop using enumerate would give you that index directly.)
As written, cost of scanning N arguments is O(N²) quadr... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python-3.x, console
I can't imagine what helpful effect that first one has,
given that our previous action was to write to that same file.
Elide it.
The chmod suffers from a
TOCTTOU
race bug.
We could accomplish this more quickly with
file_name.chmod(mode),
but that would still suffer from the race condition.
If you w... | {
"domain": "codereview.stackexchange",
"id": 45378,
"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-3.x, console",
"url": null
} |
python, python-3.x, strings
Title: A simple search-and-replace algorithm
Question: In recent times, I've been using the following algorithm repeatedly in different languages (Java, Python, ...) to search and replace strings. I don't like this implementation very much, it's very procedural and not very descriptive, bu... | {
"domain": "codereview.stackexchange",
"id": 45379,
"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, strings",
"url": null
} |
python, python-3.x, strings
You intended to assign ... = match.end().
I cannot imagine how one could "test" this source code
without noticing a fatal NameError.
missing documentation
The review context merely mentioned that this will "search and replace strings",
similar to what the identifier promises.
It's unclear h... | {
"domain": "codereview.stackexchange",
"id": 45379,
"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, strings",
"url": null
} |
java
Title: Read a list of value display it depends on pagesize and current page
Question:
code feature: read a list of value display it depends on pagesize and current page
original code:
public static <T> List<T> pageBySubList(List<T> list, int pagesize, int currentPage) {
int totalcount = list.size();
... | {
"domain": "codereview.stackexchange",
"id": 45380,
"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",
"url": null
} |
java
second try:
public static <T> List<T> pageBySubList(List<T> list, int pageSize, int currentPage) {
if (list.isEmpty()) {
return new ArrayList<>();
}
int pageCount = list.size() / pageSize + list.size() % pageSize > 0 ? 1 : 0;
boolean isLastPage = list.size() % pageSize != 0 && ... | {
"domain": "codereview.stackexchange",
"id": 45380,
"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",
"url": null
} |
java
Answer: meaningful identifier
public ... pageBySubList(List<T> list, ...) {
That final identifier there, list, is just terrible.
Tell me what's in the list, tell me about its content.
From reading the signature I already know that list is a List,
so the name isn't being helpful.
OTOH isLastPage is a terrific ide... | {
"domain": "codereview.stackexchange",
"id": 45380,
"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",
"url": null
} |
c++, number-guessing-game
Title: Number Guessing Game in C++
Question: I am very new to C++ and I've tried to write a simple number guessing game. I know it is a basic task but I'd really appreciate any feedback that could help me improve my writing. Thanks!
#include <iostream>
#include<cstdlib>
int main(){
//This ... | {
"domain": "codereview.stackexchange",
"id": 45381,
"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++, number-guessing-game",
"url": null
} |
c++, number-guessing-game
Now this,
int g; //guess
is silly. Why not
int guess;
If you use proper variable names you’ll need fewer comments. Also it makes the rest of the code easier to read, as you won’t have to refer to the earlier comment. This becomes increasingly important as the size and complexity of the prog... | {
"domain": "codereview.stackexchange",
"id": 45381,
"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++, number-guessing-game",
"url": null
} |
java, algorithm, generics, library, dijkstra
Title: Fully generic, very efficient bidirectional Dijkstra's algorithm in Java
Question: After finding out that my previous implementations are incorrect, I decided to give it another try. I relied on this post.
(The entire project resides in this GitHub repository. Conta... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
/**
* This class implements a bidirectional Dijkstra's algorithm.
*
* @param <N> the actual graph node type.
* @param <W> the value type of arc weights.
*/
public final class BidirectionalDijkstrasAlgorithm<N, W> {
/**
* Searches for a shortest {@code s... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
queueF.add(new HeapNodeWrapper<>(
weightFunction.getZero(),
source,
scoreComparator));
queueB.add(new HeapNodeWrapper<>(
weightFunction.getZero(),
target,
scor... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
parentsF.put(childNode, currentNodeF);
queueF.add(new HeapNodeWrapper<>(tentativeDistance,
childNode,
scoreComparator));
}
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
weightFunction.getWeight(parentNode,
currentNodeB));
distancesB.put(parentNode, tentativeDistance);
parentsB.put(parentNode, currentNodeB);
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
parentsB);
}
}
throw new IllegalStateException(
"The target node is not reachable from the source node.");
}
private static <N> List<N> tracebackPath(N touchNodeF,
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
com.github.coderodde.pathfinding.DijkstrasAlgorithm.java:
package com.github.coderodde.pathfinding;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
/**
* Finds the shortest {@code source/target} path or throws an
* {@link IllegalStateException} if the target node is not reachable from
* the source node.
*
* @param source the source node.
* @param target the target no... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
W tentativeDistance =
weightFunction.sum(
distanceMap.get(currentNode),
weightFunction.getWeight(currentNode,
c... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
if (scoreComparator.compare(distanceMap.get(childNode), tentativeDistance) > 0) {
distanceMap.put(childNode, tentativeDistance);
parentMap.put(childNode, currentNode);
open.add(new HeapNodeWrapper<>(ten... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
import java.util.Collection;
/**
* This interface defines the API for all the node expanders.
*
* @param <N> the actual type of the nodes.
*/
public interface NodeExpander<N> {
/**
* Returns the expansion view of the input node.
*
* @param no... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
import com.github.coderodde.pathfinding.BidirectionalDijkstrasAlgorithm;
import com.github.coderodde.pathfinding.DijkstrasAlgorithm;
import com.github.coderodde.pathfinding.NodeExpander;
import com.github.coderodde.pathfinding.WeightFunction;
import java.util.ArrayList;
imp... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
final class Benchmark {
private static final int NUMBER_OF_NODES = 100_000;
private static final int NUMBER_OF_ARCS = 1_000_000;
public static void main(String[] args) {
long seed = parseSeed(args);
System.out.println("Seed = " + seed);
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
weightFunction,
Float::compare);
System.out.printf("Dijkstra's algorithm in %d milliseconds.\n",
System.currentTimeMillis() - startTime);
startTime = System.currentTimeMillis();
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
System.out.println(node);
}
System.out.printf("Bidirectional Dijkstra's path cost: %.3f\n",
computePathCost(pathBidirectionalDijkstra,
weightFunction));
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
Set<Arc> arcs = new HashSet<>(edges);
for (int i = 0; i < nodes; i++) {
graph.add(new DirectedGraphNode());
}
while (arcs.size() < edges) {
DirectedGraphNode tail = choose(graph, random);
Directed... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
final class DirectedGraphNode {
private static int nodeIdCounter = 0;
private final int id;
private final Map<DirectedGraphNode, Float> outgoingArcs =
new HashMap<>();
private final Map<DirectedGraphNode, Float> incomingArcs =
... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
class DirectedGraphNodeParentsExpander
implements NodeExpander<DirectedGraphNode> {
@Override
public List<DirectedGraphNode> expand(DirectedGraphNode node) {
return node.getParents();
}
}
Typical output
Seed = 1705171998017
Built the graph in 1... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
Critique request
As always, I would like to hear whatever comes to mind.
Answer: (Not a full review)
A lot of things feel done right - doc comments, use of interfaces, visibility…
One alternative to keeping a set of closed nodes should be to keep open as a "PrioritySet": h... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
java, algorithm, generics, library, dijkstra
In BidirectionalDijkstrasAlgorithm.findShortestPath(), I think the declarations "qualified" by F&B a code smell.
If there was a class subsuming these, it should be possible to remove most code duplication.
class ExplorationState {
Queue<HeapNodeWrapper<N, W>> queue = ne... | {
"domain": "codereview.stackexchange",
"id": 45382,
"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, algorithm, generics, library, dijkstra",
"url": null
} |
c++, converting
Title: library for converting numbers to HEX
Question: This is library for fast converting "HEX strings" to unsigned numbers and vice versa.
The result is not defined, if input is incorrect, e.g. string "ZZZ1" will be converted to number 1.
Special attention is made the conversion to string to work in... | {
"domain": "codereview.stackexchange",
"id": 45383,
"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++, converting",
"url": null
} |
c++, converting
return { buffer, size };
}
template <typename T, uint8_t opt = options::defaults, size_t N>
std::string_view toHex(T const number, std::array<char, N> &buffer){
static_assert(
std::is_same_v<T, uint8_t > ||
std::is_same_v<T, uint16_t> ||
std::is_... | {
"domain": "codereview.stackexchange",
"id": 45383,
"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++, converting",
"url": null
} |
c++, converting
if constexpr(1){
to_string_buffer_t buffer;
printf("%s\n", hex_convert::toHex<uint64_t, о::terminate | о::lowercase>(0x00DEADBEEF, buffer).data());
printf("%s\n", hex_convert::toHex<uint64_t, о::terminate | о::uppercase>(0x00DEADBEEF, buffer).data());
printf("%s\n", he... | {
"domain": "codereview.stackexchange",
"id": 45383,
"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++, converting",
"url": null
} |
c++, converting
The char* version of toHex() requires the caller to know the required buffer size in advance and provide a suitable pointer. That's risky. I'd prefer a version that creates a std::string.
The std::array version of toHex() only asserts that the buffer has enough space for 1 character per input octet,... | {
"domain": "codereview.stackexchange",
"id": 45383,
"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++, converting",
"url": null
} |
javascript, typescript, user-interface, vue.js
Title: Virtual scroller Vue component
Question: Problem
The scrolling looks smooth on Windows, but very laggy on Linux (Webkit webview and Webkit browsers).
Any thoughts on what could be optimized or what's obviously broken?
Demo playground:
https://stackblitz.com/edit/s... | {
"domain": "codereview.stackexchange",
"id": 45384,
"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": "javascript, typescript, user-interface, vue.js",
"url": null
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.