Mercurial > hg > octave-lojdl
annotate scripts/general/private/__isequal__.m @ 11459:990c9cb52e5f
__isequal__: compare objects as if they are structures
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 07 Jan 2011 14:48:55 -0500 |
parents | 01ddaedd6ad5 |
children | caf1fd72f587 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2005, 2006, 2007, 2009 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{}) |
8812
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
23 ## Undocumented internal function. |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
24 ## @end deftypefn |
7d48766c21a5
use consistent format for doc strings of internal functions
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
25 |
5550 | 26 ## Return true if @var{x1}, @var{x2}, @dots{} are all equal and |
5549 | 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) | |
6046 | 52 print_usage (); |
5549 | 53 endif |
54 | |
55 l_v = nargin - 2; | |
56 | |
6130 | 57 ## Generic tests. |
5549 | 58 |
6609 | 59 ## All arguments must either be of the same class or they must be |
60 ## numeric values. | |
61 t = (all (strcmp (class(x), | |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
62 cellfun (@class, varargin, "uniformoutput", false))) |
9841
6f1ea8241c99
make isnumeric yield false on logicals
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
63 || ((isnumeric (x) || islogical (x)) |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
64 && all (cellfun (@isnumeric, varargin) |
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
65 | cellfun (@islogical, varargin)))); |
6609 | 66 |
6130 | 67 if (t) |
68 ## Test that everything has the same number of dimensions. | |
69 s_x = size (x); | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11126
diff
changeset
|
70 s_v = cellfun (@size, varargin, "uniformoutput", false); |
6130 | 71 t = all (length (s_x) == cellfun (@length, s_v)); |
72 endif | |
5549 | 73 |
6130 | 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++; | |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
82 t = all (s_x(idx) == s_v(idx,:)); |
6130 | 83 endwhile |
84 endif | |
5549 | 85 |
11459
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
86 ## From here on, compare objects as if they were structures. |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
87 if (isobject (x)) |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
88 x = struct (x); |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
89 varargin = cellfun (@struct, varargin, "uniformoutput", false); |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
90 endif |
990c9cb52e5f
__isequal__: compare objects as if they are structures
John W. Eaton <jwe@octave.org>
parents:
11191
diff
changeset
|
91 |
6130 | 92 if (t) |
93 ## Check individual classes. | |
94 if (isstruct (x)) | |
95 ## Test the number of fields. | |
96 fn_x = fieldnames (x); | |
97 l_fn_x = length (fn_x); | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11126
diff
changeset
|
98 fn_v = cellfun (@fieldnames, varargin, "uniformoutput", false); |
6130 | 99 t = all (l_fn_x == cellfun (@length, fn_v)); |
5549 | 100 |
6130 | 101 ## Test that all the names are equal. |
102 idx = 0; | |
103 s_fn_x = sort (fn_x); | |
104 while (t && idx < l_v) | |
10549 | 105 idx++; |
106 ## We'll allow the fieldnames to be in a different order. | |
107 t = all (strcmp (s_fn_x, sort (fn_v{idx}))); | |
6130 | 108 endwhile |
5549 | 109 |
6130 | 110 idx = 0; |
111 while (t && idx < l_fn_x) | |
10549 | 112 ## Test that all field values are equal. |
113 idx++; | |
114 args = {nans_compare_equal, {x.(fn_x{idx})}}; | |
115 for argn = 1:l_v | |
116 args{argn+2} = {varargin{argn}.(fn_x{idx})}; | |
117 endfor | |
118 ## Minimize function calls by calling for all the arguments at | |
119 ## once. | |
6130 | 120 t = __isequal__ (args{:}); |
121 endwhile | |
5549 | 122 |
6130 | 123 elseif (iscell (x)) |
124 ## Check that each element of a cell is equal. | |
125 l_x = numel (x); | |
126 idx = 0; | |
127 while (t && idx < l_x) | |
10549 | 128 idx++; |
129 args = {nans_compare_equal, x{idx}}; | |
130 for p = 1:l_v | |
131 args{p+2} = varargin{p}{idx}; | |
132 endfor | |
6130 | 133 t = __isequal__ (args{:}); |
134 endwhile | |
5549 | 135 |
6130 | 136 elseif (ischar (x)) |
5549 | 137 |
6130 | 138 ## Sizes are equal already, so we can just make everything into a |
139 ## row and test the rows. | |
140 for i = 1:l_v | |
10549 | 141 strings{i} = reshape (varargin{i}, 1, []); |
6130 | 142 endfor |
143 t = all (strcmp (reshape (x, 1, []), strings)); | |
5549 | 144 |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
145 elseif (isa (x, "function_handle")) |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
146 |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
147 ## The == operator is overloaded for handles. |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
148 t = all (cellfun (@eq, {x}, varargin)); |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
149 |
5549 | 150 else |
6130 | 151 ## Check the numeric types. |
152 | |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
153 f_x = find (x); |
6130 | 154 l_f_x = length (f_x); |
155 x = x(f_x); | |
156 for argn = 1:l_v | |
10549 | 157 y = varargin{argn}; |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
158 f_y = find (y); |
5549 | 159 |
10549 | 160 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
161 if (!t) | |
6130 | 162 return; |
10549 | 163 endif |
5549 | 164 |
10549 | 165 y = y(f_y); |
166 m = (x == y); | |
167 t = all (m); | |
5549 | 168 |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
169 if (!t && nans_compare_equal) |
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
170 t = isnan (x(!m)) && isnan (y(!m)); |
11125
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
171 endif |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
172 |
8a8eb099502e
Fix bug #31239 in isequalwithequalnans.m
Rik <octave@nomad.inbox5.com>
parents:
10650
diff
changeset
|
173 if (!t) |
11126
304b0ed4ca56
__isequal__.m: style fixes
John W. Eaton <jwe@octave.org>
parents:
11125
diff
changeset
|
174 return; |
10549 | 175 endif |
6130 | 176 endfor |
5549 | 177 |
6130 | 178 endif |
5549 | 179 endif |
180 | |
181 endfunction |