From d86101327e0d36db4fddfd878fad000c7464f3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= Date: Wed, 27 Feb 2019 14:34:28 +0100 Subject: [PATCH] Dynare++: more explicit interface for inserting into a IntSequence --- dynare++/tl/cc/fs_tensor.cc | 2 +- dynare++/tl/cc/int_sequence.cc | 41 ++++++++++++++++++---------------- dynare++/tl/cc/int_sequence.hh | 10 +++++---- dynare++/tl/cc/permutation.hh | 2 +- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/dynare++/tl/cc/fs_tensor.cc b/dynare++/tl/cc/fs_tensor.cc index 6f2794f05..92dd68fd0 100644 --- a/dynare++/tl/cc/fs_tensor.cc +++ b/dynare++/tl/cc/fs_tensor.cc @@ -31,7 +31,7 @@ FFSTensor::FFSTensor(const FFSTensor &t, const ConstVector &x) for (Tensor::index to = begin(); to != end(); ++to) for (int i = 0; i < nvar(); i++) { - IntSequence from_ind(i, to.getCoor()); + IntSequence from_ind(to.getCoor().insert(i)); Tensor::index from(t, from_ind); addColumn(x[i], t, *from, *to); } diff --git a/dynare++/tl/cc/int_sequence.cc b/dynare++/tl/cc/int_sequence.cc index 40f829868..591d3ff84 100644 --- a/dynare++/tl/cc/int_sequence.cc +++ b/dynare++/tl/cc/int_sequence.cc @@ -46,29 +46,32 @@ IntSequence::IntSequence(const Symmetry &sy, const std::vector &se) /* This constructs an ordered integer sequence from the given ordered sequence inserting the given number to the sequence. */ -IntSequence::IntSequence(int i, const IntSequence &s) - : data{new int[s.size()+1]}, length{s.size()+1} +IntSequence +IntSequence::insert(int i) const { - int j = 0; - while (j < s.size() && s[j] < i) - j++; - for (int jj = 0; jj < j; jj++) - operator[](jj) = s[jj]; - operator[](j) = i; - for (int jj = j; jj < s.size(); jj++) - operator[](jj+1) = s[jj]; + IntSequence r(size()+1); + int j; + for (j = 0; j < size() && operator[](j) < i; j++) + r[j] = operator[](j); + r[j] = i; + for (; j < size(); j++) + r[j+1] = operator[](j); + return r; } -IntSequence::IntSequence(int i, const IntSequence &s, int pos) - : data{new int[s.size()+1]}, length{s.size()+1} +IntSequence +IntSequence::insert(int i, int pos) const { - TL_RAISE_IF(pos < 0 || pos > s.size(), - "Wrong position for insertion IntSequence constructor"); - for (int jj = 0; jj < pos; jj++) - operator[](jj) = s[jj]; - operator[](pos) = i; - for (int jj = pos; jj < s.size(); jj++) - operator[](jj+1) = s[jj]; + TL_RAISE_IF(pos < 0 || pos > size(), + "Wrong position for IntSequence::insert()"); + IntSequence r(size()+1); + int j; + for (j = 0; j < pos; j++) + r[j] = operator[](j); + r[j] = i; + for (; j < size(); j++) + r[j+1] = operator[](j); + return r; } IntSequence & diff --git a/dynare++/tl/cc/int_sequence.hh b/dynare++/tl/cc/int_sequence.hh index da876bf2d..b345ab12b 100644 --- a/dynare++/tl/cc/int_sequence.hh +++ b/dynare++/tl/cc/int_sequence.hh @@ -93,10 +93,6 @@ public: IntSequence(const Symmetry &sy, const std::vector &se); // Unfolds a given integer sequence with respect to a given symmetry IntSequence(const Symmetry &sy, const IntSequence &se); - // Inserts an element in an ordered sequence - IntSequence(int i, const IntSequence &s); - // Inserts an element at a given position - IntSequence(int i, const IntSequence &s, int pos); IntSequence &operator=(const IntSequence &s); IntSequence &operator=(IntSequence &&s); @@ -139,6 +135,12 @@ public: bool lessEq(const IntSequence &s) const; bool less(const IntSequence &s) const; + // Inserts an element into an ordered sequence + IntSequence insert(int i) const; + // Inserts an element at a given position + /* For appending at the end, use pos = size() */ + IntSequence insert(int i, int pos) const; + void sort(); void monotone(); void pmonotone(const Symmetry &s); diff --git a/dynare++/tl/cc/permutation.hh b/dynare++/tl/cc/permutation.hh index 6ee3dad7e..101a02200 100644 --- a/dynare++/tl/cc/permutation.hh +++ b/dynare++/tl/cc/permutation.hh @@ -84,7 +84,7 @@ public: p1.apply(permap); } Permutation(const Permutation &p, int i) - : permap(p.size(), p.permap, i) + : permap(p.permap.insert(p.size(), i)) { } Permutation &operator=(const Permutation &) = default;