]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/private/__isequal__.m
update packages
[CreaPhase.git] / octave_packages / m / general / private / __isequal__.m
1 ## Copyright (C) 2000-2012 Paul Kienzle
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## Undocumented internal function.
20
21 ## -*- texinfo -*-
22 ## @deftypefn {Function File} {} __isequal__ (@var{nans_compare_equal}, @var{x1}, @var{x2}, @dots{})
23 ## Undocumented internal function.
24 ## @end deftypefn
25
26 ## Return true if @var{x1}, @var{x2}, @dots{} are all equal and
27 ## @var{nans_compare_equal} evaluates to false.
28 ##
29 ## If @var{nans_compare_equal} evaluates to true, then assume NaN == NaN.
30
31 ## Modified by: William Poetra Yoga Hadisoeseno
32
33 ## Algorithm:
34 ##
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
38 ##    and size as x.
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.
48
49 function t = __isequal__ (nans_compare_equal, x, varargin)
50
51   if (nargin < 3)
52     print_usage ();
53   endif
54
55   l_v = nargin - 2;
56
57   ## Generic tests.
58
59   ## All arguments must either be of the same class or they must be
60   ## numeric values.
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))));
66
67   if (t)
68     ## Test that everything has the same number of dimensions.
69     s_x = size (x);
70     s_v = cellfun (@size, varargin, "uniformoutput", false);
71     t = all (length (s_x) == cellfun ("length", s_v));
72   endif
73
74   if (t)
75     ## Test that everything is the same size since it has the same
76     ## dimensionality.
77     l_x = length (s_x);
78     s_v = reshape ([s_v{:}], length (s_x), []);
79     idx = 0;
80     while (t && idx < l_x)
81       idx++;
82       t = all (s_x(idx) == s_v(idx,:));
83     endwhile
84   endif
85
86   ## From here on, compare objects as if they were structures.
87   if (isobject (x))
88     x = builtin ("struct", x);
89     varargin = cellfun (@(x) builtin ("struct", x), varargin,
90                         "uniformoutput", false);
91   endif
92
93   if (t)
94     ## Check individual classes.
95     if (isstruct (x))
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));
101
102       ## Test that all the names are equal.
103       idx = 0;
104       s_fn_x = sort (fn_x);
105       while (t && idx < l_v)
106         idx++;
107         ## We'll allow the fieldnames to be in a different order.
108         t = all (strcmp (s_fn_x, sort (fn_v{idx})));
109       endwhile
110
111       idx = 0;
112       while (t && idx < l_fn_x)
113         ## Test that all field values are equal.
114         idx++;
115         args = {nans_compare_equal, {x.(fn_x{idx})}};
116         for argn = 1:l_v
117           args{argn+2} = {varargin{argn}.(fn_x{idx})};
118         endfor
119         ## Minimize function calls by calling for all the arguments at
120         ## once.
121         t = __isequal__ (args{:});
122       endwhile
123
124     elseif (iscell (x))
125       ## Check that each element of a cell is equal.
126       l_x = numel (x);
127       idx = 0;
128       while (t && idx < l_x)
129         idx++;
130         args = {nans_compare_equal, x{idx}};
131         for p = 1:l_v
132           args{p+2} = varargin{p}{idx};
133         endfor
134         t = __isequal__ (args{:});
135       endwhile
136
137     elseif (ischar (x))
138
139       ## Sizes are equal already, so we can just make everything into a
140       ## row and test the rows.
141       for i = 1:l_v
142         strings{i} = reshape (varargin{i}, 1, []);
143       endfor
144       t = all (strcmp (reshape (x, 1, []), strings));
145
146     elseif (isa (x, "function_handle"))
147
148       ## The == operator is overloaded for handles.
149       t = all (cellfun ("eq", {x}, varargin));
150
151     else
152       ## Check the numeric types.
153
154       f_x = find (x);
155       l_f_x = length (f_x);
156       x = x(f_x);
157       for argn = 1:l_v
158         y = varargin{argn};
159         f_y = find (y);
160
161         t = (l_f_x == length (f_y)) && all (f_x == f_y);
162         if (!t)
163           return;
164         endif
165
166         y = y(f_y);
167         m = (x == y);
168         t = all (m);
169
170         if (!t && nans_compare_equal)
171           t = isnan (x(!m)) && isnan (y(!m));
172         endif
173
174         if (!t)
175           return;
176         endif
177       endfor
178
179     endif
180   endif
181
182 endfunction