1993
|
1 // Template array classes |
228
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 |
448
|
34 #include "lo-error.h" |
354
|
35 |
1560
|
36 class idx_vector; |
|
37 |
2023
|
38 // For now, define this here if it is not already defined. Not doing |
|
39 // this can result in bugs that are very hard to find. |
|
40 |
|
41 #ifndef HEAVYWEIGHT_INDEXING |
|
42 #define HEAVYWEIGHT_INDEXING 1 |
|
43 #endif |
|
44 |
1359
|
45 // One dimensional array class. Handles the reference counting for |
|
46 // all the derived classes. |
238
|
47 |
228
|
48 template <class T> |
|
49 class Array |
|
50 { |
1619
|
51 private: |
|
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 |
|
69 ArrayRep (int n) : data (new T [n]), len (n), count (1) { } |
|
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 { |
|
88 ::qsort (data, len, sizeof (T), compare); |
|
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 |
|
135 Array (int n) |
|
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 |
2006
|
167 // XXX FIXME XXX -- would be nice to fix this so that we don't |
|
168 // unnecessarily force a copy, but that is not so easy, and I see no |
|
169 // clean way to do it. |
|
170 |
1550
|
171 T& elem (int n) |
|
172 { |
2006
|
173 make_unique (); |
1550
|
174 return rep->elem (n); |
|
175 } |
|
176 |
2006
|
177 T& Array<T>::checkelem (int n) |
|
178 { |
|
179 if (n < 0 || n >= rep->length ()) |
|
180 { |
|
181 (*current_liboctave_error_handler) ("range error"); |
|
182 static T foo; |
|
183 return foo; |
|
184 } |
|
185 else |
|
186 return elem (n); |
|
187 } |
|
188 |
|
189 #if defined (NO_BOUNDS_CHECKING) |
|
190 T& operator () (int n) { return elem (n); } |
|
191 #else |
1550
|
192 T& operator () (int n) { return checkelem (n); } |
2006
|
193 #endif |
|
194 |
|
195 T Array<T>::elem (int n) const { return rep->elem (n); } |
228
|
196 |
2006
|
197 T Array<T>::checkelem (int n) const |
|
198 { |
|
199 if (n < 0 || n >= rep->length ()) |
2049
|
200 return range_error (); |
|
201 else |
|
202 return elem (n); |
2006
|
203 } |
1989
|
204 |
2006
|
205 #if defined (NO_BOUNDS_CHECKING) |
|
206 T Array<T>::operator () (int n) const { return elem (n); } |
|
207 #else |
|
208 T Array<T>::operator () (int n) const { return checkelem (n); } |
|
209 #endif |
|
210 |
|
211 // No checking, even for multiple references, ever. |
1359
|
212 |
1550
|
213 T& xelem (int n) { return rep->elem (n); } |
1989
|
214 T xelem (int n) const { return rep->elem (n); } |
228
|
215 |
238
|
216 void resize (int n); |
|
217 void resize (int n, const T& val); |
|
218 |
1550
|
219 const T *data (void) const { return rep->data; } |
228
|
220 |
238
|
221 T *fortran_vec (void); |
1560
|
222 |
1781
|
223 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
224 { |
|
225 if (rep->count > 1) |
|
226 { |
|
227 --rep->count; |
|
228 rep = new ArrayRep (*rep); |
|
229 } |
|
230 |
|
231 rep->qsort (compare); |
1781
|
232 |
|
233 return *this; |
1756
|
234 } |
|
235 |
2049
|
236 T range_error (void) const; |
|
237 T& range_error (void); |
|
238 |
1560
|
239 #ifdef HEAVYWEIGHT_INDEXING |
1619
|
240 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
241 |
1619
|
242 void clear_index (void); |
1560
|
243 |
1619
|
244 void set_index (const idx_vector& i); |
1560
|
245 |
1619
|
246 int index_count (void) const { return idx_count; } |
1560
|
247 |
1619
|
248 idx_vector *get_idx (void) const { return idx; } |
1560
|
249 |
|
250 void maybe_delete_elements (idx_vector& i); |
|
251 |
|
252 Array<T> value (void); |
|
253 #endif |
228
|
254 }; |
|
255 |
1560
|
256 template <class LT, class RT> |
|
257 int assign (Array<LT>& lhs, const Array<RT>& rhs); |
|
258 |
228
|
259 #endif |
|
260 |
|
261 /* |
|
262 ;;; Local Variables: *** |
|
263 ;;; mode: C++ *** |
|
264 ;;; End: *** |
|
265 */ |