3665
|
1 // Template array classes |
|
2 /* |
|
3 |
|
4 Copyright (C) 2000 John W. Eaton |
|
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 |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
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 |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_ArrayN_h) |
|
25 #define octave_ArrayN_h 1 |
|
26 |
4066
|
27 #if defined (__GNUG__) && ! defined (NO_PRAGMA_INTERFACE_IMPLEMENTATION) |
3665
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <iostream> |
|
32 |
|
33 #include <cassert> |
|
34 #include <climits> |
|
35 #include <cmath> |
|
36 #include <cstdlib> |
|
37 |
|
38 #include "Array.h" |
|
39 #include "lo-error.h" |
|
40 |
|
41 class idx_vector; |
|
42 |
|
43 // N-dimensional array class. |
|
44 |
|
45 template <class T> |
|
46 class |
|
47 ArrayN : public Array<T> |
|
48 { |
|
49 protected: |
|
50 |
|
51 ArrayN (T *d, const Array<int>& dims) : Array<T> (d, get_size (dims)) |
|
52 { |
|
53 dimensions = dims; |
|
54 set_max_indices (dimensions.length ()); |
|
55 } |
|
56 |
|
57 public: |
|
58 |
|
59 static int get_size (const Array<int>& dims); |
|
60 |
|
61 // These really need to be protected (and they will be in the |
|
62 // future, so don't depend on them being here!), but they can't be |
|
63 // until template friends work correctly in g++. |
|
64 |
|
65 Array<int> dimensions; |
|
66 |
|
67 ArrayN (void) : Array<T> () { } |
|
68 |
|
69 ArrayN (const Array<int>& dims) : Array<T> (get_size (dims)) |
|
70 { |
|
71 dimensions = dims; |
|
72 set_max_indices (dimensions.length ()); |
|
73 } |
|
74 |
|
75 ArrayN (const Array<int>& dims, const T& val) |
|
76 : Array<T> (get_size (dims), val) |
|
77 { |
|
78 dimensions = dims; |
|
79 set_max_indices (dimensions.length ()); |
|
80 } |
|
81 |
|
82 ArrayN (const ArrayN<T>& a) : Array<T> (a) |
|
83 { |
|
84 dimensions = a.dimensions; |
|
85 set_max_indices (dimensions.length ()); |
|
86 } |
|
87 |
|
88 ArrayN (const Array<T>& a, const Array<int>& dims) : Array<T> (a) |
|
89 { |
|
90 dimensions = dims; |
|
91 set_max_indices (dimensions.length ()); |
|
92 } |
|
93 |
|
94 ~ArrayN (void) { } |
|
95 |
|
96 ArrayN<T>& operator = (const ArrayN<T>& a) |
|
97 { |
|
98 if (this != &a && rep != a.rep) |
|
99 { |
|
100 Array<T>::operator = (a); |
|
101 dimensions = a.dimensions; |
|
102 } |
|
103 |
|
104 return *this; |
|
105 } |
|
106 |
|
107 int compute_index (const Array<int>& idx) const; |
|
108 |
|
109 Array<int> dims (void) const { return dimensions; } |
|
110 |
|
111 T range_error (const char *fcn, const Array<int>& idx) const; |
|
112 T& range_error (const char *fcn, const Array<int>& idx); |
|
113 |
|
114 // No checking of any kind, ever. |
|
115 |
|
116 T& xelem (const Array<int>& idx) |
|
117 { return Array<T>::xelem (compute_index (idx)); } |
|
118 |
|
119 T xelem (const Array<int>& idx) const |
|
120 { return Array<T>::xelem (compute_index (idx)); } |
|
121 |
|
122 // Note that the following element selection methods don't use |
|
123 // xelem() because they need to make use of the code in |
|
124 // Array<T>::elem() that checks the reference count. |
|
125 |
|
126 T& checkelem (const Array<int>& idx) |
|
127 { |
|
128 int i = compute_index (idx); |
|
129 |
|
130 if (i < 0) |
|
131 return range_error ("ArrayN<T>::checkelem", idx); |
|
132 else |
|
133 return Array<T>::elem (i); |
|
134 } |
|
135 |
|
136 T& elem (const Array<int>& idx) |
|
137 { |
|
138 int i = compute_index (idx); |
|
139 |
|
140 return Array<T>::elem (i); |
|
141 } |
|
142 |
|
143 #if defined (BOUNDS_CHECKING) |
|
144 T& operator () (const Array<int>& idx) { return checkelem (idx); } |
|
145 #else |
|
146 T& operator () (const Array<int>& idx) { return elem (idx); } |
|
147 #endif |
|
148 |
|
149 T checkelem (const Array<int>& idx) const |
|
150 { |
|
151 int i = compute_index (idx); |
|
152 |
|
153 if (i < 0) |
|
154 return range_error ("ArrayN<T>::checkelem", idx); |
|
155 else |
|
156 return Array<T>::elem (i); |
|
157 } |
|
158 |
|
159 T elem (const Array<int>& idx) const |
|
160 { |
|
161 int i = compute_index (idx); |
|
162 |
|
163 return Array<T>::elem (i); |
|
164 } |
|
165 |
|
166 #if defined (BOUNDS_CHECKING) |
|
167 T operator () (const Array<int>& idx) const { return checkelem (idx); } |
|
168 #else |
|
169 T operator () (const Array<int>& idx) const { return elem (idx); } |
|
170 #endif |
|
171 |
|
172 void resize (const Array<int>& dims); |
|
173 void resize (const Array<int>& dims, const T& val); |
|
174 |
|
175 ArrayN<T>& insert (const ArrayN<T>& a, const Array<int>& dims); |
|
176 |
|
177 #ifdef HEAVYWEIGHT_INDEXING |
|
178 void maybe_delete_elements (Array<idx_vector>& idx); |
|
179 |
|
180 ArrayN<T> value (void); |
|
181 |
3933
|
182 ArrayN<T> index (idx_vector& idx, int resize_ok = 0, |
|
183 const T& rfv = Array<T>::resize_fill_value ()) const; |
3665
|
184 |
3933
|
185 ArrayN<T> index (Array<idx_vector>& idx, int resize_ok = 0, |
|
186 const T& rfv = Array<T>::resize_fill_value ()) const; |
|
187 |
3665
|
188 #endif |
|
189 }; |
|
190 |
|
191 template <class LT, class RT> |
|
192 int |
3836
|
193 assign (ArrayN<LT>& lhs, const ArrayN<RT>& rhs, const LT& resize_fill_value); |
|
194 |
|
195 template <class LT, class RT> |
|
196 int |
|
197 assign (ArrayN<LT>& lhs, const ArrayN<RT>& rhs) |
|
198 { |
3928
|
199 return assign (lhs, rhs, Array<LT>::resize_fill_value ()); |
3836
|
200 } |
3665
|
201 |
|
202 template <class T> |
|
203 std::ostream& |
|
204 operator << (std::ostream&, const ArrayN<T>&); |
|
205 |
|
206 #endif |
|
207 |
|
208 /* |
|
209 ;;; Local Variables: *** |
|
210 ;;; mode: C++ *** |
|
211 ;;; End: *** |
|
212 */ |