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.
parent fe005c6f
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
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
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment