From 02d6f86487666406c8589139395f809d635501fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 20 Feb 2019 14:25:32 +0100 Subject: [PATCH] Dynare++ tensor library: use smart pointers in fold() and unfold() methods --- dynare++/tl/cc/fs_tensor.cc | 9 ++++----- dynare++/tl/cc/fs_tensor.hh | 4 ++-- dynare++/tl/cc/gs_tensor.cc | 8 ++++---- dynare++/tl/cc/gs_tensor.hh | 4 ++-- dynare++/tl/cc/ps_tensor.cc | 10 ++++------ dynare++/tl/cc/ps_tensor.hh | 4 ++-- dynare++/tl/cc/rfs_tensor.cc | 12 ++++++------ dynare++/tl/cc/rfs_tensor.hh | 6 +++--- dynare++/tl/cc/tensor.hh | 5 +++-- dynare++/tl/testing/tests.cc | 6 ++---- 10 files changed, 32 insertions(+), 36 deletions(-) diff --git a/dynare++/tl/cc/fs_tensor.cc b/dynare++/tl/cc/fs_tensor.cc index 67407b345..f99da8c55 100644 --- a/dynare++/tl/cc/fs_tensor.cc +++ b/dynare++/tl/cc/fs_tensor.cc @@ -86,10 +86,10 @@ FFSTensor::FFSTensor(const UFSTensor &ut) } /* Here just make a new instance and return the reference. */ -UTensor & +std::unique_ptr FFSTensor::unfold() const { - return *(new UFSTensor(*this)); + return std::make_unique(*this); } /* Incrementing is easy. We have to increment by calling static method @@ -203,11 +203,10 @@ UFSTensor::UFSTensor(const FFSTensor &ft) unfoldData(); } -/* Here we just return a reference to new instance of folded tensor. */ -FTensor & +std::unique_ptr UFSTensor::fold() const { - return *(new FFSTensor(*this)); + return std::make_unique(*this); } // |UFSTensor| increment and decrement diff --git a/dynare++/tl/cc/fs_tensor.hh b/dynare++/tl/cc/fs_tensor.hh index f5db4645a..ca42d4a92 100644 --- a/dynare++/tl/cc/fs_tensor.hh +++ b/dynare++/tl/cc/fs_tensor.hh @@ -70,7 +70,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - UTensor&unfold() const override; + std::unique_ptr unfold() const override; Symmetry getSym() const { @@ -114,7 +114,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - FTensor&fold() const override; + std::unique_ptr fold() const override; Symmetry getSym() const { diff --git a/dynare++/tl/cc/gs_tensor.cc b/dynare++/tl/cc/gs_tensor.cc index 3d9381edc..90e39ef27 100644 --- a/dynare++/tl/cc/gs_tensor.cc +++ b/dynare++/tl/cc/gs_tensor.cc @@ -272,10 +272,10 @@ FGSTensor::increment(IntSequence &v) const } /* Return unfolded version of the tensor. */ -UTensor & +std::unique_ptr FGSTensor::unfold() const { - return *(new UGSTensor(*this)); + return std::make_unique(*this); } /* Here we implement the contraction @@ -410,10 +410,10 @@ UGSTensor::decrement(IntSequence &v) const } /* Return a new instance of folded version. */ -FTensor & +std::unique_ptr UGSTensor::fold() const { - return *(new FGSTensor(*this)); + return std::make_unique(*this); } /* Return an offset of a given index. */ diff --git a/dynare++/tl/cc/gs_tensor.hh b/dynare++/tl/cc/gs_tensor.hh index d7b52e35d..6dba02a7e 100644 --- a/dynare++/tl/cc/gs_tensor.hh +++ b/dynare++/tl/cc/gs_tensor.hh @@ -178,7 +178,7 @@ public: { tdims.decrement(v); } - UTensor&unfold() const override; + std::unique_ptr unfold() const override; const TensorDimens & getDims() const { @@ -242,7 +242,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - FTensor&fold() const override; + std::unique_ptr fold() const override; const TensorDimens & getDims() const { diff --git a/dynare++/tl/cc/ps_tensor.cc b/dynare++/tl/cc/ps_tensor.cc index 474b03152..f9880df02 100644 --- a/dynare++/tl/cc/ps_tensor.cc +++ b/dynare++/tl/cc/ps_tensor.cc @@ -58,12 +58,11 @@ UPSTensor::decrement(IntSequence &v) const UTensor::decrement(v, tdims.getNVX()); } -FTensor & +std::unique_ptr UPSTensor::fold() const { TL_RAISE("Never should come to this place in UPSTensor::fold"); - auto *nothing = new FFSTensor(0, 0, 0); - return *nothing; + return std::make_unique(0, 0, 0); } int @@ -316,12 +315,11 @@ FPSTensor::decrement(IntSequence &v) const TL_RAISE("FPSTensor::decrement not implemented"); } -UTensor & +std::unique_ptr FPSTensor::unfold() const { TL_RAISE("Unfolding of FPSTensor not implemented"); - auto *nothing = new UFSTensor(0, 0, 0); - return *nothing; + return std::make_unique(0, 0, 0); } /* We only call |calcOffset| of the |PerTensorDimens2|. */ diff --git a/dynare++/tl/cc/ps_tensor.hh b/dynare++/tl/cc/ps_tensor.hh index dd05cfd7d..a09197a56 100644 --- a/dynare++/tl/cc/ps_tensor.hh +++ b/dynare++/tl/cc/ps_tensor.hh @@ -206,7 +206,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - FTensor&fold() const override; + std::unique_ptr fold() const override; int getOffset(const IntSequence &v) const override; void addTo(FGSTensor &out) const; @@ -370,7 +370,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - UTensor&unfold() const override; + std::unique_ptr unfold() const override; int getOffset(const IntSequence &v) const override; void addTo(FGSTensor &out) const; diff --git a/dynare++/tl/cc/rfs_tensor.cc b/dynare++/tl/cc/rfs_tensor.cc index fe4774b62..25a1692e5 100644 --- a/dynare++/tl/cc/rfs_tensor.cc +++ b/dynare++/tl/cc/rfs_tensor.cc @@ -27,10 +27,10 @@ FRTensor::FRTensor(const URTensor &ut) /* Here just make a new instance and return the reference. */ -UTensor & +std::unique_ptr FRTensor::unfold() const { - return *(new URTensor(*this)); + return std::make_unique(*this); } /* Incrementing is easy. The same as for |FFSTensor|. */ @@ -77,10 +77,10 @@ URTensor::URTensor(const FRTensor &ft) /* Here we just return a reference to new instance of folded tensor. */ -FTensor & +std::unique_ptr URTensor::fold() const { - return *(new FRTensor(*this)); + return std::make_unique(*this); } /* Here we just call |UTensor| respective static methods. */ @@ -163,10 +163,10 @@ URSingleTensor::URSingleTensor(const ConstVector &v, int d) /* Here we construct |FRSingleTensor| from |URSingleTensor| and return its reference. */ -FTensor & +std::unique_ptr URSingleTensor::fold() const { - return *(new FRSingleTensor(*this)); + return std::make_unique(*this); } // |FRSingleTensor| conversion from unfolded diff --git a/dynare++/tl/cc/rfs_tensor.hh b/dynare++/tl/cc/rfs_tensor.hh index c97862919..75bba15d6 100644 --- a/dynare++/tl/cc/rfs_tensor.hh +++ b/dynare++/tl/cc/rfs_tensor.hh @@ -62,7 +62,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - FTensor&fold() const override; + std::unique_ptr fold() const override; int getOffset(const IntSequence &v) const override; int @@ -98,7 +98,7 @@ public: void increment(IntSequence &v) const override; void decrement(IntSequence &v) const override; - UTensor&unfold() const override; + std::unique_ptr unfold() const override; int nvar() const @@ -138,7 +138,7 @@ public: = default; ~URSingleTensor() override = default; - FTensor&fold() const override; + std::unique_ptr fold() const override; }; /* This class represents one column row-oriented tensor. The only way diff --git a/dynare++/tl/cc/tensor.hh b/dynare++/tl/cc/tensor.hh index 0c90a26b0..4e04fb956 100644 --- a/dynare++/tl/cc/tensor.hh +++ b/dynare++/tl/cc/tensor.hh @@ -49,6 +49,7 @@ #include "int_sequence.hh" #include "twod_matrix.hh" +#include #include /* Here is the |Tensor| class, which is nothing else than a simple subclass @@ -240,7 +241,7 @@ public: { } ~UTensor() override = default; - virtual FTensor &fold() const = 0; + virtual std::unique_ptr fold() const = 0; UTensor &operator=(const UTensor &) = delete; UTensor &operator=(UTensor &&) = delete; @@ -276,7 +277,7 @@ public: { } ~FTensor() override = default; - virtual UTensor &unfold() const = 0; + virtual std::unique_ptr unfold() const = 0; FTensor &operator=(const FTensor &) = delete; FTensor &operator=(FTensor &&) = delete; diff --git a/dynare++/tl/testing/tests.cc b/dynare++/tl/testing/tests.cc index 623aefdb6..47635a597 100644 --- a/dynare++/tl/testing/tests.cc +++ b/dynare++/tl/testing/tests.cc @@ -196,8 +196,8 @@ TestRunnable::index_offset(const Symmetry &s, const IntSequence &nvs) bool TestRunnable::fold_unfold(const FTensor *folded) { - UTensor *unfolded = &(folded->unfold()); - FTensor *folded2 = &(unfolded->fold()); + auto unfolded = folded->unfold(); + auto folded2 = unfolded->fold(); folded2->add(-1.0, *folded); double normInf = folded2->getNormInf(); double norm1 = folded2->getNorm1(); @@ -207,8 +207,6 @@ TestRunnable::fold_unfold(const FTensor *folded) << "\tdifference norm1: " << norm1 << '\n'; delete folded; - delete unfolded; - delete folded2; return normInf < 1.0e-15; }