Mercurial > hg > octave-nkf
annotate liboctave/array/dim-vector.h @ 19391:3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
author | Carnë Draug <carandraug@octave.org> |
---|---|
date | Wed, 01 Oct 2014 19:36:05 +0100 |
parents | 49a5a4be04a1 |
children | 5b263e517c95 |
rev | line source |
---|---|
4513 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
3 Copyright (C) 2003-2013 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 |
10150 | 37 // Rationale: This implementation is more tricky than Array, but the |
38 // big plus is that dim_vector requires only one allocation instead of | |
39 // two. It is (slightly) patterned after GCC's basic_string | |
40 // implementation. rep is a pointer to an array of memory, comprising | |
41 // count, length, and the data: | |
42 // | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
43 // <count> |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
44 // <ndims> |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
45 // rep --> <dims[0]> |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
46 // <dims[1]> |
10150 | 47 // ... |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
48 // |
10150 | 49 // The inlines count(), ndims() recover this data from the rep. Note |
50 // that rep points to the beginning of dims to grant faster access | |
51 // (reinterpret_cast is assumed to be an inexpensive operation). | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
52 |
4513 | 53 class |
11173
298a75c128ad
Additional exported symbols [MSVC]
Michael Goffioul <michael.goffioul@gmail.com>
parents:
10830
diff
changeset
|
54 OCTAVE_API |
4513 | 55 dim_vector |
56 { | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
57 private: |
4513 | 58 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
59 octave_idx_type *rep; |
4548 | 60 |
10150 | 61 octave_idx_type& ndims (void) const { return rep[-1]; } |
4513 | 62 |
10150 | 63 octave_idx_type& count (void) const { return rep[-2]; } |
4513 | 64 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
65 //! Construct a new rep with count = 1 and ndims given. |
10150 | 66 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
67 static octave_idx_type *newrep (int ndims) |
10150 | 68 { |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14951
diff
changeset
|
69 octave_idx_type *r = new octave_idx_type [ndims + 2]; |
10150 | 70 |
71 *r++ = 1; | |
72 *r++ = ndims; | |
4513 | 73 |
10150 | 74 return r; |
75 } | |
76 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
77 //! Clone this->rep. |
10150 | 78 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
79 octave_idx_type *clonerep (void) |
10150 | 80 { |
81 int l = ndims (); | |
82 | |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14951
diff
changeset
|
83 octave_idx_type *r = new octave_idx_type [l + 2]; |
10150 | 84 |
85 *r++ = 1; | |
86 *r++ = l; | |
4513 | 87 |
10150 | 88 for (int i = 0; i < l; i++) |
89 r[i] = rep[i]; | |
90 | |
91 return r; | |
92 } | |
93 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
94 //! Clone and resize this->rep to length n, filling by given value. |
10150 | 95 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
96 octave_idx_type *resizerep (int n, octave_idx_type fill_value) |
10150 | 97 { |
98 int l = ndims (); | |
99 | |
100 if (n < 2) | |
101 n = 2; | |
102 | |
15020
560317fd5977
maint: Cuddle open bracket used for indexing C++ arrays in source code.
Rik <rik@octave.org>
parents:
14951
diff
changeset
|
103 octave_idx_type *r = new octave_idx_type [n + 2]; |
10150 | 104 |
105 *r++ = 1; | |
106 *r++ = n; | |
107 | |
108 if (l > n) | |
109 l = n; | |
4673 | 110 |
10150 | 111 int j; |
112 for (j = 0; j < l; j++) | |
113 r[j] = rep[j]; | |
114 for (; j < n; j++) | |
115 r[j] = fill_value; | |
116 | |
117 return r; | |
118 } | |
119 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
120 //! Free the rep. |
10150 | 121 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
122 void freerep (void) |
10150 | 123 { |
124 assert (count () == 0); | |
125 delete [] (rep - 2); | |
126 } | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
127 |
4548 | 128 void make_unique (void) |
129 { | |
10150 | 130 if (count () > 1) |
4548 | 131 { |
14445 | 132 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
|
133 |
14445 | 134 if (OCTREFCOUNT_ATOMIC_DECREMENT(&(count())) == 0) |
135 freerep (); | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
136 |
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
137 rep = new_rep; |
4548 | 138 } |
139 } | |
140 | |
141 public: | |
142 | |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
143 // The constructor |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
144 // |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
145 // dim_vector (n) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
146 // |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
147 // creates an dimension vector with N rows and 1 column. It is |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
148 // deprecated because of the potentiol for confusion that it causes. |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
149 // Additional constructors of the form |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
150 // |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
151 // dim_vector (r, c) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
152 // dim_vector (r, c, p) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
153 // dim_vector (d1, d2, d3, d4, ...) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
154 // |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
155 // are available for up to 7 dimensions. |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
156 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
157 explicit dim_vector (octave_idx_type n) GCC_ATTR_DEPRECATED |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
158 : rep (newrep (2)) |
10150 | 159 { |
160 rep[0] = n; | |
161 rep[1] = 1; | |
162 } | |
4548 | 163 |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
164 #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
|
165 #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
|
166 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
|
167 : rep (newrep (N)) \ |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
168 { \ |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
169 OCT_ITERATE_MACRO (ASSIGN_REP, N) \ |
10418
6c19d6fcd7e5
up to 7th-order dim_vector constructors
Jaroslav Hajek <highegg@gmail.com>
parents:
10403
diff
changeset
|
170 } |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
171 |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
172 // Add more if needed. |
10420
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
173 DIM_VECTOR_CTOR (2) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
174 DIM_VECTOR_CTOR (3) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
175 DIM_VECTOR_CTOR (4) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
176 DIM_VECTOR_CTOR (5) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
177 DIM_VECTOR_CTOR (6) |
3373fdc0b14a
use macro for 2 and 3 arg dim_vector constructors
John W. Eaton <jwe@octave.org>
parents:
10419
diff
changeset
|
178 DIM_VECTOR_CTOR (7) |
10419
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
179 |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
180 #undef ASSIGN_REP |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
181 #undef DIM_VECTOR_CTOR |
afe44ee90cbd
implement generic macro magic for repeated decls
Jaroslav Hajek <highegg@gmail.com>
parents:
10418
diff
changeset
|
182 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
183 octave_idx_type& elem (int i) |
10150 | 184 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
185 #ifdef BOUNDS_CHECKING |
10150 | 186 assert (i >= 0 && i < ndims ()); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
187 #endif |
10150 | 188 make_unique (); |
189 return rep[i]; | |
190 } | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
191 |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
192 octave_idx_type elem (int i) const |
10150 | 193 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
194 #ifdef BOUNDS_CHECKING |
10150 | 195 assert (i >= 0 && i < ndims ()); |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10350
diff
changeset
|
196 #endif |
10150 | 197 return rep[i]; |
198 } | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
199 |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
200 void chop_trailing_singletons (void) |
10150 | 201 { |
202 int l = ndims (); | |
203 if (l > 2 && rep[l-1] == 1) | |
204 { | |
205 make_unique (); | |
206 do | |
207 l--; | |
208 while (l > 2 && rep[l-1] == 1); | |
209 ndims () = l; | |
210 } | |
211 } | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
212 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
213 void chop_all_singletons (void); |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
214 |
14951
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
215 // WARNING: Only call by jit |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
216 octave_idx_type *to_jit (void) const |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
217 { |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
218 return rep; |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
219 } |
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
220 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
221 private: |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
222 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
223 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
|
224 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
225 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
|
226 return zv.rep; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
227 } |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
228 |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
229 public: |
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
230 |
10830
b4ebfd675321
avoid static initialization disaster in dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
10810
diff
changeset
|
231 static octave_idx_type dim_max (void); |
10810
6683f0c9d742
make the maximum extent externally accessible
Jaroslav Hajek <highegg@gmail.com>
parents:
10715
diff
changeset
|
232 |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
233 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
|
234 { OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); } |
4548 | 235 |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
236 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
|
237 { OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); } |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
238 |
14951
4c9fd3e31436
Start of jit support for double matricies
Max Brister <max@2bass.com>
parents:
14625
diff
changeset
|
239 // 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
|
240 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
|
241 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
242 static dim_vector alloc (int n) |
10150 | 243 { |
244 return dim_vector (newrep (n < 2 ? 2 : n)); | |
245 } | |
4548 | 246 |
247 dim_vector& operator = (const dim_vector& dv) | |
248 { | |
249 if (&dv != this) | |
250 { | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
251 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0) |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
252 freerep (); |
4548 | 253 |
10150 | 254 rep = dv.rep; |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
255 OCTREFCOUNT_ATOMIC_INCREMENT (&(count())); |
4548 | 256 } |
257 | |
258 return *this; | |
259 } | |
260 | |
261 ~dim_vector (void) | |
262 { | |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
263 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0) |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
264 freerep (); |
4548 | 265 } |
266 | |
10150 | 267 int length (void) const { return ndims (); } |
4513 | 268 |
5275 | 269 octave_idx_type& operator () (int i) { return elem (i); } |
4513 | 270 |
5275 | 271 octave_idx_type operator () (int i) const { return elem (i); } |
4513 | 272 |
4887 | 273 void resize (int n, int fill_value = 0) |
4548 | 274 { |
275 int len = length (); | |
4513 | 276 |
4548 | 277 if (n != len) |
278 { | |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
279 octave_idx_type *r = resizerep (n, fill_value); |
4513 | 280 |
13985
43cc49c7abd1
Use thread-safe atomic reference counting (GCC and MSVC).
Michael Goffioul <michael.goffioul@gmail.com>
parents:
11586
diff
changeset
|
281 if (OCTREFCOUNT_ATOMIC_DECREMENT (&(count())) == 0) |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
282 freerep (); |
4513 | 283 |
9507
b096d11237be
dim_vector improvements
Jaroslav Hajek <highegg@gmail.com>
parents:
9420
diff
changeset
|
284 rep = r; |
4548 | 285 } |
286 } | |
4513 | 287 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
288 std::string str (char sep = 'x') const; |
4543 | 289 |
290 bool all_zero (void) const | |
4548 | 291 { |
292 bool retval = true; | |
4543 | 293 |
4548 | 294 for (int i = 0; i < length (); i++) |
295 { | |
10150 | 296 if (elem (i) != 0) |
297 { | |
298 retval = false; | |
299 break; | |
300 } | |
4548 | 301 } |
4543 | 302 |
4548 | 303 return retval; |
304 } | |
4559 | 305 |
10715
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
306 bool empty_2d (void) const |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
307 { |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
308 return length () == 2 && (elem (0) == 0 || elem (1) == 0); |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
309 } |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
310 |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
311 |
9886
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
312 bool zero_by_zero (void) const |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
313 { |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
314 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
|
315 } |
cddd5c3d5f04
fix & extend special-case optimizations for indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
9840
diff
changeset
|
316 |
4559 | 317 bool any_zero (void) const |
318 { | |
319 bool retval = false; | |
320 | |
321 for (int i = 0; i < length (); i++) | |
322 { | |
10150 | 323 if (elem (i) == 0) |
324 { | |
325 retval = true; | |
326 break; | |
327 } | |
4559 | 328 } |
329 | |
330 return retval; | |
331 } | |
4567 | 332 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
333 int num_ones (void) const; |
4635 | 334 |
10150 | 335 bool all_ones (void) const |
4655 | 336 { |
337 return (num_ones () == length ()); | |
338 } | |
339 | |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
340 //! Number of elements that a matrix with this dimensions would have. |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
341 /*! |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
342 Return the number of elements that a matrix with this dimension |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
343 vector would have, NOT the number of dimensions (elements in the |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
344 dimension vector). |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
345 */ |
4567 | 346 |
9027
9a46ba093db4
generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents:
8950
diff
changeset
|
347 octave_idx_type numel (int n = 0) const |
4567 | 348 { |
349 int n_dims = length (); | |
350 | |
9027
9a46ba093db4
generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents:
8950
diff
changeset
|
351 octave_idx_type retval = 1; |
4567 | 352 |
9027
9a46ba093db4
generalize dim_vector::numel
Jaroslav Hajek <highegg@gmail.com>
parents:
8950
diff
changeset
|
353 for (int i = n; i < n_dims; i++) |
4567 | 354 retval *= elem (i); |
355 | |
356 return retval; | |
357 } | |
4673 | 358 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
359 /*! |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
360 The following function will throw a std::bad_alloc () |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
361 exception if the requested size is larger than can be indexed by |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
362 octave_idx_type. This may be smaller than the actual amount of |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
363 memory that can be safely allocated on a system. However, if we |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
364 don't fail here, we can end up with a mysterious crash inside a |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
365 function that is iterating over an array using octave_idx_type |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
366 indices. |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
367 */ |
9840
c0b54271904b
improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9743
diff
changeset
|
368 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
369 octave_idx_type safe_numel (void) const; |
9840
c0b54271904b
improve safe numel() calculation for arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9743
diff
changeset
|
370 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
371 bool any_neg (void) const |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
372 { |
10150 | 373 int n_dims = length (); |
374 int i; | |
375 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
376 for (i = 0; i < n_dims; i++) |
10150 | 377 if (elem (i) < 0) |
378 break; | |
379 | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
380 return i < n_dims; |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
381 } |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
382 |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
383 dim_vector squeeze (void) const; |
10498
8615b55b5caf
fix & improve cat (bug #29465)
Jaroslav Hajek <highegg@gmail.com>
parents:
10420
diff
changeset
|
384 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
385 //! This corresponds to cat(). |
10715
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
386 bool concat (const dim_vector& dvb, int dim); |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
387 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
388 //! 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
|
389 // The rules are more relaxed here. |
53253f796351
make [] (hopefully) more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10681
diff
changeset
|
390 bool hvcat (const dim_vector& dvb, int dim); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
391 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
392 /*! |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
393 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
|
394 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
|
395 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
|
396 (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
|
397 */ |
10681
0ba9bd294421
make cat() (hopefully) more matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10645
diff
changeset
|
398 dim_vector redim (int n) const; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
399 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
400 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
|
401 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
402 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
|
403 return *this; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
404 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
405 return dim_vector (numel (), 1); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
406 } |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
407 |
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
408 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
|
409 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
410 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
|
411 return *this; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
412 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
413 return dim_vector (1, numel ()); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
414 } |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10150
diff
changeset
|
415 |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
416 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
|
417 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
418 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
|
419 } |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
420 |
9511
cc1fd3084cb2
implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
421 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
|
422 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
423 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
|
424 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
425 if (elem (i) != 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
426 return i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
427 } |
9511
cc1fd3084cb2
implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
428 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
429 return def; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
430 } |
9511
cc1fd3084cb2
implement dim_vector::first_non_singleton
Jaroslav Hajek <highegg@gmail.com>
parents:
9507
diff
changeset
|
431 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
432 //! Compute a linear index from an index tuple. |
10150 | 433 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
434 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
|
435 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
436 octave_idx_type k = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
437 for (int i = length () - 1; i >= 0; i--) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
438 k = k * rep[i] + idx[i]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
439 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
440 return k; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
441 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
442 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
443 //! 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
|
444 |
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
445 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
|
446 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
447 octave_idx_type k = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
448 for (int i = nidx - 1; i >= 0; i--) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
449 k = k * rep[i] + idx[i]; |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
450 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
451 return k; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
452 } |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
453 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
454 /*/! |
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
455 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
|
456 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
|
457 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
|
458 */ |
10150 | 459 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10586
diff
changeset
|
460 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
|
461 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
462 int i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
463 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
|
464 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
465 if (++(*idx) == rep[i]) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
466 *idx++ = 0; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
467 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
468 break; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
469 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
470 return i; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
471 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
472 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
473 //! Return cumulative dimensions. |
10150 | 474 |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
475 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
|
476 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
477 int nd = length (); |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
478 dim_vector retval = alloc (nd); |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
479 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
480 octave_idx_type k = 1; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
481 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
|
482 retval.rep[i] = k *= rep[i]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
483 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
484 return retval; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
485 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
486 |
19391
3a6fd52e1458
liboctave/array/dim-vector.h: convert comments to doxygen
Carnë Draug <carandraug@octave.org>
parents:
17769
diff
changeset
|
487 //! 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
|
488 //! required to be cumulative. |
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 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
|
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 = idx[0]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
493 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
494 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
|
495 k += rep[i-1] * idx[i]; |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
496 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
497 return k; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
498 } |
9739
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
499 |
13b57eec9440
a few handy methods for dim_vector
Jaroslav Hajek <highegg@gmail.com>
parents:
9694
diff
changeset
|
500 |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
501 friend bool operator == (const dim_vector& a, const dim_vector& b); |
4513 | 502 }; |
503 | |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
504 inline bool |
4543 | 505 operator == (const dim_vector& a, const dim_vector& b) |
506 { | |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
507 // Fast case. |
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
508 if (a.rep == b.rep) |
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
509 return true; |
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
510 |
4543 | 511 bool retval = true; |
512 | |
513 int a_len = a.length (); | |
514 int b_len = b.length (); | |
515 | |
516 if (a_len != b_len) | |
517 retval = false; | |
518 else | |
519 { | |
520 for (int i = 0; i < a_len; i++) | |
10150 | 521 { |
522 if (a(i) != b(i)) | |
523 { | |
524 retval = false; | |
525 break; | |
526 } | |
527 } | |
4543 | 528 } |
529 | |
530 return retval; | |
531 } | |
532 | |
9694
50db3c5175b5
allow unpacked form of LU
Jaroslav Hajek <highegg@gmail.com>
parents:
9666
diff
changeset
|
533 inline bool |
4543 | 534 operator != (const dim_vector& a, const dim_vector& b) |
535 { | |
536 return ! operator == (a, b); | |
537 } | |
538 | |
4513 | 539 #endif |