GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Cell.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-2017 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 the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 <http://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 
64 Cell::Cell (const std::list<std::string>& lst)
65  : Array<octave_value> ()
66 {
67  size_t n = lst.size ();
68 
69  if (n > 0)
70  {
71  resize (dim_vector (n, 1));
72 
73  octave_idx_type i = 0;
74 
75  for (const auto& str : lst)
76  elem(i++,0) = str;
77  }
78 }
79 
81  : Array<octave_value> (sa.dims ())
82 {
83  octave_idx_type n = sa.numel ();
84 
85  octave_value *dst = fortran_vec ();
86  const std::string *src = sa.data ();
87 
88  for (octave_idx_type i = 0; i < n; i++)
89  dst[i] = src[i];
90 }
91 
92 // Set size to DV, filling with []. Then fill with as many elements of
93 // SV as possible.
94 
95 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim)
96  : Array<octave_value> (dv, Matrix ())
97 {
98  octave_idx_type n = sv.numel ();
99 
100  if (n > 0)
101  {
102  octave_idx_type m = numel ();
103 
104  octave_idx_type len = n > m ? m : n;
105 
106  for (octave_idx_type i = 0; i < len; i++)
107  {
108  std::string s = sv[i];
109 
110  if (trim)
111  {
112  size_t pos = s.find_last_not_of (' ');
113 
114  s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
115  }
116 
117  elem(i) = s;
118  }
119  }
120 }
121 
122 bool
123 Cell::is_cellstr (void) const
124 {
125  bool retval = true;
126 
127  octave_idx_type n = numel ();
128 
129  for (octave_idx_type i = 0; i < n; i++)
130  {
131  if (! elem(i).is_string ())
132  {
133  retval = false;
134  break;
135  }
136  }
137 
138  return retval;
139 }
140 
143 {
145 
146  octave_idx_type n = numel ();
147 
148  for (octave_idx_type i = 0; i < n; i++)
149  retval.xelem (i) = elem (i).string_value ();
150 
151  return retval;
152 }
153 
154 Cell
155 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const
156 {
157  Cell retval;
158 
159  octave_idx_type n = idx_arg.length ();
160 
161  // If we catch an indexing error in index_vector, we flag an error
162  // in index k. Ensure it is the right value befor each idx_vector
163  // call. Same variable as used in for loop in default case.
164 
165  octave_idx_type k = 0;
166 
167  try
168  {
169  switch (n)
170  {
171  case 0:
172  retval = *this;
173  break;
174 
175  case 1:
176  {
177  idx_vector i = idx_arg(0).index_vector ();
178 
179  retval = Array<octave_value>::index (i, resize_ok, Matrix ());
180  }
181  break;
182 
183  case 2:
184  {
185  idx_vector i = idx_arg(0).index_vector ();
186 
187  k = 1;
188  idx_vector j = idx_arg(1).index_vector ();
189 
190  retval = Array<octave_value>::index (i, j, resize_ok, Matrix ());
191  }
192  break;
193 
194  default:
195  {
196  Array<idx_vector> iv (dim_vector (n, 1));
197 
198  for (k = 0; k < n; k++)
199  iv(k) = idx_arg(k).index_vector ();
200 
201  retval = Array<octave_value>::index (iv, resize_ok, Matrix ());
202  }
203  break;
204  }
205  }
206  catch (octave::index_exception& e)
207  {
208  // Rethrow to allow more info to be reported later.
209  e.set_pos_if_unset (n, k+1);
210  throw;
211  }
212 
213  return retval;
214 }
215 
216 /*
217 %!test
218 %! a = {"foo", "bar"};
219 %! assert (a(), a);
220 */
221 
222 void
223 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs,
224  const octave_value& fill_val)
225 
226 {
227  octave_idx_type len = idx_arg.length ();
228 
230 
231  for (octave_idx_type i = 0; i < len; i++)
232  {
233  try
234  {
235  ra_idx(i) = idx_arg(i).index_vector ();
236  }
237  catch (octave::index_exception& e)
238  {
239  // Rethrow to allow more info to be reported later.
240  e.set_pos (len, i+1);
241  throw;
242  }
243  }
244 
245  Array<octave_value>::assign (ra_idx, rhs, fill_val);
246 }
247 
248 void
250 
251 {
252  octave_idx_type len = idx_arg.length ();
253 
255 
256  for (octave_idx_type i = 0; i < len; i++)
257  try
258  {
259  ra_idx.xelem (i) = idx_arg(i).index_vector ();
260  }
261  catch (octave::index_exception& e)
262  {
263  // Rethrow to allow more info to be reported later.
264  e.set_pos (len, i+1);
265  throw;
266  }
267 
269 }
270 
272 Cell::nnz (void) const
273 {
274  err_wrong_type_arg ("nnz", "cell array");
275 }
276 
277 /*
278 %!error <wrong type argument 'cell array'> nnz ({0, 1, 2})
279 %!error <wrong type argument 'cell array'> nnz (cell ())
280 %!error <wrong type argument 'cell array'> nnz ({"foo", "bar"})
281 */
282 
283 Cell
285 {
286  Cell retval;
287 
288  if (ndims () > 2)
289  error ("Cell::column: requires 2-D cell array");
290 
291  if (i < 0 || i >= cols ())
292  error ("invalid column selection");
293 
294  octave_idx_type nr = rows ();
295 
296  retval.resize (dim_vector (nr, 1));
297 
298  for (octave_idx_type j = 0; j < nr; j++)
299  retval.xelem (j) = elem (j, i);
300 
301  return retval;
302 }
303 
304 Cell
306 {
307  return insert (rb, ra_idx);
308 }
309 
310 Cell&
312 {
313  Array<octave_value>::insert (a, r, c);
314  return *this;
315 }
316 
317 Cell&
319 {
320  Array<octave_value>::insert (a, ra_idx);
321  return *this;
322 }
323 
324 Cell
325 Cell::map (ctype_mapper fcn) const
326 {
327  Cell retval (dims ());
328  octave_value *r = retval.fortran_vec ();
329 
330  const octave_value *p = data ();
331 
332  for (octave_idx_type i = 0; i < numel (); i++)
333  r[i] = ((p++)->*fcn) ();
334 
335  return retval;
336 }
337 
340 {
341  static octave_value rfv = octave_value (Matrix ());
342  return rfv;
343 }
344 
345 Cell
347 {
348  return Array<octave_value>::diag (k);
349 }
350 
351 Cell
353 {
354  return Array<octave_value>::diag (m, n);
355 }
octave_idx_type nnz(void) const
Definition: Cell.cc:272
Definition: Cell.h:37
octave_value resize_fill_value(void) const
Definition: Cell.cc:339
const octave_base_value const Array< octave_idx_type > & ra_idx
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
int ndims(void) const
Definition: Array.h:590
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:363
void set_pos_if_unset(octave_idx_type nd_arg, octave_idx_type dim_arg)
void delete_elements(const idx_vector &i)
Deleting elements.
Definition: Array.cc:1407
octave_idx_type length(void) const
Definition: ovl.h:96
for large enough k
Definition: lu.cc:606
void assign(const octave_value_list &idx, const Cell &rhs, const octave_value &fill_val=Matrix())
Definition: Cell.cc:223
void error(const char *fmt,...)
Definition: error.cc:570
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2529
octave_value & elem(octave_idx_type n)
Definition: Array.h:482
Cell map(ctype_mapper) const
Definition: Cell.cc:325
s
Definition: file-io.cc:2682
i e
Definition: data.cc:2724
octave_idx_type rows(void) const
Definition: Array.h:401
octave_function * fcn
Definition: ov-class.cc:1743
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:1601
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:398
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:439
Array< std::string > cellstr_value(void) const
Definition: Cell.cc:142
std::string string_value(bool force=false) const
Definition: ov.h:908
nd deftypefn *octave_map m
Definition: ov-struct.cc:2058
std::string str
Definition: hash.cc:118
const T * data(void) const
Definition: Array.h:582
void resize(const dim_vector &dv, const octave_value &rfv)
Cell concat(const Cell &rb, const Array< octave_idx_type > &ra_idx)
Definition: Cell.cc:305
octave_value retval
Definition: data.cc:6294
bool is_cellstr(void) const
Definition: Cell.cc:123
Cell & insert(const Cell &a, octave_idx_type r, octave_idx_type c)
Definition: Cell.cc:311
Definition: dMatrix.h:37
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
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:156
T & xelem(octave_idx_type n)
Definition: Array.h:455
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:126
void delete_elements(const octave_value_list &idx)
Definition: Cell.cc:249
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
Cell(void)
Definition: Cell.h:43
Cell column(octave_idx_type i) const
Definition: Cell.cc:284
Cell index(const octave_value_list &idx, bool resize_ok=false) const
Definition: Cell.cc:155
void set_pos(octave_idx_type nd_arg, octave_idx_type dim_arg)
void assign(const idx_vector &i, const Array< T > &rhs, const T &rfv)
Indexed assignment (always with resize & fill).
Definition: Array.cc:1133
const octave_value * fortran_vec(void) const
Definition: Array.h:584
octave_idx_type cols(void) const
Definition: Array.h:409
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
Cell diag(octave_idx_type k=0) const
Definition: Cell.cc:346
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:854
dim_vector dv
Definition: sub2ind.cc:263
Array< T > index(const idx_vector &i) const
Indexing without resizing.
Definition: Array.cc:718
return octave_value(v1.char_array_value().concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string())? '\'': '"'))