Skip to content

Commit 2e9dae2

Browse files
committed
Use C++11 range-based loops in extensions
Also use `auto` and structured binding for iterators. This is based on running `clang-tidy` with the `modernize-loop-convert` and `modernize-use-auto` checks. Note though that I did not (yet) apply any `auto` suggestions that were not `for` loops.
1 parent cc82d4f commit 2e9dae2

File tree

4 files changed

+65
-78
lines changed

4 files changed

+65
-78
lines changed

src/_backend_agg_basic_types.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ class Dashes
5959
void dash_to_stroke(T &stroke, double dpi, bool isaa)
6060
{
6161
double scaleddpi = dpi / 72.0;
62-
for (dash_t::const_iterator i = dashes.begin(); i != dashes.end(); ++i) {
63-
double val0 = i->first;
64-
double val1 = i->second;
62+
for (auto [val0, val1] : dashes) {
6563
val0 = val0 * scaleddpi;
6664
val1 = val1 * scaleddpi;
6765
if (!isaa) {

src/_path.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -602,24 +602,20 @@ struct ygt : public bisecty
602602
template <class Filter>
603603
inline void clip_to_rect_one_step(const Polygon &polygon, Polygon &result, const Filter &filter)
604604
{
605-
double sx, sy, px, py, bx, by;
606605
bool sinside, pinside;
607606
result.clear();
608607

609608
if (polygon.size() == 0) {
610609
return;
611610
}
612611

613-
sx = polygon.back().x;
614-
sy = polygon.back().y;
615-
for (Polygon::const_iterator i = polygon.begin(); i != polygon.end(); ++i) {
616-
px = i->x;
617-
py = i->y;
618-
612+
auto [sx, sy] = polygon.back();
613+
for (auto [px, py] : polygon) {
619614
sinside = filter.is_inside(sx, sy);
620615
pinside = filter.is_inside(px, py);
621616

622617
if (sinside ^ pinside) {
618+
double bx, by;
623619
filter.bisect(sx, sy, px, py, &bx, &by);
624620
result.push_back(XY(bx, by));
625621
}

src/ft2font.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ FT2Font::FT2Font(FT_Open_Args &open_args,
293293

294294
FT2Font::~FT2Font()
295295
{
296-
for (size_t i = 0; i < glyphs.size(); i++) {
297-
FT_Done_Glyph(glyphs[i]);
296+
for (auto & glyph : glyphs) {
297+
FT_Done_Glyph(glyph);
298298
}
299299

300300
if (face) {
@@ -308,16 +308,16 @@ void FT2Font::clear()
308308
bbox.xMin = bbox.yMin = bbox.xMax = bbox.yMax = 0;
309309
advance = 0;
310310

311-
for (size_t i = 0; i < glyphs.size(); i++) {
312-
FT_Done_Glyph(glyphs[i]);
311+
for (auto & glyph : glyphs) {
312+
FT_Done_Glyph(glyph);
313313
}
314314

315315
glyphs.clear();
316316
glyph_to_font.clear();
317317
char_to_font.clear();
318318

319-
for (size_t i = 0; i < fallbacks.size(); i++) {
320-
fallbacks[i]->clear();
319+
for (auto & fallback : fallbacks) {
320+
fallback->clear();
321321
}
322322
}
323323

@@ -331,8 +331,8 @@ void FT2Font::set_size(double ptsize, double dpi)
331331
FT_Matrix transform = { 65536 / hinting_factor, 0, 0, 65536 };
332332
FT_Set_Transform(face, &transform, 0);
333333

334-
for (size_t i = 0; i < fallbacks.size(); i++) {
335-
fallbacks[i]->set_size(ptsize, dpi);
334+
for (auto & fallback : fallbacks) {
335+
fallback->set_size(ptsize, dpi);
336336
}
337337
}
338338

@@ -393,8 +393,8 @@ int FT2Font::get_kerning(FT_UInt left, FT_UInt right, FT_Kerning_Mode mode,
393393
void FT2Font::set_kerning_factor(int factor)
394394
{
395395
kerning_factor = factor;
396-
for (size_t i = 0; i < fallbacks.size(); i++) {
397-
fallbacks[i]->set_kerning_factor(factor);
396+
for (auto & fallback : fallbacks) {
397+
fallback->set_kerning_factor(factor);
398398
}
399399
}
400400

@@ -594,8 +594,8 @@ bool FT2Font::load_char_with_fallback(FT2Font *&ft_object_with_glyph,
594594
return true;
595595
}
596596
else {
597-
for (size_t i = 0; i < fallbacks.size(); ++i) {
598-
bool was_found = fallbacks[i]->load_char_with_fallback(
597+
for (auto & fallback : fallbacks) {
598+
bool was_found = fallback->load_char_with_fallback(
599599
ft_object_with_glyph, final_glyph_index, parent_glyphs,
600600
parent_char_to_font, parent_glyph_to_font, charcode, flags,
601601
charcode_error, glyph_error, glyph_seen_fonts, override);
@@ -674,14 +674,14 @@ void FT2Font::draw_glyphs_to_bitmap(bool antialiased)
674674

675675
image.resize(width, height);
676676

677-
for (size_t n = 0; n < glyphs.size(); n++) {
677+
for (auto & glyph : glyphs) {
678678
FT_Error error = FT_Glyph_To_Bitmap(
679-
&glyphs[n], antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
679+
&glyph, antialiased ? FT_RENDER_MODE_NORMAL : FT_RENDER_MODE_MONO, 0, 1);
680680
if (error) {
681681
throw_ft_error("Could not convert glyph to bitmap", error);
682682
}
683683

684-
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyphs[n];
684+
FT_BitmapGlyph bitmap = (FT_BitmapGlyph)glyph;
685685
// now, draw to our target surface (convert position)
686686

687687
// bitmap left and top in pixel, string bbox in subpixel

src/tri/_tri.cpp

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ void ContourLine::push_back(const XY& point)
184184
void ContourLine::write() const
185185
{
186186
std::cout << "ContourLine of " << size() << " points:";
187-
for (const_iterator it = begin(); it != end(); ++it)
188-
std::cout << ' ' << *it;
187+
for (const auto & it : *this) {
188+
std::cout << ' ' << it;
189+
}
189190
std::cout << std::endl;
190191
}
191192

@@ -194,8 +195,9 @@ void ContourLine::write() const
194195
void write_contour(const Contour& contour)
195196
{
196197
std::cout << "Contour of " << contour.size() << " lines." << std::endl;
197-
for (Contour::const_iterator it = contour.begin(); it != contour.end(); ++it)
198-
it->write();
198+
for (const auto & it : contour) {
199+
it.write();
200+
}
199201
}
200202

201203

@@ -264,7 +266,7 @@ void Triangulation::calculate_boundaries()
264266
// time, initialise the _tri_edge_to_boundary_map.
265267
while (!boundary_edges.empty()) {
266268
// Start of new boundary.
267-
BoundaryEdges::iterator it = boundary_edges.begin();
269+
auto it = boundary_edges.cbegin();
268270
int tri = it->tri;
269271
int edge = it->edge;
270272
_boundaries.push_back(Boundary());
@@ -321,9 +323,9 @@ void Triangulation::calculate_edges()
321323
auto edges = _edges.mutable_data();
322324

323325
int i = 0;
324-
for (EdgeSet::const_iterator it = edge_set.begin(); it != edge_set.end(); ++it) {
325-
edges[i++] = it->start;
326-
edges[i++] = it->end;
326+
for (const auto & it : edge_set) {
327+
edges[i++] = it.start;
328+
edges[i++] = it.end;
327329
}
328330
}
329331

@@ -351,8 +353,7 @@ void Triangulation::calculate_neighbors()
351353
for (edge = 0; edge < 3; ++edge) {
352354
int start = get_triangle_point(tri, edge);
353355
int end = get_triangle_point(tri, (edge+1)%3);
354-
EdgeToTriEdgeMap::iterator it =
355-
edge_to_tri_edge_map.find(Edge(end,start));
356+
const auto it = edge_to_tri_edge_map.find(Edge(end, start));
356357
if (it == edge_to_tri_edge_map.end()) {
357358
// No neighbor edge exists in the edge_to_tri_edge_map, so
358359
// add this edge to it.
@@ -464,10 +465,8 @@ void Triangulation::get_boundary_edge(const TriEdge& triEdge,
464465
int& edge) const
465466
{
466467
get_boundaries(); // Ensure _tri_edge_to_boundary_map has been created.
467-
TriEdgeToBoundaryMap::const_iterator it =
468-
_tri_edge_to_boundary_map.find(triEdge);
469-
assert(it != _tri_edge_to_boundary_map.end() &&
470-
"TriEdge is not on a boundary");
468+
const auto it = _tri_edge_to_boundary_map.find(triEdge);
469+
assert(it != _tri_edge_to_boundary_map.end() && "TriEdge is not on a boundary");
471470
boundary = it->second.boundary;
472471
edge = it->second.edge;
473472
}
@@ -587,13 +586,12 @@ void Triangulation::set_mask(const MaskArray& mask)
587586

588587
void Triangulation::write_boundaries() const
589588
{
590-
const Boundaries& bs = get_boundaries();
591-
std::cout << "Number of boundaries: " << bs.size() << std::endl;
592-
for (Boundaries::const_iterator it = bs.begin(); it != bs.end(); ++it) {
593-
const Boundary& b = *it;
594-
std::cout << " Boundary of " << b.size() << " points: ";
595-
for (Boundary::const_iterator itb = b.begin(); itb != b.end(); ++itb) {
596-
std::cout << *itb << ", ";
589+
const Boundaries& boundaries = get_boundaries();
590+
std::cout << "Number of boundaries: " << boundaries.size() << std::endl;
591+
for (const auto & boundary : boundaries) {
592+
std::cout << " Boundary of " << boundary.size() << " points: ";
593+
for (const auto & point : boundary) {
594+
std::cout << point << ", ";
597595
}
598596
std::cout << std::endl;
599597
}
@@ -625,18 +623,18 @@ void TriContourGenerator::clear_visited_flags(bool include_boundaries)
625623

626624
// Initialise _boundaries_visited.
627625
_boundaries_visited.reserve(boundaries.size());
628-
for (Boundaries::const_iterator it = boundaries.begin();
629-
it != boundaries.end(); ++it)
630-
_boundaries_visited.push_back(BoundaryVisited(it->size()));
626+
for (const auto & boundary : boundaries) {
627+
_boundaries_visited.push_back(BoundaryVisited(boundary.size()));
628+
}
631629

632630
// Initialise _boundaries_used.
633631
_boundaries_used = BoundariesUsed(boundaries.size());
634632
}
635633

636634
// Clear _boundaries_visited.
637-
for (BoundariesVisited::iterator it = _boundaries_visited.begin();
638-
it != _boundaries_visited.end(); ++it)
639-
std::fill(it->begin(), it->end(), false);
635+
for (auto & it : _boundaries_visited) {
636+
std::fill(it.begin(), it.end(), false);
637+
}
640638

641639
// Clear _boundaries_used.
642640
std::fill(_boundaries_used.begin(), _boundaries_used.end(), false);
@@ -672,12 +670,12 @@ py::tuple TriContourGenerator::contour_line_to_segs_and_kinds(const Contour& con
672670
CodeArray codes(codes_dims);
673671
unsigned char* codes_ptr = codes.mutable_data();
674672

675-
for (ContourLine::const_iterator it = contour_line.begin();
676-
it != contour_line.end(); ++it) {
677-
*segs_ptr++ = it->x;
678-
*segs_ptr++ = it->y;
679-
*codes_ptr++ = (it == contour_line.begin() ? MOVETO : LINETO);
673+
for (const auto & point : contour_line) {
674+
*segs_ptr++ = point.x;
675+
*segs_ptr++ = point.y;
676+
*codes_ptr++ = LINETO;
680677
}
678+
*codes.mutable_data(0) = MOVETO;
681679

682680
// Closed line loop has identical first and last (x, y) points.
683681
if (contour_line.size() > 1 &&
@@ -707,13 +705,11 @@ py::tuple TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
707705
// and they are returned in the Python lists vertices_list and codes_list
708706
// respectively.
709707

710-
Contour::const_iterator line;
711-
ContourLine::const_iterator point;
712-
713708
// Find total number of points in all contour lines.
714709
py::ssize_t n_points = 0;
715-
for (line = contour.begin(); line != contour.end(); ++line)
716-
n_points += static_cast<py::ssize_t>(line->size());
710+
for (const auto & line : contour) {
711+
n_points += static_cast<py::ssize_t>(line.size());
712+
}
717713

718714
// Create segs array for point coordinates.
719715
py::ssize_t segs_dims[2] = {n_points, 2};
@@ -725,15 +721,16 @@ py::tuple TriContourGenerator::contour_to_segs_and_kinds(const Contour& contour)
725721
CodeArray codes(codes_dims);
726722
unsigned char* codes_ptr = codes.mutable_data();
727723

728-
for (line = contour.begin(); line != contour.end(); ++line) {
729-
for (point = line->begin(); point != line->end(); point++) {
724+
for (const auto & line : contour) {
725+
for (auto point = line.cbegin(); point != line.cend(); point++) {
730726
*segs_ptr++ = point->x;
731727
*segs_ptr++ = point->y;
732-
*codes_ptr++ = (point == line->begin() ? MOVETO : LINETO);
728+
*codes_ptr++ = (point == line.cbegin() ? MOVETO : LINETO);
733729
}
734730

735-
if (line->size() > 1)
731+
if (line.size() > 1) {
736732
*(codes_ptr-1) = CLOSEPOLY;
733+
}
737734
}
738735

739736
py::list vertices_list(1);
@@ -787,13 +784,10 @@ void TriContourGenerator::find_boundary_lines(Contour& contour,
787784
// line to its end before continuing.
788785
const Triangulation& triang = _triangulation;
789786
const Boundaries& boundaries = get_boundaries();
790-
for (Boundaries::const_iterator it = boundaries.begin();
791-
it != boundaries.end(); ++it) {
792-
const Boundary& boundary = *it;
787+
for (const auto & boundary : boundaries) {
793788
bool startAbove, endAbove = false;
794-
for (Boundary::const_iterator itb = boundary.begin();
795-
itb != boundary.end(); ++itb) {
796-
if (itb == boundary.begin())
789+
for (auto itb = boundary.cbegin(); itb != boundary.cend(); ++itb) {
790+
if (itb == boundary.cbegin())
797791
startAbove = get_z(triang.get_triangle_point(*itb)) >= level;
798792
else
799793
startAbove = endAbove;
@@ -867,9 +861,10 @@ void TriContourGenerator::find_boundary_lines_filled(Contour& contour,
867861
if (z >= lower_level && z < upper_level) {
868862
contour.push_back(ContourLine());
869863
ContourLine& contour_line = contour.back();
870-
for (Boundary::size_type j = 0; j < boundary.size(); ++j)
864+
for (auto edge : boundary) {
871865
contour_line.push_back(triang.get_point_coords(
872-
triang.get_triangle_point(boundary[j])));
866+
triang.get_triangle_point(edge)));
867+
}
873868

874869
// Close polygon.
875870
contour_line.push_back(contour_line.front());
@@ -1638,9 +1633,7 @@ TrapezoidMapTriFinder::Node::assert_valid(bool tree_complete) const
16381633
{
16391634
#ifndef NDEBUG
16401635
// Check parents.
1641-
for (Parents::const_iterator it = _parents.begin();
1642-
it != _parents.end(); ++it) {
1643-
Node* parent = *it;
1636+
for (const auto & parent : _parents) {
16441637
assert(parent != this && "Cannot be parent of self");
16451638
assert(parent->has_child(this) && "Parent missing child");
16461639
}
@@ -1779,7 +1772,7 @@ TrapezoidMapTriFinder::Node::remove_parent(Node* parent)
17791772
{
17801773
assert(parent != 0 && "Null parent");
17811774
assert(parent != this && "Cannot be parent of self");
1782-
Parents::iterator it = std::find(_parents.begin(), _parents.end(), parent);
1775+
auto it = std::find(_parents.begin(), _parents.end(), parent);
17831776
assert(it != _parents.end() && "Parent not in collection");
17841777
_parents.erase(it);
17851778
return _parents.empty();

0 commit comments

Comments
 (0)