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 |
4066
|
27 #if defined (__GNUG__) && ! defined (NO_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> |
3585
|
51 class |
|
52 Array |
228
|
53 { |
3504
|
54 protected: |
1619
|
55 |
1735
|
56 // The real representation of all arrays. |
|
57 |
|
58 class ArrayRep |
|
59 { |
|
60 public: |
|
61 |
|
62 T *data; |
|
63 int len; |
|
64 int count; |
|
65 |
|
66 ArrayRep& operator = (const ArrayRep& a); |
|
67 |
|
68 ArrayRep (T *d, int l) : data (d), len (l), count (1) { } |
|
69 |
|
70 ArrayRep (void) : data (0), len (0), count (1) { } |
|
71 |
3585
|
72 explicit ArrayRep (int n) : data (new T [n]), len (n), count (1) { } |
1735
|
73 |
|
74 ArrayRep (const ArrayRep& a) |
|
75 : data (new T [a.len]), len (a.len), count (1) |
|
76 { |
|
77 for (int i = 0; i < len; i++) |
|
78 data[i] = a.data[i]; |
|
79 } |
|
80 |
|
81 ~ArrayRep (void) { delete [] data; } |
|
82 |
|
83 int length (void) const { return len; } |
|
84 |
|
85 T& elem (int n) { return data[n]; } |
|
86 |
|
87 T elem (int n) const { return data[n]; } |
1756
|
88 |
|
89 void qsort (int (*compare) (const void *, const void *)) |
|
90 { |
3613
|
91 octave_qsort (data, static_cast<size_t> (len), sizeof (T), compare); |
1756
|
92 } |
1735
|
93 }; |
|
94 |
2006
|
95 void make_unique (void) |
|
96 { |
|
97 if (rep->count > 1) |
|
98 { |
|
99 --rep->count; |
|
100 rep = new ArrayRep (*rep); |
|
101 } |
|
102 } |
|
103 |
1619
|
104 #ifdef HEAVYWEIGHT_INDEXING |
|
105 idx_vector *idx; |
|
106 int max_indices; |
|
107 int idx_count; |
|
108 #endif |
|
109 |
238
|
110 protected: |
|
111 |
4054
|
112 typename Array<T>::ArrayRep *rep; |
238
|
113 |
1550
|
114 Array (T *d, int l) |
|
115 { |
4054
|
116 rep = new typename Array<T>::ArrayRep (d, l); |
1619
|
117 |
|
118 #ifdef HEAVYWEIGHT_INDEXING |
|
119 idx = 0; |
|
120 max_indices = 1; |
|
121 idx_count = 0; |
|
122 #endif |
1550
|
123 } |
238
|
124 |
228
|
125 public: |
238
|
126 |
1550
|
127 Array (void) |
|
128 { |
4054
|
129 rep = new typename Array<T>::ArrayRep (); |
1619
|
130 |
|
131 #ifdef HEAVYWEIGHT_INDEXING |
|
132 idx = 0; |
|
133 max_indices = 1; |
|
134 idx_count = 0; |
|
135 #endif |
1550
|
136 } |
|
137 |
3585
|
138 explicit Array (int n) |
1550
|
139 { |
4054
|
140 rep = new typename Array<T>::ArrayRep (n); |
1619
|
141 |
|
142 #ifdef HEAVYWEIGHT_INDEXING |
|
143 idx = 0; |
|
144 max_indices = 1; |
|
145 idx_count = 0; |
|
146 #endif |
1550
|
147 } |
|
148 |
238
|
149 Array (int n, const T& val); |
|
150 |
1550
|
151 Array (const Array<T>& a) |
|
152 { |
|
153 rep = a.rep; |
|
154 rep->count++; |
1619
|
155 |
|
156 #ifdef HEAVYWEIGHT_INDEXING |
|
157 max_indices = a.max_indices; |
|
158 idx_count = 0; |
|
159 idx = 0; |
|
160 #endif |
1550
|
161 } |
228
|
162 |
1619
|
163 ~Array (void); |
228
|
164 |
|
165 Array<T>& operator = (const Array<T>& a); |
238
|
166 |
1550
|
167 int capacity (void) const { return rep->length (); } |
|
168 int length (void) const { return rep->length (); } |
228
|
169 |
3665
|
170 T range_error (const char *fcn, int n) const; |
|
171 T& range_error (const char *fcn, int n); |
|
172 |
2108
|
173 // No checking, even for multiple references, ever. |
|
174 |
|
175 T& xelem (int n) { return rep->elem (n); } |
|
176 T xelem (int n) const { return rep->elem (n); } |
|
177 |
2006
|
178 // XXX FIXME XXX -- would be nice to fix this so that we don't |
|
179 // unnecessarily force a copy, but that is not so easy, and I see no |
|
180 // clean way to do it. |
|
181 |
2802
|
182 T& checkelem (int n) |
2006
|
183 { |
|
184 if (n < 0 || n >= rep->length ()) |
2109
|
185 return range_error ("T& Array<T>::checkelem", n); |
2006
|
186 else |
2108
|
187 { |
|
188 make_unique (); |
|
189 return xelem (n); |
|
190 } |
2006
|
191 } |
|
192 |
2108
|
193 T& elem (int n) |
|
194 { |
|
195 make_unique (); |
2109
|
196 return xelem (n); |
2108
|
197 } |
2306
|
198 |
|
199 #if defined (BOUNDS_CHECKING) |
|
200 T& operator () (int n) { return checkelem (n); } |
|
201 #else |
|
202 T& operator () (int n) { return elem (n); } |
2006
|
203 #endif |
|
204 |
2802
|
205 T checkelem (int n) const |
2006
|
206 { |
|
207 if (n < 0 || n >= rep->length ()) |
2109
|
208 return range_error ("T Array<T>::checkelem", n); |
2049
|
209 else |
2108
|
210 return xelem (n); |
2006
|
211 } |
1989
|
212 |
2802
|
213 T elem (int n) const { return xelem (n); } |
2306
|
214 |
2108
|
215 #if defined (BOUNDS_CHECKING) |
2802
|
216 T operator () (int n) const { return checkelem (n); } |
2006
|
217 #else |
2802
|
218 T operator () (int n) const { return elem (n); } |
2006
|
219 #endif |
|
220 |
238
|
221 void resize (int n); |
|
222 void resize (int n, const T& val); |
|
223 |
1550
|
224 const T *data (void) const { return rep->data; } |
228
|
225 |
3952
|
226 const T *fortran_vec (void) const { return data (); } |
|
227 |
238
|
228 T *fortran_vec (void); |
1560
|
229 |
1781
|
230 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
231 { |
2347
|
232 make_unique (); |
1756
|
233 |
|
234 rep->qsort (compare); |
1781
|
235 |
|
236 return *this; |
1756
|
237 } |
|
238 |
1560
|
239 #ifdef HEAVYWEIGHT_INDEXING |
3933
|
240 |
1619
|
241 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
242 |
1619
|
243 void clear_index (void); |
1560
|
244 |
1619
|
245 void set_index (const idx_vector& i); |
1560
|
246 |
1619
|
247 int index_count (void) const { return idx_count; } |
1560
|
248 |
1619
|
249 idx_vector *get_idx (void) const { return idx; } |
1560
|
250 |
|
251 void maybe_delete_elements (idx_vector& i); |
|
252 |
|
253 Array<T> value (void); |
2382
|
254 |
3933
|
255 Array<T> index (idx_vector& i, int resize_ok = 0, |
|
256 const T& rfv = Array<T>::resize_fill_value ()) const; |
|
257 |
1560
|
258 #endif |
3928
|
259 |
3944
|
260 static T resize_fill_value (void) { return T (); } |
3933
|
261 |
|
262 void print_info (std::ostream& os, const std::string& prefix) const; |
228
|
263 }; |
|
264 |
1560
|
265 template <class LT, class RT> |
3665
|
266 int |
3836
|
267 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& resize_fill_value); |
|
268 |
|
269 template <class LT, class RT> |
|
270 int |
|
271 assign (Array<LT>& lhs, const Array<RT>& rhs) |
|
272 { |
3928
|
273 return assign (lhs, rhs, Array<LT>::resize_fill_value ()); |
3836
|
274 } |
1560
|
275 |
228
|
276 #endif |
|
277 |
|
278 /* |
|
279 ;;; Local Variables: *** |
|
280 ;;; mode: C++ *** |
|
281 ;;; End: *** |
|
282 */ |