1993
|
1 // Template array classes |
228
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
228
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
228
|
21 |
|
22 */ |
|
23 |
382
|
24 #if !defined (octave_Array_h) |
|
25 #define octave_Array_h 1 |
|
26 |
4192
|
27 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
28 #pragma interface |
|
29 #endif |
|
30 |
1366
|
31 #include <cassert> |
4152
|
32 #include <cstddef> |
3613
|
33 |
3933
|
34 #include <iostream> |
|
35 |
3613
|
36 #include "lo-utils.h" |
228
|
37 |
1560
|
38 class idx_vector; |
|
39 |
2023
|
40 // For now, define this here if it is not already defined. Not doing |
|
41 // this can result in bugs that are very hard to find. |
|
42 |
|
43 #ifndef HEAVYWEIGHT_INDEXING |
|
44 #define HEAVYWEIGHT_INDEXING 1 |
|
45 #endif |
|
46 |
1359
|
47 // One dimensional array class. Handles the reference counting for |
|
48 // all the derived classes. |
238
|
49 |
228
|
50 template <class T> |
4459
|
51 T |
|
52 resize_fill_value (const T& x) |
|
53 { |
|
54 return x; |
|
55 } |
|
56 |
|
57 template <class T> |
3585
|
58 class |
|
59 Array |
228
|
60 { |
3504
|
61 protected: |
1619
|
62 |
1735
|
63 // The real representation of all arrays. |
|
64 |
|
65 class ArrayRep |
|
66 { |
|
67 public: |
|
68 |
|
69 T *data; |
|
70 int len; |
|
71 int count; |
|
72 |
|
73 ArrayRep& operator = (const ArrayRep& a); |
|
74 |
|
75 ArrayRep (T *d, int l) : data (d), len (l), count (1) { } |
|
76 |
|
77 ArrayRep (void) : data (0), len (0), count (1) { } |
|
78 |
3585
|
79 explicit ArrayRep (int n) : data (new T [n]), len (n), count (1) { } |
1735
|
80 |
|
81 ArrayRep (const ArrayRep& a) |
|
82 : data (new T [a.len]), len (a.len), count (1) |
|
83 { |
|
84 for (int i = 0; i < len; i++) |
|
85 data[i] = a.data[i]; |
|
86 } |
|
87 |
|
88 ~ArrayRep (void) { delete [] data; } |
|
89 |
|
90 int length (void) const { return len; } |
|
91 |
|
92 T& elem (int n) { return data[n]; } |
|
93 |
|
94 T elem (int n) const { return data[n]; } |
1756
|
95 |
|
96 void qsort (int (*compare) (const void *, const void *)) |
|
97 { |
3613
|
98 octave_qsort (data, static_cast<size_t> (len), sizeof (T), compare); |
1756
|
99 } |
1735
|
100 }; |
|
101 |
2006
|
102 void make_unique (void) |
|
103 { |
|
104 if (rep->count > 1) |
|
105 { |
|
106 --rep->count; |
|
107 rep = new ArrayRep (*rep); |
|
108 } |
|
109 } |
|
110 |
1619
|
111 #ifdef HEAVYWEIGHT_INDEXING |
|
112 idx_vector *idx; |
|
113 int max_indices; |
|
114 int idx_count; |
|
115 #endif |
|
116 |
238
|
117 protected: |
|
118 |
4054
|
119 typename Array<T>::ArrayRep *rep; |
238
|
120 |
1550
|
121 Array (T *d, int l) |
|
122 { |
4054
|
123 rep = new typename Array<T>::ArrayRep (d, l); |
1619
|
124 |
|
125 #ifdef HEAVYWEIGHT_INDEXING |
|
126 idx = 0; |
|
127 max_indices = 1; |
|
128 idx_count = 0; |
|
129 #endif |
1550
|
130 } |
238
|
131 |
228
|
132 public: |
238
|
133 |
1550
|
134 Array (void) |
|
135 { |
4054
|
136 rep = new typename Array<T>::ArrayRep (); |
1619
|
137 |
|
138 #ifdef HEAVYWEIGHT_INDEXING |
|
139 idx = 0; |
|
140 max_indices = 1; |
|
141 idx_count = 0; |
|
142 #endif |
1550
|
143 } |
|
144 |
3585
|
145 explicit Array (int n) |
1550
|
146 { |
4054
|
147 rep = new typename Array<T>::ArrayRep (n); |
1619
|
148 |
|
149 #ifdef HEAVYWEIGHT_INDEXING |
|
150 idx = 0; |
|
151 max_indices = 1; |
|
152 idx_count = 0; |
|
153 #endif |
1550
|
154 } |
|
155 |
238
|
156 Array (int n, const T& val); |
|
157 |
1550
|
158 Array (const Array<T>& a) |
|
159 { |
|
160 rep = a.rep; |
|
161 rep->count++; |
1619
|
162 |
|
163 #ifdef HEAVYWEIGHT_INDEXING |
|
164 max_indices = a.max_indices; |
|
165 idx_count = 0; |
|
166 idx = 0; |
|
167 #endif |
1550
|
168 } |
228
|
169 |
1619
|
170 ~Array (void); |
228
|
171 |
|
172 Array<T>& operator = (const Array<T>& a); |
238
|
173 |
1550
|
174 int capacity (void) const { return rep->length (); } |
|
175 int length (void) const { return rep->length (); } |
228
|
176 |
3665
|
177 T range_error (const char *fcn, int n) const; |
|
178 T& range_error (const char *fcn, int n); |
|
179 |
2108
|
180 // No checking, even for multiple references, ever. |
|
181 |
|
182 T& xelem (int n) { return rep->elem (n); } |
|
183 T xelem (int n) const { return rep->elem (n); } |
|
184 |
2006
|
185 // XXX FIXME XXX -- would be nice to fix this so that we don't |
|
186 // unnecessarily force a copy, but that is not so easy, and I see no |
|
187 // clean way to do it. |
|
188 |
2802
|
189 T& checkelem (int n) |
2006
|
190 { |
|
191 if (n < 0 || n >= rep->length ()) |
2109
|
192 return range_error ("T& Array<T>::checkelem", n); |
2006
|
193 else |
2108
|
194 { |
|
195 make_unique (); |
|
196 return xelem (n); |
|
197 } |
2006
|
198 } |
|
199 |
2108
|
200 T& elem (int n) |
|
201 { |
|
202 make_unique (); |
2109
|
203 return xelem (n); |
2108
|
204 } |
2306
|
205 |
|
206 #if defined (BOUNDS_CHECKING) |
|
207 T& operator () (int n) { return checkelem (n); } |
|
208 #else |
|
209 T& operator () (int n) { return elem (n); } |
2006
|
210 #endif |
|
211 |
2802
|
212 T checkelem (int n) const |
2006
|
213 { |
|
214 if (n < 0 || n >= rep->length ()) |
2109
|
215 return range_error ("T Array<T>::checkelem", n); |
2049
|
216 else |
2108
|
217 return xelem (n); |
2006
|
218 } |
1989
|
219 |
2802
|
220 T elem (int n) const { return xelem (n); } |
2306
|
221 |
2108
|
222 #if defined (BOUNDS_CHECKING) |
2802
|
223 T operator () (int n) const { return checkelem (n); } |
2006
|
224 #else |
2802
|
225 T operator () (int n) const { return elem (n); } |
2006
|
226 #endif |
|
227 |
238
|
228 void resize (int n); |
|
229 void resize (int n, const T& val); |
|
230 |
1550
|
231 const T *data (void) const { return rep->data; } |
228
|
232 |
3952
|
233 const T *fortran_vec (void) const { return data (); } |
|
234 |
238
|
235 T *fortran_vec (void); |
1560
|
236 |
1781
|
237 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
238 { |
2347
|
239 make_unique (); |
1756
|
240 |
|
241 rep->qsort (compare); |
1781
|
242 |
|
243 return *this; |
1756
|
244 } |
|
245 |
1560
|
246 #ifdef HEAVYWEIGHT_INDEXING |
3933
|
247 |
1619
|
248 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
249 |
1619
|
250 void clear_index (void); |
1560
|
251 |
1619
|
252 void set_index (const idx_vector& i); |
1560
|
253 |
1619
|
254 int index_count (void) const { return idx_count; } |
1560
|
255 |
1619
|
256 idx_vector *get_idx (void) const { return idx; } |
1560
|
257 |
|
258 void maybe_delete_elements (idx_vector& i); |
|
259 |
|
260 Array<T> value (void); |
2382
|
261 |
3933
|
262 Array<T> index (idx_vector& i, int resize_ok = 0, |
4459
|
263 const T& rfv = resize_fill_value (T ())) const; |
3933
|
264 |
1560
|
265 #endif |
3928
|
266 |
4459
|
267 // static T resize_fill_value (void) { return T (); } |
3933
|
268 |
|
269 void print_info (std::ostream& os, const std::string& prefix) const; |
228
|
270 }; |
|
271 |
1560
|
272 template <class LT, class RT> |
3665
|
273 int |
4461
|
274 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv); |
3836
|
275 |
4459
|
276 |
3836
|
277 template <class LT, class RT> |
|
278 int |
|
279 assign (Array<LT>& lhs, const Array<RT>& rhs) |
|
280 { |
4459
|
281 // return assign (lhs, rhs, Array<LT>::resize_fill_value ()); |
|
282 return assign (lhs, rhs, resize_fill_value (LT ())); |
3836
|
283 } |
1560
|
284 |
228
|
285 #endif |
|
286 |
|
287 /* |
|
288 ;;; Local Variables: *** |
|
289 ;;; mode: C++ *** |
|
290 ;;; End: *** |
|
291 */ |