1993
|
1 // Template array classes |
237
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 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 |
3503
|
34 #include <iostream> |
1560
|
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 void |
|
98 Array<T>::resize (int n) |
|
99 { |
|
100 if (n < 0) |
|
101 { |
1560
|
102 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
103 return; |
|
104 } |
|
105 |
|
106 if (n == length ()) |
|
107 return; |
|
108 |
1735
|
109 ArrayRep *old_rep = rep; |
237
|
110 const T *old_data = data (); |
|
111 int old_len = length (); |
|
112 |
1735
|
113 rep = new ArrayRep (n); |
237
|
114 |
|
115 if (old_data && old_len > 0) |
|
116 { |
|
117 int min_len = old_len < n ? old_len : n; |
|
118 |
|
119 for (int i = 0; i < min_len; i++) |
|
120 xelem (i) = old_data[i]; |
|
121 } |
|
122 |
|
123 if (--old_rep->count <= 0) |
|
124 delete old_rep; |
|
125 } |
|
126 |
|
127 template <class T> |
|
128 void |
|
129 Array<T>::resize (int n, const T& val) |
|
130 { |
|
131 if (n < 0) |
|
132 { |
1560
|
133 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
134 return; |
|
135 } |
|
136 |
|
137 if (n == length ()) |
|
138 return; |
|
139 |
1735
|
140 ArrayRep *old_rep = rep; |
237
|
141 const T *old_data = data (); |
|
142 int old_len = length (); |
|
143 |
1735
|
144 rep = new ArrayRep (n); |
237
|
145 |
|
146 int min_len = old_len < n ? old_len : n; |
|
147 |
|
148 if (old_data && old_len > 0) |
|
149 { |
|
150 for (int i = 0; i < min_len; i++) |
|
151 xelem (i) = old_data[i]; |
|
152 } |
|
153 |
|
154 for (int i = old_len; i < n; i++) |
|
155 xelem (i) = val; |
|
156 |
|
157 if (--old_rep->count <= 0) |
|
158 delete old_rep; |
|
159 } |
|
160 |
|
161 template <class T> |
|
162 T * |
|
163 Array<T>::fortran_vec (void) |
|
164 { |
|
165 if (rep->count > 1) |
|
166 { |
|
167 --rep->count; |
1735
|
168 rep = new ArrayRep (*rep); |
237
|
169 } |
|
170 return rep->data; |
|
171 } |
2049
|
172 template <class T> |
|
173 T |
2109
|
174 Array<T>::range_error (const char *fcn, int n) const |
2049
|
175 { |
2109
|
176 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
177 return T (); |
|
178 } |
|
179 |
|
180 template <class T> |
|
181 T& |
2109
|
182 Array<T>::range_error (const char *fcn, int n) |
2049
|
183 { |
2109
|
184 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
185 static T foo; |
|
186 return foo; |
|
187 } |
|
188 |
237
|
189 /* |
|
190 ;;; Local Variables: *** |
|
191 ;;; mode: C++ *** |
|
192 ;;; End: *** |
|
193 */ |