GNU Octave  4.4.1
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-2018 Jaroslav Hajek
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "ov-null-mat.h"
28 #include "ops.h"
29 #include "defun.h"
30 
32  "double");
33 
35 
36 static octave_base_value *
38 {
39  // The cast is not necessary?
40  // const octave_null_matrix& v = dynamic_cast<const octave_null_matrix&> (a);
41 
42  return a.empty_clone ();
43 }
44 
47 {
51 }
52 
54 
56 
57 static octave_base_value *
59 {
60  // The cast is not necessary?
61  // const octave_null_str& v = dynamic_cast<const octave_null_str&> (a);
62 
63  return a.empty_clone ();
64 }
65 
68 {
72 }
73 
75  "char");
76 
78 
79 static octave_base_value *
81 {
82  // The cast is not necessary?
83  // const octave_null_sq_str& v = dynamic_cast<const octave_null_sq_str&> (a);
84 
85  return a.empty_clone ();
86 }
87 
90 {
94 }
95 
96 DEFUN (isnull, args, ,
97  doc: /* -*- texinfo -*-
98 @deftypefn {} {} isnull (@var{x})
99 Return true if @var{x} is a special null matrix, string, or single quoted
100 string.
101 
102 Indexed assignment with such a null value on the right-hand side should delete
103 array elements. This function is used in place of @code{isempty} when
104 overloading the indexed assignment method (@code{subsasgn}) for user-defined
105 classes. @code{isnull} is used to distinguish between these two cases:
106 
107 @code{@var{A}(@var{I}) = []}
108 
109 and
110 
111 @code{@var{X} = []; @var{A}(@var{I}) = @var{X}}
112 
113 In the first assignment, the right-hand side is @code{[]} which is a special
114 null value. As long as the index @var{I} is not empty, this code should
115 delete elements from @var{A} rather than perform assignment.
116 
117 In the second assignment, the right-hand side is empty (because @var{X} is
118 @code{[]}), but it is @strong{not} null. This code should assign the empty
119 value to elements in @var{A}.
120 
121 An example from Octave's built-in char class demonstrates the interpreter
122 behavior when @code{isnull} is used correctly.
123 
124 @example
125 @group
126 str = "Hello World";
127 nm = "Wally";
128 str(7:end) = nm # indexed assignment
129  @result{} str = Hello Wally
130 str(7:end) = "" # indexed deletion
131  @result{} str = Hello
132 @end group
133 @end example
134 @seealso{isempty, isindex}
135 @end deftypefn */)
136 {
137  if (args.length () != 1)
138  print_usage ();
139 
140  return ovl (args(0).isnull ());
141 }
142 
143 /*
144 %!assert (isnull ([]), true)
145 %!assert (isnull ([1]), false)
146 %!assert (isnull (zeros (0,3)), false)
147 %!assert (isnull (""), true)
148 %!assert (isnull ("A"), false)
149 %!assert (isnull (''), true)
150 %!assert (isnull ('A'), false)
151 %!test
152 %! x = [];
153 %! assert (isnull (x), false);
154 */
static octave_base_value * default_null_sq_str_numeric_conversion_function(const octave_base_value &a)
Definition: ov-null-mat.cc:80
static const octave_value instance
Definition: ov-null-mat.h:70
type_conv_info numeric_conversion_function(void) const
Definition: ov-null-mat.cc:67
OCTINTERP_API void print_usage(void)
Definition: defun.cc:54
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
static int static_type_id(void)
Definition: ov-str-mat.h:177
#define DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA(t, n, c)
Definition: ov-base.h:180
static octave_base_value * default_null_str_numeric_conversion_function(const octave_base_value &a)
Definition: ov-null-mat.cc:58
calling an anonymous function involves an overhead quite comparable to the overhead of an m file function Passing a handle to a built in function is because the interpreter is not involved in the internal loop For a
Definition: cellfun.cc:400
static const octave_value instance
Definition: ov-null-mat.h:91
type_conv_info numeric_conversion_function(void) const
Definition: ov-null-mat.cc:46
static const octave_value instance
Definition: ov-null-mat.h:49
static int static_type_id(void)
Definition: ov-re-mat.h:248
type_conv_info numeric_conversion_function(void) const
Definition: ov-null-mat.cc:89
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
static int static_type_id(void)
Definition: ov-str-mat.h:258
static octave_base_value * default_null_matrix_numeric_conversion_function(const octave_base_value &a)
Definition: ov-null-mat.cc:37