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