[PPL-devel] [GIT] ppl/ppl(sparse_matrices): Unlimited_Sparse_Row: swap elements instead of copy-constructing them, in combine_needs_second() .

Marco Poletti poletti.marco at gmail.com
Thu Mar 25 19:14:19 CET 2010


Module: ppl/ppl
Branch: sparse_matrices
Commit: 6d7dd4361749a6d2782acb2ec0b3d5da2aad7d13
URL:    http://www.cs.unipr.it/git/gitweb.cgi?p=ppl/ppl.git;a=commit;h=6d7dd4361749a6d2782acb2ec0b3d5da2aad7d13

Author: Marco Poletti <poletti.marco at gmail.com>
Date:   Thu Mar 25 18:30:10 2010 +0100

Unlimited_Sparse_Row: swap elements instead of copy-constructing them, in combine_needs_second().

---

 src/Unlimited_Sparse_Row.templates.hh |   22 +++++++++++++++-------
 1 files changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/Unlimited_Sparse_Row.templates.hh b/src/Unlimited_Sparse_Row.templates.hh
index 4b18c51..ada3a3d 100644
--- a/src/Unlimited_Sparse_Row.templates.hh
+++ b/src/Unlimited_Sparse_Row.templates.hh
@@ -180,15 +180,17 @@ Unlimited_Sparse_Row::combine_needs_second(const Unlimited_Sparse_Row& y,
                                            const Func1& g, const Func2& h) {
   Unlimited_Sparse_Row row;
   iterator itr = row.end();
-  const_iterator i = begin();
-  const_iterator i_end = end();
+  iterator i = begin();
+  iterator i_end = end();
   const_iterator j = y.begin();
   const_iterator j_end = y.end();
   if (i == i_end && j == j_end)
     return;
   if (j == j_end
       || (i != i_end && (*i).first < (*j).first)) {
-    itr = row.find_create(*i);
+    itr = row.find_create((*i).first);
+    // We need to do (*itr).second = (*i).second, but this is faster.
+    std::swap((*itr).second, (*i).second);
     ++i;
   } else
       if (i == i_end
@@ -200,7 +202,9 @@ Unlimited_Sparse_Row::combine_needs_second(const Unlimited_Sparse_Row& y,
         PPL_ASSERT(i != i_end);
         PPL_ASSERT(j != j_end);
         PPL_ASSERT((*i).first == (*j).first);
-        itr = row.find_create(*i);
+        itr = row.find_create((*i).first);
+        // We need to do (*itr).second = (*i).second, but this is faster.
+        std::swap((*itr).second, (*i).second);
         g((*itr).second, (*j).second);
         ++i;
         ++j;
@@ -208,16 +212,20 @@ Unlimited_Sparse_Row::combine_needs_second(const Unlimited_Sparse_Row& y,
   PPL_ASSERT(itr != row.end());
   while (i != i_end && j != j_end) {
     if ((*i).first < (*j).first) {
-      itr = row.find_create(*i, itr);
+      itr = row.find_create((*i).first, itr);
+      // We need to do (*itr).second = (*i).second, but this is faster.
+      std::swap((*itr).second, (*i).second);
       ++i;
     } else {
       if ((*i).first > (*j).first) {
-        itr = row.find_create(*j, itr);
+        itr = row.find_create((*j).first, itr);
         h((*itr).second, (*j).second);
         ++j;
       } else {
         PPL_ASSERT((*i).first == (*j).first);
-        itr = row.find_create(*i, itr);
+        itr = row.find_create((*i).first, itr);
+        // We need to do (*itr).second = (*i).second, but this is faster.
+        std::swap((*itr).second, (*i).second);
         g((*itr).second, (*j).second);
         ++i;
         ++j;




More information about the PPL-devel mailing list