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
Array.h
Go to the documentation of this file.
1 // Template array classes
2 /*
3 
4 Copyright (C) 1993-2015 John W. Eaton
5 Copyright (C) 2008-2009 Jaroslav Hajek
6 Copyright (C) 2010 VZLU Prague
7 
8 This file is part of Octave.
9 
10 Octave is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 3 of the License, or (at your
13 option) any later version.
14 
15 Octave is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with Octave; see the file COPYING. If not, see
22 <http://www.gnu.org/licenses/>.
23 
24 */
25 
26 #if !defined (octave_Array_h)
27 #define octave_Array_h 1
28 
29 #include <cassert>
30 #include <cstddef>
31 
32 #include <algorithm>
33 #include <iosfwd>
34 
35 #include "dim-vector.h"
36 #include "idx-vector.h"
37 #include "lo-traits.h"
38 #include "lo-utils.h"
39 #include "oct-sort.h"
40 #include "quit.h"
41 #include "oct-refcount.h"
42 
43 //!Handles the reference counting for all the derived classes.
44 template <class T>
45 class
46 Array
47 {
48 protected:
49 
50  //! The real representation of all arrays.
51  class ArrayRep
52  {
53  public:
54 
55  T *data;
58 
60  : data (new T [l]), len (l), count (1)
61  {
62  std::copy (d, d+l, data);
63  }
64 
65  template <class U>
67  : data (new T [l]), len (l), count (1)
68  {
69  std::copy (d, d+l, data);
70  }
71 
72  ArrayRep (void) : data (0), len (0), count (1) { }
73 
75  : data (new T [n]), len (n), count (1) { }
76 
77  explicit ArrayRep (octave_idx_type n, const T& val)
78  : data (new T [n]), len (n), count (1)
79  {
80  std::fill_n (data, n, val);
81  }
82 
83  ArrayRep (const ArrayRep& a)
84  : data (new T [a.len]), len (a.len), count (1)
85  {
86  std::copy (a.data, a.data + a.len, data);
87  }
88 
89  ~ArrayRep (void) { delete [] data; }
90 
91  octave_idx_type length (void) const { return len; }
92 
93  private:
94 
95  // No assignment!
96 
97  ArrayRep& operator = (const ArrayRep& a);
98  };
99 
100  //--------------------------------------------------------------------
101 
102 public:
103 
104  void make_unique (void)
105  {
106  if (rep->count > 1)
107  {
108  ArrayRep *r = new ArrayRep (slice_data, slice_len);
109 
110  if (--rep->count == 0)
111  delete rep;
112 
113  rep = r;
114  slice_data = rep->data;
115  }
116  }
117 
118  typedef T element_type;
119 
120  typedef typename ref_param<T>::type crefT;
121 
122  typedef bool (*compare_fcn_type) (typename ref_param<T>::type,
123  typename ref_param<T>::type);
124 
125 protected:
126 
128 
130 
131  // Rationale:
132  // slice_data is a pointer to rep->data, denoting together with slice_len the
133  // actual portion of the data referenced by this Array<T> object. This allows
134  // to make shallow copies not only of a whole array, but also of contiguous
135  // subranges. Every time rep is directly manipulated, slice_data and slice_len
136  // need to be properly updated.
137 
140 
141  //! slice constructor
142  Array (const Array<T>& a, const dim_vector& dv,
144  : dimensions (dv), rep(a.rep), slice_data (a.slice_data+l), slice_len (u-l)
145  {
146  rep->count++;
147  dimensions.chop_trailing_singletons ();
148  }
149 
150 private:
151 
152  typename Array<T>::ArrayRep *nil_rep (void) const
153  {
154  // NR was originally allocated with new, but that does not seem
155  // to be necessary since it will never be deleted. So just use
156  // a static object instead.
157 
158  static typename Array<T>::ArrayRep nr;
159  return &nr;
160  }
161 
162 protected:
163 
164  //! For jit support
165  Array (T *sdata, octave_idx_type slen, octave_idx_type *adims, void *arep)
166  : dimensions (adims),
167  rep (reinterpret_cast<typename Array<T>::ArrayRep *> (arep)),
168  slice_data (sdata), slice_len (slen) { }
169 
170 public:
171 
172  //! Empty ctor (0 by 0).
173  Array (void)
174  : dimensions (), rep (nil_rep ()), slice_data (rep->data),
175  slice_len (rep->len)
176  {
177  rep->count++;
178  }
179 
180  //! nD uninitialized ctor.
181  explicit Array (const dim_vector& dv)
182  : dimensions (dv),
183  rep (new typename Array<T>::ArrayRep (dv.safe_numel ())),
184  slice_data (rep->data), slice_len (rep->len)
185  {
186  dimensions.chop_trailing_singletons ();
187  }
188 
189  //! nD initialized ctor.
190  explicit Array (const dim_vector& dv, const T& val)
191  : dimensions (dv),
192  rep (new typename Array<T>::ArrayRep (dv.safe_numel ())),
193  slice_data (rep->data), slice_len (rep->len)
194  {
195  fill (val);
196  dimensions.chop_trailing_singletons ();
197  }
198 
199  //! Reshape constructor.
200  Array (const Array<T>& a, const dim_vector& dv);
201 
202  //! Type conversion case.
203  template <class U>
204  Array (const Array<U>& a)
205  : dimensions (a.dims ()),
206  rep (new typename Array<T>::ArrayRep (a.data (), a.length ())),
207  slice_data (rep->data), slice_len (rep->len)
208  { }
209 
210  //! No type conversion case.
211  Array (const Array<T>& a)
212  : dimensions (a.dimensions), rep (a.rep), slice_data (a.slice_data),
213  slice_len (a.slice_len)
214  {
215  rep->count++;
216  }
217 
218 public:
219 
220  virtual ~Array (void)
221  {
222  if (--rep->count == 0)
223  delete rep;
224  }
225 
226  Array<T>& operator = (const Array<T>& a)
227  {
228  if (this != &a)
229  {
230  if (--rep->count == 0)
231  delete rep;
232 
233  rep = a.rep;
234  rep->count++;
235 
236  dimensions = a.dimensions;
237  slice_data = a.slice_data;
238  slice_len = a.slice_len;
239  }
240 
241  return *this;
242  }
243 
244  void fill (const T& val);
245 
246  void clear (void);
247  void clear (const dim_vector& dv);
248 
250  { clear (dim_vector (r, c)); }
251 
252  // Number of elements in the array. These are all synonyms.
253  //@{
254  //! Number of elements in the array.
255  //! Synonymous with length(), nelem(), and numel().
256  octave_idx_type capacity (void) const { return slice_len; }
257 
258  //! Number of elements in the array.
259  /*! Synonymous with capacity(), nelem(), and numel().
260 
261  @note
262  This is @em not the same as @c %length() at the Octave interpreter.
263  At the Octave interpreter, the function @c %length() returns the
264  length of the greatest dimension. This method returns the total
265  number of elements.
266  */
267  octave_idx_type length (void) const { return capacity (); }
268 
269  //! Number of elements in the array.
270  //! Synonymous with capacity(), length(), and numel().
271  octave_idx_type nelem (void) const { return capacity (); }
272 
273  //! Number of elements in the array.
274  //! Synonymous with capacity(), length(), and nelem().
275  octave_idx_type numel (void) const { return nelem (); }
276  //@}
277 
278  //! Return the array as a column vector.
279  Array<T> as_column (void) const
280  {
281  Array<T> retval (*this);
282  if (dimensions.length () != 2 || dimensions(1) != 1)
283  retval.dimensions = dim_vector (numel (), 1);
284 
285  return retval;
286  }
287 
288  //! Return the array as a row vector.
289  Array<T> as_row (void) const
290  {
291  Array<T> retval (*this);
292  if (dimensions.length () != 2 || dimensions(0) != 1)
293  retval.dimensions = dim_vector (1, numel ());
294 
295  return retval;
296  }
297 
298  //! Return the array as a matrix.
299  Array<T> as_matrix (void) const
300  {
301  Array<T> retval (*this);
302  if (dimensions.length () != 2)
303  retval.dimensions = dimensions.redim (2);
304 
305  return retval;
306  }
307 
308  //! @name First dimension
309  //!
310  //! Get the first dimension of the array (number of rows)
311  //@{
312  octave_idx_type dim1 (void) const { return dimensions(0); }
313  octave_idx_type rows (void) const { return dim1 (); }
314  //@}
315 
316  //! @name Second dimension
317  //!
318  //! Get the second dimension of the array (number of columns)
319  //@{
320  octave_idx_type dim2 (void) const { return dimensions(1); }
321  octave_idx_type cols (void) const { return dim2 (); }
322  octave_idx_type columns (void) const { return dim2 (); }
323  //@}
324 
325  //! @name Third dimension
326  //!
327  //! Get the third dimension of the array (number of pages)
328  //@{
329  octave_idx_type dim3 (void) const { return dimensions(2); }
330  octave_idx_type pages (void) const { return dim3 (); }
331  //@}
332 
333  size_t byte_size (void) const
334  { return static_cast<size_t> (numel ()) * sizeof (T); }
335 
336  //! Return a const-reference so that dims ()(i) works efficiently.
337  const dim_vector& dims (void) const { return dimensions; }
338 
339  //! Chop off leading singleton dimensions
340  Array<T> squeeze (void) const;
341 
344  octave_idx_type k) const;
346 
348  const
349  { return dimensions.compute_index (ra_idx.data (), ra_idx.length ()); }
350 
351  // No checking, even for multiple references, ever.
352 
353  T& xelem (octave_idx_type n) { return slice_data[n]; }
354  crefT xelem (octave_idx_type n) const { return slice_data[n]; }
355 
357  { return xelem (dim1 ()*j+i); }
359  { return xelem (dim1 ()*j+i); }
360 
362  { return xelem (i, dim2 ()*k+j); }
364  { return xelem (i, dim2 ()*k+j); }
365 
367  { return xelem (compute_index_unchecked (ra_idx)); }
368 
369  crefT xelem (const Array<octave_idx_type>& ra_idx) const
370  { return xelem (compute_index_unchecked (ra_idx)); }
371 
372  // FIXME: would be nice to fix this so that we don't unnecessarily force
373  // a copy, but that is not so easy, and I see no clean way to do it.
374 
375  T& checkelem (octave_idx_type n);
376  T& checkelem (octave_idx_type i, octave_idx_type j);
377  T& checkelem (octave_idx_type i, octave_idx_type j, octave_idx_type k);
378  T& checkelem (const Array<octave_idx_type>& ra_idx);
379 
381  {
382  make_unique ();
383  return xelem (n);
384  }
385 
386  T& elem (octave_idx_type i, octave_idx_type j) { return elem (dim1 ()*j+i); }
387 
389  { return elem (i, dim2 ()*k+j); }
390 
392  { return Array<T>::elem (compute_index_unchecked (ra_idx)); }
393 
394 #if defined (BOUNDS_CHECKING)
395  T& operator () (octave_idx_type n) { return checkelem (n); }
396  T& operator () (octave_idx_type i, octave_idx_type j)
397  { return checkelem (i, j); }
398  T& operator () (octave_idx_type i, octave_idx_type j, octave_idx_type k)
399  { return checkelem (i, j, k); }
400  T& operator () (const Array<octave_idx_type>& ra_idx)
401  { return checkelem (ra_idx); }
402 #else
403  T& operator () (octave_idx_type n) { return elem (n); }
404  T& operator () (octave_idx_type i, octave_idx_type j) { return elem (i, j); }
406  { return elem (i, j, k); }
407  T& operator () (const Array<octave_idx_type>& ra_idx)
408  { return elem (ra_idx); }
409 #endif
410 
411  crefT checkelem (octave_idx_type n) const;
412  crefT checkelem (octave_idx_type i, octave_idx_type j) const;
413  crefT checkelem (octave_idx_type i, octave_idx_type j,
414  octave_idx_type k) const;
415  crefT checkelem (const Array<octave_idx_type>& ra_idx) const;
416 
417  crefT elem (octave_idx_type n) const { return xelem (n); }
418 
420  { return xelem (i, j); }
421 
423  { return xelem (i, j, k); }
424 
425  crefT elem (const Array<octave_idx_type>& ra_idx) const
426  { return Array<T>::xelem (compute_index_unchecked (ra_idx)); }
427 
428 #if defined (BOUNDS_CHECKING)
429  crefT operator () (octave_idx_type n) const { return checkelem (n); }
430  crefT operator () (octave_idx_type i, octave_idx_type j) const
431  { return checkelem (i, j); }
432  crefT operator () (octave_idx_type i, octave_idx_type j,
433  octave_idx_type k) const
434  { return checkelem (i, j, k); }
435  crefT operator () (const Array<octave_idx_type>& ra_idx) const
436  { return checkelem (ra_idx); }
437 #else
438  crefT operator () (octave_idx_type n) const { return elem (n); }
439  crefT operator () (octave_idx_type i, octave_idx_type j) const
440  { return elem (i, j); }
441  crefT operator () (octave_idx_type i, octave_idx_type j,
442  octave_idx_type k) const
443  { return elem (i, j, k); }
444  crefT operator () (const Array<octave_idx_type>& ra_idx) const
445  { return elem (ra_idx); }
446 #endif
447 
448  // Fast extractors. All of these produce shallow copies.
449  // Warning: none of these do check bounds, unless BOUNDS_CHECKING is on!
450 
451  //! Extract column: A(:,k+1).
452  Array<T> column (octave_idx_type k) const;
453  //! Extract page: A(:,:,k+1).
454  Array<T> page (octave_idx_type k) const;
455 
456  //! Extract a slice from this array as a column vector: A(:)(lo+1:up).
457  //! Must be 0 <= lo && up <= numel. May be up < lo.
458  Array<T> linear_slice (octave_idx_type lo, octave_idx_type up) const;
459 
461  { return Array<T> (*this, dim_vector (nr, nc)); }
462 
463  Array<T> reshape (const dim_vector& new_dims) const
464  { return Array<T> (*this, new_dims); }
465 
466  Array<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const;
468  { return permute (vec, true); }
469 
470  bool is_square (void) const { return (dim1 () == dim2 ()); }
471 
472  bool is_empty (void) const { return numel () == 0; }
473 
474  bool is_vector (void) const { return dimensions.is_vector (); }
475 
476  Array<T> transpose (void) const;
477  Array<T> hermitian (T (*fcn) (const T&) = 0) const;
478 
479  const T *data (void) const { return slice_data; }
480 
481  const T *fortran_vec (void) const { return data (); }
482 
483  T *fortran_vec (void);
484 
485  bool is_shared (void) { return rep->count > 1; }
486 
487  int ndims (void) const { return dimensions.length (); }
488 
489  //@{
490  //! Indexing without resizing.
491  Array<T> index (const idx_vector& i) const;
492 
493  Array<T> index (const idx_vector& i, const idx_vector& j) const;
494 
495  Array<T> index (const Array<idx_vector>& ia) const;
496  //@}
497 
498  virtual T resize_fill_value (void) const;
499 
500  //@{
501  //! Resizing (with fill).
502  void resize2 (octave_idx_type nr, octave_idx_type nc, const T& rfv);
504  {
505  resize2 (nr, nc, resize_fill_value ());
506  }
507 
508  void resize1 (octave_idx_type n, const T& rfv);
509  void resize1 (octave_idx_type n) { resize1 (n, resize_fill_value ()); }
510 
511  void resize (const dim_vector& dv, const T& rfv);
512  void resize (const dim_vector& dv) { resize (dv, resize_fill_value ()); }
513  //@}
514 
515  //@{
516  //! Indexing with possible resizing and fill
517 
518  // FIXME: this is really a corner case, that should better be
519  // handled directly in liboctinterp.
520 
521 
522  Array<T> index (const idx_vector& i, bool resize_ok, const T& rfv) const;
523  Array<T> index (const idx_vector& i, bool resize_ok) const
524  {
525  return index (i, resize_ok, resize_fill_value ());
526  }
527 
528  Array<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok,
529  const T& rfv) const;
530  Array<T> index (const idx_vector& i, const idx_vector& j,
531  bool resize_ok) const
532  {
533  return index (i, j, resize_ok, resize_fill_value ());
534  }
535 
536  Array<T> index (const Array<idx_vector>& ia, bool resize_ok,
537  const T& rfv) const;
538  Array<T> index (const Array<idx_vector>& ia, bool resize_ok) const
539  {
540  return index (ia, resize_ok, resize_fill_value ());
541  }
542  //@}
543 
544 
545  //@{
546  //! Indexed assignment (always with resize & fill).
547  void assign (const idx_vector& i, const Array<T>& rhs, const T& rfv);
548  void assign (const idx_vector& i, const Array<T>& rhs)
549  {
550  assign (i, rhs, resize_fill_value ());
551  }
552 
553  void assign (const idx_vector& i, const idx_vector& j, const Array<T>& rhs,
554  const T& rfv);
555  void assign (const idx_vector& i, const idx_vector& j, const Array<T>& rhs)
556  {
557  assign (i, j, rhs, resize_fill_value ());
558  }
559 
560  void assign (const Array<idx_vector>& ia, const Array<T>& rhs, const T& rfv);
561  void assign (const Array<idx_vector>& ia, const Array<T>& rhs)
562  {
563  assign (ia, rhs, resize_fill_value ());
564  }
565  //@}
566 
567  //@{
568  //! Deleting elements.
569 
570  //! A(I) = [] (with a single subscript)
571  void delete_elements (const idx_vector& i);
572 
573  //! A(:,...,I,...,:) = [] (>= 2 subscripts, one of them is non-colon)
574  void delete_elements (int dim, const idx_vector& i);
575 
576  //! Dispatcher to the above two.
577  void delete_elements (const Array<idx_vector>& ia);
578  //@}
579 
580  //! Insert an array into another at a specified position. If
581  //! size (a) is [d1 d2 ... dN] and idx is [i1 i2 ... iN], this
582  //! method is equivalent to x(i1:i1+d1-1, i2:i2+d2-1, ... ,
583  //! iN:iN+dN-1) = a.
584  Array<T>& insert (const Array<T>& a, const Array<octave_idx_type>& idx);
585 
586  //! This is just a special case for idx = [r c 0 ...]
587  Array<T>& insert (const Array<T>& a, octave_idx_type r, octave_idx_type c);
588 
589  void maybe_economize (void)
590  {
591  if (rep->count == 1 && slice_len != rep->len)
592  {
593  ArrayRep *new_rep = new ArrayRep (slice_data, slice_len);
594  delete rep;
595  rep = new_rep;
596  slice_data = rep->data;
597  }
598  }
599 
600  void print_info (std::ostream& os, const std::string& prefix) const;
601 
602  //! Give a pointer to the data in mex format. Unsafe. This function
603  //! exists to support the MEX interface. You should not use it
604  //! anywhere else.
605  void *mex_get_data (void) const { return const_cast<T *> (data ()); }
606 
607  Array<T> sort (int dim = 0, sortmode mode = ASCENDING) const;
608  Array<T> sort (Array<octave_idx_type> &sidx, int dim = 0,
609  sortmode mode = ASCENDING) const;
610 
611  //! Ordering is auto-detected or can be specified.
612  sortmode is_sorted (sortmode mode = UNSORTED) const;
613 
614  //! Sort by rows returns only indices.
615  Array<octave_idx_type> sort_rows_idx (sortmode mode = ASCENDING) const;
616 
617  //! Ordering is auto-detected or can be specified.
618  sortmode is_sorted_rows (sortmode mode = UNSORTED) const;
619 
620  //! @brief Do a binary lookup in a sorted array. Must not contain NaNs.
621  //! Mode can be specified or is auto-detected by comparing 1st and last element.
622  octave_idx_type lookup (const T& value, sortmode mode = UNSORTED) const;
623 
624  //! Ditto, but for an array of values, specializing on the case when values
625  //! are sorted. NaNs get the value N.
626  Array<octave_idx_type> lookup (const Array<T>& values,
627  sortmode mode = UNSORTED) const;
628 
629  //! Count nonzero elements.
630  octave_idx_type nnz (void) const;
631 
632  //! Find indices of (at most n) nonzero elements. If n is specified,
633  //! backward specifies search from backward.
635  bool backward = false) const;
636 
637  //! Returns the n-th element in increasing order, using the same
638  //! ordering as used for sort. n can either be a scalar index or a
639  //! contiguous range.
640  Array<T> nth_element (const idx_vector& n, int dim = 0) const;
641 
642  //! Get the kth super or subdiagonal. The zeroth diagonal is the
643  //! ordinary diagonal.
644  Array<T> diag (octave_idx_type k = 0) const;
645 
646  Array<T> diag (octave_idx_type m, octave_idx_type n) const;
647 
648  //! Concatenation along a specified (0-based) dimension, equivalent
649  //! to cat(). dim = -1 corresponds to dim = 0 and dim = -2
650  //! corresponds to dim = 1, but apply the looser matching rules of
651  //! vertcat/horzcat.
652  static Array<T>
653  cat (int dim, octave_idx_type n, const Array<T> *array_list);
654 
655  //! Apply function fcn to each element of the Array<T>. This function
656  //! is optimised with a manually unrolled loop.
657  template <class U, class F>
658  Array<U>
659  map (F fcn) const
660  {
661  octave_idx_type len = length ();
662 
663  const T *m = data ();
664 
665  Array<U> result (dims ());
666  U *p = result.fortran_vec ();
667 
668  octave_idx_type i;
669  for (i = 0; i < len - 3; i += 4)
670  {
671  octave_quit ();
672 
673  p[i] = fcn (m[i]);
674  p[i+1] = fcn (m[i+1]);
675  p[i+2] = fcn (m[i+2]);
676  p[i+3] = fcn (m[i+3]);
677  }
678 
679  octave_quit ();
680 
681  for (; i < len; i++)
682  p[i] = fcn (m[i]);
683 
684  return result;
685  }
686 
687  //@{
688  //! Overloads for function references.
689  template <class U>
690  Array<U>
691  map (U (&fcn) (T)) const
692  { return map<U, U (&) (T)> (fcn); }
693 
694  template <class U>
695  Array<U>
696  map (U (&fcn) (const T&)) const
697  { return map<U, U (&) (const T&)> (fcn); }
698  //@}
699 
700  //! Generic any/all test functionality with arbitrary predicate.
701  template <class F, bool zero>
702  bool test (F fcn) const
703  {
704  return any_all_test<F, T, zero> (fcn, data (), length ());
705  }
706 
707  //@{
708  //! Simpler calls.
709  template <class F>
710  bool test_any (F fcn) const
711  { return test<F, false> (fcn); }
712 
713  template <class F>
714  bool test_all (F fcn) const
715  { return test<F, true> (fcn); }
716  //@}
717 
718  //@{
719  //! Overloads for function references.
720  bool test_any (bool (&fcn) (T)) const
721  { return test<bool (&) (T), false> (fcn); }
722 
723  bool test_any (bool (&fcn) (const T&)) const
724  { return test<bool (&) (const T&), false> (fcn); }
725 
726  bool test_all (bool (&fcn) (T)) const
727  { return test<bool (&) (T), true> (fcn); }
728 
729  bool test_all (bool (&fcn) (const T&)) const
730  { return test<bool (&) (const T&), true> (fcn); }
731  //@}
732 
733  template <class U> friend class Array;
734 
735  //! Returns true if this->dims () == dv, and if so, replaces this->dimensions
736  //! by a shallow copy of dv. This is useful for maintaining several arrays with
737  //! supposedly equal dimensions (e.g. structs in the interpreter).
738  bool optimize_dimensions (const dim_vector& dv);
739 
740  //@{
741  //! WARNING: Only call these functions from jit
742 
743  int *jit_ref_count (void) { return rep->count.get (); }
744 
745  T *jit_slice_data (void) const { return slice_data; }
746 
747  octave_idx_type *jit_dimensions (void) const { return dimensions.to_jit (); }
748 
749  void *jit_array_rep (void) const { return rep; }
750  //@}
751 
752 private:
753  static void instantiation_guard ();
754 };
755 
756 //! This is a simple wrapper template that will subclass an Array<T>
757 //! type or any later type derived from it and override the default
758 //! non-const operator() to not check for the array's uniqueness. It
759 //! is, however, the user's responsibility to ensure the array is
760 //! actually unaliased whenever elements are accessed.
761 template<class ArrayClass>
762 class NoAlias : public ArrayClass
763 {
764  typedef typename ArrayClass::element_type T;
765 public:
766  NoAlias () : ArrayClass () { }
767 
768  // FIXME: this would be simpler once C++0x is available
769  template <class X>
770  explicit NoAlias (X x) : ArrayClass (x) { }
771 
772  template <class X, class Y>
773  explicit NoAlias (X x, Y y) : ArrayClass (x, y) { }
774 
775  template <class X, class Y, class Z>
776  explicit NoAlias (X x, Y y, Z z) : ArrayClass (x, y, z) { }
777 
779  { return ArrayClass::xelem (n); }
781  { return ArrayClass::xelem (i, j); }
783  { return ArrayClass::xelem (i, j, k); }
785  { return ArrayClass::xelem (ra_idx); }
786 };
787 
788 template <class T>
789 std::ostream&
790 operator << (std::ostream& os, const Array<T>& a);
791 
792 #endif
dim_vector dimensions
Definition: Array.h:127
Array< T > as_matrix(void) const
Return the array as a matrix.
Definition: Array.h:299
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:178
T & elem(octave_idx_type i, octave_idx_type j, octave_idx_type k)
Definition: Array.h:388
octave_idx_type compute_index_unchecked(const Array< octave_idx_type > &ra_idx) const
Definition: Array.h:347
int bool
Definition: mex.h:56
static void clear(octave_shlib &oct_file)
Definition: dynamic-ld.cc:236
bool is_empty(void) const
Definition: Array.h:472
Array(T *sdata, octave_idx_type slen, octave_idx_type *adims, void *arep)
For jit support.
Definition: Array.h:165
crefT elem(const Array< octave_idx_type > &ra_idx) const
Definition: Array.h:425
Array< T > reshape(const dim_vector &new_dims) const
Definition: Array.h:463
Array(void)
Empty ctor (0 by 0).
Definition: Array.h:173
ref_param< T >::type crefT
Definition: Array.h:120
const octave_base_value const Array< octave_idx_type > & ra_idx
void resize(const dim_vector &dv)
Definition: Array.h:512
NoAlias(X x, Y y, Z z)
Definition: Array.h:776
octave_idx_type len
Definition: Array.h:56
bool is_vector(void) const
Definition: dim-vector.h:430
NoAlias()
Definition: Array.h:766
void assign(const Array< idx_vector > &ia, const Array< T > &rhs)
Definition: Array.h:561
int ndims(void) const
Definition: Array.h:487
sortmode
Definition: oct-sort.h:103
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
ArrayRep(octave_idx_type n, const T &val)
Definition: Array.h:77
crefT xelem(octave_idx_type i, octave_idx_type j) const
Definition: Array.h:358
Array< U > map(U(&fcn)(T)) const
Overloads for function references.
Definition: Array.h:691
T & operator()(octave_idx_type n)
Definition: Array.h:778
octave_idx_type dim2(void) const
Definition: Array.h:320
ArrayRep(U *d, octave_idx_type l)
Definition: Array.h:66
void * mex_get_data(void) const
Give a pointer to the data in mex format.
Definition: Array.h:605
Array(const Array< T > &a, const dim_vector &dv, octave_idx_type l, octave_idx_type u)
slice constructor
Definition: Array.h:142
octave_idx_type * jit_dimensions(void) const
Definition: Array.h:747
static void transpose(octave_idx_type N, const octave_idx_type *ridx, const octave_idx_type *cidx, octave_idx_type *ridx2, octave_idx_type *cidx2)
Definition: symrcm.cc:382
virtual ~Array(void)
Definition: Array.h:220
Array(const Array< T > &a)
No type conversion case.
Definition: Array.h:211
T * jit_slice_data(void) const
Definition: Array.h:745
Array(const dim_vector &dv, const T &val)
nD initialized ctor.
Definition: Array.h:190
T & elem(octave_idx_type n)
Definition: Array.h:380
octave_idx_type lookup(const T *x, octave_idx_type n, T y)
crefT xelem(const Array< octave_idx_type > &ra_idx) const
Definition: Array.h:369
F77_RET_T const octave_idx_type const octave_idx_type const octave_idx_type double const octave_idx_type double const octave_idx_type double const octave_idx_type double * Z
Definition: qz.cc:114
bool test_all(bool(&fcn)(T)) const
Definition: Array.h:726
T & xelem(octave_idx_type i, octave_idx_type j, octave_idx_type k)
Definition: Array.h:361
Array< T > as_row(void) const
Return the array as a row vector.
Definition: Array.h:289
crefT xelem(octave_idx_type i, octave_idx_type j, octave_idx_type k) const
Definition: Array.h:363
The real representation of all arrays.
Definition: Array.h:51
octave_idx_type dim1(void) const
Definition: Array.h:312
Array(const dim_vector &dv)
nD uninitialized ctor.
Definition: Array.h:181
ArrayRep(const ArrayRep &a)
Definition: Array.h:83
octave_idx_type rows(void) const
Definition: Array.h:313
F77_RET_T const double const double double * d
void clear(octave_idx_type r, octave_idx_type c)
Definition: Array.h:249
bool optimize_dimensions(const dim_vector &dv)
Returns true if this->dims () == dv, and if so, replaces this->dimensions by a shallow copy of dv...
Definition: Array.cc:2756
octave_idx_type nelem(void) const
Number of elements in the array.
Definition: Array.h:271
octave_idx_type * to_jit(void) const
Definition: dim-vector.h:230
Array< T > as_column(void) const
Return the array as a column vector.
Definition: Array.h:279
static void instantiation_guard()
Definition: Array.cc:2766
octave_idx_type dim3(void) const
Definition: Array.h:329
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:337
size_t byte_size(void) const
Definition: Array.h:333
void resize1(octave_idx_type n)
Definition: Array.h:509
Array< T >::ArrayRep * nil_rep(void) const
Definition: Array.h:152
int * jit_ref_count(void)
WARNING: Only call these functions from jit.
Definition: Array.h:743
bool test_any(F fcn) const
Simpler calls.
Definition: Array.h:710
void assign(const idx_vector &i, const Array< T > &rhs)
Definition: Array.h:548
void make_unique(void)
Definition: Array.h:104
Array< U > map(F fcn) const
Apply function fcn to each element of the Array.
Definition: Array.h:659
T & xelem(const Array< octave_idx_type > &ra_idx)
Definition: Array.h:366
bool is_vector(void) const
Definition: Array.h:474
T & xelem(octave_idx_type i, octave_idx_type j)
Definition: Array.h:356
const T * data(void) const
Definition: Array.h:479
ArrayClass::element_type T
Definition: Array.h:764
ArrayRep(T *d, octave_idx_type l)
Definition: Array.h:59
static int elem
Definition: __contourc__.cc:49
ArrayRep(void)
Definition: Array.h:72
Array< T > index(const Array< idx_vector > &ia, bool resize_ok) const
Definition: Array.h:538
bool is_square(void) const
Definition: Array.h:470
octave_idx_type capacity(void) const
Number of elements in the array.
Definition: Array.h:256
dim_vector redim(int n) const
Definition: dim-vector.cc:266
bool test_any(bool(&fcn)(const T &)) const
Definition: Array.h:723
octave_idx_type slice_len
Definition: Array.h:139
NoAlias(X x, Y y)
Definition: Array.h:773
crefT xelem(octave_idx_type n) const
Definition: Array.h:354
if_then_else< is_class_type< T >::no, T, T const & >::result type
Definition: lo-traits.h:116
T & elem(octave_idx_type i, octave_idx_type j)
Definition: Array.h:386
static octave_idx_type find(octave_idx_type i, octave_idx_type *pp)
Definition: colamd.cc:111
bool test_any(bool(&fcn)(T)) const
Overloads for function references.
Definition: Array.h:720
T & xelem(octave_idx_type n)
Definition: Array.h:353
count_type * get(void)
Definition: oct-refcount.h:85
bool test_all(F fcn) const
Definition: Array.h:714
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
bool test(F fcn) const
Generic any/all test functionality with arbitrary predicate.
Definition: Array.h:702
T * slice_data
Definition: Array.h:138
This is a simple wrapper template that will subclass an Array type or any later type derived from ...
Definition: Array.h:762
octave_idx_type compute_index(const octave_idx_type *idx) const
Compute a linear index from an index tuple.
Definition: dim-vector.h:448
crefT elem(octave_idx_type i, octave_idx_type j) const
Definition: Array.h:419
bool test_all(bool(&fcn)(const T &)) const
Definition: Array.h:729
crefT elem(octave_idx_type i, octave_idx_type j, octave_idx_type k) const
Definition: Array.h:422
octave_refcount< int > count
Definition: Array.h:57
bool is_shared(void)
Definition: Array.h:485
octave_idx_type length(void) const
Definition: Array.h:91
T element_type
Definition: Array.h:118
Array< T > index(const idx_vector &i, const idx_vector &j, bool resize_ok) const
Definition: Array.h:530
Array< T > index(const idx_vector &i, bool resize_ok) const
Definition: Array.h:523
~ArrayRep(void)
Definition: Array.h:89
Array(const Array< U > &a)
Type conversion case.
Definition: Array.h:204
void assign(const idx_vector &i, const idx_vector &j, const Array< T > &rhs)
Definition: Array.h:555
Array< T > reshape(octave_idx_type nr, octave_idx_type nc) const
Definition: Array.h:460
octave_idx_type pages(void) const
Definition: Array.h:330
T & elem(const Array< octave_idx_type > &ra_idx)
Definition: Array.h:391
void resize2(octave_idx_type nr, octave_idx_type nc)
Definition: Array.h:503
void maybe_economize(void)
Definition: Array.h:589
Array< T > ipermute(const Array< octave_idx_type > &vec) const
Definition: Array.h:467
const T * fortran_vec(void) const
Definition: Array.h:481
octave_idx_type cols(void) const
Definition: Array.h:321
ArrayRep(octave_idx_type n)
Definition: Array.h:74
void chop_trailing_singletons(void)
Definition: dim-vector.h:214
int length(void) const
Definition: dim-vector.h:281
octave_idx_type columns(void) const
Definition: Array.h:322
crefT elem(octave_idx_type n) const
Definition: Array.h:417
NoAlias(X x)
Definition: Array.h:770
void * jit_array_rep(void) const
Definition: Array.h:749
F77_RET_T const double * x
Array< T >::ArrayRep * rep
Definition: Array.h:129
void F(const TSRC *v, TRES *r, octave_idx_type m, octave_idx_type n)
Definition: mx-inlines.cc:527
Array< U > map(U(&fcn)(const T &)) const
Definition: Array.h:696