From 608870c2b4dca5a69ca710542da7f21e46ac3cd3 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Thu, 21 Sep 2023 13:46:09 +0200 Subject: [PATCH] More range-based loops --- src/ivoc/graph.cpp | 17 ++++++++--------- src/ivoc/scene.cpp | 21 ++++++--------------- src/ivoc/symdir.cpp | 7 ++++--- src/nrncvode/netcvode.cpp | 4 +--- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/ivoc/graph.cpp b/src/ivoc/graph.cpp index 9babcd0106..b073a0f350 100644 --- a/src/ivoc/graph.cpp +++ b/src/ivoc/graph.cpp @@ -1526,18 +1526,17 @@ GLabel* Graph::new_proto_label() const { } bool Graph::change_label(GLabel* glab, const char* text, GLabel* gl) { - GlyphIndex i, cnt = line_list_.size(); if (strcmp(glab->text(), text)) { - for (i = 0; i < cnt; ++i) { - if (line_list_[i]->label() == glab) { - if (!line_list_[i]->change_expr(text, &symlist_)) { + for (auto& line: line_list_) { + if (line->label() == glab) { + if (!line->change_expr(text, &symlist_)) { return false; } } } glab->text(text); } - i = glyph_index(glab); + GlyphIndex i = glyph_index(glab); if (glab->fixtype() != gl->fixtype()) { if (gl->fixed()) { glab->fixed(gl->scale()); @@ -1570,8 +1569,8 @@ void Graph::change_line_color(GPolyLine* glin) { } GlyphIndex Graph::glyph_index(const Glyph* gl) { - GlyphIndex i, cnt = count(); - for (i = 0; i < cnt; ++i) { + GlyphIndex cnt = count(); + for (GlyphIndex i = 0; i < cnt; ++i) { Glyph* g = ((GraphItem*) component(i))->body(); if (g == gl) { return i; @@ -1616,8 +1615,8 @@ void Graph::ascii_save(std::ostream& o) const { } if (lcnt) { o << lcnt << " addvar/addexpr lines:"; - for (i = 0; i < lcnt; ++i) { - o << " " << line_list_[i]->name(); + for (const auto& line: line_list_) { + o << " " << line->name(); } o << std::endl; } diff --git a/src/ivoc/scene.cpp b/src/ivoc/scene.cpp index a01187fcbd..18787b17d0 100644 --- a/src/ivoc/scene.cpp +++ b/src/ivoc/scene.cpp @@ -576,18 +576,14 @@ void Scene::draw(Canvas* canvas, const Allocation& a) const { canvas->pop_transform(); } } - GlyphIndex count = info_->size(); bool are_fixed = false; - for (GlyphIndex index = 0; index < count; ++index) { - SceneInfo& info = (*info_)[index]; + for (auto& info: *info_) { if (info.status_ & SceneInfoFixed) { are_fixed = true; } else if (info.glyph_ != NULL && (info.status_ & SceneInfoShowing)) { Allocation& a = info.allocation_; Extension b; b.set(canvas, a); - // printf("%d alloc %g %g %g %g\n", index, a.left(), a.bottom(), a.right(), a.top()); - // printf("%d exten %g %g %g %g\n", index, b.left(), b.bottom(), b.right(), b.top()); if (canvas->damaged(b)) { info.glyph_->draw(canvas, a); } @@ -602,8 +598,7 @@ void Scene::draw(Canvas* canvas, const Allocation& a) const { const Transformer& tv = XYView::current_draw_view()->s2o(); canvas->transform(tv); IfIdraw(pict(tv)); - for (GlyphIndex index = 0; index < count; ++index) { - SceneInfo& info = (*info_)[index]; + for (auto& info: *info_) { if ((info.status_ & SceneInfoFixed) && info.glyph_ != NULL && (info.status_ & SceneInfoShowing)) { Allocation a = info.allocation_; @@ -635,10 +630,8 @@ void Scene::print(Printer* canvas, const Allocation& a) const { if (background_ != NULL) { background_->print(canvas, a); } - GlyphIndex count = info_->size(); bool are_fixed = false; - for (GlyphIndex index = 0; index < count; ++index) { - SceneInfo& info = (*info_)[index]; + for (auto& info: *info_) { if (info.status_ & SceneInfoFixed) { are_fixed = true; } else if (info.glyph_ != NULL && (info.status_ & SceneInfoShowing)) { @@ -658,8 +651,7 @@ void Scene::print(Printer* canvas, const Allocation& a) const { // view_transform(canvas, 2, tv); const Transformer& tv = XYView::current_draw_view()->s2o(); canvas->transform(tv); - for (GlyphIndex index = 0; index < count; ++index) { - SceneInfo& info = (*info_)[index]; + for (auto& info: *info_) { if ((info.status_ & SceneInfoFixed) && info.glyph_ != NULL && (info.status_ & SceneInfoShowing)) { Allocation a = info.allocation_; @@ -780,9 +772,8 @@ void Scene::save_all(std::ostream& o) { Sprintf(buf, "objectvar scene_vector_[%ld]", scene_list->size()); o << buf << std::endl; } - long count = scene_list->size(); - for (long i = 0; i < count; ++i) { - (*scene_list)[i]->mark(false); + for (auto& scene: *scene_list) { + scene->mark(false); } } diff --git a/src/ivoc/symdir.cpp b/src/ivoc/symdir.cpp index bfc744dab7..46c55efbd9 100644 --- a/src/ivoc/symdir.cpp +++ b/src/ivoc/symdir.cpp @@ -203,6 +203,7 @@ SymDirectory::~SymDirectory() { delete item; } impl_->symbol_lists_.clear(); + impl_->symbol_lists_.shrink_to_fit(); if (impl_->obj_) { ObjObservable::Detach(impl_->obj_, impl_); } @@ -579,9 +580,9 @@ void SymDirectoryImpl::append(Object* ob) { symbol_lists_.push_back(new SymbolItem(ob)); } void SymDirectoryImpl::un_append(Object* ob) { - for (std::size_t i = 0; i < symbol_lists_.size(); ++i) { - if (symbol_lists_[i]->object() == ob) { - symbol_lists_[i]->no_object(); + for (auto& symbol: symbol_lists_) { + if (symbol->object() == ob) { + symbol->no_object(); break; } } diff --git a/src/nrncvode/netcvode.cpp b/src/nrncvode/netcvode.cpp index af4d597471..76c03a6c50 100644 --- a/src/nrncvode/netcvode.cpp +++ b/src/nrncvode/netcvode.cpp @@ -6431,7 +6431,6 @@ void NetCvode::vec_remove() { void NetCvode::playrec_setup() { long i, j; - long prlc = prl_->size(); fixed_record_->clear(); fixed_play_->clear(); if (gcv_) { @@ -6442,8 +6441,7 @@ void NetCvode::playrec_setup() { } } std::vector to_delete{}; - for (long iprl = 0; iprl < prlc; ++iprl) { - PlayRecord* pr = (*prl_)[iprl]; + for (auto& pr: *prl_) { if (!pr->pd_) { // Presumably the recorded value was invalidated elsewhere, e.g. it // was a voltage of a deleted node, or a range variable of a deleted