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