6130
|
1 ## Copyright (C) 2000, 2006 Paul Kienzle |
5549
|
2 ## |
7016
|
3 ## This file is part of Octave. |
5549
|
4 ## |
7016
|
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. |
5549
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5549
|
18 |
6945
|
19 ## Undocumented internal function. |
|
20 |
5549
|
21 ## -*- texinfo -*- |
5550
|
22 ## @deftypefn {Function File} {} __isequal__ (@var{nans_compare_equal}, @var{x1}, @var{x2}, @dots{}) |
|
23 ## Return true if @var{x1}, @var{x2}, @dots{} are all equal and |
5549
|
24 ## @var{nans_compare_equal} evaluates to false. |
|
25 ## |
|
26 ## If @var{nans_compare_equal} evaluates to true, then assume NaN == NaN. |
5642
|
27 ## @seealso{isequal, isequalwithequalnans} |
5549
|
28 ## @end deftypefn |
|
29 |
|
30 ## Modified by: William Poetra Yoga Hadisoeseno |
|
31 |
|
32 ## Algorithm: |
|
33 ## |
|
34 ## 1. Determine the class of x |
|
35 ## 2. If x is of the struct, cell, list or char class, for each |
|
36 ## argument after x, determine whether it has the same class |
|
37 ## and size as x. |
|
38 ## Otherwise, for each argument after x, verify that it is not |
|
39 ## of the struct, cell, list or char class, and that it has |
|
40 ## the same size as x. |
|
41 ## 3. For each argument after x, compare it for equality with x: |
|
42 ## a. struct compare each member by name, not by order (recursive) |
|
43 ## b. cell/list compare each member by order (recursive) |
|
44 ## c. char compare each member with strcmp |
|
45 ## d. <other> compare each nonzero member, and assume NaN == NaN |
|
46 ## if nans_compare_equal is nonzero. |
|
47 |
|
48 function t = __isequal__ (nans_compare_equal, x, varargin) |
|
49 |
|
50 if (nargin < 3) |
6046
|
51 print_usage (); |
5549
|
52 endif |
|
53 |
|
54 l_v = nargin - 2; |
|
55 |
6130
|
56 ## Generic tests. |
5549
|
57 |
6130
|
58 ## Give an error for a list (that will make the code simpler and lists |
|
59 ## are deprecated anyway. |
|
60 if (islist (x)) |
|
61 error ("__isequal__: list objects are deprecated and cannot be tested for equality here; use cell arrays instead"); |
|
62 endif |
5549
|
63 |
6609
|
64 ## All arguments must either be of the same class or they must be |
|
65 ## numeric values. |
|
66 t = (all (strcmp (class(x), |
|
67 cellfun (@class, varargin, "UniformOutput", false))) |
|
68 || (isnumeric (x) |
|
69 && all (cellfun (@isnumeric, varargin, "UniformOutput", true)))); |
|
70 |
6130
|
71 if (t) |
|
72 ## Test that everything has the same number of dimensions. |
|
73 s_x = size (x); |
|
74 s_v = cellfun (@size, varargin, "UniformOutput", false); |
|
75 t = all (length (s_x) == cellfun (@length, s_v)); |
|
76 endif |
5549
|
77 |
6130
|
78 if (t) |
|
79 ## Test that everything is the same size since it has the same |
|
80 ## dimensionality. |
|
81 l_x = length (s_x); |
|
82 s_v = reshape ([s_v{:}], length (s_x), []); |
|
83 idx = 0; |
|
84 while (t && idx < l_x) |
|
85 idx++; |
|
86 t = all (s_x(idx) == s_v(idx, :)); |
|
87 endwhile |
|
88 endif |
5549
|
89 |
6130
|
90 if (t) |
|
91 ## Check individual classes. |
|
92 if (isstruct (x)) |
|
93 ## Test the number of fields. |
|
94 fn_x = fieldnames (x); |
|
95 l_fn_x = length (fn_x); |
|
96 fn_v = cellfun (@fieldnames, varargin, "UniformOutput", false); |
|
97 t = all (l_fn_x == cellfun (@length, fn_v)); |
5549
|
98 |
6130
|
99 ## Test that all the names are equal. |
|
100 idx = 0; |
|
101 s_fn_x = sort (fn_x); |
|
102 while (t && idx < l_v) |
|
103 idx++; |
|
104 ## We'll allow the fieldnames to be in a different order. |
|
105 t = all (strcmp (s_fn_x, sort (fn_v{idx}))); |
|
106 endwhile |
5549
|
107 |
6130
|
108 idx = 0; |
|
109 while (t && idx < l_fn_x) |
|
110 ## Test that all field values are equal. |
|
111 idx++; |
6157
|
112 args = {nans_compare_equal, {x.(fn_x{idx})}}; |
6130
|
113 for argn = 1:l_v |
6157
|
114 args{argn+2} = {varargin{argn}.(fn_x{idx})}; |
6130
|
115 endfor |
|
116 ## Minimize function calls by calling for all the arguments at |
|
117 ## once. |
|
118 t = __isequal__ (args{:}); |
|
119 endwhile |
5549
|
120 |
6130
|
121 elseif (iscell (x)) |
|
122 ## Check that each element of a cell is equal. |
|
123 l_x = numel (x); |
|
124 idx = 0; |
|
125 while (t && idx < l_x) |
|
126 idx++; |
|
127 args = {nans_compare_equal, x{idx}}; |
|
128 for p = 1:l_v |
|
129 args{p+2} = varargin{p}{idx}; |
|
130 endfor |
|
131 t = __isequal__ (args{:}); |
|
132 endwhile |
5549
|
133 |
6130
|
134 elseif (ischar (x)) |
5549
|
135 |
6130
|
136 ## Sizes are equal already, so we can just make everything into a |
|
137 ## row and test the rows. |
|
138 for i = 1:l_v |
|
139 strings{i} = reshape (varargin{i}, 1, []); |
|
140 endfor |
|
141 t = all (strcmp (reshape (x, 1, []), strings)); |
5549
|
142 |
|
143 else |
6130
|
144 ## Check the numeric types. |
|
145 |
|
146 if (issparse (x)) |
|
147 f_x = spfind (x); |
5549
|
148 else |
6130
|
149 f_x = find (x); |
5549
|
150 endif |
6130
|
151 l_f_x = length (f_x); |
|
152 x = x(f_x); |
|
153 for argn = 1:l_v |
|
154 y = varargin{argn}; |
|
155 if (issparse (y)) |
|
156 f_y = spfind (y); |
|
157 else |
|
158 f_y = find (y); |
|
159 endif |
5549
|
160 |
6130
|
161 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
|
162 if (!t) |
|
163 return; |
|
164 endif |
5549
|
165 |
6130
|
166 y = y(f_y); |
|
167 m = (x == y); |
|
168 t = all (m); |
5549
|
169 |
6130
|
170 if (!t) |
|
171 if (nans_compare_equal) |
|
172 t = isnan (x(!m)) && isnan (y(!m)); |
|
173 else |
|
174 return; |
|
175 endif |
|
176 endif |
|
177 endfor |
5549
|
178 |
6130
|
179 endif |
5549
|
180 endif |
|
181 |
|
182 endfunction |
|
183 |
6130
|
184 ## test size and shape |
|
185 %!assert(__isequal__(0,[1,2,3,4],[1,2,3,4]), true) |
|
186 %!assert(__isequal__(0,[1;2;3;4],[1;2;3;4]), true) |
|
187 %!assert(__isequal__(0,[1,2,3,4],[1;2;3;4]), false) |
|
188 %!assert(__isequal__(0,[1,2,3,4],[1,2;3,4]), false) |
|
189 %!assert(__isequal__(0,[1,2,3,4],[1,3;2,4]), false) |
|
190 |
|
191 %!test |
|
192 %! A = 1:8; |
|
193 %! B = reshape (A, 2, 2, 2); |
|
194 %! assert (__isequal__ (0, A, B), false); |
|
195 |
|
196 %!test |
|
197 %! A = reshape (1:8, 2, 2, 2); |
|
198 %! B = A; |
|
199 %! assert (__isequal__ (0, A, B), true); |
|
200 |
|
201 %!test |
|
202 %! A = reshape (1:8, 2, 4); |
|
203 %! B = reshape (A, 2, 2, 2); |
|
204 %! assert (__isequal__ (0, A, B), false); |
|
205 |
|
206 ## test for equality |
|
207 %!assert(__isequal__(0,[1,2,3,4],[1,2,3,4]), true) |
|
208 %!assert(__isequal__(1,{1,2,NaN,4},{1,2,NaN,4}), true) |
|
209 %!assert(__isequal__(1,[1,2,NaN,4],[1,2,NaN,4]), true) |
|
210 %!assert(__isequal__(0,['a','b','c','d'],['a','b','c','d']), true) |
|
211 ## Test multi-line strings |
|
212 %!assert(__isequal__(0,["test";"strings"],["test";"strings"],["test";"strings"]), true) |
|
213 ## test for inequality |
5549
|
214 %!assert(__isequal__(0,[1,2,3,4],[1;2;3;4]),false) |
|
215 %!assert(__isequal__(0,{1,2,3,4},[1,2,3,4]),false) |
|
216 %!assert(__isequal__(0,[1,2,3,4],{1,2,3,4}),false) |
|
217 %!assert(__isequal__(0,[1,2,NaN,4],[1,2,NaN,4]),false) |
|
218 %!assert(__isequal__(1,[1,2,NaN,4],[1,NaN,3,4]),false) |
|
219 %!assert(__isequal__(1,[1,2,NaN,4],[1,2,3,4]),false) |
|
220 %!assert(__isequal__(0,['a','b','c','d'],['a';'b';'c';'d']),false) |
6130
|
221 %!assert(__isequal__(0,{'a','b','c','d'},{'a';'b';'c';'d'}),false) |
|
222 ## test for equality (struct) |
|
223 %!assert(__isequal__(0,struct('a',1,'b',2),struct('a',1,'b',2)),true) |
|
224 %!assert(__isequal__(0,struct('a',1,'b',2),struct('a',1,'b',2),struct('a',1,'b',2)),true) |
|
225 %!assert(__isequal__(0,struct('a','abc','b',2),struct('a','abc','b',2)),true) |
|
226 %!assert(__isequal__(1,struct('a',NaN,'b',2),struct('a',NaN,'b',2),struct('a',NaN,'b',2)),true) |
|
227 ## test for inequality (struct) |
|
228 %!assert(__isequal__(0,struct('a',NaN,'b',2),struct('a',NaN,'b',2),struct('a',NaN,'b',2)),false) |
|
229 |