GNU Octave  4.0.0
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-2015 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 #ifdef 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 "gripes.h"
33 #include "oct-obj.h"
34 
36  : Array<octave_value> (ovl.cell_value ())
37 {
38 }
39 
40 Cell::Cell (const string_vector& sv, bool trim)
41  : Array<octave_value> ()
42 {
43  octave_idx_type n = sv.length ();
44 
45  if (n > 0)
46  {
47  resize (dim_vector (n, 1));
48 
49  for (octave_idx_type i = 0; i < n; i++)
50  {
51  std::string s = sv[i];
52 
53  if (trim)
54  {
55  size_t pos = s.find_last_not_of (' ');
56 
57  s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
58  }
59 
60  elem(i,0) = s;
61  }
62  }
63 }
64 
65 Cell::Cell (const std::list<std::string>& lst)
66  : Array<octave_value> ()
67 {
68  size_t n = lst.size ();
69 
70  if (n > 0)
71  {
72  resize (dim_vector (n, 1));
73 
74  octave_idx_type i = 0;
75 
76  for (std::list<std::string>::const_iterator it = lst.begin ();
77  it != lst.end (); it++)
78  {
79  elem(i++,0) = *it;
80  }
81  }
82 }
83 
85  : Array<octave_value> (sa.dims ())
86 {
87  octave_idx_type n = sa.numel ();
88 
89  octave_value *dst = fortran_vec ();
90  const std::string *src = sa.data ();
91 
92  for (octave_idx_type i = 0; i < n; i++)
93  dst[i] = src[i];
94 }
95 
96 // Set size to DV, filling with []. Then fill with as many elements of
97 // SV as possible.
98 
99 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim)
100  : Array<octave_value> (dv, Matrix ())
101 {
102  octave_idx_type n = sv.length ();
103 
104  if (n > 0)
105  {
106  octave_idx_type m = numel ();
107 
108  octave_idx_type len = n > m ? m : n;
109 
110  for (octave_idx_type i = 0; i < len; i++)
111  {
112  std::string s = sv[i];
113 
114  if (trim)
115  {
116  size_t pos = s.find_last_not_of (' ');
117 
118  s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
119  }
120 
121  elem(i) = s;
122  }
123  }
124 }
125 
126 bool
127 Cell::is_cellstr (void) const
128 {
129  bool retval = true;
130 
131  octave_idx_type n = numel ();
132 
133  for (octave_idx_type i = 0; i < n; i++)
134  {
135  if (! elem(i).is_string ())
136  {
137  retval = false;
138  break;
139  }
140  }
141 
142  return retval;
143 }
144 
147 {
148  Array<std::string> retval (dims ());
149 
150  octave_idx_type n = numel ();
151 
152  for (octave_idx_type i = 0; i < n; i++)
153  retval.xelem (i) = elem (i).string_value ();
154 
155  return retval;
156 }
157 
158 Cell
159 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const
160 {
161  Cell retval;
162 
163  octave_idx_type n = idx_arg.length ();
164 
165  switch (n)
166  {
167  case 0:
168  retval = *this;
169  break;
170 
171  case 1:
172  {
173  idx_vector i = idx_arg(0).index_vector ();
174 
175  if (! error_state)
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  if (! error_state)
185  {
186  idx_vector j = idx_arg(1).index_vector ();
187 
188  if (! error_state)
189  retval = Array<octave_value>::index (i, j, resize_ok, Matrix ());
190  }
191  }
192  break;
193 
194  default:
195  {
196  Array<idx_vector> iv (dim_vector (n, 1));
197 
198  for (octave_idx_type i = 0; i < n; i++)
199  {
200  iv(i) = idx_arg(i).index_vector ();
201 
202  if (error_state)
203  break;
204  }
205 
206  if (!error_state)
207  retval = Array<octave_value>::index (iv, resize_ok, Matrix ());
208  }
209  break;
210  }
211 
212  return retval;
213 }
214 
215 /*
216 %!test
217 %! a = {"foo", "bar"};
218 %! assert (a(), a)
219 */
220 
221 void
222 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs,
223  const octave_value& fill_val)
224 
225 {
226  octave_idx_type len = idx_arg.length ();
227 
229 
230  for (octave_idx_type i = 0; i < len; i++)
231  ra_idx(i) = idx_arg(i).index_vector ();
232 
233  Array<octave_value>::assign (ra_idx, rhs, fill_val);
234 }
235 
236 void
238 
239 {
240  octave_idx_type len = idx_arg.length ();
241 
243 
244  for (octave_idx_type i = 0; i < len; i++)
245  ra_idx.xelem (i) = idx_arg(i).index_vector ();
246 
248 }
249 
251 Cell::nnz (void) const
252 {
253  gripe_wrong_type_arg ("nnz", "cell array");
254  return -1;
255 }
256 
257 /*
258 %!error <wrong type argument 'cell array'> nnz ({0, 1, 2})
259 %!error <wrong type argument 'cell array'> nnz (cell ())
260 %!error <wrong type argument 'cell array'> nnz ({"foo", "bar"})
261 */
262 
263 Cell
265 {
266  Cell retval;
267 
268  if (ndims () < 3)
269  {
270  if (i < 0 || i >= cols ())
271  error ("invalid column selection");
272  else
273  {
274  octave_idx_type nr = rows ();
275 
276  retval.resize (dim_vector (nr, 1));
277 
278  for (octave_idx_type j = 0; j < nr; j++)
279  retval.xelem (j) = elem (j, i);
280  }
281  }
282  else
283  error ("Cell::column: requires 2-d cell array");
284 
285  return retval;
286 }
287 
288 Cell
290 {
291  return insert (rb, ra_idx);
292 }
293 
294 Cell&
296 {
297  Array<octave_value>::insert (a, r, c);
298  return *this;
299 }
300 
301 Cell&
303 {
304  Array<octave_value>::insert (a, ra_idx);
305  return *this;
306 }
307 
308 Cell
309 Cell::map (ctype_mapper fcn) const
310 {
311  Cell retval (dims ());
312  octave_value *r = retval.fortran_vec ();
313 
314  const octave_value *p = data ();
315 
316  for (octave_idx_type i = 0; i < numel (); i++)
317  r[i] = ((p++)->*fcn) ();
318 
319  return retval;
320 }
321 
322 Cell
324 {
325  return Array<octave_value>::diag (k);
326 }
327 
328 Cell
330 {
331  return Array<octave_value>::diag (m, n);
332 }
octave_idx_type nnz(void) const
Definition: Cell.cc:251
Definition: Cell.h:35
void gripe_wrong_type_arg(const char *name, const char *s, bool is_error)
Definition: gripes.cc:135
const octave_base_value const Array< octave_idx_type > & ra_idx
int ndims(void) const
Definition: Array.h:487
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
void delete_elements(const idx_vector &i)
Deleting elements.
Definition: Array.cc:1393
octave_idx_type length(void) const
Definition: oct-obj.h:89
void assign(const octave_value_list &idx, const Cell &rhs, const octave_value &fill_val=Matrix())
Definition: Cell.cc:222
void error(const char *fmt,...)
Definition: error.cc:476
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2526
octave_value & elem(octave_idx_type n)
Definition: Array.h:380
Cell map(ctype_mapper) const
Definition: Cell.cc:309
octave_idx_type rows(void) const
Definition: Array.h:313
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:1591
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:337
Array< std::string > cellstr_value(void) const
Definition: Cell.cc:146
std::string string_value(bool force=false) const
Definition: ov.h:897
const T * data(void) const
Definition: Array.h:479
int error_state
Definition: error.cc:101
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:289
bool is_cellstr(void) const
Definition: Cell.cc:127
Cell & insert(const Cell &a, octave_idx_type r, octave_idx_type c)
Definition: Cell.cc:295
Definition: dMatrix.h:35
T & xelem(octave_idx_type n)
Definition: Array.h:353
Handles the reference counting for all the derived classes.
Definition: Array.h:45
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:267
octave_value_list ovl(const octave_value &a0)
Definition: oct-obj.h:178
void delete_elements(const octave_value_list &idx)
Definition: Cell.cc:237
Cell(void)
Definition: Cell.h:41
Cell column(octave_idx_type i) const
Definition: Cell.cc:264
Cell index(const octave_value_list &idx, bool resize_ok=false) const
Definition: Cell.cc:159
void assign(const idx_vector &i, const Array< T > &rhs, const T &rfv)
Indexed assignment (always with resize & fill).
Definition: Array.cc:1140
const octave_value * fortran_vec(void) const
Definition: Array.h:481
octave_idx_type cols(void) const
Definition: Array.h:321
Cell diag(octave_idx_type k=0) const
Definition: Cell.cc:323
Array< T > index(const idx_vector &i) const
Indexing without resizing.
Definition: Array.cc:716