GNU Octave  6.2.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-2021 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 *
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 {
54 }
55 
57 
59 
60 static octave_base_value *
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 {
75 }
76 
78  "char");
79 
81 
82 static octave_base_value *
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 {
97 }
98 
99 DEFUN (isnull, args, ,
100  doc: /* -*- texinfo -*-
101 @deftypefn {} {} isnull (@var{x})
102 Return true if @var{x} is a special null matrix, string, or single quoted
103 string.
104 
105 Indexed assignment with such a null value on the right-hand side should delete
106 array elements. This function is used in place of @code{isempty} when
107 overloading the indexed assignment method (@code{subsasgn}) for user-defined
108 classes. @code{isnull} is used to distinguish between these two cases:
109 
110 @code{@var{A}(@var{I}) = []}
111 
112 and
113 
114 @code{@var{X} = []; @var{A}(@var{I}) = @var{X}}
115 
116 In the first assignment, the right-hand side is @code{[]} which is a special
117 null value. As long as the index @var{I} is not empty, this code should
118 delete elements from @var{A} rather than perform assignment.
119 
120 In the second assignment, the right-hand side is empty (because @var{X} is
121 @code{[]}), but it is @strong{not} null. This code should assign the empty
122 value to elements in @var{A}.
123 
124 An example from Octave's built-in char class demonstrates the interpreter
125 behavior when @code{isnull} is used correctly.
126 
127 @example
128 @group
129 str = "Hello World";
130 nm = "Wally";
131 str(7:end) = nm # indexed assignment
132  @result{} str = Hello Wally
133 str(7:end) = "" # indexed deletion
134  @result{} str = Hello
135 @end group
136 @end example
137 @seealso{isempty, isindex}
138 @end deftypefn */)
139 {
140  if (args.length () != 1)
141  print_usage ();
142 
143  return ovl (args(0).isnull ());
144 }
145 
146 /*
147 %!assert (isnull ([]), true)
148 %!assert (isnull ([1]), false)
149 %!assert (isnull (zeros (0,3)), false)
150 %!assert (isnull (""), true)
151 %!assert (isnull ("A"), false)
152 %!assert (isnull (''), true)
153 %!assert (isnull ('A'), false)
154 %!test
155 %! x = [];
156 %! assert (isnull (x), false);
157 */
virtual octave_base_value * empty_clone(void) const
Definition: ov-base.cc:106
static int static_type_id(void)
Definition: ov-str-mat.h:270
static int static_type_id(void)
Definition: ov-str-mat.h:184
static int static_type_id(void)
Definition: ov-re-mat.h:250
type_conv_info numeric_conversion_function(void) const
Definition: ov-null-mat.cc:49
static const octave_value instance
Definition: ov-null-mat.h:52
static const octave_value instance
Definition: ov-null-mat.h:94
type_conv_info numeric_conversion_function(void) const
Definition: ov-null-mat.cc:92
static const octave_value instance
Definition: ov-null-mat.h:73
type_conv_info numeric_conversion_function(void) const
Definition: ov-null-mat.cc:70
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#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:180
static octave_base_value * default_null_matrix_numeric_conversion_function(const octave_base_value &a)
Definition: ov-null-mat.cc:40
static octave_base_value * default_null_sq_str_numeric_conversion_function(const octave_base_value &a)
Definition: ov-null-mat.cc:83
static octave_base_value * default_null_str_numeric_conversion_function(const octave_base_value &a)
Definition: ov-null-mat.cc:61
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211