Skip to content

Commit 8e01289

Browse files
committed
Use emplace_back in extensions
With `emplace_back`, we don't need to create an object ourselves, to be copied into the container, plus it returns a reference to the new object for us to use. This used clang-tidy's `modernize-use-emplace` check, plus some followups to use the return values.
1 parent 4a1d131 commit 8e01289

File tree

3 files changed

+25
-31
lines changed

3 files changed

+25
-31
lines changed

src/_backend_agg_basic_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Dashes
4848
}
4949
void add_dash_pair(double length, double skip)
5050
{
51-
dashes.push_back(std::make_pair(length, skip));
51+
dashes.emplace_back(length, skip);
5252
}
5353
size_t size() const
5454
{

src/_path.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,11 +617,11 @@ inline void clip_to_rect_one_step(const Polygon &polygon, Polygon &result, const
617617
if (sinside ^ pinside) {
618618
double bx, by;
619619
filter.bisect(sx, sy, px, py, &bx, &by);
620-
result.push_back(XY(bx, by));
620+
result.emplace_back(bx, by);
621621
}
622622

623623
if (pinside) {
624-
result.push_back(XY(px, py));
624+
result.emplace_back(px, py);
625625
}
626626

627627
sx = px;
@@ -668,7 +668,7 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto
668668
polygon1.clear();
669669
do {
670670
if (code == agg::path_cmd_move_to) {
671-
polygon1.push_back(XY(x, y));
671+
polygon1.emplace_back(x, y);
672672
}
673673

674674
code = curve.vertex(&x, &y);
@@ -678,7 +678,7 @@ clip_path_to_rect(PathIterator &path, agg::rect_d &rect, bool inside, std::vecto
678678
}
679679

680680
if (code != agg::path_cmd_move_to) {
681-
polygon1.push_back(XY(x, y));
681+
polygon1.emplace_back(x, y);
682682
}
683683
} while ((code & agg::path_cmd_end_poly) != agg::path_cmd_end_poly);
684684

@@ -974,23 +974,20 @@ void convert_path_to_polygons(PathIterator &path,
974974
simplify_t simplified(clipped, simplify, path.simplify_threshold());
975975
curve_t curve(simplified);
976976

977-
result.push_back(Polygon());
978-
Polygon *polygon = &result.back();
977+
Polygon& polygon = result.emplace_back();
979978
double x, y;
980979
unsigned code;
981980

982981
while ((code = curve.vertex(&x, &y)) != agg::path_cmd_stop) {
983982
if ((code & agg::path_cmd_end_poly) == agg::path_cmd_end_poly) {
984983
_finalize_polygon(result, 1);
985-
result.push_back(Polygon());
986-
polygon = &result.back();
984+
polygon = result.emplace_back();
987985
} else {
988986
if (code == agg::path_cmd_move_to) {
989987
_finalize_polygon(result, closed_only);
990-
result.push_back(Polygon());
991-
polygon = &result.back();
988+
polygon = result.emplace_back();
992989
}
993-
polygon->push_back(XY(x, y));
990+
polygon.emplace_back(x, y);
994991
}
995992
}
996993

src/tri/_tri.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ void Triangulation::calculate_boundaries()
269269
auto it = boundary_edges.cbegin();
270270
int tri = it->tri;
271271
int edge = it->edge;
272-
_boundaries.push_back(Boundary());
272+
_boundaries.emplace_back();
273273
Boundary& boundary = _boundaries.back();
274274

275275
while (true) {
276-
boundary.push_back(TriEdge(tri, edge));
276+
boundary.emplace_back(tri, edge);
277277
boundary_edges.erase(it);
278278
_tri_edge_to_boundary_map[TriEdge(tri, edge)] =
279279
BoundaryEdge(_boundaries.size()-1, boundary.size()-1);
@@ -624,7 +624,7 @@ void TriContourGenerator::clear_visited_flags(bool include_boundaries)
624624
// Initialise _boundaries_visited.
625625
_boundaries_visited.reserve(boundaries.size());
626626
for (const auto & boundary : boundaries) {
627-
_boundaries_visited.push_back(BoundaryVisited(boundary.size()));
627+
_boundaries_visited.emplace_back(boundary.size());
628628
}
629629

630630
// Initialise _boundaries_used.
@@ -796,8 +796,7 @@ void TriContourGenerator::find_boundary_lines(Contour& contour,
796796
if (startAbove && !endAbove) {
797797
// This boundary edge is the start point for a contour line,
798798
// so follow the line.
799-
contour.push_back(ContourLine());
800-
ContourLine& contour_line = contour.back();
799+
ContourLine& contour_line = contour.emplace_back();
801800
TriEdge tri_edge = *itb;
802801
follow_interior(contour_line, tri_edge, true, level, false);
803802
}
@@ -830,8 +829,7 @@ void TriContourGenerator::find_boundary_lines_filled(Contour& contour,
830829

831830
if (decr_lower || incr_upper) {
832831
// Start point for contour line, so follow it.
833-
contour.push_back(ContourLine());
834-
ContourLine& contour_line = contour.back();
832+
ContourLine& contour_line = contour.emplace_back();
835833
TriEdge start_tri_edge = boundary[j];
836834
TriEdge tri_edge = start_tri_edge;
837835

@@ -859,8 +857,7 @@ void TriContourGenerator::find_boundary_lines_filled(Contour& contour,
859857
const Boundary& boundary = boundaries[i];
860858
double z = get_z(triang.get_triangle_point(boundary[0]));
861859
if (z >= lower_level && z < upper_level) {
862-
contour.push_back(ContourLine());
863-
ContourLine& contour_line = contour.back();
860+
ContourLine& contour_line = contour.emplace_back();
864861
for (auto edge : boundary) {
865862
contour_line.push_back(triang.get_point_coords(
866863
triang.get_triangle_point(edge)));
@@ -894,8 +891,7 @@ void TriContourGenerator::find_interior_lines(Contour& contour,
894891
continue; // Contour does not pass through this triangle.
895892

896893
// Found start of new contour line loop.
897-
contour.push_back(ContourLine());
898-
ContourLine& contour_line = contour.back();
894+
ContourLine& contour_line = contour.emplace_back();
899895
TriEdge tri_edge = triang.get_neighbor_edge(tri, edge);
900896
follow_interior(contour_line, tri_edge, false, level, on_upper);
901897

@@ -1434,10 +1430,10 @@ TrapezoidMapTriFinder::initialize()
14341430

14351431
// Set up edges array.
14361432
// First the bottom and top edges of the enclosing rectangle.
1437-
_edges.push_back(Edge(&_points[npoints], &_points[npoints+1], -1, -1,
1438-
nullptr, nullptr));
1439-
_edges.push_back(Edge(&_points[npoints+2], &_points[npoints+3], -1, -1,
1440-
nullptr, nullptr));
1433+
_edges.emplace_back(&_points[npoints], &_points[npoints+1], -1, -1,
1434+
nullptr, nullptr);
1435+
_edges.emplace_back(&_points[npoints+2], &_points[npoints+3], -1, -1,
1436+
nullptr, nullptr);
14411437

14421438
// Add all edges in the triangulation that point to the right. Do not
14431439
// explicitly include edges that point to the left as the neighboring
@@ -1456,11 +1452,12 @@ TrapezoidMapTriFinder::initialize()
14561452
const Point* neighbor_point_below = (neighbor.tri == -1) ?
14571453
nullptr : _points + triang.get_triangle_point(
14581454
neighbor.tri, (neighbor.edge+2)%3);
1459-
_edges.push_back(Edge(start, end, neighbor.tri, tri,
1460-
neighbor_point_below, other));
1455+
_edges.emplace_back(start, end, neighbor.tri, tri,
1456+
neighbor_point_below, other);
1457+
}
1458+
else if (neighbor.tri == -1) {
1459+
_edges.emplace_back(end, start, tri, -1, other, nullptr);
14611460
}
1462-
else if (neighbor.tri == -1)
1463-
_edges.push_back(Edge(end, start, tri, -1, other, nullptr));
14641461

14651462
// Set triangle associated with start point if not already set.
14661463
if (start->tri == -1)

0 commit comments

Comments
 (0)