lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 49f4289 3/3: Modernize for statements


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 49f4289 3/3: Modernize for statements
Date: Thu, 19 Jan 2017 23:02:41 +0000 (UTC)

branch: master
commit 49f4289488518c0a1289bc155dcccb3e28016220
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Modernize for statements
---
 ledger.cpp                 |   38 +++++++-------------
 ledger_base.cpp            |   86 ++++++++++++--------------------------------
 ledger_text_formats.cpp    |   39 +++++++++-----------
 ledger_xml_io.cpp          |   18 +++++-----
 multiple_cell_document.cpp |   37 +++++++++----------
 5 files changed, 77 insertions(+), 141 deletions(-)

diff --git a/ledger.cpp b/ledger.cpp
index 2248c00..4f3b2c0 100644
--- a/ledger.cpp
+++ b/ledger.cpp
@@ -134,14 +134,10 @@ void Ledger::SetRunBases(int length)
             }
         }
 
-    for
-        (ledger_map_t::iterator p = l_map_rep.begin()
-        ;p != l_map_rep.end()
-        ;++p
-        )
+    for(auto& i : l_map_rep)
         {
-        ledger_map_t::key_type const& key = (*p).first;
-        ledger_map_t::mapped_type& data = (*p).second;
+        ledger_map_t::key_type const& key = i.first;
+        ledger_map_t::mapped_type& data = i.second;
 
         run_bases_.push_back(key);
         data.set_run_basis(key);
@@ -312,13 +308,9 @@ int Ledger::GetMaxLength() const
     ledger_map_t const& l_map_rep = ledger_map_->held();
     double max_length = 0.0;
 
-    for
-        (ledger_map_t::const_iterator lmci = l_map_rep.begin()
-        ;lmci != l_map_rep.end()
-        ;++lmci
-        )
+    for(auto const& i : l_map_rep)
         {
-        max_length = std::max(max_length, (*lmci).second.LapseYear);
+        max_length = std::max(max_length, i.second.LapseYear);
         }
     return static_cast<int>(max_length);
 }
@@ -331,18 +323,16 @@ void Ledger::AutoScale()
     double mult = ledger_invariant_->DetermineScaleFactor();
 
     ledger_map_t& l_map_rep = ledger_map_->held_;
-    ledger_map_t::const_iterator lmci = l_map_rep.begin();
-    for(;lmci != l_map_rep.end(); ++lmci)
+    for(auto const& i : l_map_rep)
         {
-        mult = std::min(mult, (*lmci).second.DetermineScaleFactor());
+        mult = std::min(mult, i.second.DetermineScaleFactor());
         }
 
     ledger_invariant_->ApplyScaleFactor(mult);
 
-    ledger_map_t::iterator lmi = l_map_rep.begin();
-    for(;lmi != l_map_rep.end(); ++lmi)
+    for(auto& i : l_map_rep)
         {
-        (*lmi).second.ApplyScaleFactor(mult);
+        i.second.ApplyScaleFactor(mult);
         }
 }
 
@@ -352,10 +342,9 @@ unsigned int Ledger::CalculateCRC() const
     CRC crc;
     ledger_invariant_->UpdateCRC(crc);
     ledger_map_t const& l_map_rep = ledger_map_->held();
-    ledger_map_t::const_iterator lmci = l_map_rep.begin();
-    for(;lmci != l_map_rep.end(); ++lmci)
+    for(auto const& i : l_map_rep)
         {
-        (*lmci).second.UpdateCRC(crc);
+        i.second.UpdateCRC(crc);
         }
 
     return crc.value();
@@ -366,10 +355,9 @@ void Ledger::Spew(std::ostream& os) const
 {
     ledger_invariant_->Spew(os);
     ledger_map_t const& l_map_rep = ledger_map_->held();
-    ledger_map_t::const_iterator lmci = l_map_rep.begin();
-    for(;lmci != l_map_rep.end(); ++lmci)
+    for(auto const& i : l_map_rep)
         {
-        (*lmci).second.Spew(os);
+        i.second.Spew(os);
         }
 }
 
diff --git a/ledger_base.cpp b/ledger_base.cpp
index 273e646..31eeb25 100644
--- a/ledger_base.cpp
+++ b/ledger_base.cpp
@@ -85,22 +85,14 @@ void LedgerBase::Alloc()
 //============================================================================
 void LedgerBase::Initialize(int a_Length)
 {
-    for
-        (double_vector_map::iterator i = AllVectors.begin()
-        ;i != AllVectors.end()
-        ;i++
-        )
+    for(auto& i : AllVectors)
         {
-        (*i).second->assign(a_Length, 0.0);
+        i.second->assign(a_Length, 0.0);
         }
 
-    for
-        (scalar_map::iterator i = AllScalars.begin()
-        ;i != AllScalars.end()
-        ;i++
-        )
+    for(auto& i : AllScalars)
         {
-        *(*i).second = 0.0;
+        *i.second = 0.0;
         }
 }
 
@@ -333,13 +325,9 @@ double LedgerBase::DetermineScaleFactor() const
     double min_val = 0.0;
     double max_val = 0.0;
 
-    for
-        (double_vector_map::const_iterator svmi = ScalableVectors.begin()
-        ;svmi != ScalableVectors.end()
-        ;svmi++
-        )
+    for(auto const& i : ScalableVectors)
         {
-        minmax<double> extrema(*(*svmi).second);
+        minmax<double> extrema(*i.second);
         min_val = std::min(min_val, extrema.minimum());
         max_val = std::max(max_val, extrema.maximum());
         }
@@ -461,14 +449,10 @@ void LedgerBase::ApplyScaleFactor(double a_Mult)
 
     // TODO ?? Would be clearer with bind1st.
     std::vector<double>M(GetLength(), m_scaling_factor);
-    for
-        (double_vector_map::iterator svmi = ScalableVectors.begin()
-        ;svmi != ScalableVectors.end()
-        ;svmi++
-        )
+    for(auto& i : ScalableVectors)
         {
-        // ET !! *(*svmi).second *= M;
-        std::vector<double>& v = *(*svmi).second;
+        // ET !! *i.second *= M;
+        std::vector<double>& v = *i.second;
         std::transform
             (v.begin()
             ,v.end()
@@ -495,31 +479,19 @@ double LedgerBase::ScaleFactor() const
 void LedgerBase::UpdateCRC(CRC& crc) const
 {
 // TODO ?? std::transform() might be cleaner.
-    for
-        (double_vector_map::const_iterator vmi = AllVectors.begin()
-        ;vmi != AllVectors.end()
-        ;vmi++
-        )
+    for(auto const& i : AllVectors)
         {
-        crc += *(*vmi).second;
+        crc += *i.second;
         }
 
-    for
-        (scalar_map::const_iterator sci = AllScalars.begin()
-        ;sci != AllScalars.end()
-        ;sci++
-        )
+    for(auto const& i : AllScalars)
         {
-        crc += *(*sci).second;
+        crc += *i.second;
         }
 
-    for
-        (string_map::const_iterator sti = Strings.begin()
-        ;sti != Strings.end()
-        ;sti++
-        )
+    for(auto const& i : Strings)
         {
-        crc += *(*sti).second;
+        crc += *i.second;
         }
 }
 
@@ -528,39 +500,27 @@ void LedgerBase::Spew(std::ostream& os) const
 {
     static int const prec = max_stream_precision();
 
-    for
-        (double_vector_map::const_iterator vmi = AllVectors.begin()
-        ;vmi != AllVectors.end()
-        ;vmi++
-        )
+    for(auto const& i : AllVectors)
         {
-        SpewVector(os, (*vmi).first, *(*vmi).second);
+        SpewVector(os, i.first, *i.second);
         }
 
-    for
-        (scalar_map::const_iterator sci = AllScalars.begin()
-        ;sci != AllScalars.end()
-        ;sci++
-        )
+    for(auto const& i : AllScalars)
         {
         os
-            << (*sci).first
+            << i.first
             << "=="
-            << std::setprecision(prec) << *(*sci).second
+            << std::setprecision(prec) << *i.second
             << '\n'
             ;
         }
 
-    for
-        (string_map::const_iterator sti = Strings.begin()
-        ;sti != Strings.end()
-        ;sti++
-        )
+    for(auto const& i : Strings)
         {
         os
-            << (*sti).first
+            << i.first
             << "=="
-            << *(*sti).second
+            << *i.second
             << '\n'
             ;
         }
diff --git a/ledger_text_formats.cpp b/ledger_text_formats.cpp
index 14aad6b..c8a7cf5 100644
--- a/ledger_text_formats.cpp
+++ b/ledger_text_formats.cpp
@@ -321,15 +321,14 @@ std::string 
calculation_summary_formatter::format_as_html() const
         }
 
     std::string const width = value_cast<std::string>(100 / columns_.size());
-    typedef std::vector<std::string>::const_iterator vsci;
     oss
         << "<hr>\n"
         << "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" 
width=\"100%\">\n"
         << "<tr align=\"right\">\n"
         ;
-    for(vsci i = columns_.begin(); i != columns_.end(); ++i)
+    for(auto const& i : columns_)
         {
-        ledger_metadata const& z = map_lookup(ledger_metadata_map(), *i);
+        ledger_metadata const& z = map_lookup(ledger_metadata_map(), i);
         oss << "<td valign=\"bottom\" width=\"" << width << "%\">" << 
z.legend_ << " </td>\n";
         }
     oss << "</tr>\n";
@@ -341,11 +340,11 @@ std::string 
calculation_summary_formatter::format_as_html() const
             oss << "<tr><td><br></td></tr>\n";
             }
         oss << "<tr align=\"right\">\n";
-        for(vsci i = columns_.begin(); i != columns_.end(); ++i)
+        for(auto const& i : columns_)
             {
-            ledger_metadata const& z = map_lookup(ledger_metadata_map(), *i);
+            ledger_metadata const& z = map_lookup(ledger_metadata_map(), i);
             std::string s = ledger_format
-                (numeric_vector(ledger_, *i)[j]
+                (numeric_vector(ledger_, i)[j]
                 ,std::make_pair(z.decimals_, z.style_)
                 );
             oss << "<td nowrap>&nbsp;&nbsp;&nbsp;" << s << "</td>\n";
@@ -410,22 +409,21 @@ std::string 
calculation_summary_formatter::format_as_tsv() const
         }
 
     std::string const width = value_cast<std::string>(100 / columns_.size());
-    typedef std::vector<std::string>::const_iterator vsci;
     oss << '\n';
-    for(vsci i = columns_.begin(); i != columns_.end(); ++i)
+    for(auto const& i : columns_)
         {
-        ledger_metadata const& z = map_lookup(ledger_metadata_map(), *i);
+        ledger_metadata const& z = map_lookup(ledger_metadata_map(), i);
         oss << z.legend_ << '\t';
         }
     oss << '\n';
 
     for(int j = 0; j < max_length_; ++j)
         {
-        for(vsci i = columns_.begin(); i != columns_.end(); ++i)
+        for(auto const& i : columns_)
             {
-            ledger_metadata const& z = map_lookup(ledger_metadata_map(), *i);
+            ledger_metadata const& z = map_lookup(ledger_metadata_map(), i);
             std::string s = ledger_format
-                (numeric_vector(ledger_, *i)[j]
+                (numeric_vector(ledger_, i)[j]
                 ,std::make_pair(z.decimals_, z.style_)
                 );
             oss << s << '\t';
@@ -583,10 +581,9 @@ void PrintCellTabDelimited
         (cheaders
         ,cheaders + lmi_array_size(cheaders)
         );
-    typedef std::vector<std::string>::const_iterator vsi;
-    for(vsi i = sheaders.begin(); i != sheaders.end(); ++i)
+    for(auto const& i : sheaders)
         {
-        os << *i << '\t';
+        os << i << '\t';
         }
     os << "\n\n";
 
@@ -754,10 +751,9 @@ void PrintRosterHeaders(std::string const& file_name)
         (cheaders
         ,cheaders + lmi_array_size(cheaders)
         );
-    typedef std::vector<std::string>::const_iterator vsi;
-    for(vsi i = sheaders.begin(); i != sheaders.end(); ++i)
+    for(auto const& i : sheaders)
         {
-        os << *i << '\t';
+        os << i << '\t';
         }
     os << "\n\n";
 
@@ -1035,9 +1031,8 @@ void FlatTextLedgerPrinter::PrintNumericalSummary() const
 
     int summary_rows[] = {4, 9, 19, std::min(99, 69 - 
value_cast<int>(invar().Age))};
 
-    for(int j = 0; j < static_cast<int>(sizeof summary_rows / sizeof(int)); 
++j)
+    for(auto const& row : summary_rows)
         {
-        int row = summary_rows[j];
         // Skip row if it doesn't exist. For instance, if issue age is
         // 85 and maturity age is 100, then there is no twentieth duration.
         if(ledger_.GetMaxLength() < 1 + row)
@@ -1226,9 +1221,9 @@ std::vector<std::string> ledger_format
     )
 {
     std::vector<std::string> sv;
-    for(unsigned int j = 0; j < dv.size(); ++j)
+    for(auto const& i : dv)
         {
-        sv.push_back(ledger_format(dv[j], f));
+        sv.push_back(ledger_format(i, f));
         }
     return sv;
 }
diff --git a/ledger_xml_io.cpp b/ledger_xml_io.cpp
index d2b163e..498447b 100644
--- a/ledger_xml_io.cpp
+++ b/ledger_xml_io.cpp
@@ -752,14 +752,12 @@ void Ledger::write(xml::element& x) const
 
     // That was the tricky part. Now it's all downhill.
 
-    ledger_map_t const& l_map_rep = ledger_map_->held();
-    typedef ledger_map_t::const_iterator lmci;
-    for(lmci i = l_map_rep.begin(); i != l_map_rep.end(); i++)
+    for(auto const& i : ledger_map_->held())
         {
-        std::string suffix = suffixes[i->first];
+        std::string suffix = suffixes[i.first];
         for
-            (scalar_map::const_iterator j = i->second.AllScalars.begin()
-            ;j != i->second.AllScalars.end()
+            (scalar_map::const_iterator j = i.second.AllScalars.begin()
+            ;j != i.second.AllScalars.end()
             ;++j
             )
             {
@@ -768,16 +766,16 @@ void Ledger::write(xml::element& x) const
                 stringscalars[j->first + suffix] = ledger_format(*j->second, 
format_map[j->first]);
             }
         for
-            (string_map::const_iterator j = i->second.Strings.begin()
-            ;j != i->second.Strings.end()
+            (string_map::const_iterator j = i.second.Strings.begin()
+            ;j != i.second.Strings.end()
             ;++j
             )
             {
             strings[j->first + suffix] = j->second;
             }
         for
-            (double_vector_map::const_iterator j = i->second.AllVectors.begin()
-            ;j != i->second.AllVectors.end()
+            (double_vector_map::const_iterator j = i.second.AllVectors.begin()
+            ;j != i.second.AllVectors.end()
             ;++j
             )
             {
diff --git a/multiple_cell_document.cpp b/multiple_cell_document.cpp
index 68b5053..acb34a2 100644
--- a/multiple_cell_document.cpp
+++ b/multiple_cell_document.cpp
@@ -142,24 +142,22 @@ void multiple_cell_document::parse(xml_lmi::dom_parser 
const& parser)
     class_parms_.clear();
     cell_parms_ .clear();
 
-    xml::const_nodes_view const elements(root.elements());
-    typedef xml::const_nodes_view::const_iterator cnvi;
     Input cell;
     int counter = 0;
-    for(cnvi i = elements.begin(); i != elements.end(); ++i)
+    for(auto const& i : root.elements())
         {
-        std::string const tag(i->get_name());
+        std::string const tag(i.get_name());
         std::vector<Input>& v
             ( ("case_default"     == tag) ? case_parms_
             : ("class_defaults"   == tag) ? class_parms_
             : ("particular_cells" == tag) ? cell_parms_
             : hurl<std::vector<Input> >("Unexpected element '" + tag + "'.")
             );
-        xml::const_nodes_view const subelements(i->elements());
+        xml::const_nodes_view const subelements(i.elements());
         v.reserve(std::distance(subelements.begin(), subelements.end()));
-        for(cnvi j = subelements.begin(); j != subelements.end(); ++j)
+        for(auto const& j : subelements)
             {
-            *j >> cell;
+            j >> cell;
             v.push_back(cell);
             status() << "Read " << ++counter << " cells." << std::flush;
             }
@@ -177,6 +175,9 @@ void multiple_cell_document::parse_v0(xml_lmi::dom_parser 
const& parser)
     Input temp;
 
     xml::const_nodes_view const elements(root.elements());
+    // With C++14 we would write
+    //   auto i = elements.cbegin();
+    // instead of the next two lines.
     typedef xml::const_nodes_view::const_iterator cnvi;
     cnvi i = elements.begin();
 
@@ -383,20 +384,16 @@ bool 
multiple_cell_document::data_source_is_external(xml::document const& d) con
     // INPUT !! Remove "InforceDataSource" and the following code when
     // external systems are updated to use the "data_source" attribute.
 
-    typedef xml::const_nodes_view::const_iterator cnvi;
-
     // Tag names vary: {"case_default", "class_defaults", "particular_cells"}.
     xml::const_nodes_view const i_nodes(root.elements());
     LMI_ASSERT(3 == i_nodes.size());
-    for(cnvi i = i_nodes.begin(); i != i_nodes.end(); ++i)
+    for(auto const& i : i_nodes)
         {
-        xml::const_nodes_view const j_nodes(i->elements("cell"));
-        for(cnvi j = j_nodes.begin(); j != j_nodes.end(); ++j)
+        for(auto const& j : i.elements("cell"))
             {
-            xml::const_nodes_view const 
k_nodes(j->elements("InforceDataSource"));
-            for(cnvi k = k_nodes.begin(); k != k_nodes.end(); ++k)
+            for(auto const& k : j.elements("InforceDataSource"))
                 {
-                std::string s(xml_lmi::get_content(*k));
+                std::string s(xml_lmi::get_content(k));
                 if("0" != s && "1" != s)
                     {
                     return true;
@@ -479,20 +476,18 @@ void multiple_cell_document::write(std::ostream& os) const
     xml::node::iterator case_i = root.insert(case_default);
     case_parms_[0].write(*case_i);
 
-    typedef std::vector<Input>::const_iterator svii;
-
     xml::element class_defaults("class_defaults");
     xml::node::iterator classes_i = root.insert(class_defaults);
-    for(svii i = class_parms_.begin(); i != class_parms_.end(); ++i)
+    for(auto const& i : class_parms_)
         {
-        i->write(*classes_i);
+        i.write(*classes_i);
         }
 
     xml::element particular_cells("particular_cells");
     xml::node::iterator cells_i = root.insert(particular_cells);
-    for(svii i = cell_parms_.begin(); i != cell_parms_.end(); ++i)
+    for(auto const& i : cell_parms_)
         {
-        i->write(*cells_i);
+        i.write(*cells_i);
         }
 
     os << document;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]