GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
pt-tm-const.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 John W. Eaton
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 (octave_pt_tm_const_h)
24 #define octave_pt_tm_const_h 1
25 
26 #include "octave-config.h"
27 
28 #include <string>
29 
30 #include "Array.h"
31 #include "Sparse.h"
32 #include "base-list.h"
33 
34 #include "data.h"
35 #include "dim-vector.h"
36 #include "oct-map.h"
37 #include "ov.h"
38 #include "ovl.h"
39 #include "pt-arg-list.h"
40 
41 namespace octave
42 {
43  class tree_evaluator;
44  class tree_matrix;
45 
46  // General matrices. This list type is much more work to handle than
47  // constant matrices, but it allows us to construct matrices from
48  // other matrices, variables, and functions.
49 
50  // But first, some internal classes that make our job much easier.
51 
52  class
54  {
55  private:
56 
57  class
59  {
60  public:
61 
63  : m_count (1), m_dv (0, 0), m_all_str (false),
64  m_all_sq_str (false), m_all_dq_str (false),
65  m_some_str (false), m_all_real (false), m_all_cmplx (false),
66  m_all_mt (true), m_any_cell (false), m_any_sparse (false),
67  m_any_class (false), m_all_1x1 (false),
68  m_first_elem_is_struct (false), m_class_nm (), m_ok (false)
69  { }
70 
72  : m_count (1), m_dv (0, 0), m_all_str (false), m_all_sq_str (false),
73  m_some_str (false), m_all_real (false), m_all_cmplx (false),
74  m_all_mt (true), m_any_cell (false), m_any_sparse (false),
75  m_any_class (false), m_all_1x1 (! row.empty ()),
76  m_first_elem_is_struct (false), m_class_nm (), m_ok (false)
77  { init (row, tw); }
78 
79  ~tm_row_const_rep (void) = default;
80 
82 
84 
85  bool m_all_str;
88  bool m_some_str;
89  bool m_all_real;
91  bool m_all_mt;
92  bool m_any_cell;
95  bool m_all_1x1;
97 
99 
100  bool m_ok;
101 
102  void do_init_element (const octave_value&, bool&);
103 
104  void init (const tree_argument_list&, tree_evaluator *tw);
105 
106  void cellify (void);
107 
108  private:
109 
111 
112  tm_row_const_rep& operator = (const tm_row_const_rep&);
113 
114  };
115 
116  public:
117 
120 
122  : m_rep (nullptr) { }
123 
125  : m_rep (new tm_row_const_rep (row, tw)) { }
126 
128  : m_rep (x.m_rep)
129  {
130  if (m_rep)
131  m_rep->m_count++;
132  }
133 
134  tm_row_const& operator = (const tm_row_const& x)
135  {
136  if (this != &x && m_rep != x.m_rep)
137  {
138  if (m_rep && --m_rep->m_count == 0)
139  delete m_rep;
140 
141  m_rep = x.m_rep;
142 
143  if (m_rep)
144  m_rep->m_count++;
145  }
146 
147  return *this;
148  }
149 
151  {
152  if (m_rep && --m_rep->m_count == 0)
153  delete m_rep;
154  }
155 
156  octave_idx_type rows (void) { return m_rep->m_dv(0); }
157  octave_idx_type cols (void) { return m_rep->m_dv(1); }
158 
159  bool empty (void) const { return m_rep->empty (); }
160 
161  size_t length (void) const { return m_rep->length (); }
162 
163  dim_vector dims (void) { return m_rep->m_dv; }
164 
165  bool all_strings_p (void) const { return m_rep->m_all_str; }
166  bool all_sq_strings_p (void) const { return m_rep->m_all_sq_str; }
167  bool all_dq_strings_p (void) const { return m_rep->m_all_dq_str; }
168  bool some_strings_p (void) const { return m_rep->m_some_str; }
169  bool all_real_p (void) const { return m_rep->m_all_real; }
170  bool all_complex_p (void) const { return m_rep->m_all_cmplx; }
171  bool all_empty_p (void) const { return m_rep->m_all_mt; }
172  bool any_cell_p (void) const { return m_rep->m_any_cell; }
173  bool any_sparse_p (void) const { return m_rep->m_any_sparse; }
174  bool any_class_p (void) const { return m_rep->m_any_class; }
175  bool all_1x1_p (void) const { return m_rep->m_all_1x1; }
176  bool first_elem_struct_p (void) const { return m_rep->m_first_elem_is_struct; }
177 
178  std::string class_name (void) const { return m_rep->m_class_nm; }
179 
180  void cellify (void) { m_rep->cellify (); }
181 
182  operator bool () const { return (m_rep && m_rep->m_ok); }
183 
184  iterator begin (void) { return m_rep->begin (); }
185  const_iterator begin (void) const { return m_rep->begin (); }
186 
187  iterator end (void) { return m_rep->end (); }
188  const_iterator end (void) const { return m_rep->end (); }
189 
190  private:
191 
193  };
194 
195  class
197  {
198  public:
199 
200  tm_const (const tree_matrix& tm, tree_evaluator *tw = nullptr)
201  : m_dv (0, 0), m_all_str (false), m_all_sq_str (false),
202  m_all_dq_str (false),
203  m_some_str (false), m_all_real (false), m_all_cmplx (false),
204  m_all_mt (true), m_any_cell (false), m_any_sparse (false),
205  m_any_class (false), m_class_nm (), m_ok (false)
206  { init (tm, tw); }
207 
208  ~tm_const (void) = default;
209 
210  octave_idx_type rows (void) const { return m_dv.elem (0); }
211  octave_idx_type cols (void) const { return m_dv.elem (1); }
212 
213  dim_vector dims (void) const { return m_dv; }
214 
215  bool all_strings_p (void) const { return m_all_str; }
216  bool all_sq_strings_p (void) const { return m_all_sq_str; }
217  bool all_dq_strings_p (void) const { return m_all_dq_str; }
218  bool some_strings_p (void) const { return m_some_str; }
219  bool all_real_p (void) const { return m_all_real; }
220  bool all_complex_p (void) const { return m_all_cmplx; }
221  bool all_empty_p (void) const { return m_all_mt; }
222  bool any_cell_p (void) const { return m_any_cell; }
223  bool any_sparse_p (void) const { return m_any_sparse; }
224  bool any_class_p (void) const { return m_any_class; }
225  bool all_1x1_p (void) const { return m_all_1x1; }
226 
227  std::string class_name (void) const { return m_class_nm; }
228 
229  operator bool () const { return m_ok; }
230 
231  private:
232 
234 
235  bool m_all_str;
241  bool m_all_mt;
245  bool m_all_1x1;
246 
248 
249  bool m_ok;
250 
251  tm_const (void);
252 
253  tm_const (const tm_const&);
254 
255  tm_const& operator = (const tm_const&);
256 
257  void init (const tree_matrix& tm, tree_evaluator *tw);
258  };
259 
260  template <typename TYPE, typename T>
261  void
263  {
264  octave_idx_type r = 0;
265  octave_idx_type c = 0;
266 
267  for (tm_row_const& row : tmp)
268  {
269  // Skip empty arrays to allow looser rules.
270  if (row.dims ().any_zero ())
271  continue;
272 
273  for (auto& elt : row)
274  {
275  octave_quit ();
276 
277  TYPE ra = octave_value_extract<TYPE> (elt);
278 
279  // Skip empty arrays to allow looser rules.
280 
281  if (! ra.isempty ())
282  {
283  result.insert (ra, r, c);
284 
285  c += ra.columns ();
286  }
287  }
288 
289  r += row.rows ();
290  c = 0;
291  }
292  }
293 
294  template <typename TYPE, typename T>
295  void
297  tm_const& tmp)
298  {
299  if (dv.any_zero ())
300  {
301  result = Array<T> (dv);
302  return;
303  }
304 
305  if (tmp.length () == 1)
306  {
307  // If possible, forward the operation to liboctave.
308  // Single row.
309  tm_row_const& row = tmp.front ();
311  && row.all_1x1_p ())
312  {
313  // Optimize all scalars case.
314  result.clear (dv);
315  assert (static_cast<size_t> (result.numel ()) == row.length ());
316  octave_idx_type i = 0;
317  for (const auto& elt : row)
318  result(i++) = octave_value_extract<T> (elt);
319 
320  return;
321  }
322 
323  octave_idx_type ncols = row.length ();
324  octave_idx_type i = 0;
325  OCTAVE_LOCAL_BUFFER (Array<T>, array_list, ncols);
326 
327  for (const auto& elt : row)
328  {
329  octave_quit ();
330 
331  array_list[i++] = octave_value_extract<TYPE> (elt);
332  }
333 
334  result = Array<T>::cat (-2, ncols, array_list);
335  }
336  else
337  {
338  result = Array<T> (dv);
339  single_type_concat<TYPE> (result, tmp);
340  }
341  }
342 
343  template <typename TYPE, typename T>
344  void
346  tm_const& tmp)
347  {
348  if (dv.any_zero ())
349  {
350  result = Sparse<T> (dv);
351  return;
352  }
353 
354  // Sparse matrices require preallocation for efficient indexing; besides,
355  // only horizontal concatenation can be efficiently handled by indexing.
356  // So we just cat all rows through liboctave, then cat the final column.
357  octave_idx_type nrows = tmp.length ();
358  octave_idx_type j = 0;
359  OCTAVE_LOCAL_BUFFER (Sparse<T>, sparse_row_list, nrows);
360  for (tm_row_const& row : tmp)
361  {
362  octave_idx_type ncols = row.length ();
363  octave_idx_type i = 0;
364  OCTAVE_LOCAL_BUFFER (Sparse<T>, sparse_list, ncols);
365 
366  for (auto& elt : row)
367  {
368  octave_quit ();
369 
370  sparse_list[i] = octave_value_extract<TYPE> (elt);
371  i++;
372  }
373 
374  Sparse<T> stmp = Sparse<T>::cat (-2, ncols, sparse_list);
375  sparse_row_list[j] = stmp;
376  j++;
377  }
378 
379  result = Sparse<T>::cat (-1, nrows, sparse_row_list);
380  }
381 
382  template <typename MAP>
383  void
385  tm_const& tmp)
386  {
387  if (dv.any_zero ())
388  {
389  result = octave_map (dv);
390  return;
391  }
392 
393  octave_idx_type nrows = tmp.length ();
394  octave_idx_type j = 0;
395  OCTAVE_LOCAL_BUFFER (octave_map, map_row_list, nrows);
396  for (tm_row_const& row : tmp)
397  {
398  octave_idx_type ncols = row.length ();
399  octave_idx_type i = 0;
400  OCTAVE_LOCAL_BUFFER (MAP, map_list, ncols);
401 
402  for (auto& elt : row)
403  {
404  octave_quit ();
405 
406  map_list[i] = octave_value_extract<MAP> (elt);
407  i++;
408  }
409 
410  octave_map mtmp = octave_map::cat (-2, ncols, map_list);
411  map_row_list[j] = mtmp;
412  j++;
413  }
414 
415  result = octave_map::cat (-1, nrows, map_row_list);
416  }
417 
418  template <typename TYPE>
421  {
422  TYPE result;
423 
424  single_type_concat<TYPE> (result, dv, tmp);
425 
426  return result;
427  }
428 
429  template <>
432  tm_const& tmp);
433 
434  extern octave_value do_class_concat (tm_const& tmc);
435 }
436 
437 #endif
octave_idx_type rows(void)
Definition: pt-tm-const.h:156
static octave_map cat(int dim, octave_idx_type n, const octave_scalar_map *map_list)
Definition: oct-map.cc:691
size_t length(void) const
Definition: pt-tm-const.h:161
bool all_strings_p(void) const
Definition: pt-tm-const.h:165
bool any_sparse_p(void) const
Definition: pt-tm-const.h:223
bool all_sq_strings_p(void) const
Definition: pt-tm-const.h:166
bool empty(void) const
Definition: pt-tm-const.h:159
bool some_strings_p(void) const
Definition: pt-tm-const.h:218
dim_vector dims(void)
Definition: pt-tm-const.h:163
std::list< octave_value >::iterator iterator
Definition: base-list.h:40
bool any_sparse_p(void) const
Definition: pt-tm-const.h:173
octave_idx_type cols(void) const
Definition: pt-tm-const.h:211
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
bool all_empty_p(void) const
Definition: pt-tm-const.h:221
tm_row_const_rep::iterator iterator
Definition: pt-tm-const.h:118
octave_idx_type cols(void)
Definition: pt-tm-const.h:157
octave_value do_single_type_concat(const dim_vector &dv, tm_const &tmp)
Definition: pt-tm-const.h:420
bool any_zero(void) const
Definition: dim-vector.h:343
bool any_class_p(void) const
Definition: pt-tm-const.h:224
tm_const(const tree_matrix &tm, tree_evaluator *tw=nullptr)
Definition: pt-tm-const.h:200
bool all_real_p(void) const
Definition: pt-tm-const.h:169
bool all_strings_p(void) const
Definition: pt-tm-const.h:215
create a structure array and initialize its values The dimensions of each cell array of values must match Singleton cells and non cell values are repeated so that they fill the entire array If the cells are empty
Definition: ov-struct.cc:1736
bool all_dq_strings_p(void) const
Definition: pt-tm-const.h:167
iterator end(void)
Definition: pt-tm-const.h:187
bool all_complex_p(void) const
Definition: pt-tm-const.h:170
bool some_strings_p(void) const
Definition: pt-tm-const.h:168
tm_row_const_rep * m_rep
Definition: pt-tm-const.h:192
static Array< T > cat(int dim, octave_idx_type n, const Array< T > *array_list)
Concatenation along a specified (0-based) dimension, equivalent to cat().
Definition: Array.cc:2631
double tmp
Definition: data.cc:6252
is false
Definition: cellfun.cc:400
octave_value do_single_type_concat< octave_map >(const dim_vector &dv, tm_const &tmp)
Definition: pt-tm-const.cc:368
bool all_dq_strings_p(void) const
Definition: pt-tm-const.h:217
std::string class_name(void) const
Definition: pt-tm-const.h:227
bool first_elem_struct_p(void) const
Definition: pt-tm-const.h:176
dim_vector m_dv
Definition: pt-tm-const.h:233
iterator begin(void)
Definition: pt-tm-const.h:184
With real return the complex result
Definition: data.cc:3260
std::string m_class_nm
Definition: pt-tm-const.h:247
const_iterator end(void) const
Definition: pt-tm-const.h:188
bool all_1x1_p(void) const
Definition: pt-tm-const.h:225
N Dimensional Array with copy-on-write semantics.
Definition: Array.h:125
bool any_class_p(void) const
Definition: pt-tm-const.h:174
std::list< octave_value >::const_iterator const_iterator
Definition: base-list.h:41
tm_row_const_rep(const tree_argument_list &row, tree_evaluator *tw)
Definition: pt-tm-const.h:71
bool any_cell_p(void) const
Definition: pt-tm-const.h:222
tm_row_const(const tree_argument_list &row, tree_evaluator *tw)
Definition: pt-tm-const.h:124
octave_value do_class_concat(tm_const &tmc)
Definition: pt-tm-const.cc:381
bool all_real_p(void) const
Definition: pt-tm-const.h:219
tm_row_const(const tm_row_const &x)
Definition: pt-tm-const.h:127
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:41
bool all_complex_p(void) const
Definition: pt-tm-const.h:220
for i
Definition: data.cc:5264
bool all_1x1_p(void) const
Definition: pt-tm-const.h:175
tm_row_const_rep::const_iterator const_iterator
Definition: pt-tm-const.h:119
octave_idx_type rows(void) const
Definition: pt-tm-const.h:210
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
bool any_cell_p(void) const
Definition: pt-tm-const.h:172
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
void single_type_concat(Array< T > &result, tm_const &tmp)
Definition: pt-tm-const.h:262
std::string class_name(void) const
Definition: pt-tm-const.h:178
dim_vector dv
Definition: sub2ind.cc:263
where the brackets indicate optional arguments and and character or cell array For character arrays the conversion is repeated for every row
Definition: str2double.cc:342
static Sparse< T > cat(int dim, octave_idx_type n, const Sparse< T > *sparse_list)
Definition: Sparse.cc:2577
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
bool all_empty_p(void) const
Definition: pt-tm-const.h:171
bool all_sq_strings_p(void) const
Definition: pt-tm-const.h:216
const_iterator begin(void) const
Definition: pt-tm-const.h:185
dim_vector dims(void) const
Definition: pt-tm-const.h:213