toon-members
[Top][All Lists]
Advanced

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

[Toon-members] TooN internal/allocator.hh internal/vector.hh t...


From: Edward Rosten
Subject: [Toon-members] TooN internal/allocator.hh internal/vector.hh t...
Date: Wed, 18 Feb 2009 16:46:29 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/02/18 16:46:29

Modified files:
        internal       : allocator.hh vector.hh 
        test           : test2.cc 

Log message:
        Make copy constructors copy data, if there is data to copy and it is not
        done automatically. The copying is done in allocator.hh, since it is 
known
        only at this level whether it is data or references that should be 
copied.
        
        Instrumenting Vector's copy constructor shows that RVO eliminates all 
copy
        constructors in test2.cc except for the one in 
"make_a_copy_constructor_happen"

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&r1=1.11&r2=1.12
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/vector.hh?cvsroot=toon&r1=1.13&r2=1.14
http://cvs.savannah.gnu.org/viewcvs/TooN/test/test2.cc?cvsroot=toon&r1=1.4&r2=1.5

Patches:
Index: internal/allocator.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/allocator.hh,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -b -r1.11 -r1.12
--- internal/allocator.hh       17 Feb 2009 13:40:31 -0000      1.11
+++ internal/allocator.hh       18 Feb 2009 16:46:28 -0000      1.12
@@ -1,4 +1,11 @@
-// Allocators never copy
+// Allocators always copy data on copy construction.
+//
+// When a Vector/Matrix is constructed from a different, but compatible type
+// copying is done at a much higher level: above the level that knows how the
+// data is laid out in memory.
+//
+// At this level, copy construction is required since it is only known here 
+// whether data or a reference to data should be copied.
 
 template<int Size, class Precision, bool heap> class StackOrHeap
 {
@@ -6,9 +13,6 @@
                StackOrHeap()
                {}
 
-               StackOrHeap(const StackOrHeap&)
-               {}
-
                Precision my_data[Size];
 };
 
@@ -26,8 +30,12 @@
 
                Precision *my_data;
        
-       private:
-               StackOrHeap(const StackOrHeap&);
+               StackOrHeap(const StackOrHeap& from)
+               :my_data(new Precision[Size])
+               {
+                       for(int i=0; i < Size; i++)
+                               my_data[i] = from.my_data[i];
+               }
 };
 
 
@@ -53,6 +61,13 @@
        Precision * const my_data;
        const int my_size;
 
+       VectorAlloc(const VectorAlloc& v)
+       :my_data(new Precision[v.my_size]), my_size(v.my_size)
+       { 
+               for(int i=0; i < my_size; i++)
+                       my_data[i] = v.my_data[i];
+       }
+
        VectorAlloc(int s)
        :my_data(new Precision[s]), my_size(s)
        { }
@@ -117,6 +132,13 @@
        const int my_cols;
        Precision* const my_data;
 
+       MatrixAlloc(const MatrixAlloc& m)
+       :my_rows(m.my_rows),my_cols(m.my_cols),my_data(new 
Precision[my_rows*my_cols]) {
+               const int size=my_rows*my_cols;
+               for(int i=0; i < size; i++)
+                       my_data[i] = m.my_data[i];
+       }
+
        MatrixAlloc(int r, int c)
        :my_rows(r),my_cols(c),my_data(new Precision[r*c]) {
        }

Index: internal/vector.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/vector.hh,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -b -r1.13 -r1.14
--- internal/vector.hh  18 Feb 2009 14:27:57 -0000      1.13
+++ internal/vector.hh  18 Feb 2009 16:46:29 -0000      1.14
@@ -27,15 +27,21 @@
   }
 
   // copy constructor listed explicitly
-  inline Vector(const Vector<Size,Precision,Base>& from)
-    : Base(from) {
-    (*this)=from;
+  // this is a very special case. Copy construction
+  // goes all the way down to the bottom. GenericVBase has no
+  // idea how to copy itself. However, the underlying allocator objects do.
+  // In the case of static sized objects, C++ automatically copies the data.
+  // For slice objects, C++ copies all parts (pointer and size), which is 
correct.
+  // For dynamically sized non-slice objects the copying has to be done by 
hand.
+  inline Vector(const Vector&from)
+       : Base(from){
+       std::cout<< "Hello. My name is Inigo Montoya.\n";
   }
 
   // constructor from arbitrary vector
   template<int Size2, typename Precision2, typename Base2>
   inline Vector(const Vector<Size2,Precision2,Base2>& from):
-    Base(from) {
+    Base(from.size()) {
     operator=(from);
   }
 

Index: test/test2.cc
===================================================================
RCS file: /cvsroot/toon/TooN/test/test2.cc,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -b -r1.4 -r1.5
--- test/test2.cc       18 Feb 2009 14:27:58 -0000      1.4
+++ test/test2.cc       18 Feb 2009 16:46:29 -0000      1.5
@@ -8,11 +8,20 @@
        cout << __PRETTY_FUNCTION__ << endl;
 }
 
+void make_a_copy_constructor_happen(const Vector<4>& v)
+{
+       cout << "Pre CC\n";
+       Vector<4> v2(v);
+       cout << "Post CC\n";
+}
+
 int main()
 {
        Vector<4> v1 = makeVector(1, 2, 3, 4);
        Vector<4> v2 = makeVector(5, 6, 7, 8);
 
+       make_a_copy_constructor_happen(v1);
+
        cout << 1+(v1 + v2)+2 << endl;
 
        v1.slice<0, 2>() /= 2;




reply via email to

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