lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [lmi] master 3870f3f 3/6: Refactor for reuse (VZ)


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 3870f3f 3/6: Refactor for reuse (VZ)
Date: Wed, 8 Feb 2017 09:15:40 -0500 (EST)

branch: master
commit 3870f3f3c7b089f493e92ebec3f2f825e176d23e
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Refactor for reuse (VZ)
    
    See especially:
      http://lists.nongnu.org/archive/html/lmi/2017-02/msg00025.html
---
 input_sequence.cpp | 48 ++++++++++++++++--------------------------------
 input_sequence.hpp |  3 +++
 2 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/input_sequence.cpp b/input_sequence.cpp
index 6817aa7..0b8fd04 100644
--- a/input_sequence.cpp
+++ b/input_sequence.cpp
@@ -38,6 +38,7 @@
 #include <ostream>
 #include <sstream>
 #include <stdexcept>
+#include <type_traits>
 
 ValueInterval::ValueInterval()
     :value_number     (0.0)
@@ -810,44 +811,29 @@ InputSequence::InputSequence
 InputSequence::InputSequence(std::vector<double> const& v)
     :years_to_maturity_(v.size())
 {
-    ValueInterval dummy;
-    dummy.value_is_keyword = false;
-
-    double prior_value = v.empty() ? 0.0 : v.front();
-    double current_value = prior_value;
-
-    intervals_.push_back(dummy);
-    intervals_.back().value(current_value);
-
-    for(auto const& vi : v)
-        {
-        current_value = vi;
-        if(prior_value == current_value)
-            {
-            ++intervals_.back().end_duration;
-            }
-        else
-            {
-            int value_change_duration = intervals_.back().end_duration;
-            intervals_.push_back(dummy);
-            intervals_.back().value(current_value);
-            intervals_.back().begin_duration = value_change_duration;
-            intervals_.back().end_duration = ++value_change_duration;
-            prior_value = current_value;
-            }
-        }
-
+    initialize_from_vector(v);
     realize_vector();
 }
 
 InputSequence::InputSequence(std::vector<std::string> const& v)
     :years_to_maturity_(v.size())
 {
+    initialize_from_vector(v);
+    realize_vector();
+}
+
+template<typename T>
+void InputSequence::initialize_from_vector(std::vector<T> const& v)
+{
+    bool const T_is_double = std::is_same<T,double     >::value;
+    bool const T_is_string = std::is_same<T,std::string>::value;
+    static_assert(T_is_double || T_is_string, "");
+
     ValueInterval dummy;
-    dummy.value_is_keyword = true;
+    dummy.value_is_keyword = T_is_string;
 
-    std::string prior_value = v.empty() ? std::string() : v.front();
-    std::string current_value = prior_value;
+    T prior_value = v.empty() ? T() : v.front();
+    T current_value = prior_value;
 
     intervals_.push_back(dummy);
     intervals_.back().value(current_value);
@@ -869,8 +855,6 @@ InputSequence::InputSequence(std::vector<std::string> 
const& v)
             prior_value = current_value;
             }
         }
-
-    realize_vector();
 }
 
 InputSequence::InputSequence
diff --git a/input_sequence.hpp b/input_sequence.hpp
index aa93bdd..45bf337 100644
--- a/input_sequence.hpp
+++ b/input_sequence.hpp
@@ -285,6 +285,9 @@ class LMI_SO InputSequence
     std::vector<ValueInterval> const& interval_representation() const;
 
   private:
+    template<typename T>
+    void initialize_from_vector(std::vector<T> const&);
+
     void realize_vector();
 
     // Copies of ctor args that are identical to class SequenceParser's.



reply via email to

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