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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
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 { |
4054
|
51 rep = new typename Array<T>::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 idx_count = 0; |
|
89 idx = 0; |
|
90 #endif |
|
91 |
237
|
92 return *this; |
|
93 } |
|
94 |
|
95 template <class T> |
|
96 void |
|
97 Array<T>::resize (int n) |
|
98 { |
|
99 if (n < 0) |
|
100 { |
1560
|
101 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
102 return; |
|
103 } |
|
104 |
|
105 if (n == length ()) |
|
106 return; |
|
107 |
4054
|
108 typename Array<T>::ArrayRep *old_rep = rep; |
237
|
109 const T *old_data = data (); |
|
110 int old_len = length (); |
|
111 |
4054
|
112 rep = new typename Array<T>::ArrayRep (n); |
237
|
113 |
|
114 if (old_data && old_len > 0) |
|
115 { |
|
116 int min_len = old_len < n ? old_len : n; |
|
117 |
|
118 for (int i = 0; i < min_len; i++) |
|
119 xelem (i) = old_data[i]; |
|
120 } |
|
121 |
|
122 if (--old_rep->count <= 0) |
|
123 delete old_rep; |
|
124 } |
|
125 |
|
126 template <class T> |
|
127 void |
|
128 Array<T>::resize (int n, const T& val) |
|
129 { |
|
130 if (n < 0) |
|
131 { |
1560
|
132 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
237
|
133 return; |
|
134 } |
|
135 |
|
136 if (n == length ()) |
|
137 return; |
|
138 |
4054
|
139 typename Array<T>::ArrayRep *old_rep = rep; |
237
|
140 const T *old_data = data (); |
|
141 int old_len = length (); |
|
142 |
4054
|
143 rep = new typename Array<T>::ArrayRep (n); |
237
|
144 |
|
145 int min_len = old_len < n ? old_len : n; |
|
146 |
|
147 if (old_data && old_len > 0) |
|
148 { |
|
149 for (int i = 0; i < min_len; i++) |
|
150 xelem (i) = old_data[i]; |
|
151 } |
|
152 |
|
153 for (int i = old_len; i < n; i++) |
|
154 xelem (i) = val; |
|
155 |
|
156 if (--old_rep->count <= 0) |
|
157 delete old_rep; |
|
158 } |
|
159 |
|
160 template <class T> |
|
161 T * |
|
162 Array<T>::fortran_vec (void) |
|
163 { |
|
164 if (rep->count > 1) |
|
165 { |
|
166 --rep->count; |
4054
|
167 rep = new typename Array<T>::ArrayRep (*rep); |
237
|
168 } |
|
169 return rep->data; |
|
170 } |
2049
|
171 template <class T> |
|
172 T |
2109
|
173 Array<T>::range_error (const char *fcn, int n) const |
2049
|
174 { |
2109
|
175 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
176 return T (); |
|
177 } |
|
178 |
|
179 template <class T> |
|
180 T& |
2109
|
181 Array<T>::range_error (const char *fcn, int n) |
2049
|
182 { |
2109
|
183 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
2049
|
184 static T foo; |
|
185 return foo; |
|
186 } |
|
187 |
3933
|
188 template <class T> |
|
189 void |
|
190 Array<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
191 { |
|
192 os << prefix << "rep address: " << rep << "\n" |
|
193 << prefix << "rep->len: " << rep->len << "\n" |
|
194 << prefix << "rep->data: " << static_cast<void *> (rep->data) << "\n" |
|
195 << prefix << "rep->count: " << rep->count << "\n"; |
|
196 } |
|
197 |
237
|
198 /* |
|
199 ;;; Local Variables: *** |
|
200 ;;; mode: C++ *** |
|
201 ;;; End: *** |
|
202 */ |