237
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 John W. Eaton |
237
|
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. |
237
|
21 |
|
22 */ |
|
23 |
1296
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
237
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
237
|
30 #endif |
|
31 |
1367
|
32 #include <cassert> |
449
|
33 |
1560
|
34 #include <iostream.h> |
|
35 |
237
|
36 #include "Array.h" |
|
37 |
1560
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "Array-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
1360
|
45 // One dimensional array class. Handles the reference counting for |
|
46 // all the derived classes. |
237
|
47 |
|
48 template <class T> |
|
49 Array<T>::Array (int n, const T& val) |
|
50 { |
1735
|
51 rep = new ArrayRep (n); |
1619
|
52 |
237
|
53 for (int i = 0; i < n; i++) |
|
54 rep->data[i] = val; |
1619
|
55 |
|
56 #ifdef HEAVYWEIGHT_INDEXING |
|
57 max_indices = 1; |
|
58 idx_count = 0; |
|
59 idx = 0; |
|
60 #endif |
|
61 } |
|
62 |
|
63 template <class T> |
|
64 Array<T>::~Array (void) |
|
65 { |
|
66 if (--rep->count <= 0) |
|
67 delete rep; |
|
68 |
|
69 #ifdef HEAVYWEIGHT_INDEXING |
|
70 delete [] idx; |
|
71 #endif |
237
|
72 } |
|
73 |
|
74 template <class T> |
|
75 Array<T>& |
|
76 Array<T>::operator = (const Array<T>& a) |
|
77 { |
1700
|
78 if (this != &a && rep != a.rep) |
659
|
79 { |
|
80 if (--rep->count <= 0) |
|
81 delete rep; |
237
|
82 |
659
|
83 rep = a.rep; |
|
84 rep->count++; |
|
85 } |
1619
|
86 |
|
87 #ifdef HEAVYWEIGHT_INDEXING |
|
88 max_indices = 1; |
|
89 idx_count = 0; |
|
90 idx = 0; |
|
91 #endif |
|
92 |
237
|
93 return *this; |
|
94 } |
|
95 |
|
96 template <class T> |
|
97 T& |
|
98 Array<T>::checkelem (int n) |
|
99 { |
|
100 if (n < 0 || n >= rep->length ()) |
|
101 { |
|
102 (*current_liboctave_error_handler) ("range error"); |
254
|
103 static T foo; |
237
|
104 return foo; |
|
105 } |
|
106 return elem (n); |
|
107 } |
|
108 |
|
109 template <class T> |
|
110 T |
|
111 Array<T>::elem (int n) const |
|
112 { |
|
113 return rep->elem (n); |
|
114 } |
|
115 |
|
116 template <class T> |
|
117 T |
|
118 Array<T>::checkelem (int n) const |
|
119 { |
|
120 if (n < 0 || n >= rep->length ()) |
|
121 { |
|
122 (*current_liboctave_error_handler) ("range error"); |
254
|
123 T foo; |
1470
|
124 static T *bar = &foo; |
254
|
125 return foo; |
237
|
126 } |
|
127 return elem (n); |
|
128 } |
|
129 |
|
130 template <class T> |
|
131 T |
|
132 Array<T>::operator () (int n) const |
|
133 { |
|
134 return checkelem (n); |
|
135 } |
|
136 |
|
137 template <class T> |
|
138 void |
|
139 Array<T>::resize (int n) |
|
140 { |
|
141 if (n < 0) |
|
142 { |
1560
|
143 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
144 return; |
|
145 } |
|
146 |
|
147 if (n == length ()) |
|
148 return; |
|
149 |
1735
|
150 ArrayRep *old_rep = rep; |
237
|
151 const T *old_data = data (); |
|
152 int old_len = length (); |
|
153 |
1735
|
154 rep = new ArrayRep (n); |
237
|
155 |
|
156 if (old_data && old_len > 0) |
|
157 { |
|
158 int min_len = old_len < n ? old_len : n; |
|
159 |
|
160 for (int i = 0; i < min_len; i++) |
|
161 xelem (i) = old_data[i]; |
|
162 } |
|
163 |
|
164 if (--old_rep->count <= 0) |
|
165 delete old_rep; |
|
166 } |
|
167 |
|
168 template <class T> |
|
169 void |
|
170 Array<T>::resize (int n, const T& val) |
|
171 { |
|
172 if (n < 0) |
|
173 { |
1560
|
174 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
175 return; |
|
176 } |
|
177 |
|
178 if (n == length ()) |
|
179 return; |
|
180 |
1735
|
181 ArrayRep *old_rep = rep; |
237
|
182 const T *old_data = data (); |
|
183 int old_len = length (); |
|
184 |
1735
|
185 rep = new ArrayRep (n); |
237
|
186 |
|
187 int min_len = old_len < n ? old_len : n; |
|
188 |
|
189 if (old_data && old_len > 0) |
|
190 { |
|
191 for (int i = 0; i < min_len; i++) |
|
192 xelem (i) = old_data[i]; |
|
193 } |
|
194 |
|
195 for (int i = old_len; i < n; i++) |
|
196 xelem (i) = val; |
|
197 |
|
198 if (--old_rep->count <= 0) |
|
199 delete old_rep; |
|
200 } |
|
201 |
|
202 template <class T> |
|
203 T * |
|
204 Array<T>::fortran_vec (void) |
|
205 { |
|
206 if (rep->count > 1) |
|
207 { |
|
208 --rep->count; |
1735
|
209 rep = new ArrayRep (*rep); |
237
|
210 } |
|
211 return rep->data; |
|
212 } |
|
213 |
|
214 /* |
|
215 ;;; Local Variables: *** |
|
216 ;;; mode: C++ *** |
|
217 ;;; page-delimiter: "^/\\*" *** |
|
218 ;;; End: *** |
|
219 */ |