5549
|
1 ## Copyright (C) 2000 Paul Kienzle |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2 of the License, or |
|
6 ## (at your option) any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, |
|
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 ## GNU General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this program; if not, write to the Free Software |
|
15 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
16 |
|
17 ## -*- texinfo -*- |
5550
|
18 ## @deftypefn {Function File} {} __isequal__ (@var{nans_compare_equal}, @var{x1}, @var{x2}, @dots{}) |
|
19 ## Return true if @var{x1}, @var{x2}, @dots{} are all equal and |
5549
|
20 ## @var{nans_compare_equal} evaluates to false. |
|
21 ## |
|
22 ## If @var{nans_compare_equal} evaluates to true, then assume NaN == NaN. |
|
23 ## @end deftypefn |
|
24 ## |
|
25 ## @seealso{isequal, isequalwithequalnans} |
|
26 |
|
27 ## Modified by: William Poetra Yoga Hadisoeseno |
|
28 |
|
29 ## Algorithm: |
|
30 ## |
|
31 ## 1. Determine the class of x |
|
32 ## 2. If x is of the struct, cell, list or char class, for each |
|
33 ## argument after x, determine whether it has the same class |
|
34 ## and size as x. |
|
35 ## Otherwise, for each argument after x, verify that it is not |
|
36 ## of the struct, cell, list or char class, and that it has |
|
37 ## the same size as x. |
|
38 ## 3. For each argument after x, compare it for equality with x: |
|
39 ## a. struct compare each member by name, not by order (recursive) |
|
40 ## b. cell/list compare each member by order (recursive) |
|
41 ## c. char compare each member with strcmp |
|
42 ## d. <other> compare each nonzero member, and assume NaN == NaN |
|
43 ## if nans_compare_equal is nonzero. |
|
44 |
|
45 function t = __isequal__ (nans_compare_equal, x, varargin) |
|
46 |
|
47 if (nargin < 3) |
|
48 usage ("__isequal__ (nans_compare_equal, x1, x2, ...)"); |
|
49 endif |
|
50 |
|
51 l_v = nargin - 2; |
|
52 |
|
53 if (isstruct (x)) |
|
54 |
|
55 n_x = length (fieldnames (x)); |
|
56 |
|
57 t = true; |
|
58 for argn = 1:l_v |
|
59 y = varargin{argn}; |
|
60 t = t && isstruct (y) && (n_x == length (fieldnames (y))); |
|
61 endfor |
|
62 if (!t) |
|
63 return; |
|
64 endif |
|
65 |
|
66 for argn = 1:l_v |
|
67 y = varargin{argn}; |
|
68 for [v, k] = x |
|
69 t = t && struct_contains (y, k) \ |
|
70 && __isequal__ (nans_compare_equal, v, getfield (y, k)); |
|
71 endfor |
|
72 if (!t) |
|
73 return; |
|
74 endif |
|
75 endfor |
|
76 |
|
77 elseif ((iscell (x)) || (islist (x))) |
|
78 |
|
79 x = x(:); |
|
80 l_x = length (x); |
|
81 |
|
82 t = true; |
|
83 for argn = 1:l_v |
|
84 y = varargin{argn}(:); |
|
85 t = t && (iscell (y) || islist (y)) && (l_x == length (y)); |
|
86 endfor |
|
87 if (!t) |
|
88 return; |
|
89 endif |
|
90 |
|
91 for argn = 1:l_v |
|
92 y = varargin{argn}(:); |
|
93 for p = 1:l_x |
|
94 t = t && __isequal__ (nans_compare_equal, x{p}, y{p}); |
|
95 endfor |
|
96 if (!t) |
|
97 return; |
|
98 endif |
|
99 endfor |
|
100 |
|
101 elseif (ischar (x)) |
|
102 |
|
103 l_x = size (x); |
|
104 |
|
105 t = true; |
|
106 for argn = 1:l_v |
|
107 y = varargin{argn}; |
|
108 t = t && ischar (y) && (l_x == size (y)); |
|
109 endfor |
|
110 if (!t) |
|
111 return; |
|
112 endif |
|
113 |
|
114 for argn = 1:l_v |
|
115 t = t && strcmp (x, varargin{argn}); |
|
116 endfor |
|
117 |
|
118 else |
|
119 |
|
120 s_x = size (x); |
|
121 |
|
122 t = true; |
|
123 for argn = 1:l_v |
|
124 y = varargin{argn}; |
|
125 t = t && (! (isstruct (y) || iscell (y) || islist (y) || ischar (y))) \ |
|
126 && (s_x == size (y)); |
|
127 endfor |
|
128 if (!t) |
|
129 return; |
|
130 endif |
|
131 |
|
132 if (issparse (x)) |
|
133 f_x = spfind (x); |
|
134 else |
|
135 f_x = find (x); |
|
136 endif |
|
137 l_f_x = length (f_x); |
|
138 x = x(f_x); |
|
139 for argn = 1:l_v |
|
140 y = varargin{argn}; |
|
141 if (issparse (y)) |
|
142 f_y = spfind (y); |
|
143 else |
|
144 f_y = find (y); |
|
145 endif |
|
146 |
|
147 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
|
148 if (!t) |
|
149 return; |
|
150 endif |
|
151 |
|
152 y = y(f_y); |
|
153 m = (x == y); |
|
154 t = all (m); |
|
155 |
|
156 if (!t) |
|
157 if (nans_compare_equal) |
|
158 t = isnan (x(!m)) && isnan (y(!m)); |
|
159 else |
|
160 return; |
|
161 endif |
|
162 endif |
|
163 endfor |
|
164 |
|
165 endif |
|
166 |
|
167 endfunction |
|
168 |
|
169 # test for equality |
|
170 %!assert(__isequal__(0,[1,2,3,4],[1,2,3,4])) |
|
171 %!assert(__isequal__(1,{1,2,NaN,4},{1,2,NaN,4})) |
|
172 %!assert(__isequal__(1,[1,2,NaN,4],[1,2,NaN,4])) |
|
173 %!assert(__isequal__(0,['a','b','c','d'],['a','b','c','d'])) |
|
174 # test for inequality |
|
175 %!assert(__isequal__(0,[1,2,3,4],[1;2;3;4]),false) |
|
176 %!assert(__isequal__(0,{1,2,3,4},[1,2,3,4]),false) |
|
177 %!assert(__isequal__(0,[1,2,3,4],{1,2,3,4}),false) |
|
178 %!assert(__isequal__(0,[1,2,NaN,4],[1,2,NaN,4]),false) |
|
179 %!assert(__isequal__(1,[1,2,NaN,4],[1,NaN,3,4]),false) |
|
180 %!assert(__isequal__(1,[1,2,NaN,4],[1,2,3,4]),false) |
|
181 %!assert(__isequal__(0,['a','b','c','d'],['a';'b';'c';'d']),false) |
|
182 # test for equality (struct) |
|
183 %!test |
|
184 %! A = struct (); |
|
185 %! A.char1 = "abcd"; |
|
186 %! A.int1 = 123; |
|
187 %! B = struct (); |
|
188 %! B.char1 = "abcd"; |
|
189 %! B.int1 = 123; |
|
190 %! C = struct (); |
|
191 %! C.char1 = "abcd"; |
|
192 %! C.int1 = 123; |
|
193 %! assert (__isequal__ (0, A, B, C)); |
|
194 # test for inequality (struct) |
|
195 %!test |
|
196 %! A = struct (); |
|
197 %! A.char1 = "abcd"; |
|
198 %! A.int1 = NaN; |
|
199 %! B = struct (); |
|
200 %! B.char1 = "abcd"; |
|
201 %! B.int1 = NaN; |
|
202 %! C = struct (); |
|
203 %! C.char1 = "abcd"; |
|
204 %! C.int1 = NaN; |
|
205 %! assert (__isequal__ (0, A, B, C), false); |