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 |
1359
|
38 // One dimensional array class. Handles the reference counting for |
|
39 // all the derived classes. |
238
|
40 |
228
|
41 template <class T> |
|
42 class Array |
|
43 { |
1619
|
44 private: |
|
45 |
1735
|
46 // The real representation of all arrays. |
|
47 |
|
48 class ArrayRep |
|
49 { |
|
50 public: |
|
51 |
|
52 T *data; |
|
53 int len; |
|
54 int count; |
|
55 |
|
56 ArrayRep& operator = (const ArrayRep& a); |
|
57 |
|
58 ArrayRep (T *d, int l) : data (d), len (l), count (1) { } |
|
59 |
|
60 ArrayRep (void) : data (0), len (0), count (1) { } |
|
61 |
|
62 ArrayRep (int n) : data (new T [n]), len (n), count (1) { } |
|
63 |
|
64 ArrayRep (const ArrayRep& a) |
|
65 : data (new T [a.len]), len (a.len), count (1) |
|
66 { |
|
67 for (int i = 0; i < len; i++) |
|
68 data[i] = a.data[i]; |
|
69 } |
|
70 |
|
71 ~ArrayRep (void) { delete [] data; } |
|
72 |
|
73 int length (void) const { return len; } |
|
74 |
|
75 T& elem (int n) { return data[n]; } |
|
76 |
|
77 T elem (int n) const { return data[n]; } |
1756
|
78 |
|
79 void qsort (int (*compare) (const void *, const void *)) |
|
80 { |
|
81 ::qsort (data, len, sizeof (T), compare); |
|
82 } |
1735
|
83 }; |
|
84 |
1619
|
85 #ifdef HEAVYWEIGHT_INDEXING |
|
86 idx_vector *idx; |
|
87 int max_indices; |
|
88 int idx_count; |
|
89 #endif |
|
90 |
238
|
91 protected: |
|
92 |
1735
|
93 ArrayRep *rep; |
238
|
94 |
1550
|
95 Array (T *d, int l) |
|
96 { |
1735
|
97 rep = new ArrayRep (d, l); |
1619
|
98 |
|
99 #ifdef HEAVYWEIGHT_INDEXING |
|
100 idx = 0; |
|
101 max_indices = 1; |
|
102 idx_count = 0; |
|
103 #endif |
1550
|
104 } |
238
|
105 |
228
|
106 public: |
238
|
107 |
1550
|
108 Array (void) |
|
109 { |
1735
|
110 rep = new ArrayRep (); |
1619
|
111 |
|
112 #ifdef HEAVYWEIGHT_INDEXING |
|
113 idx = 0; |
|
114 max_indices = 1; |
|
115 idx_count = 0; |
|
116 #endif |
1550
|
117 } |
|
118 |
|
119 Array (int n) |
|
120 { |
1735
|
121 rep = new ArrayRep (n); |
1619
|
122 |
|
123 #ifdef HEAVYWEIGHT_INDEXING |
|
124 idx = 0; |
|
125 max_indices = 1; |
|
126 idx_count = 0; |
|
127 #endif |
1550
|
128 } |
|
129 |
238
|
130 Array (int n, const T& val); |
|
131 |
1550
|
132 Array (const Array<T>& a) |
|
133 { |
|
134 rep = a.rep; |
|
135 rep->count++; |
1619
|
136 |
|
137 #ifdef HEAVYWEIGHT_INDEXING |
|
138 max_indices = a.max_indices; |
|
139 idx_count = 0; |
|
140 idx = 0; |
|
141 #endif |
1550
|
142 } |
228
|
143 |
1619
|
144 ~Array (void); |
228
|
145 |
|
146 Array<T>& operator = (const Array<T>& a); |
238
|
147 |
1550
|
148 int capacity (void) const { return rep->length (); } |
|
149 int length (void) const { return rep->length (); } |
228
|
150 |
1550
|
151 T& elem (int n) |
|
152 { |
|
153 if (rep->count > 1) |
|
154 { |
|
155 --rep->count; |
1735
|
156 rep = new ArrayRep (*rep); |
1550
|
157 } |
|
158 return rep->elem (n); |
|
159 } |
|
160 |
228
|
161 T& checkelem (int n); |
1550
|
162 T& operator () (int n) { return checkelem (n); } |
228
|
163 |
1989
|
164 T elem (int n) const; |
|
165 T checkelem (int n) const; |
|
166 T operator () (int n) const; |
|
167 |
1359
|
168 // No checking. |
|
169 |
1550
|
170 T& xelem (int n) { return rep->elem (n); } |
1989
|
171 T xelem (int n) const { return rep->elem (n); } |
228
|
172 |
238
|
173 void resize (int n); |
|
174 void resize (int n, const T& val); |
|
175 |
1550
|
176 const T *data (void) const { return rep->data; } |
228
|
177 |
238
|
178 T *fortran_vec (void); |
1560
|
179 |
1781
|
180 Array<T>& qsort (int (*compare) (const void *, const void *)) |
1756
|
181 { |
|
182 if (rep->count > 1) |
|
183 { |
|
184 --rep->count; |
|
185 rep = new ArrayRep (*rep); |
|
186 } |
|
187 |
|
188 rep->qsort (compare); |
1781
|
189 |
|
190 return *this; |
1756
|
191 } |
|
192 |
1560
|
193 #ifdef HEAVYWEIGHT_INDEXING |
1619
|
194 void set_max_indices (int mi) { max_indices = mi; } |
1560
|
195 |
1619
|
196 void clear_index (void); |
1560
|
197 |
1619
|
198 void set_index (const idx_vector& i); |
1560
|
199 |
1619
|
200 int index_count (void) const { return idx_count; } |
1560
|
201 |
1619
|
202 idx_vector *get_idx (void) const { return idx; } |
1560
|
203 |
|
204 void maybe_delete_elements (idx_vector& i); |
|
205 |
|
206 Array<T> value (void); |
|
207 #endif |
228
|
208 }; |
|
209 |
1560
|
210 template <class LT, class RT> |
|
211 int assign (Array<LT>& lhs, const Array<RT>& rhs); |
|
212 |
228
|
213 #endif |
|
214 |
|
215 /* |
|
216 ;;; Local Variables: *** |
|
217 ;;; mode: C++ *** |
|
218 ;;; End: *** |
|
219 */ |