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