text stringlengths 1 2.12k | source dict |
|---|---|
java, beginner, game, console
import java.util.Optional;
import java.util.Random;
import java.util.Scanner;
public class Game {
public record Hit(
boolean blocked,
int attack_points
) {
@Override
public String toString() {
if (attack_points == 0)
ret... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_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, beginner, game, console",
"url": null
} |
java, beginner, game, console
@Override
public String toString() {
return name;
}
}
private final Random rand = new Random();
private final Scanner scanner = new Scanner(System.in);
private final Character hero;
private final Character enemy = new Character("Spock", ran... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_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, beginner, game, console",
"url": null
} |
java, beginner, game, console
public void doBattle() {
Optional<Character> goesFirst = rollInitiative();
if (goesFirst.isPresent()) {
Character attacker = goesFirst.get();
System.out.printf("%s takes initiative!%n", attacker);
Character defender = hero == attacker? e... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_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, beginner, game, console",
"url": null
} |
java, beginner, game, console
Kirk's Stats:
---------------
Health: 68
Defense: 59
Strength: 50
Spock's Stats:
---------------
Health: 56
Defense: 3
Strength: 49
Enter option (1 to battle, 2 to escape)! 1
Kirk takes initiative!
Kirk strikes Spock for 47 points of damage!
(Crowd boos)
Kirk's Stats:
----... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_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, beginner, game, console",
"url": null
} |
java, beginner, game, console
Kirk's Stats:
---------------
Health: 67
Defense: 48
Strength: 50
Spock's Stats:
---------------
Health: 0
Defense: 3
Strength: 49
Kirk utterly smites Spock!
(Crowd ROARS)
Stats tables
A simple way to condense your stats to a table-like format could look like
public s... | {
"domain": "codereview.stackexchange",
"id": 45425,
"lm_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, beginner, game, console",
"url": null
} |
c#
Title: Should I automatically swap matrix dimensions if the passed matrices are formatted incorrectly? | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
Question: I'm working on making a matrix math object that's as generic as possible. I currently have done multiplication. I've run into a decision I have to make, but first, here's the code.
namespace MatrixMath
{
/// <summary>
/// Matrix is a class designed to do math on matrices for you.
/// Matrix me... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
if (secondMatrix is null)
throw new ArgumentNullException(nameof(secondMatrix)); | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
if (!TypeIsNumber<T>())
throw new ArgumentException($"{typeof(T)} is a data type that can't be multiplied.");
int firstMatrixRowCount = firstMatrix.GetLength(0);
int firstMatrixColumnCount = firstMatrix.GetLength(1);
int secondMatrixRowCount = secondMatrix.GetLen... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
return resultMatrix;
}
/// <summary>
/// Multiplies two matrices together. Assumes formatting is [rows][columns]. Will automatically
/// correct this when wrong, except when both matrices have the same number of rows and columns.
/// </summary>
/// /// <example>
... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
return ConvertToArrayOfArrays(convertedResultMatrix);
}
/// <summary>
/// Multiplies a matrix by a given multiplier.
/// </summary>
/// <typeparam name="T">The datatype the matrices are. If <typeparamref name="T"/>'s a non numeric datatype, an <see cref="ArgumentException"/>... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
/// <summary>
/// Swaps the rows and columns of the passed matrix.
/// </summary>
/// <typeparam name="T">The data type of the matrix.</typeparam>
/// <param name="matrix">The matrix you want to swap the dimensions of.</param>
/// <returns>A copy of the matrix with the dimens... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
for (int i = 0; i < matrix.GetLength(0); i++)
{
result[i] = new T[matrix.GetLength(1)];
}
for (int i = 0; i < matrix.GetLength(0); i++)
{
for (int j = 0; j < matrix.GetLength(1); j++)
{
result[i][j] ... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
try
{
T bar = (T)((dynamic)foo * foo);
if (bar.Equals(foo))
return true;
return false;
}
catch
{
return false;
}
}
}
}
Here's my dilemma. I would like to mak... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c#
Answer: I’m glad you’re asking / exploring the question.
Is auto-magically (silently) “fixing” the input a bad practice?
Yes.
Why is that?
It’s much nicer to alert the caller to a code defect, a contract violation, with informative diagnostic than to conceal the latent bug and sometimes give wrong answer without wa... | {
"domain": "codereview.stackexchange",
"id": 45426,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c#",
"url": null
} |
c++, c++14
Title: Fill vector of unique_ptr only with required derived types and enforce multiplicities constraints
Question: I wrote a function that accepts a destination vector and a vector of all the types that are currently available. Its job is to iterate over all the available types (strings) and for each type,... | {
"domain": "codereview.stackexchange",
"id": 45427,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, c++14",
"url": null
} |
c++, c++14
for (const auto& availableType : availableTypes) {
auto it = requiredTypeCount.find(availableType);
if (it != requiredTypeCount.end()) {
requiredTypeCount[availableType]++;
if (availableType == DerivedA::getType()) {
dstVector.emplace_back(std::make_un... | {
"domain": "codereview.stackexchange",
"id": 45427,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, c++14",
"url": null
} |
c++, c++14
I changed the type of the parameter availableTypes to a std::set; this doesn't allow duplicates, so this function doesn't have to worry about that anymore. You could always add another function that builds this std::set from a std::vector and that does assert() when it finds duplicates.
Then requiredTypes i... | {
"domain": "codereview.stackexchange",
"id": 45427,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, c++14",
"url": null
} |
c++, c++14
Consider returning a std::vector<std::unique_ptr<Base>> instead of passing it as an out parameter.
Consider using typeid() instead of having a getType() function. Comparing the result of typeid() might also be faster than comparing std::strings.
assert() becomes a no-op when NDEBUG is defined. This might ha... | {
"domain": "codereview.stackexchange",
"id": 45427,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, c++14",
"url": null
} |
c++, search, binary-search
Title: Incremental upper bound in sorted range
Question: Since initial question Increasing binary search for mismatch in sorted range contained defects in code and can confuse the readers, here is the updated version of the question.
Is the code below correct and efficiently implemented to ... | {
"domain": "codereview.stackexchange",
"id": 45428,
"lm_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++, search, binary-search",
"url": null
} |
c++, search, binary-search
const auto current = *first++;
size_t short_search_range = starting_search_range;
while (first != last) {
const auto short_search_end = (size_t)std::distance(first, last) < short_search_range ? last : std::next(first, short_search_range);
first = std::upper_bound(f... | {
"domain": "codereview.stackexchange",
"id": 45428,
"lm_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++, search, binary-search",
"url": null
} |
c++, search, binary-search
Amount of comparisons (predicate calls)
13 122 916 104
943 337 060
Running time, seconds
436
79
Conclusion
The results are as expected. Let’s see the last case. For the index array std:upper_bound needs about 28 iterations on average to find the answer and incremental_upper_bound takes two... | {
"domain": "codereview.stackexchange",
"id": 45428,
"lm_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++, search, binary-search",
"url": null
} |
c++, search, binary-search
Answer: current should be a reference
If comparisons are time-consuming, then the values you are comparing are probably non-trivial. Your declaration of current makes a copy of a value, which can be expensive, or perhaps even impossible if the value type of the container is non-copyable. Mak... | {
"domain": "codereview.stackexchange",
"id": 45428,
"lm_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++, search, binary-search",
"url": null
} |
c++, collections, c++20, memory-optimization, set
Title: Collection that uses a bitset to store a huge set of unique integers from a given range (rev. 1) | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
Question: Some (skippable) context:
I'm writing program that minimizes an arbitrary logical function. Such function takes an unsigned integer as an argument and returns either true or false.
The input to my program is a list of all values for which the minimized functi... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
The main purpose of the review, however, is too gauge the quality and readability of the code.
I have quite a lot of experience with C++ but I'm mostly self-learned. I've never worked professionally with other people knowing this language so I might be not aware of som... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
"Minterm.hh" defines the type Minterm which is just an alias to std::uint_fast32_t.
"global.hh" defines a value maxMinterm which depends on the program's input but it doesn't change after its initialization at the beginning. It's value is in range from 0 to 2^32 - 1.
... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
class Minterms
{
std::vector<bool> bitset;
std::size_t size = 0;
public:
using overlapping_t = std::vector<Minterm>;
class ConstIterator
{
const Minterms &minterms;
std::uint_fast64_t i;
ConstIterator(const Minterms... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
[[nodiscard]] ConstIterator cbegin() const { return begin(); }
[[nodiscard]] ConstIterator cend() const { return end(); }
#ifndef NDEBUG
void validate() const;
#endif
}; | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
Minterms::ConstIterator& Minterms::ConstIterator::operator++()
{
for (++i; i != minterms.bitset.size() && !minterms.bitset[i]; ++i) { }
return *this;
}
Minterms::ConstIterator& Minterms::ConstIterator::operator--()
{
for (--i; i != 0 && !minterms.bitset[i]... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
Answer: Don't call the class itself Minterms
While your program might need to store a list of "minterms", the data structure you are using for this doesn't care what is stored in it. I would call the class you make Bitset or something like that, and then in your actual... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
c++, collections, c++20, memory-optimization, set
If you don't need to know the number of set bits often and/or efficiently, then I would just use std::vector<bool> directly.
Use std::bitset if maxMinterm is known at compile time
If you know maxMinterm at compile time, then you can use std::bitset instead of a std::ve... | {
"domain": "codereview.stackexchange",
"id": 45429,
"lm_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++, collections, c++20, memory-optimization, set",
"url": null
} |
beginner, php, ruby, ruby-on-rails
Title: Session Logic for User Verification in Rails Migration
Question: This is a logic to create sessions on RoR 7.1.2 based on the last version of the website which was in vanilla PHP, with the upgrade I have to deal with the users that were already signed up but not have a passwo... | {
"domain": "codereview.stackexchange",
"id": 45430,
"lm_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, php, ruby, ruby-on-rails",
"url": null
} |
beginner, php, ruby, ruby-on-rails
if (password_verify($password, $hased_password)) {
echo 'true';
} else {
echo 'false';
}
Answer: This mostly looks good and sensible, but it does raise some serious concerns.
naming
Using a php_ prefix is accurate, and perhaps a good name.
But consider using a legacy_ or v1_ pre... | {
"domain": "codereview.stackexchange",
"id": 45430,
"lm_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, php, ruby, ruby-on-rails",
"url": null
} |
beginner, php, ruby, ruby-on-rails
user = User.find_by(email: username)
return user if user
Usernames are drawn from one namespace; roughly they match /^\w+$/.
Email addresses drawn from another, and we definitely expect
they shall contain an @ at-sign, e.g. /^[\w.-]+@[\w.-]+$/.
(Yeah, yeah, I know, localpart can... | {
"domain": "codereview.stackexchange",
"id": 45430,
"lm_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, php, ruby, ruby-on-rails",
"url": null
} |
beginner, php, ruby, ruby-on-rails
There's two different interpreters involved: bash + php.
Each is powerful.
Each does their own interpretation of argument characters.
There is room to lose.
Now, if we just ask for ls output, there's no shell interpreter
involved, as ruby does fork / exec of an ls child.
But if the b... | {
"domain": "codereview.stackexchange",
"id": 45430,
"lm_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, php, ruby, ruby-on-rails",
"url": null
} |
c++, strings, search
Title: Fast search for the longest repeating substrings in large text (Rev.3)
Question: This is the third iteration of the Fast search for the longest repeating substrings in large text (Rev.2) code review. Special thanks goes to G. Sliepen who conducted the first two reviews.
Functional specific... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
Here is the current implementation of calculateAdjacents:
// Returns found sequence(s) length
size_t calculateAdjacents(const Index<>& index,
std::vector<index_t>& adjacent_matches,
size_t repetitions,
bool strong_repeti... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
return match_length;
});
return max_match_length;
}
On keeping the findMatchLength, this is kind of adherence to Occam's razor principle to avoid unnecessary entities and a kind of rule of thumb like “If you can’t explain in simple words (preferably by function name) what it does gen... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
Major changes
Since in real project I will use lexem identifiers instead of plain text, I did some “emulation” in code for this, making pseudo tokenizer which for the sake of simplicity just converts characters’ codes to ints. This is just a trick to go to real types I use and move from std::stri... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
Here is how it looks like now:
size_t findMatchLengthWithOffsetInSuffixArray(const Index<>& index,
const size_t idx,
ptrdiff_t offset)
{
const auto& text = index.text;
return std::mismatch(text.be... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
for (size_t repetitions = 2 ; repetitions <= max_repetitions; ++repetitions) {
size_t sequence_length = calculateAdjacents(index, adjacent_matches, repetitions, strong_repetition_count);
if (sequence_length < 1)
continue;
std::cout << "Longest equal sequence(s... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
Before these code reviews I raised the question The best place to store index to data where I didn’t get better proposals than my way to wrap up the references to index and indexed text into Index structure:
template <typename LexemType=lexem_t, typename IndexType = index_t>
struct Index {
con... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
I hope, this is the correct way and place to use std::span.
Generalization
One of the questions from G. Sliepen was “Why limit it to strings at all? It seems to me that you could make this work for any kind of range.”.
The only reason I am keeping this not generic is that I want to keep implementa... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
Be consistent
If you are using return type deduction for begin() and end() in Index, why not use the same for size() and operator[]()?
If you make Index a template so you can vary LexemType and IndexType, why isn't SubstringOccurences a template in the same way? And why are calculateAdjacents() an... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
No. Again, this unnecessarily complicates RepeatedSubsequences. What if you don't want to output the result to a stream, but do something else with it? Or what if I want to be able to format the output as JSON or XML instead of printing something human readable? You can't just add more and more me... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
return result;
}
Now you can create a printRepeatedSubsequences() function that formats the contents of a RepeatedSubsequences object.
Another option is to keep the outer for-loop in findRepeatedSubsequences(), but pass a callback function to it that will be called for every iteration:
template<t... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
c++, strings, search
But that's fine. Splitting code into multiple functions usually makes it easier to read, more maintainable and more self-documenting. This is well worth the extra number of lines.
The biggest increase in code size comes from calculateAdjacents(), and in particular to support the strong_repetition_... | {
"domain": "codereview.stackexchange",
"id": 45431,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, strings, search",
"url": null
} |
java, database
Title: which layer is more fit to check data integrity: service or database?
Question: I read a book named Effective SQL and found this interesting part.
Enforcing and maintaining business rules and relationships in
the data is part of the data model, and the responsibility belongs to the database, no... | {
"domain": "codereview.stackexchange",
"id": 45432,
"lm_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, database",
"url": null
} |
java, database
Answer: You're essentially asking about CREATE UNIQUE INDEX, such as a table's
PK,
and how to interact with it.
Firstly, in the system you describe there absolutely should
be a UNIQUE key.
Databases are very good at enforcing that kind of thing.
What if there wasn't one, and the app tried to prevent dup... | {
"domain": "codereview.stackexchange",
"id": 45432,
"lm_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, database",
"url": null
} |
java, performance, spring, jdbc, jpa
Title: Inserting large number of rows into database with Spring boot
Question: I need to insert many million rows + many GB of data into a database for a project that uses Spring boot. I recreated a minimal example with a one to many relationship and am trying to find the fastest ... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
private final int NUMBER_OF_USERS = 10000;
private final int NUMBER_OF_EMAILS = 10;
public void insert() {
List<User> users = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_USERS; i++) {
User user = new User();
user.setName("User "... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
public void insertJdbc() {
List<User> users = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_USERS; i++) {
User user = new User();
user.setName("User " + i);
List<Email> emails = new ArrayList<>();
for (int j = 0; j ... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
public void insertJdbcBatch() {
List<User> users = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_USERS; i++) {
User user = new User();
user.setName("User " + i);
List<Email> emails = new ArrayList<>();
for (int j = ... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
emailPs.setString(2, email.getText());
emailPs.setLong(3, user.getId());
emailPs.addBatch();
}
}
// Execute the batch update for emails
emailPs.executeBatch();
} catch (Exception e) ... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
}
@PostConstruct
public void benchmark() {
long startTime = System.currentTimeMillis();
insertJdbcBatch();
long endTime = System.currentTimeMillis();
System.out.println("Inserting " + NUMBER_OF_USERS + " users with " + NUMBER_OF_EMAILS + " e... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
Answer: It makes sense to me that JDBC with prepared statements
would win that contest, as we don't have to keep
re-parsing the same old SQL command.
Batch size before COMMIT tends to be pretty important.
Increase it from "1 row at a time", which gives terrible
throughput, to ten r... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
java, performance, spring, jdbc, jpa
something like that.
Be sure to COMMIT every couple of seconds.
As a separate operation, do a giant INSERT ... SELECT FROM scratch_user,
as a single transaction with explicit COMMIT at the end.
This makes network effects disappear from the second transaction,
and gives the query pl... | {
"domain": "codereview.stackexchange",
"id": 45433,
"lm_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, spring, jdbc, jpa",
"url": null
} |
r
Title: Correctness of newborn feeding data visualization
Question: Just wanted some code review for correctness here. This is a little app for plotting newborn feeding data (important to keep track of in the first 48 hours of so) that I hope to provide to a clinic, but I wanted some extra eyes on it if possible bef... | {
"domain": "codereview.stackexchange",
"id": 45434,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "r",
"url": null
} |
r
daysoffset= c(alldays[2:length(alldays)],NaN)
#today = c()
timebtwnhrs = (daysoffset-alldays)/60
range = round(c(min(timebtwnhrs,na.rm=TRUE),max(timebtwnhrs,na.rm=TRUE)),1)
plot(alldays,timeix,
pch=20,
#pch=3,
#xlab=paste0("Minutes \n Hours between feeds: mean = ", round(mean(timebtwnhrs,na.rm=TRUE),... | {
"domain": "codereview.stackexchange",
"id": 45434,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "r",
"url": null
} |
c++, array, iterator, collections, vectors
Title: A dynamic array with an iterator compatible with std::sort
Question: I wrote a Vector (dynamic array) class with an iterator usable with std::sort() . It seems to pass my tests.
I wondering about the issues one can spot on this implementation. I compiled with C++17.
#... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
size_t operator-(const Iterator& other) const
{
return m_ptr - other.m_ptr;
}
Iterator operator-=(const size_t n) const
{
Iterator it = *this;
it.m_ptr -= n;
return it;
}
Iterato... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
cout << "-n " << *(end-1) << endl;
cout << "+n " << *(beg+1) << endl;
cout << " - " << end-beg << endl;
cout << *(end -= 2) << endl;
std::sort(V1.begin(), V1.end());
for (auto& v : V1)
cout << v << endl;
}
int main()
{
cout << "starting... ... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
Answer: Before I get into the review, I need to offer some advice.
std::vector is a type of dynamic array, but not the only type. The thing that makes std::vector special is that it has extra capacity. You can reserve space in a vector, and fill that space in later with actua... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
So to summarize: | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
Re-implementing std::vector is a lot harder than you think it is.
You will get it wrong. I say this with almost 100% certainty.
You will not learn anything useful from the attempt.
Now, if you just want to make a simple dynamic array… that is a good learning project. And, it... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
put it in a dedicated header file; or
if you have access to more modern tools, put it in a module.
You should also put everything you make in your own namespace. You should avoid putting anything in the global namespace as much as possible.
So, assuming you are still using h... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
The convention in C++ is to use class if a type has an invariant. Your class does have an invariant; several actually. One is that m_capacity must always be greater than or equal to m_index. Another is that if m_data is not nullptr, then m_index (and/or) m_capacity must be eq... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
auto test()
{
Vector<int> v;
test_1(v);
}
The hardest part of container const-correctness is getting the iterators right. A common mistake people make is assuming that const_iterator is the same thing as const iterator. I’ve even seen blog points by purported C++ “e... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
std::cout.setf(std::ios_base::boolalpha);
std::cout << "indirectly readable: " << std::indirectly_readable<iter> << '\n';
std::cout << "input-or-output iterator: " << std::input_or_output_iterator<iter> << '\n';
std::cout << "input iterator: " ... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
This should not be a public constructor, and it absolutely should not be an implicit converting constructor. The way the iterator is written now, any T* can be implicitly converted to a Vector<T>::Iterator. That’s bad, because any random pointer is not likely to be a valid it... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
This will eliminate the need for operator!= and all the ordering operators… which, by the way…:
bool operator<(const Iterator& other) const
{
return *m_ptr < *other.m_ptr;
}
bool operator>(const Iterator& other) const
{
... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
m_capacity = capacity;
delete[] m_data;
m_data = data;
}
This really shouldn’t be a public function at all. There is no reason to give the the world this kind of access. If a user wants to add more objects, let them push_back() (or emplace_back()) one or... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
m_data[m_index++] = value;
}
You are making unnecessary copies. At the very least you should move in the last line.
Now, here is where I get back to what I talked about in the beginning. Forget having extra capacity. Drop m_capacity completely, and just keep track of the... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
This should be const qualified, and noexcept (and constexpr, of course).
Also, you should consider that containers require a few standard types to be defined. A container needs value_type (which is just T), but it also needs:
reference
const_reference
iterator
const_iterator... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
using difference_type = /*...*/;
using size_type = /*...*/;
// Default initializable ===========================================
// Should be no-fail.
constexpr Vector() noexcept;
// Copyable ========================================================... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
constexpr auto rbegin() noexcept -> reverse_iterator;
constexpr auto rbegin() const noexcept -> const_reverse_iterator;
constexpr auto rend() noexcept -> reverse_iterator;
constexpr auto rend() const noexcept -> const_reverse_iterator;
... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
// Insertion =======================================================
constexpr auto insert(const_iterator, value_type const&) -> iterator;
constexpr auto insert(const_iterator, value_type&&) -> iterator;
constexpr auto insert(const_iterator, size_type, value_type ... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
constexpr auto push_front(value_type const&) -> void;
constexpr auto push_front(value_type&&) -> void;
template <std::ranges::input_range R>
constexpr auto prepend_range(R&&) -> void;
constexpr auto pop_front() -> void;
// Back operations ==============... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
Then all the following are just in terms of that:
// Insertion =======================================================
constexpr auto insert(const_iterator p, value_type const& v) -> iterator
{
return emplace(p, v);
}
constexpr auto insert(const_iterator p, value_type&& ... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
c++, array, iterator, collections, vectors
constexpr auto push_back(value_type const&) -> void
{
emplace(end(), v);
}
constexpr auto push_back(value_type&&) -> void
{
emplace(end(), std::move(v));
}
template <std::ranges::input_range R>
constexpr auto append_range(R&& r) -> void
{
insert(end(), std::forw... | {
"domain": "codereview.stackexchange",
"id": 45435,
"lm_label": null,
"lm_name": null,
"lm_q1_score": null,
"lm_q1q2_score": null,
"lm_q2_score": null,
"openwebmath_perplexity": null,
"openwebmath_score": null,
"tags": "c++, array, iterator, collections, vectors",
"url": null
} |
php, ruby, ruby-on-rails
Title: Rails session after migrate from PHP (improved)
Question: previusly I make a post for Session Logic for User Verification in Rails Migration.
Now the improvement version following advice of the comments is here.
The problem was the use of `` for executing in shell on a RoR application ... | {
"domain": "codereview.stackexchange",
"id": 45436,
"lm_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, ruby, ruby-on-rails",
"url": null
} |
php, ruby, ruby-on-rails
if (password_verify($password, $hased_password)) {
exit(0);
} else {
exit(1);
}
the form that call the method
<%= form_for(@user, html: { class: 'log-form' }) do |f| %>
<%= f.text_field :firstname, placeholder: 'First Name' %>
<%= f.text_field :lastname, placeholder: 'Last Name' %>
... | {
"domain": "codereview.stackexchange",
"id": 45436,
"lm_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, ruby, ruby-on-rails",
"url": null
} |
beginner, c, homework, binary-tree
Title: Binary tree struct in C
Question: Its been done to death, but the particular prompt (see Assignment 4) guiding me has strict+simple requirements that lead to a nice reduced environment to critique poor form. The project implements a heap-based binary tree structure with inser... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
int find_node_data(int node_id);
void remove_node(int node_id);
void delete_tree();
#endif
bintree.c
// bintree.c interfaces with the node struct defined in bintree.h
// to create and manipulate a heap-based binary tree. All functions
// meant for interfacing by the user are def... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
///*** DO NOT CHANGE ANY FUNCTION DEFINITIONS ***///
// Initialize tree
node *root = NULL;
// Insert a new node into the binary tree with node_id and data
void insert_node(int node_id, int data) {
// reserve memory for node
node *newNode = (node *)malloc(sizeof(node));
... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
// Returns pointer to node's parent given node id
node *find_parent(int node_id) {
if (root == NULL) { printf("root is NULL\n"); return NULL; }
else if (root->node_id == node_id) { return NULL; } // root has no parent
node *tmp = root;
node *parent = NULL;
while (... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
/* OPTIONAL: Challenge yourself w/ deletion if you want
Find and remove a node in the binary tree with node_id.
Children nodes are fixed appropriately. */
void remove_node(int node_id) {
node *parent = find_parent(node_id);
node *tmp = find_node(node_id);
if (tmp == NUL... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
// • 'left-child' and 'right-child' refer to the
// children of the node being deleted.
// • 'appropriate leg' refers to whichever (left or right)
// side of the parent the node being deleted is attached to.
//
// find the right-most... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
// There is only one tree and it starts where 'root' points,
// delete_tree unallocates all memory for every node connected
// to root, including root.
void delete_tree() {
delete_node(root);
}
I would have liked to change find_node_data to be cast to a pointer so that NULL cou... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
Answer: General Observations
The use of the typedef for the node struct is a good practice.
The fact that all of the loops and if statements contain the code as blocks by using the braces ({ and }) is a good practice.
It would be much easier to do a code review if we had the working ... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
In C there is no reason to cast the return value of malloc().
Test for Possible Memory Allocation Errors
In modern high-level languages such as C++, memory allocation errors throw an exception that the programmer can catch. This is not the case in the C programming language. While it... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
beginner, c, homework, binary-tree
Avoid Global Variables
It is very difficult to read, write, debug and maintain programs that use global variables. Global variables can be modified by any function within the program and therefore require each function to be examined before making changes in the code. In C and C++ gl... | {
"domain": "codereview.stackexchange",
"id": 45437,
"lm_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, c, homework, binary-tree",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
Title: A recursive_all_of Template Function Implementation in C++
Question: This is a follow-up question for A recursive_foreach_all Template Function Implementation in C++. I am trying to implement recursive_all_of template function in this post.
The experimenta... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
template<class...Ts1, template<class...>class Container1, typename... Ts>
struct recursive_unwrap_type<1, Container1<Ts1...>, Ts...>
{
using type = std::ranges::range_value_t<Container1<Ts1...>>;
};
template<std::size_t unwrap_level, class...Ts1, template<cla... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
template<std::size_t unwrap_level, typename F, class...Ts1, template<class...>class Container1, typename... Ts>
requires ( std::ranges::input_range<Container1<Ts1...>> &&
requires { typename recursive_variadic_invoke_result<
... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
template< std::size_t unwrap_level,
typename F,
template<class, std::size_t> class Container,
typename T,
std::size_t N>
requires ( std::ranges::input_range<Container<T, N>> &&
requires { typename rec... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
template<std::size_t unwrap_level, template<class, std::size_t> class Container,
typename T,
std::size_t N>
requires ( std::ranges::input_range<Container<T, N>> &&
requires { typename recursive_array_unwrap_type<
... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
template<typename T_Base, std::ranges::input_range Range>
requires (!std::same_as<Range, T_Base>)
constexpr std::size_t recursive_depth()
{
return recursive_depth<T_Base, std::ranges::range_value_t<Range>>() + std::size_t{1};
}
/* recursive_foreach_all templ... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
c++, recursion, template, c++20, constrained-templates
template<std::ranges::input_range T, class UnaryPredicate>
requires(std::ranges::input_range<std::ranges::range_value_t<T>>)
constexpr auto recursive_all_of(T& inputRange, UnaryPredicate p) {
for (auto& item: inputRange)
{
if(im... | {
"domain": "codereview.stackexchange",
"id": 45438,
"lm_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++, recursion, template, c++20, constrained-templates",
"url": null
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.