GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Cell.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-2018 John W. Eaton
4 Copyright (C) 2009-2010 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "idx-vector.h"
29 
30 #include "Cell.h"
31 #include "error.h"
32 #include "errwarn.h"
33 #include "ovl.h"
34 
36  : Array<octave_value> (ovl.cell_value ())
37 { }
38 
39 Cell::Cell (const string_vector& sv, bool trim)
40  : Array<octave_value> ()
41 {
42  octave_idx_type n = sv.numel ();
43 
44  if (n > 0)
45  {
46  resize (dim_vector (n, 1));
47 
48  for (octave_idx_type i = 0; i < n; i++)
49  {
50  std::string s = sv[i];
51 
52  if (trim)
53  {
54  size_t pos = s.find_last_not_of (' ');
55 
56  s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
57  }
58 
59  elem(i,0) = s;
60  }
61  }
62 }
63 
65  : Array<octave_value> (sa.dims ())
66 {
67  octave_idx_type n = sa.numel ();
68 
69  octave_value *dst = fortran_vec ();
70  const std::string *src = sa.data ();
71 
72  for (octave_idx_type i = 0; i < n; i++)
73  dst[i] = src[i];
74 }
75 
76 // Set size to DV, filling with []. Then fill with as many elements of
77 // SV as possible.
78 
79 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim)
80  : Array<octave_value> (dv, Matrix ())
81 {
82  octave_idx_type n = sv.numel ();
83 
84  if (n > 0)
85  {
86  octave_idx_type m = numel ();
87 
88  octave_idx_type len = (n > m ? m : n);
89 
90  for (octave_idx_type i = 0; i < len; i++)
91  {
92  std::string s = sv[i];
93 
94  if (trim)
95  {
96  size_t pos = s.find_last_not_of (' ');
97 
98  s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
99  }
100 
101  elem(i) = s;
102  }
103  }
104 }
105 
106 bool
107 Cell::iscellstr (void) const
108 {
109  bool retval = true;
110 
111  octave_idx_type n = numel ();
112 
113  for (octave_idx_type i = 0; i < n; i++)
114  {
115  if (! elem(i).is_string ())
116  {
117  retval = false;
118  break;
119  }
120  }
121 
122  return retval;
123 }
124 
127 {
129 
130  octave_idx_type n = numel ();
131 
132  for (octave_idx_type i = 0; i < n; i++)
133  retval.xelem (i) = elem (i).string_value ();
134 
135  return retval;
136 }
137 
140 {
141  octave_idx_type n = numel ();
142 
143  string_vector retval (n);
144 
145  for (octave_idx_type i = 0; i < n; i++)
146  retval.xelem (i) = elem (i).string_value ();
147 
148  return retval;
149 }
150 
151 Cell
152 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const
153 {
154  Cell retval;
155 
156  octave_idx_type n = idx_arg.length ();
157 
158  // If we catch an indexing error in index_vector, we flag an error
159  // in index k. Ensure it is the right value befor each idx_vector
160  // call. Same variable as used in for loop in default case.
161 
162  octave_idx_type k = 0;
163 
164  try
165  {
166  switch (n)
167  {
168  case 0:
169  retval = *this;
170  break;
171 
172  case 1:
173  {
174  idx_vector i = idx_arg(0).index_vector ();
175 
176  retval = Array<octave_value>::index (i, resize_ok, Matrix ());
177  }
178  break;
179 
180  case 2:
181  {
182  idx_vector i = idx_arg(0).index_vector ();
183 
184  k = 1;
185  idx_vector j = idx_arg(1).index_vector ();
186 
187  retval = Array<octave_value>::index (i, j, resize_ok, Matrix ());
188  }
189  break;
190 
191  default:
192  {
193  Array<idx_vector> iv (dim_vector (n, 1));
194 
195  for (k = 0; k < n; k++)
196  iv(k) = idx_arg(k).index_vector ();
197 
198  retval = Array<octave_value>::index (iv, resize_ok, Matrix ());
199  }
200  break;
201  }
202  }
203  catch (octave::index_exception& e)
204  {
205  // Rethrow to allow more info to be reported later.
206  e.set_pos_if_unset (n, k+1);
207  throw;
208  }
209 
210  return retval;
211 }
212 
213 /*
214 %!test
215 %! a = {"foo", "bar"};
216 %! assert (a(), a);
217 */
218 
219 void
220 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs,
221  const octave_value& fill_val)
222 
223 {
224  octave_idx_type len = idx_arg.length ();
225 
227 
228  for (octave_idx_type i = 0; i < len; i++)
229  {
230  try
231  {
232  ra_idx(i) = idx_arg(i).index_vector ();
233  }
234  catch (octave::index_exception& e)
235  {
236  // Rethrow to allow more info to be reported later.
237  e.set_pos (len, i+1);
238  throw;
239  }
240  }
241 
242  Array<octave_value>::assign (ra_idx, rhs, fill_val);
243 }
244 
245 void
247 
248 {
249  octave_idx_type len = idx_arg.length ();
250 
252 
253  for (octave_idx_type i = 0; i < len; i++)
254  try
255  {
256  ra_idx.xelem (i) = idx_arg(i).index_vector ();
257  }
258  catch (octave::index_exception& e)
259  {
260  // Rethrow to allow more info to be reported later.
261  e.set_pos (len, i+1);
262  throw;
263  }
264 
266 }
267 
269 Cell::nnz (void) const
270 {
271  err_wrong_type_arg ("nnz", "cell array");
272 }
273 
274 /*
275 %!error <wrong type argument 'cell array'> nnz ({0, 1, 2})
276 %!error <wrong type argument 'cell array'> nnz (cell ())
277 %!error <wrong type argument 'cell array'> nnz ({"foo", "bar"})
278 */
279 
280 Cell
282 {
283  Cell retval;
284 
285  if (ndims () > 2)
286  error ("Cell::column: requires 2-D cell array");
287 
288  if (i < 0 || i >= cols ())
289  error ("invalid column selection");
290 
291  octave_idx_type nr = rows ();
292 
293  retval.resize (dim_vector (nr, 1));
294 
295  for (octave_idx_type j = 0; j < nr; j++)
296  retval.xelem (j) = elem (j, i);
297 
298  return retval;
299 }
300 
301 Cell
303 {
304  return insert (rb, ra_idx);
305 }
306 
307 Cell&
309 {
311  return *this;
312 }
313 
314 Cell&
316 {
318  return *this;
319 }
320 
321 Cell
322 Cell::map (ctype_mapper fcn) const
323 {
324  Cell retval (dims ());
325  octave_value *r = retval.fortran_vec ();
326 
327  const octave_value *p = data ();
328 
329  for (octave_idx_type i = 0; i < numel (); i++)
330  r[i] = ((p++)->*fcn) ();
331 
332  return retval;
333 }
334 
337 {
338  static octave_value rfv = octave_value (Matrix ());
339  return rfv;
340 }
341 
342 Cell
344 {
345  return Array<octave_value>::diag (k);
346 }
347 
348 Cell
350 {
351  return Array<octave_value>::diag (m, n);
352 }
octave_idx_type rows(void) const
Definition: Array.h:404
Cell index(const octave_value_list &idx, bool resize_ok=false) const
Definition: Cell.cc:152
Definition: Cell.h:37
const octave_base_value const Array< octave_idx_type > & ra_idx
std::string string_value(bool force=false) const
Definition: ov.h:955
Cell diag(octave_idx_type k=0) const
Definition: Cell.cc:343
const T * data(void) const
Definition: Array.h:582
void delete_elements(const idx_vector &i)
Deleting elements.
Definition: Array.cc:1389
string_vector string_vector_value(void) const
Definition: Cell.cc:139
for large enough k
Definition: lu.cc:617
const octave_value * fortran_vec(void) const
Definition: Array.h:584
void assign(const octave_value_list &idx, const Cell &rhs, const octave_value &fill_val=Matrix())
Definition: Cell.cc:220
void error(const char *fmt,...)
Definition: error.cc:578
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:442
Cell map(ctype_mapper) const
Definition: Cell.cc:322
octave_value & elem(octave_idx_type n)
Definition: Array.h:488
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
s
Definition: file-io.cc:2729
i e
Definition: data.cc:2591
octave_function * fcn
Definition: ov-class.cc:1754
octave_idx_type cols(void) const
Definition: Array.h:412
Array< T > & insert(const Array< T > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
Definition: Array.cc:1583
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:511
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
Array< T > index(const idx_vector &i) const
Indexing without resizing.
Definition: Array.cc:697
bool iscellstr(void) const
Definition: Cell.cc:107
void resize(const dim_vector &dv, const octave_value &rfv)
Definition: Array.cc:1010
Cell concat(const Cell &rb, const Array< octave_idx_type > &ra_idx)
Definition: Cell.cc:302
octave_value retval
Definition: data.cc:6246
Cell & insert(const Cell &a, octave_idx_type r, octave_idx_type c)
Definition: Cell.cc:308
Definition: dMatrix.h:36
the exceeded dimensions are set to if fewer subscripts than dimensions are the exceeding dimensions are merged into the final requested dimension For consider the following dims
Definition: sub2ind.cc:255
void err_wrong_type_arg(const char *name, const char *s)
Definition: errwarn.cc:162
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
Array< std::string > cellstr_value(void) const
Definition: Cell.cc:126
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
void delete_elements(const octave_value_list &idx)
Definition: Cell.cc:246
octave_idx_type nnz(void) const
Definition: Cell.cc:269
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2530
p
Definition: lu.cc:138
Cell(void)
Definition: Cell.h:43
octave_idx_type length(void) const
Definition: ovl.h:96
void assign(const idx_vector &i, const Array< T > &rhs, const T &rfv)
Indexed assignment (always with resize & fill).
Definition: Array.cc:1115
for i
Definition: data.cc:5264
Cell column(octave_idx_type i) const
Definition: Cell.cc:281
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:366
octave_value resize_fill_value(void) const
Definition: Cell.cc:336
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
dim_vector dv
Definition: sub2ind.cc:263
int ndims(void) const
Definition: Array.h:590