From 6beaf7b00a9334ecd896508a48dfcf333d3c2002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= <stephane.adjemian@univ-lemans.fr> Date: Tue, 17 Nov 2015 20:02:19 +0100 Subject: [PATCH] Changed the behaviour of the sort method... ... and added a new method. sort_ sorts a dates object in place (without a copy). sort first make a copy and then sorts the copy. The first routine, with in place modification, is more efficient because we do not have to create a new object. Example ======= If o is a dates object with more than one element, then o.sort() returns a sorted version of o without modifying o, while o.sort_() sorts the elements in o. --- src/@dates/sort.m | 43 ++++++-------------------- src/@dates/sort_.m | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/@dates/subsref.m | 2 +- 3 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 src/@dates/sort_.m diff --git a/src/@dates/sort.m b/src/@dates/sort.m index 882decb..2b2bda7 100644 --- a/src/@dates/sort.m +++ b/src/@dates/sort.m @@ -1,6 +1,6 @@ function o = sort(o) % --*-- Unitary tests --*-- -% Sort method for dates class. +% Sort method for dates class (with copy). % % INPUTS % - o [dates] @@ -23,11 +23,8 @@ function o = sort(o) % --*-- Unitary tests --*-- % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -if isequal(o.ndat(),1) - return -end - -o.time = sortrows(o.time,[1,2]); +o = copy(o); +o.sort_(); %@test:1 %$ % Define some dates @@ -39,36 +36,16 @@ o.time = sortrows(o.time,[1,2]); %$ % Define expected results. %$ e.time = [1945 3; 1950 1; 1950 2; 1953 4]; %$ e.freq = 4; +%$ f.time = [1953 4; 1950 2; 1950 1; 1945 3]; %$ %$ % Call the tested routine. %$ d = dates(B1,B2,B3,B4); -%$ d = d.sort; +%$ c = d.sort(); %$ %$ % Check the results. -%$ t(1) = dassert(d.time,e.time); -%$ t(2) = dassert(d.freq,e.freq); -%$ t(3) = size(e.time,1) == d.ndat(); -%$ T = all(t); -%@eof:1 - -%@test:1 -%$ % Define some dates -%$ B1 = '1953Q4'; -%$ B2 = '1950Q2'; -%$ B3 = '1950Q1'; -%$ B4 = '1945Q3'; -%$ -%$ % Define expected results. -%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4]; -%$ e.freq = 4; -%$ -%$ % Call the tested routine. -%$ d = dates(B1,B2,B3,B4); -%$ d = d.sort(); -%$ -%$ % Check the results. -%$ t(1) = dassert(d.time,e.time); -%$ t(2) = dassert(d.freq,e.freq); -%$ t(3) = size(e.time,1) == d.ndat(); +%$ t(1) = ~dassert(d.time,e.time); +%$ t(2) = dassert(c.time,e.time); +%$ t(3) = dassert(d.freq,e.freq); +%$ t(4) = dassert(c.freq,e.freq); %$ T = all(t); -%@eof:1 +%@eof:1 \ No newline at end of file diff --git a/src/@dates/sort_.m b/src/@dates/sort_.m new file mode 100644 index 0000000..7b3f97e --- /dev/null +++ b/src/@dates/sort_.m @@ -0,0 +1,72 @@ +function o = sort_(o) % --*-- Unitary tests --*-- + +% Sort method for dates class (in place modification). +% +% INPUTS +% - o [dates] +% +% OUTPUTS +% - o [dates] with dates sorted by increasing order. + +% Copyright (C) 2015 Dynare Team +% +% This code is free software: you can redistribute it and/or modify +% it under the terms of the GNU General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare dates submodule is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with Dynare. If not, see <http://www.gnu.org/licenses/>. + +if isequal(o.ndat(),1) + return +end + +o.time = sortrows(o.time,[1,2]); + +%@test:1 +%$ % Define some dates +%$ B1 = '1953Q4'; +%$ B2 = '1950Q2'; +%$ B3 = '1950Q1'; +%$ B4 = '1945Q3'; +%$ +%$ % Define expected results. +%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4]; +%$ e.freq = 4; +%$ +%$ % Call the tested routine. +%$ d = dates(B1,B2,B3,B4); +%$ d.sort_(); +%$ +%$ % Check the results. +%$ t(1) = dassert(d.time,e.time); +%$ t(2) = dassert(d.freq,e.freq); +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define some dates +%$ B1 = '1953Q4'; +%$ B2 = '1950Q2'; +%$ B3 = '1950Q1'; +%$ B4 = '1945Q3'; +%$ +%$ % Define expected results. +%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4]; +%$ e.freq = 4; +%$ +%$ % Call the tested routine. +%$ d = dates(B1,B2,B3,B4); +%$ c = sort_(d); +%$ +%$ % Check the results. +%$ t(1) = dassert(d.time,e.time); +%$ t(2) = dassert(d.freq,e.freq); +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dates/subsref.m b/src/@dates/subsref.m index 9f14322..1af52a1 100644 --- a/src/@dates/subsref.m +++ b/src/@dates/subsref.m @@ -36,7 +36,7 @@ switch S(1).type error(['dates::subsref: ' S(1).subs ' is not a method but a member!']) end B = builtin('subsref', A, S(1)); - case {'sort','unique','double','isempty','length','char','ndat'}% Public methods (without input arguments) + case {'sort','sort_','unique','double','isempty','length','char','ndat'}% Public methods (without input arguments) B = feval(S(1).subs,A); if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) S = shiftS(S,1); -- GitLab