lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [4954] Rewrite reshape()


From: Greg Chicares
Subject: [lmi-commits] [4954] Rewrite reshape()
Date: Mon, 17 May 2010 16:52:38 +0000

Revision: 4954
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=4954
Author:   chicares
Date:     2010-05-17 16:52:38 +0000 (Mon, 17 May 2010)
Log Message:
-----------
Rewrite reshape()

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/dbvalue.cpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2010-05-16 21:25:43 UTC (rev 4953)
+++ lmi/trunk/ChangeLog 2010-05-17 16:52:38 UTC (rev 4954)
@@ -25805,3 +25805,14 @@
   http://lists.nongnu.org/archive/html/lmi/2010-05/msg00023.html
 et seqq.
 
+20100516T2125Z <address@hidden> [706]
+
+  dbvalue.cpp
+  dbvalue.hpp
+Refactor.
+
+20100517T1652Z <address@hidden> [704]
+
+  dbvalue.cpp
+Rewrite reshape().
+

Modified: lmi/trunk/dbvalue.cpp
===================================================================
--- lmi/trunk/dbvalue.cpp       2010-05-16 21:25:43 UTC (rev 4953)
+++ lmi/trunk/dbvalue.cpp       2010-05-17 16:52:38 UTC (rev 4954)
@@ -32,8 +32,9 @@
 #include "assert_lmi.hpp"
 #include "contains.hpp"
 #include "dbnames.hpp"
+#include "et_vector.hpp"
 #include "handle_exceptions.hpp"
-#include "math_functors.hpp" // greater_of(), lesser_of()
+#include "math_functors.hpp" // lesser_of()
 #include "print_matrix.hpp"
 #include "value_cast.hpp"
 #include "xml_serialize.hpp"
@@ -111,82 +112,50 @@
 {
 }
 
-void database_entity::reshape(std::vector<int> const& dims)
+/// Change dimensions.
+///
+/// Preconditions:
+///   - argument specifies the expected number of axes;
+///   - each axis in the argument has a permissible value;
+///   - data size would not be excessive.
+///
+/// Postconditions: all ctor postconditions are satisfied.
+
+void database_entity::reshape(std::vector<int> const& new_dims)
 {
-    LMI_ASSERT(e_number_of_axes == dims.size());
-    // Create a new instance of this class having the same
-    // key but the desired dimensions.
-    std::vector<double> new_data
-        (
-        std::accumulate
-            (dims.begin()
-            ,dims.end()
-            ,1
-            ,std::multiplies<int>()
-            )
-        );
-    database_entity new_object
-        (key()
-        ,dims
-        ,new_data
-        );
+    LMI_ASSERT(e_number_of_axes == new_dims.size());
+    LMI_ASSERT(1 == new_dims[0] || e_max_dim_gender    == new_dims[0]);
+    LMI_ASSERT(1 == new_dims[1] || e_max_dim_class     == new_dims[1]);
+    LMI_ASSERT(1 == new_dims[2] || e_max_dim_smoking   == new_dims[2]);
+    LMI_ASSERT(1 == new_dims[3] || e_max_dim_issue_age == new_dims[3]);
+    LMI_ASSERT(1 == new_dims[4] || e_max_dim_uw_basis  == new_dims[4]);
+    LMI_ASSERT(1 == new_dims[5] || e_max_dim_state     == new_dims[5]);
+    LMI_ASSERT(1 <= new_dims[6] && new_dims[6] <= e_max_dim_duration);
 
-    // ET !! std::vector<int> max_dims_used = max(axis_lengths_, dims);
-    // ...and then expunge this comment:
-    // greater length of src or dst along each axis
-    std::vector<int> max_dims_used(e_number_of_axes);
-    std::transform
-        (axis_lengths_.begin()
-        ,axis_lengths_.end()
-        ,dims.begin()
-        ,max_dims_used.begin()
-        ,greater_of<int>()
-        );
-    // TODO ?? Oops--erase above std::transform() call--want only dst axes.
-    max_dims_used = dims;
-
     // Number of times we'll go through the assignment loop.
-    // TODO ?? prolly should use max_dims_used instead of dims here (they're 
the same).
-    int n_iter = std::accumulate
-        (dims.begin()
-        ,dims.end()
-        ,1
-        ,std::multiplies<int>()
-        );
+    int n_iter = getndata(new_dims);
 
-    // ET !! std::vector<int> dst_max_idx = dims - 1;
-    // ...and then expunge this comment:
-    // max index of dst along each axis
-    std::vector<int> dst_max_idx(dims);
-    std::transform
-        (dst_max_idx.begin()
-        ,dst_max_idx.end()
-        ,dst_max_idx.begin()
-        ,std::bind2nd(std::minus<int>(), 1)
-        );
-    // ET !! std::vector<int> src_max_idx = axis_lengths_ - 1;
-    // ...and then expunge this comment:
-    // max index of src along each axis
-    std::vector<int> src_max_idx(axis_lengths_);
-    std::transform
-        (src_max_idx.begin()
-        ,src_max_idx.end()
-        ,src_max_idx.begin()
-        ,std::bind2nd(std::minus<int>(), 1)
-        );
+    // Create a new instance of this class having the same key but the
+    // desired dimensions, for convenient use of operator[]().
+    std::vector<double> new_data(n_iter);
+    database_entity new_object(key(), new_dims, new_data);
 
-    // indexes new_object
-    std::vector<int> dst_idx(e_number_of_axes);
-    // indexes '*this'
-    std::vector<int> src_idx(e_number_of_axes);
+    std::vector<int> dst_max_idx(e_number_of_axes);
+    assign(dst_max_idx, new_dims - 1);
 
+    std::vector<int> src_max_idx(e_number_of_axes);
+    assign(src_max_idx, axis_lengths_ - 1);
+
+    std::vector<int> dst_idx(e_number_of_axes); // indexes new_object
+    std::vector<int> src_idx(e_number_of_axes); // indexes '*this'
+
     std::vector<int> working_idx(e_number_of_axes);
     for(int j = 0; j < n_iter; j++)
         {
         int z = j;
-        std::vector<int>::const_iterator i = max_dims_used.begin();
+        std::vector<int>::const_iterator i = new_dims.begin();
         std::vector<int>::iterator w = working_idx.begin();
-        while(i != max_dims_used.end())
+        while(i != new_dims.end())
             {
             LMI_ASSERT(0 != *i);
             *w = z % *i;
@@ -196,30 +165,15 @@
             }
         LMI_ASSERT(0 == z);
 
-        // ET !! dst_idx = min(working_idx, dst_max_idx)
-        // ET !! src_idx = min(working_idx, src_max_idx)
-        // ...and then expunge this comment:
         // limit dst and source indexes to those that actually vary
-        std::transform
-            (working_idx.begin()
-            ,working_idx.end()
-            ,dst_max_idx.begin()
-            ,dst_idx.begin()
-            ,lesser_of<int>()
-            );
-        std::transform
-            (working_idx.begin()
-            ,working_idx.end()
-            ,src_max_idx.begin()
-            ,src_idx.begin()
-            ,lesser_of<int>()
-            );
+        assign(dst_idx, apply_binary(lesser_of<int>(), working_idx, 
dst_max_idx));
+        assign(src_idx, apply_binary(lesser_of<int>(), working_idx, 
src_max_idx));
         new_object[dst_idx] = operator[](src_idx);
         }
 
-// erase    (*this) = new_object;
-    axis_lengths_ = dims;
-    data_values_ = new_object.data_values_;
+    axis_lengths_ = new_dims;
+    data_values_  = new_object.data_values_;
+    assert_invariants();
 }
 
 /// Indexing operator for reshape() and product editor only.




reply via email to

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