Skip to content

Commit 04d0345

Browse files
committed
Use std::min()/std::max() where it improves readability.
1 parent 5ba2ee5 commit 04d0345

8 files changed

+30
-30
lines changed

.clang-tidy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Checks: >
2727
performance-*,
2828
-performance-enum-size,
2929
readability-*,
30+
-readability-avoid-unconditional-preprocessor-if,
3031
-readability-braces-around-statements,
3132
-readability-convert-member-functions-to-static,
3233
-readability-else-after-return,

src/common/linebuf-reader_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class InputStreamSimulator {
4444
for (const char *line : kSampleLines) {
4545
if (line == nullptr) break;
4646
buffer_.append(line).append(endline);
47-
if (strlen(line) > longest_line_len_) longest_line_len_ = strlen(line);
47+
longest_line_len_ = std::max(strlen(line), longest_line_len_);
4848
}
4949
}
5050

src/determine-print-stats.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <stdio.h>
2424
#include <stdlib.h>
2525

26+
#include <algorithm>
2627
#include <cstring>
2728

2829
#include "gcode-machine-control.h"
@@ -79,8 +80,8 @@ class StatsCollectingEventDelegator : public GCodeParser::EventReceiver {
7980

8081
private:
8182
static void set_min_max(float value, float *min, float *max) {
82-
if (value < *min) *min = value;
83-
if (value > *max) *max = value;
83+
*min = std::min(value, *min);
84+
*max = std::max(value, *max);
8485
}
8586
void update_coordinate_stats(const AxesRegister &axis) {
8687
set_min_max(axis[AXIS_X], &stats_->x_min, &stats_->x_max);
@@ -104,7 +105,7 @@ class StatsSegmentQueue : public SegmentQueue {
104105
int max_steps = 0;
105106
for (int i = 0; i < BEAGLEG_NUM_MOTORS; ++i) {
106107
const int steps = abs(param.steps[i]);
107-
if (steps > max_steps) max_steps = steps;
108+
max_steps = std::max(steps, max_steps);
108109
}
109110

110111
// max_steps = a/2*t^2 + v0*t; a = (v1-v0)/t

src/gcode-machine-control.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <time.h>
3535
#include <unistd.h>
3636

37+
#include <algorithm>
3738
#include <string>
3839

3940
#include "adc.h"
@@ -218,9 +219,8 @@ bool GCodeMachineControl::Impl::Init() {
218219
}
219220

220221
for (const GCodeParserAxis axis : AllAxes()) {
221-
if (cfg_.max_feedrate[axis] > g0_feedrate_mm_per_sec_) {
222-
g0_feedrate_mm_per_sec_ = cfg_.max_feedrate[axis];
223-
}
222+
g0_feedrate_mm_per_sec_ =
223+
std::max(cfg_.max_feedrate[axis], g0_feedrate_mm_per_sec_);
224224
}
225225
prog_speed_factor_ = 1.0f;
226226

@@ -992,7 +992,7 @@ int GCodeMachineControl::Impl::move_to_probe(enum GCodeParserAxis axis,
992992
int total_movement = 0;
993993
float v0 = 0;
994994
float v1 = feedrate;
995-
if (v1 > cfg_.max_probe_feedrate[axis]) v1 = cfg_.max_probe_feedrate[axis];
995+
v1 = std::min(v1, cfg_.max_probe_feedrate[axis]);
996996
while (!hardware_mapping_->TestProbeSwitch()) {
997997
total_movement += planner_->DirectDrive(axis, dir * kProbeMM, v0, v1);
998998
v0 = v1; // TODO: possibly acceleration over multiple segments.

src/gcode-print-stats.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <string.h>
2424
#include <unistd.h>
2525

26+
#include <algorithm>
27+
2628
#include "common/logging.h"
2729
#include "config-parser.h"
2830
#include "determine-print-stats.h"
@@ -115,7 +117,7 @@ int main(int argc, char *argv[]) {
115117
int longest_filename = strlen("#[filename]"); // table header
116118
for (int i = optind; i < argc; ++i) {
117119
const int len = strlen(argv[i]);
118-
if (len > longest_filename) longest_filename = len;
120+
longest_filename = std::max(len, longest_filename);
119121
}
120122
if (print_header) {
121123
printf("%-*s %10s %7s %7s %7s %7s %7s %7s %7s %7s\n", longest_filename,

src/gcode2ps.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,12 @@ class AxesRange {
119119

120120
// Update with a range we should cover at least.
121121
void Update(const AxesRegister &axes) {
122-
if (axes[AXIS_X] < min_[AXIS_X]) min_[AXIS_X] = axes[AXIS_X];
123-
if (axes[AXIS_Y] < min_[AXIS_Y]) min_[AXIS_Y] = axes[AXIS_Y];
124-
if (axes[AXIS_Z] < min_[AXIS_Z]) min_[AXIS_Z] = axes[AXIS_Z];
125-
if (axes[AXIS_X] > max_[AXIS_X]) max_[AXIS_X] = axes[AXIS_X];
126-
if (axes[AXIS_Y] > max_[AXIS_Y]) max_[AXIS_Y] = axes[AXIS_Y];
127-
if (axes[AXIS_Z] > max_[AXIS_Z]) max_[AXIS_Z] = axes[AXIS_Z];
122+
min_[AXIS_X] = std::min(axes[AXIS_X], min_[AXIS_X]);
123+
min_[AXIS_Y] = std::min(axes[AXIS_Y], min_[AXIS_Y]);
124+
min_[AXIS_Z] = std::min(axes[AXIS_Z], min_[AXIS_Z]);
125+
max_[AXIS_X] = std::max(axes[AXIS_X], max_[AXIS_X]);
126+
max_[AXIS_Y] = std::max(axes[AXIS_Y], max_[AXIS_Y]);
127+
max_[AXIS_Z] = std::max(axes[AXIS_Z], max_[AXIS_Z]);
128128
}
129129

130130
void ZeroUnusedAxes() {
@@ -772,8 +772,8 @@ class GCodePrintVisualizer : public GCodeParser::EventReceiver {
772772
void change_spindle_speed(float value) final {
773773
// Hack to visualize brightness in lasing applications.
774774
laser_intensity_ = value;
775-
if (laser_intensity_ < laser_min_) laser_min_ = laser_intensity_;
776-
if (laser_intensity_ > laser_max_) laser_max_ = laser_intensity_;
775+
laser_min_ = std::min(laser_intensity_, laser_min_);
776+
laser_max_ = std::max(laser_intensity_, laser_max_);
777777
}
778778

779779
const char *unprocessed(char letter, float value, const char *remain) final {
@@ -871,8 +871,8 @@ class SegmentQueuePrinter final : public SegmentQueue {
871871
}
872872

873873
void RememberMinMax(float v) {
874-
if (v > max_v_) max_v_ = v;
875-
if (v < min_v_) min_v_ = v;
874+
max_v_ = std::max(v, max_v_);
875+
min_v_ = std::min(v, min_v_);
876876
}
877877

878878
bool Enqueue(const LinearSegmentSteps &param) final {

src/motion-queue-motor-operations.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <stdio.h>
2626
#include <stdlib.h>
2727

28+
#include <algorithm>
2829
#include <deque>
2930

3031
#include "hardware-mapping.h"
@@ -255,9 +256,7 @@ void MotionQueueMotorOperations::SetExternalPosition(int axis, int steps) {
255256
static int get_defining_axis_steps(const LinearSegmentSteps &param) {
256257
int defining_axis_steps = abs(param.steps[0]);
257258
for (int i = 1; i < BEAGLEG_NUM_MOTORS; ++i) {
258-
if (abs(param.steps[i]) > defining_axis_steps) {
259-
defining_axis_steps = abs(param.steps[i]);
260-
}
259+
defining_axis_steps = std::max(abs(param.steps[i]), defining_axis_steps);
261260
}
262261
return defining_axis_steps;
263262
}

src/planner.cc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ static double determine_joining_speed(const struct AxisTarget *from,
385385
const double goal = to_speed * speed_conversion;
386386
if (goal < 0.0) return 0.0;
387387
if (is_first || within_acceptable_range(goal, from_defining_speed, 1e-5)) {
388-
if (goal < from_defining_speed) from_defining_speed = goal;
388+
from_defining_speed = std::min(goal, from_defining_speed);
389389
is_first = false;
390390
} else {
391391
return 0.0; // Too far off.
@@ -646,15 +646,14 @@ bool Planner::Impl::machine_move(const AxesRegister &axis, float feedrate) {
646646

647647
// Clamp the next speed to insure that this segment does not go over.
648648
// The new target speed must be equal or lower than the previously planned.
649-
if (new_previous_speed < new_pos->start_speed)
650-
new_pos->start_speed = new_previous_speed;
649+
new_pos->start_speed = std::min(new_previous_speed, new_pos->start_speed);
651650

652651
// Make sure the target feedrate for the move is clamped to what all the
653652
// moving axes can reach.
654653
const double max_speed =
655654
clamp_defining_axis_limit(new_pos->delta_steps, cfg_->max_feedrate,
656655
defining_axis, cfg_->steps_per_mm);
657-
if (max_speed < new_pos->speed) new_pos->speed = max_speed;
656+
new_pos->speed = std::min(max_speed, new_pos->speed);
658657

659658
// Define the maximum acceleration given the following motion angles and
660659
// defining axis.
@@ -914,11 +913,9 @@ int Planner::Impl::DirectDrive(GCodeParserAxis axis, float distance, float v0,
914913
struct LinearSegmentSteps move_command = {};
915914

916915
move_command.v0 = v0 * steps_per_mm;
917-
if (move_command.v0 > max_axis_speed_[axis])
918-
move_command.v0 = max_axis_speed_[axis];
916+
move_command.v0 = std::min(move_command.v0, max_axis_speed_[axis]);
919917
move_command.v1 = v1 * steps_per_mm;
920-
if (move_command.v1 > max_axis_speed_[axis])
921-
move_command.v1 = max_axis_speed_[axis];
918+
move_command.v1 = std::min(move_command.v1, max_axis_speed_[axis]);
922919

923920
move_command.aux_bits = hardware_mapping_->GetAuxBits();
924921

0 commit comments

Comments
 (0)