Mercurial > octave-dspies
annotate liboctave/array/Array-util.h @ 19009:8d47ce2053f2 draft
Added safety checks to Array::xelem
There's no reason to have a method which never checks invariants, ever. Added
debugging checks to Array::xelem to help catch and debug out-of-bounds errors
and reference overlap
* configure.ac: Added configuration option for uniqueness-checking with xelem
* jit-typeinfo.cc (octave_jit_paren_scalar): Call const Array::xelem rather
than Array::xelem
* Array-util.h, Array-util.cc (check_out_of_range): Extract common pattern to
method
(check_index): Methods to check index is in-bounds
(compute_index): Added bool parameter check. does not check bounds when check
is false and BOUNDS_CHECKING is off
* Array.h, Array.cc (xelem): Use methods from Array-util.h to compute indices
(is_unique): Check if this is the only reference to data
* CmplxQR.cc, dbleQR.cc, fCmplxQR.cc, floatQR.cc
(form): Move second assignment to after the call to xelem
* lo-array-gripes.h, lo-array-gripes.cc (gripe_modifying_nonunique): Added
error message for when non-const xelem is called on non-unique array
author | David Spies <dnspies@gmail.com> |
---|---|
date | Mon, 14 Jul 2014 13:07:59 -0600 |
parents | 49a5a4be04a1 |
children |
rev | line source |
---|---|
4588 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
15271
diff
changeset
|
3 Copyright (C) 2000-2013 John W. Eaton |
4588 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
4588 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
4588 | 20 |
21 */ | |
22 | |
23 #if !defined (octave_Array_util_h) | |
24 #define octave_Array_util_h 1 | |
25 | |
26 #include "dim-vector.h" | |
27 #include "idx-vector.h" | |
11130
7c573eb981eb
consistently give error for operator not applied to NaN values
John W. Eaton <jwe@octave.org>
parents:
11129
diff
changeset
|
28 #include "lo-array-gripes.h" |
4588 | 29 |
19009
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
30 template <typename T> |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
31 class Array; |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
32 |
6108 | 33 extern OCTAVE_API bool index_in_bounds (const Array<octave_idx_type>& ra_idx, |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
34 const dim_vector& dimensions); |
4588 | 35 |
6108 | 36 extern OCTAVE_API void increment_index (Array<octave_idx_type>& ra_idx, |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
37 const dim_vector& dimensions, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
38 int start_dimension = 0); |
4588 | 39 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
40 extern OCTAVE_API octave_idx_type get_scalar_idx (Array<octave_idx_type>& idx, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
41 dim_vector& dims); |
4588 | 42 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
43 extern OCTAVE_API octave_idx_type num_ones (const Array<octave_idx_type>& |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
44 ra_idx); |
4588 | 45 |
6108 | 46 extern OCTAVE_API bool is_scalar (const dim_vector& dim); |
4588 | 47 |
7642
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7246
diff
changeset
|
48 extern OCTAVE_API bool is_vector (const dim_vector& dim); |
9a4541c622b5
refactor Array::assignN dimensioning code for empty initial matrices
David Bateman <dbateman@free.fr>
parents:
7246
diff
changeset
|
49 |
6108 | 50 extern OCTAVE_API bool any_ones (const Array<octave_idx_type>& arr); |
4588 | 51 |
19009
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
52 extern OCTAVE_API void |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
53 check_index_bounds (int nd, int dim, octave_idx_type i, octave_idx_type size); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
54 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
55 extern OCTAVE_API void |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
56 check_index (octave_idx_type n, const dim_vector& dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
57 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
58 extern OCTAVE_API void |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
59 check_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
60 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
61 extern OCTAVE_API void |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
62 check_index (octave_idx_type i, octave_idx_type j, octave_idx_type k, |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
63 const dim_vector& dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
64 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
65 extern OCTAVE_API void |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
66 check_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
67 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
68 #if defined(BOUNDS_CHECKING) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
69 #define BOUNDS_CHECKING_DEFINED true |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
70 #else |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
71 #define BOUNDS_CHECKING_DEFINED false |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
72 #endif |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
73 |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
74 // These four compute a linear index for given dimensions, throwing |
19009
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
75 // exceptions on invalid indices if check is true or BOUNDS_CHECKING is defined. |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
76 inline octave_idx_type |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
77 compute_index (octave_idx_type n, const dim_vector& dims, bool check = true) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
78 { |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
79 if (BOUNDS_CHECKING_DEFINED || check) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
80 check_index (n, dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
81 return n; |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
82 } |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
83 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
84 inline octave_idx_type |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
85 compute_index (octave_idx_type i, octave_idx_type j, const dim_vector& dims, |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
86 bool check = true) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
87 { |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
88 if (BOUNDS_CHECKING_DEFINED || check) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
89 check_index (i, j, dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
90 return j * dims(0) + i; |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
91 } |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
92 |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
93 inline octave_idx_type |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
94 compute_index (octave_idx_type i, octave_idx_type j, octave_idx_type k, |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
95 const dim_vector& dims, bool check = true) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
96 { |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
97 if (BOUNDS_CHECKING_DEFINED || check) |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
98 check_index (i, j, k, dims); |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
99 return (k * dims (1) + j) * dims(0) + i; |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
100 } |
10645
8645b7087859
abstract scalar index checking off Array<T> (prep for struct optimizations)
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
101 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 extern OCTAVE_API octave_idx_type |
19009
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
103 compute_index (const Array<octave_idx_type>& ra_idx, const dim_vector& dims, |
8d47ce2053f2
Added safety checks to Array::xelem
David Spies <dnspies@gmail.com>
parents:
17769
diff
changeset
|
104 bool check = true); |
4588 | 105 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
106 extern OCTAVE_API Array<octave_idx_type> |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
107 conv_to_int_array (const Array<idx_vector>& a); |
4588 | 108 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
109 extern OCTAVE_API Array<idx_vector> conv_to_array (const idx_vector *tmp, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
110 const octave_idx_type len); |
4588 | 111 |
6108 | 112 extern OCTAVE_API dim_vector freeze (Array<idx_vector>& ra_idx, |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
113 const dim_vector& dimensions, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
114 int resize_ok); |
4588 | 115 |
6108 | 116 extern OCTAVE_API bool vector_equivalent (const dim_vector& dv); |
4588 | 117 |
6108 | 118 extern OCTAVE_API bool all_ok (const Array<idx_vector>& ra_idx); |
4588 | 119 |
6108 | 120 extern OCTAVE_API bool any_orig_empty (const Array<idx_vector>& ra_idx); |
4588 | 121 |
6108 | 122 extern OCTAVE_API bool all_colon_equiv (const Array<idx_vector>& ra_idx, |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
123 const dim_vector& frozen_lengths); |
4588 | 124 |
6108 | 125 extern OCTAVE_API bool all_ones (const Array<octave_idx_type>& arr); |
4588 | 126 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
127 extern OCTAVE_API Array<octave_idx_type> |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
128 get_elt_idx (const Array<idx_vector>& ra_idx, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 const Array<octave_idx_type>& result_idx); |
4588 | 130 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
131 extern OCTAVE_API Array<octave_idx_type> get_ra_idx (octave_idx_type idx, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
132 const dim_vector& dims); |
4588 | 133 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
134 extern OCTAVE_API dim_vector zero_dims_inquire (const Array<idx_vector>& ia, |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
135 const dim_vector& rhdv); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
136 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 extern OCTAVE_API dim_vector zero_dims_inquire (const idx_vector& i, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
138 const idx_vector& j, |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
7922
diff
changeset
|
139 const dim_vector& rhdv); |
4588 | 140 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
141 extern OCTAVE_API idx_vector sub2ind (const dim_vector& dv, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
142 const Array<idx_vector>& idxa); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
143 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
144 extern OCTAVE_API Array<idx_vector> ind2sub (const dim_vector& dv, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
145 const idx_vector& idx); |
9479
d9716e3ee0dd
supply optimized compiled sub2ind & ind2sub
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
146 |
7241 | 147 struct |
148 permute_vector | |
149 { | |
150 octave_idx_type pidx; | |
151 octave_idx_type iidx; | |
152 }; | |
153 | |
7246 | 154 extern int OCTAVE_API permute_vector_compare (const void *a, const void *b); |
7241 | 155 |
4588 | 156 #endif |