Mercurial > hg > octave-lojdl
annotate scripts/general/private/__isequal__.m @ 10650:f0dc41c824ce
Replace calls to deprecated functions.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Mon, 24 May 2010 17:20:08 -0700 |
parents | 95c3e38098bf |
children | 8a8eb099502e |
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), | |
10549 | 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)) |
10549 | 64 && all ((cellfun (@isnumeric, varargin) | cellfun (@islogical, varargin))))); |
6609 | 65 |
6130 | 66 if (t) |
67 ## Test that everything has the same number of dimensions. | |
68 s_x = size (x); | |
69 s_v = cellfun (@size, varargin, "UniformOutput", false); | |
70 t = all (length (s_x) == cellfun (@length, s_v)); | |
71 endif | |
5549 | 72 |
6130 | 73 if (t) |
74 ## Test that everything is the same size since it has the same | |
75 ## dimensionality. | |
76 l_x = length (s_x); | |
77 s_v = reshape ([s_v{:}], length (s_x), []); | |
78 idx = 0; | |
79 while (t && idx < l_x) | |
80 idx++; | |
81 t = all (s_x(idx) == s_v(idx, :)); | |
82 endwhile | |
83 endif | |
5549 | 84 |
6130 | 85 if (t) |
86 ## Check individual classes. | |
87 if (isstruct (x)) | |
88 ## Test the number of fields. | |
89 fn_x = fieldnames (x); | |
90 l_fn_x = length (fn_x); | |
91 fn_v = cellfun (@fieldnames, varargin, "UniformOutput", false); | |
92 t = all (l_fn_x == cellfun (@length, fn_v)); | |
5549 | 93 |
6130 | 94 ## Test that all the names are equal. |
95 idx = 0; | |
96 s_fn_x = sort (fn_x); | |
97 while (t && idx < l_v) | |
10549 | 98 idx++; |
99 ## We'll allow the fieldnames to be in a different order. | |
100 t = all (strcmp (s_fn_x, sort (fn_v{idx}))); | |
6130 | 101 endwhile |
5549 | 102 |
6130 | 103 idx = 0; |
104 while (t && idx < l_fn_x) | |
10549 | 105 ## Test that all field values are equal. |
106 idx++; | |
107 args = {nans_compare_equal, {x.(fn_x{idx})}}; | |
108 for argn = 1:l_v | |
109 args{argn+2} = {varargin{argn}.(fn_x{idx})}; | |
110 endfor | |
111 ## Minimize function calls by calling for all the arguments at | |
112 ## once. | |
6130 | 113 t = __isequal__ (args{:}); |
114 endwhile | |
5549 | 115 |
6130 | 116 elseif (iscell (x)) |
117 ## Check that each element of a cell is equal. | |
118 l_x = numel (x); | |
119 idx = 0; | |
120 while (t && idx < l_x) | |
10549 | 121 idx++; |
122 args = {nans_compare_equal, x{idx}}; | |
123 for p = 1:l_v | |
124 args{p+2} = varargin{p}{idx}; | |
125 endfor | |
6130 | 126 t = __isequal__ (args{:}); |
127 endwhile | |
5549 | 128 |
6130 | 129 elseif (ischar (x)) |
5549 | 130 |
6130 | 131 ## Sizes are equal already, so we can just make everything into a |
132 ## row and test the rows. | |
133 for i = 1:l_v | |
10549 | 134 strings{i} = reshape (varargin{i}, 1, []); |
6130 | 135 endfor |
136 t = all (strcmp (reshape (x, 1, []), strings)); | |
5549 | 137 |
10260
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
138 elseif (isa (x, "function_handle")) |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
139 |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
140 ## The == operator is overloaded for handles. |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
141 t = all (cellfun (@eq, {x}, varargin)); |
14d5fee02b3b
basic support for comparing function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9899
diff
changeset
|
142 |
5549 | 143 else |
6130 | 144 ## Check the numeric types. |
145 | |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
146 f_x = find (x); |
6130 | 147 l_f_x = length (f_x); |
148 x = x(f_x); | |
149 for argn = 1:l_v | |
10549 | 150 y = varargin{argn}; |
10650
f0dc41c824ce
Replace calls to deprecated functions.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
151 f_y = find (y); |
5549 | 152 |
10549 | 153 t = (l_f_x == length (f_y)) && all (f_x == f_y); |
154 if (!t) | |
6130 | 155 return; |
10549 | 156 endif |
5549 | 157 |
10549 | 158 y = y(f_y); |
159 m = (x == y); | |
160 t = all (m); | |
5549 | 161 |
10549 | 162 if (!t) |
6130 | 163 if (nans_compare_equal) |
164 t = isnan (x(!m)) && isnan (y(!m)); | |
165 else | |
166 return; | |
167 endif | |
10549 | 168 endif |
6130 | 169 endfor |
5549 | 170 |
6130 | 171 endif |
5549 | 172 endif |
173 | |
174 endfunction |