GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-null-mat.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2008-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "ov-null-mat.h"
31 #include "ops.h"
32 #include "defun.h"
33 
35  "double");
36 
38 
39 static octave_base_value *
40 default_null_matrix_numeric_conversion_function (const octave_base_value& a)
41 {
42  // The cast is not necessary?
43  // const octave_null_matrix& v = dynamic_cast<const octave_null_matrix&> (a);
44 
45  return a.empty_clone ();
46 }
47 
50 {
52  (default_null_matrix_numeric_conversion_function,
54 }
55 
57 
59 
60 static octave_base_value *
61 default_null_str_numeric_conversion_function (const octave_base_value& a)
62 {
63  // The cast is not necessary?
64  // const octave_null_str& v = dynamic_cast<const octave_null_str&> (a);
65 
66  return a.empty_clone ();
67 }
68 
71 {
73  (default_null_str_numeric_conversion_function,
75 }
76 
78  "char");
79 
81 
82 static octave_base_value *
83 default_null_sq_str_numeric_conversion_function (const octave_base_value& a)
84 {
85  // The cast is not necessary?
86  // const octave_null_sq_str& v = dynamic_cast<const octave_null_sq_str&> (a);
87 
88  return a.empty_clone ();
89 }
90 
93 {
95  (default_null_sq_str_numeric_conversion_function,
97 }
98 
100 
101 DEFUN (isnull, args, ,
102  doc: /* -*- texinfo -*-
103 @deftypefn {} {@var{tf} =} isnull (@var{x})
104 Return true if @var{x} is a special null array, string, or single quoted
105 string.
106 
107 Indexed assignment with such a null value on the right-hand side should delete
108 array elements. This function is used in place of @code{isempty} when
109 overloading the indexed assignment method (@code{subsasgn}) for user-defined
110 classes. @code{isnull} is used to distinguish between these two cases:
111 
112 @code{@var{A}(@var{I}) = []}
113 
114 and
115 
116 @code{@var{X} = []; @var{A}(@var{I}) = @var{X}}
117 
118 In the first assignment, the right-hand side is @code{[]} which is a special
119 null value. As long as the index @var{I} is not empty, this code should
120 delete elements from @var{A} rather than perform assignment.
121 
122 In the second assignment, the right-hand side is empty (because @var{X} is
123 @code{[]}), but it is @strong{not} null. This code should assign the empty
124 value to elements in @var{A}.
125 
126 An example from Octave's built-in char class demonstrates the interpreter
127 behavior when @code{isnull} is used correctly.
128 
129 @example
130 @group
131 str = "Hello World";
132 nm = "Wally";
133 str(7:end) = nm # indexed assignment
134  @result{} str = Hello Wally
135 str(7:end) = "" # indexed deletion
136  @result{} str = Hello
137 @end group
138 @end example
139 @seealso{isempty, isindex}
140 @end deftypefn */)
141 {
142  if (args.length () != 1)
143  print_usage ();
144 
145  return ovl (args(0).isnull ());
146 }
147 
148 /*
149 %!assert (isnull ([]), true)
150 %!assert (isnull ([1]), false)
151 %!assert (isnull (zeros (0,3)), false)
152 %!assert (isnull (""), true)
153 %!assert (isnull ("A"), false)
154 %!assert (isnull (''), true)
155 %!assert (isnull ('A'), false)
156 %!test
157 %! x = [];
158 %! assert (isnull (x), false);
159 */
160 
161 OCTAVE_END_NAMESPACE(octave)
virtual octave_base_value * empty_clone() const
Definition: ov-base.cc:121
static int static_type_id()
Definition: ov-str-mat.h:270
static int static_type_id()
Definition: ov-str-mat.h:184
static int static_type_id()
Definition: ov-re-mat.h:249
type_conv_info numeric_conversion_function() const
Definition: ov-null-mat.cc:49
static const octave_value instance
Definition: ov-null-mat.h:52
type_conv_info numeric_conversion_function() const
Definition: ov-null-mat.cc:92
static const octave_value instance
Definition: ov-null-mat.h:98
type_conv_info numeric_conversion_function() const
Definition: ov-null-mat.cc:70
static const octave_value instance
Definition: ov-null-mat.h:74
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:235
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219