1 ## Copyright (C) 2000-2012 Paul Kienzle
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
19 ## Undocumented internal function.
22 ## @deftypefn {Function File} {} __isequal__ (@var{nans_compare_equal}, @var{x1}, @var{x2}, @dots{})
23 ## Undocumented internal function.
26 ## Return true if @var{x1}, @var{x2}, @dots{} are all equal and
27 ## @var{nans_compare_equal} evaluates to false.
29 ## If @var{nans_compare_equal} evaluates to true, then assume NaN == NaN.
31 ## Modified by: William Poetra Yoga Hadisoeseno
35 ## 1. Determine the class of x
36 ## 2. If x is of the struct, cell, list or char class, for each
37 ## argument after x, determine whether it has the same class
39 ## Otherwise, for each argument after x, verify that it is not
40 ## of the struct, cell, list or char class, and that it has
41 ## the same size as x.
42 ## 3. For each argument after x, compare it for equality with x:
43 ## a. struct compare each member by name, not by order (recursive)
44 ## b. cell/list compare each member by order (recursive)
45 ## c. char compare each member with strcmp
46 ## d. <other> compare each nonzero member, and assume NaN == NaN
47 ## if nans_compare_equal is nonzero.
49 function t = __isequal__ (nans_compare_equal, x, varargin)
59 ## All arguments must either be of the same class or they must be
61 t = (all (strcmp (class(x),
62 cellfun ("class", varargin, "uniformoutput", false)))
63 || ((isnumeric (x) || islogical (x))
64 && all (cellfun ("isnumeric", varargin)
65 | cellfun ("islogical", varargin))));
68 ## Test that everything has the same number of dimensions.
70 s_v = cellfun (@size, varargin, "uniformoutput", false);
71 t = all (length (s_x) == cellfun ("length", s_v));
75 ## Test that everything is the same size since it has the same
78 s_v = reshape ([s_v{:}], length (s_x), []);
80 while (t && idx < l_x)
82 t = all (s_x(idx) == s_v(idx,:));
86 ## From here on, compare objects as if they were structures.
88 x = builtin ("struct", x);
89 varargin = cellfun (@(x) builtin ("struct", x), varargin,
90 "uniformoutput", false);
94 ## Check individual classes.
96 ## Test the number of fields.
97 fn_x = fieldnames (x);
98 l_fn_x = length (fn_x);
99 fn_v = cellfun ("fieldnames", varargin, "uniformoutput", false);
100 t = all (l_fn_x == cellfun ("length", fn_v));
102 ## Test that all the names are equal.
104 s_fn_x = sort (fn_x);
105 while (t && idx < l_v)
107 ## We'll allow the fieldnames to be in a different order.
108 t = all (strcmp (s_fn_x, sort (fn_v{idx})));
112 while (t && idx < l_fn_x)
113 ## Test that all field values are equal.
115 args = {nans_compare_equal, {x.(fn_x{idx})}};
117 args{argn+2} = {varargin{argn}.(fn_x{idx})};
119 ## Minimize function calls by calling for all the arguments at
121 t = __isequal__ (args{:});
125 ## Check that each element of a cell is equal.
128 while (t && idx < l_x)
130 args = {nans_compare_equal, x{idx}};
132 args{p+2} = varargin{p}{idx};
134 t = __isequal__ (args{:});
139 ## Sizes are equal already, so we can just make everything into a
140 ## row and test the rows.
142 strings{i} = reshape (varargin{i}, 1, []);
144 t = all (strcmp (reshape (x, 1, []), strings));
146 elseif (isa (x, "function_handle"))
148 ## The == operator is overloaded for handles.
149 t = all (cellfun ("eq", {x}, varargin));
152 ## Check the numeric types.
155 l_f_x = length (f_x);
161 t = (l_f_x == length (f_y)) && all (f_x == f_y);
170 if (!t && nans_compare_equal)
171 t = isnan (x(!m)) && isnan (y(!m));