Mercurial > hg > octave-nkf
annotate liboctave/array/dim-vector.h @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | a3bf35bd5b44 |
children |
rev | line source |
---|---|
4513 | 1 /* |
2 | |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19519
diff
changeset
|
3 Copyright (C) 2003-2015 John W. Eaton |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10498
diff
changeset
|
4 Copyirght (C) 2009, 2010 VZLU Prague |
4513 | 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 | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
4513 | 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 | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
4513 | 21 |
22 */ | |
23 | |
24 #if !defined (octave_dim_vector_h) | |
25 #define octave_dim_vector_h 1 | |
26 | |
27 #include <cassert> | |
9840
c0b54271904b
improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9743
diff
changeset
|
28 #include <limits> |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
29 |
5765 | 30 #include <sstream> |
4543 | 31 #include <string> |
32 | |
6216 | 33 #include "lo-error.h" |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
34 #include "lo-macros.h" |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
35 #include "oct-refcount.h" |
4513 | 36 |
20446
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
37 //! Vector representing the dimensions (size) of an Array. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
38 /*! |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
39 A dim_vector is used to represent dimensions of an Array. It is used |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
40 on its constructor to specify its size, or when reshaping it. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
41 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
42 @code{.cc} |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
43 // Matrix with 10 rows and 20 columns. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
44 Matrix m Matrix (dim_vector (10, 20)); |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
45 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
46 // Change its size to 5 rows and 40 columns. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
47 Matrix m2 = m.reshape (dim_vector (5, 40)); |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
48 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
49 // Five dimensional Array of length 10, 20, 3, 8, 7 on each dimension. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
50 NDArray a (dim_vector (10, 20, 3, 8, 7)); |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
51 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
52 // Uninitialized array of same size as other. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
53 NDArray b (a.dims ()); |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
54 @endcode |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
55 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
56 The main thing to understand about this class, is that methods such as |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
57 ndims() and numel(), return the value for an Array of these dimensions, |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
58 not the actual number of elements in the dim_vector. |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
59 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
60 @code{.cc} |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
61 dim_vector d (10, 5, 3); |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
62 octave_idx_type n = d.numel (); // returns 150 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
63 octave_idx_type nd = d.ndims (); // returns 2 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
64 @endcode |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
65 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
66 ## Implementation details ## |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
67 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
68 This implementation is more tricky than Array, but the big plus is that |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
69 dim_vector requires only one allocation instead of two. It is (slightly) |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
70 patterned after GCC's basic_string implementation. rep is a pointer to an |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
71 array of memory, comprising count, length, and the data: |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
72 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
73 @verbatim |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
74 <count> |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
75 <ndims> |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
76 rep --> <dims[0]> |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
77 <dims[1]> |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
78 ... |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
79 @endverbatim |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
80 |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
81 The inlines count(), ndims() recover this data from the rep. Note |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
82 that rep points to the beginning of dims to grant faster access |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
83 (reinterpret_cast is assumed to be an inexpensive operation). |
a3bf35bd5b44
doc: doxygen documentation for dim_vector header.
Carnë Draug <carandraug@octave.org>
parents:
20440
diff
changeset
|
84 */ |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
85 |
4513 | 86 class |
11173
298a75c128ad
Additional exported symbols [MSVC]
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10830
diff
changeset
|
87 OCTAVE_API |
4513 | 88 dim_vector |
89 { | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
90 private: |
4513 | 91 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
92 octave_idx_type *rep; |
4548 | 93 |
10150 | 94 octave_idx_type& ndims (void) const { return rep[-1]; } |
4513 | 95 |
10150 | 96 octave_idx_type& count (void) const { return rep[-2]; } |
4513 | 97 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
98 //! Construct a new rep with count = 1 and ndims given. |
10150 | 99 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
100 static octave_idx_type *newrep (int ndims) |
10150 | 101 { |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14951
diff
changeset
|
102 octave_idx_type *r = new octave_idx_type [ndims + 2]; |
10150 | 103 |
104 *r++ = 1; | |
105 *r++ = ndims; | |
4513 | 106 |
10150 | 107 return r; |
108 } | |
109 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
110 //! Clone this->rep. |
10150 | 111 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
112 octave_idx_type *clonerep (void) |
10150 | 113 { |
114 int l = ndims (); | |
115 | |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14951
diff
changeset
|
116 octave_idx_type *r = new octave_idx_type [l + 2]; |
10150 | 117 |
118 *r++ = 1; | |
119 *r++ = l; | |
4513 | 120 |
10150 | 121 for (int i = 0; i < l; i++) |
122 r[i] = rep[i]; | |
123 | |
124 return r; | |
125 } | |
126 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
127 //! Clone and resize this->rep to length n, filling by given value. |
10150 | 128 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
129 octave_idx_type *resizerep (int n, octave_idx_type fill_value) |
10150 | 130 { |
131 int l = ndims (); | |
132 | |
133 if (n < 2) | |
134 n = 2; | |
135 | |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14951
diff
changeset
|
136 octave_idx_type *r = new octave_idx_type [n + 2]; |
10150 | 137 |
138 *r++ = 1; | |
139 *r++ = n; | |
140 | |
141 if (l > n) | |
142 l = n; | |
4673 | 143 |
10150 | 144 int j; |
145 for (j = 0; j < l; j++) | |
146 r[j] = rep[j]; | |
147 for (; j < n; j++) | |
148 r[j] = fill_value; | |
149 | |
150 return r; | |
151 } | |
152 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
153 //! Free the rep. |
10150 | 154 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
155 void freerep (void) |
10150 | 156 { |
157 assert (count () == 0); | |
158 delete [] (rep - 2); | |
159 } | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
160 |
4548 | 161 void make_unique (void) |
162 { | |
10150 | 163 if (count () > 1) |
4548 | 164 { |
14445 | 165 octave_idx_type *new_rep = clonerep (); |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
166 |
14445 | 167 if (OCTREFCOUNT_ATOMIC_DECREMENT(&(count())) == 0) |
168 freerep (); | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
169 |
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
170 rep = new_rep; |
4548 | 171 } |
172 } | |
173 | |
174 public: | |
175 | |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
176 // There are constructors for up to 7 dimensions initialized this way. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
177 // More can be added if necessary. |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
178 #define ASSIGN_REP(i) rep[i] = d ## i; |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
179 #define DIM_VECTOR_CTOR(N) \ |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
180 dim_vector (OCT_MAKE_DECL_LIST (octave_idx_type, d, N)) \ |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
181 : rep (newrep (N)) \ |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
182 { \ |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
183 OCT_ITERATE_MACRO (ASSIGN_REP, N) \ |
10418
6c19d6fcd7e5
up to 7th-order dim_vector constructors
Jaroslav Hajek <highegg@gmail.com>
parents:
10403
diff
changeset
|
184 } |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
185 |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
186 //! Construct dim_vector for 2 dimensional array. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
187 /*! |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
188 It can be used to construct a 2D array. Example: |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
189 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
190 @code{.cc} |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
191 dim_vector dv (7, 5); |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
192 Matrix mat (dv); |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
193 @endcode |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
194 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
195 The constructed dim_vector @c dv will have two elements, @f$[7, 5]@f$, |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
196 one for each dimension. It can then be used to construct a Matrix |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
197 with such dimensions, i.e., 7 rows and 5 columns. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
198 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
199 There are constructors available for up to 7 dimensions. For a higher |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
200 number of dimensions, use redim() or resize(). |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
201 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
202 Note that that there is no constructor for a 1 element dim_vector. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
203 This is because there are no 1 dimensional Array in liboctave. Such |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
204 constructor did exist in liboctave but was removed in version 4.0.0 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
205 due to its potential for confusion. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
206 */ |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
207 DIM_VECTOR_CTOR (2) |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
208 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
209 //! Construct dim_vector for 3 dimensional array. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
210 /*! |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
211 It can be used to construct a 3D array. Example: |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
212 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
213 @code{.cc} |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
214 NDArray A (dim_vector (7, 5, 4)); |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
215 @endcode |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
216 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
217 This will construct a 3 dimensional NDArray of lengths 7, 5, and 4, |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
218 on the first, second, and third dimension (rows, columns, and pages) |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
219 respectively. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
220 */ |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
221 DIM_VECTOR_CTOR (3) |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
222 |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
223 //! Construct dim_vector for 4 dimensional array. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
224 //! @see dim_vector(octave_idx_type d0, octave_idx_type d1) |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
225 DIM_VECTOR_CTOR (4) |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
226 //! Construct dim_vector for 5 dimensional array. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
227 //! @see dim_vector(octave_idx_type d0, octave_idx_type d1) |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
228 DIM_VECTOR_CTOR (5) |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
229 //! Construct dim_vector for 6 dimensional array. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
230 //! @see dim_vector(octave_idx_type d0, octave_idx_type d1) |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
231 DIM_VECTOR_CTOR (6) |
20418
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
232 //! Construct dim_vector for 7 dimensional array. |
f638a61af5a8
doxygen: improve dim_vector constructor documentation (bug #45105)
Piotr Held <pjheld@gmail.com>
parents:
20068
diff
changeset
|
233 //! @see dim_vector(octave_idx_type d0, octave_idx_type d1) |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
234 DIM_VECTOR_CTOR (7) |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
235 |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
236 #undef ASSIGN_REP |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
237 #undef DIM_VECTOR_CTOR |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
238 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
239 octave_idx_type& elem (int i) |
10150 | 240 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
241 #ifdef BOUNDS_CHECKING |
10150 | 242 assert (i >= 0 && i < ndims ()); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
243 #endif |
10150 | 244 make_unique (); |
245 return rep[i]; | |
246 } | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
247 |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
248 octave_idx_type elem (int i) const |
10150 | 249 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
250 #ifdef BOUNDS_CHECKING |
10150 | 251 assert (i >= 0 && i < ndims ()); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
252 #endif |
10150 | 253 return rep[i]; |
254 } | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
255 |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
256 void chop_trailing_singletons (void) |
10150 | 257 { |
258 int l = ndims (); | |
259 if (l > 2 && rep[l-1] == 1) | |
260 { | |
261 make_unique (); | |
262 do | |
263 l--; | |
264 while (l > 2 && rep[l-1] == 1); | |
265 ndims () = l; | |
266 } | |
267 } | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
268 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
269 void chop_all_singletons (void); |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
270 |
14951
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
271 // WARNING: Only call by jit |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
272 octave_idx_type *to_jit (void) const |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
273 { |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
274 return rep; |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
275 } |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
276 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
277 private: |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
278 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
279 static octave_idx_type *nil_rep (void) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
280 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
281 static dim_vector zv (0, 0); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
282 return zv.rep; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
283 } |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
284 |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
285 public: |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
286 |
10830
b4ebfd675321
avoid static initialization disaster in dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
10810
diff
changeset
|
287 static octave_idx_type dim_max (void); |
10810
6683f0c9d742
make the maximum extent externally accessible
Jaroslav Hajek <highegg@gmail.com>
parents:
10715
diff
changeset
|
288 |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
289 explicit dim_vector (void) : rep (nil_rep ()) |
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
290 { OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); } |
4548 | 291 |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
292 dim_vector (const dim_vector& dv) : rep (dv.rep) |
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
293 { OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); } |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
294 |
14951
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
295 // FIXME: Should be private, but required by array constructor for jit |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
296 explicit dim_vector (octave_idx_type *r) : rep (r) { } |
14951
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
297 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
298 static dim_vector alloc (int n) |
10150 | 299 { |
300 return dim_vector (newrep (n < 2 ? 2 : n)); | |
301 } | |
4548 | 302 |
303 dim_vector& operator = (const dim_vector& dv) | |
304 { | |
305 if (&dv != this) | |
306 { | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
307 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0) |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
308 freerep (); |
4548 | 309 |
10150 | 310 rep = dv.rep; |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
311 OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); |
4548 | 312 } |
313 | |
314 return *this; | |
315 } | |
316 | |
317 ~dim_vector (void) | |
318 { | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
319 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0) |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
320 freerep (); |
4548 | 321 } |
322 | |
10150 | 323 int length (void) const { return ndims (); } |
4513 | 324 |
5275 | 325 octave_idx_type& operator () (int i) { return elem (i); } |
4513 | 326 |
5275 | 327 octave_idx_type operator () (int i) const { return elem (i); } |
4513 | 328 |
4887 | 329 void resize (int n, int fill_value = 0) |
4548 | 330 { |
331 int len = length (); | |
4513 | 332 |
4548 | 333 if (n != len) |
334 { | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
335 octave_idx_type *r = resizerep (n, fill_value); |
4513 | 336 |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
337 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0) |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
338 freerep (); |
4513 | 339 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
340 rep = r; |
4548 | 341 } |
342 } | |
4513 | 343 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
344 std::string str (char sep = 'x') const; |
4543 | 345 |
346 bool all_zero (void) const | |
4548 | 347 { |
348 bool retval = true; | |
4543 | 349 |
4548 | 350 for (int i = 0; i < length (); i++) |
351 { | |
10150 | 352 if (elem (i) != 0) |
353 { | |
354 retval = false; | |
355 break; | |
356 } | |
4548 | 357 } |
4543 | 358 |
4548 | 359 return retval; |
360 } | |
4559 | 361 |
10715
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
362 bool empty_2d (void) const |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
363 { |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
364 return length () == 2 && (elem (0) == 0 || elem (1) == 0); |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
365 } |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
366 |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
367 |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
368 bool zero_by_zero (void) const |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
369 { |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
370 return length () == 2 && elem (0) == 0 && elem (1) == 0; |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
371 } |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
372 |
4559 | 373 bool any_zero (void) const |
374 { | |
375 bool retval = false; | |
376 | |
377 for (int i = 0; i < length (); i++) | |
378 { | |
10150 | 379 if (elem (i) == 0) |
380 { | |
381 retval = true; | |
382 break; | |
383 } | |
4559 | 384 } |
385 | |
386 return retval; | |
387 } | |
4567 | 388 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
389 int num_ones (void) const; |
4635 | 390 |
10150 | 391 bool all_ones (void) const |
4655 | 392 { |
393 return (num_ones () == length ()); | |
394 } | |
395 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
396 //! Number of elements that a matrix with this dimensions would have. |
20068
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
397 /*! |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
398 Return the number of elements that a matrix with this dimension |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
399 vector would have, NOT the number of dimensions (elements in the |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
400 dimension vector). |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
401 */ |
4567 | 402 |
9027
9a46ba093db4
generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents:
8950
diff
changeset
|
403 octave_idx_type numel (int n = 0) const |
4567 | 404 { |
405 int n_dims = length (); | |
406 | |
9027
9a46ba093db4
generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents:
8950
diff
changeset
|
407 octave_idx_type retval = 1; |
4567 | 408 |
9027
9a46ba093db4
generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents:
8950
diff
changeset
|
409 for (int i = n; i < n_dims; i++) |
4567 | 410 retval *= elem (i); |
411 | |
412 return retval; | |
413 } | |
4673 | 414 |
20068
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
415 /*! |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
416 The following function will throw a std::bad_alloc () |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
417 exception if the requested size is larger than can be indexed by |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
418 octave_idx_type. This may be smaller than the actual amount of |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
419 memory that can be safely allocated on a system. However, if we |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
420 don't fail here, we can end up with a mysterious crash inside a |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
421 function that is iterating over an array using octave_idx_type |
19755f4fc851
maint: Cleanup C++ code to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
422 indices. |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
423 */ |
9840
c0b54271904b
improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9743
diff
changeset
|
424 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
425 octave_idx_type safe_numel (void) const; |
9840
c0b54271904b
improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9743
diff
changeset
|
426 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
427 bool any_neg (void) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
428 { |
10150 | 429 int n_dims = length (); |
430 int i; | |
431 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
432 for (i = 0; i < n_dims; i++) |
10150 | 433 if (elem (i) < 0) |
434 break; | |
435 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
436 return i < n_dims; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
437 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
438 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
439 dim_vector squeeze (void) const; |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10420
diff
changeset
|
440 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
441 //! This corresponds to cat(). |
10715
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
442 bool concat (const dim_vector& dvb, int dim); |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
443 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
444 //! This corresponds to [,] (horzcat, dim = 0) and [;] (vertcat, dim = 1). |
10715
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
445 // The rules are more relaxed here. |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
446 bool hvcat (const dim_vector& dvb, int dim); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
447 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
448 /*! |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
449 Force certain dimensionality, preserving numel (). Missing |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
450 dimensions are set to 1, redundant are folded into the trailing |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
451 one. If n = 1, the result is 2d and the second dim is 1 |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
452 (dim_vectors are always at least 2D). |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
453 */ |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
454 dim_vector redim (int n) const; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
455 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
456 dim_vector as_column (void) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
457 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
458 if (length () == 2 && elem (1) == 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
459 return *this; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
460 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
461 return dim_vector (numel (), 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
462 } |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
463 |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
464 dim_vector as_row (void) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
465 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
466 if (length () == 2 && elem (0) == 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
467 return *this; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
468 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
469 return dim_vector (1, numel ()); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
470 } |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
471 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
472 bool is_vector (void) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
473 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
474 return (length () == 2 && (elem (0) == 1 || elem (1) == 1)); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
475 } |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
476 |
9511
cc1fd3084cb2
implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
477 int first_non_singleton (int def = 0) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
478 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
479 for (int i = 0; i < length (); i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
480 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
481 if (elem (i) != 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
482 return i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
483 } |
9511
cc1fd3084cb2
implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
484 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
485 return def; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
486 } |
9511
cc1fd3084cb2
implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
487 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
488 //! Compute a linear index from an index tuple. |
10150 | 489 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
490 octave_idx_type compute_index (const octave_idx_type *idx) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
491 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
492 octave_idx_type k = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
493 for (int i = length () - 1; i >= 0; i--) |
20440
e914b5399c67
Use in-place operators in C++ code where possible.
Rik <rik@octave.org>
parents:
20418
diff
changeset
|
494 k = rep[i] * k + idx[i]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
495 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
496 return k; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
497 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
498 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
499 //! Ditto, but the tuple may be incomplete (nidx < length ()). |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
500 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
501 octave_idx_type compute_index (const octave_idx_type *idx, int nidx) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
502 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
503 octave_idx_type k = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
504 for (int i = nidx - 1; i >= 0; i--) |
20440
e914b5399c67
Use in-place operators in C++ code where possible.
Rik <rik@octave.org>
parents:
20418
diff
changeset
|
505 k = rep[i] * k + idx[i]; |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
506 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
507 return k; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
508 } |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
509 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
510 /*/! |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
511 Increment a multi-dimensional index tuple, optionally starting |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
512 from an offset position and return the index of the last index |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
513 position that was changed, or length () if just cycled over. |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
514 */ |
10150 | 515 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
516 int increment_index (octave_idx_type *idx, int start = 0) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
517 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
518 int i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
519 for (i = start; i < length (); i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
520 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
521 if (++(*idx) == rep[i]) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
522 *idx++ = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
523 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
524 break; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
525 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
526 return i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
527 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
528 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
529 //! Return cumulative dimensions. |
10150 | 530 |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
531 dim_vector cumulative (void) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
532 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
533 int nd = length (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
534 dim_vector retval = alloc (nd); |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
535 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
536 octave_idx_type k = 1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
537 for (int i = 0; i < nd; i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
538 retval.rep[i] = k *= rep[i]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
539 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
540 return retval; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
541 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
542 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
543 //! Compute a linear index from an index tuple. Dimensions are |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
544 //! required to be cumulative. |
10150 | 545 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
546 octave_idx_type cum_compute_index (const octave_idx_type *idx) const |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
547 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
548 octave_idx_type k = idx[0]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
549 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
550 for (int i = 1; i < length (); i++) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
551 k += rep[i-1] * idx[i]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
552 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
553 return k; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
554 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
555 |
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
556 |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
557 friend bool operator == (const dim_vector& a, const dim_vector& b); |
4513 | 558 }; |
559 | |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
560 inline bool |
4543 | 561 operator == (const dim_vector& a, const dim_vector& b) |
562 { | |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
563 // Fast case. |
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
564 if (a.rep == b.rep) |
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
565 return true; |
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
566 |
4543 | 567 bool retval = true; |
568 | |
569 int a_len = a.length (); | |
570 int b_len = b.length (); | |
571 | |
572 if (a_len != b_len) | |
573 retval = false; | |
574 else | |
575 { | |
576 for (int i = 0; i < a_len; i++) | |
10150 | 577 { |
578 if (a(i) != b(i)) | |
579 { | |
580 retval = false; | |
581 break; | |
582 } | |
583 } | |
4543 | 584 } |
585 | |
586 return retval; | |
587 } | |
588 | |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
589 inline bool |
4543 | 590 operator != (const dim_vector& a, const dim_vector& b) |
591 { | |
592 return ! operator == (a, b); | |
593 } | |
594 | |
4513 | 595 #endif |