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