Dhairya Malhotra 7 år sedan
förälder
incheckning
b01209d6f0
4 ändrade filer med 25 tillägg och 40 borttagningar
  1. 2 0
      include/sctl/matrix.hpp
  2. 10 16
      include/sctl/matrix.txx
  3. 2 0
      include/sctl/vector.hpp
  4. 11 24
      include/sctl/vector.txx

+ 2 - 0
include/sctl/matrix.hpp

@@ -117,6 +117,8 @@ template <class ValueType> class Matrix {
   Matrix<ValueType> pinv(ValueType eps = -1);
 
  private:
+  void Init(Long dim1, Long dim2, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+
   StaticArray<Long, 2> dim;
   Iterator<ValueType> data_ptr;
   bool own_data;

+ 10 - 16
include/sctl/matrix.txx

@@ -26,14 +26,7 @@ template <class ValueType> std::ostream& operator<<(std::ostream& output, const
   return output;
 }
 
-template <class ValueType> Matrix<ValueType>::Matrix() {
-  dim[0] = 0;
-  dim[1] = 0;
-  own_data = true;
-  data_ptr = nullptr;
-}
-
-template <class ValueType> Matrix<ValueType>::Matrix(Long dim1, Long dim2, Iterator<ValueType> data_, bool own_data_) {
+template <class ValueType> void Matrix<ValueType>::Init(Long dim1, Long dim2, Iterator<ValueType> data_, bool own_data_) {
   dim[0] = dim1;
   dim[1] = dim2;
   own_data = own_data_;
@@ -49,15 +42,16 @@ template <class ValueType> Matrix<ValueType>::Matrix(Long dim1, Long dim2, Itera
     data_ptr = data_;
 }
 
+template <class ValueType> Matrix<ValueType>::Matrix() {
+  Init(0, 0);
+}
+
+template <class ValueType> Matrix<ValueType>::Matrix(Long dim1, Long dim2, Iterator<ValueType> data_, bool own_data_) {
+  Init(dim1, dim2, data_, own_data_);
+}
+
 template <class ValueType> Matrix<ValueType>::Matrix(const Matrix<ValueType>& M) {
-  dim[0] = M.dim[0];
-  dim[1] = M.dim[1];
-  own_data = true;
-  if (dim[0] * dim[1] > 0) {
-    data_ptr = aligned_new<ValueType>(dim[0] * dim[1]);
-    memcopy(data_ptr, M.data_ptr, dim[0] * dim[1]);
-  } else
-    data_ptr = nullptr;
+  Init(M.Dim(0), M.Dim(1), (Iterator<ValueType>)M.begin());
 }
 
 template <class ValueType> Matrix<ValueType>::~Matrix() {

+ 2 - 0
include/sctl/vector.hpp

@@ -102,6 +102,8 @@ template <class ValueType> class Vector {
   Vector operator/(ValueType s) const;
 
  private:
+  void Init(Long dim_, Iterator<ValueType> data_ = nullptr, bool own_data_ = true);
+
   Long dim;
   Long capacity;
   Iterator<ValueType> data_ptr;

+ 11 - 24
include/sctl/vector.txx

@@ -7,14 +7,7 @@
 
 namespace SCTL_NAMESPACE {
 
-template <class ValueType> Vector<ValueType>::Vector() {
-  dim = 0;
-  capacity = 0;
-  own_data = true;
-  data_ptr = nullptr;
-}
-
-template <class ValueType> Vector<ValueType>::Vector(Long dim_, Iterator<ValueType> data_, bool own_data_) {
+template <class ValueType> void Vector<ValueType>::Init(Long dim_, Iterator<ValueType> data_, bool own_data_) {
   dim = dim_;
   capacity = dim;
   own_data = own_data_;
@@ -30,26 +23,20 @@ template <class ValueType> Vector<ValueType>::Vector(Long dim_, Iterator<ValueTy
     data_ptr = data_;
 }
 
+template <class ValueType> Vector<ValueType>::Vector() {
+  Init(0);
+}
+
+template <class ValueType> Vector<ValueType>::Vector(Long dim_, Iterator<ValueType> data_, bool own_data_) {
+  Init(dim_, data_, own_data_);
+}
+
 template <class ValueType> Vector<ValueType>::Vector(const Vector<ValueType>& V) {
-  dim = V.dim;
-  capacity = dim;
-  own_data = true;
-  if (dim > 0) {
-    data_ptr = aligned_new<ValueType>(capacity);
-    memcopy(data_ptr, V.data_ptr, dim);
-  } else
-    data_ptr = nullptr;
+  Init(V.Dim(), (Iterator<ValueType>)V.begin());
 }
 
 template <class ValueType> Vector<ValueType>::Vector(const std::vector<ValueType>& V) {
-  dim = V.size();
-  capacity = dim;
-  own_data = true;
-  if (dim > 0) {
-    data_ptr = aligned_new<ValueType>(capacity);
-    memcopy(data_ptr, Ptr2ConstItr<ValueType>(&V[0], V.size()), dim);
-  } else
-    data_ptr = nullptr;
+  Init(V.Dim(), Ptr2ConstItr<ValueType>(&V[0], V.size()));
 }
 
 template <class ValueType> Vector<ValueType>::~Vector() {