1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
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 |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cassert> |
|
33 |
|
34 #include <iostream.h> |
|
35 |
|
36 #include "Array2.h" |
|
37 |
|
38 #if defined (HEAVYWEIGHT_INDEXING) |
|
39 #include "idx-vector.h" |
|
40 #include "Array2-idx.h" |
|
41 #endif |
|
42 |
|
43 #include "lo-error.h" |
|
44 |
|
45 // Two dimensional array class. |
|
46 |
|
47 template <class T> |
|
48 T& |
|
49 Array2<T>::checkelem (int i, int j) |
|
50 { |
|
51 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
52 { |
|
53 (*current_liboctave_error_handler) ("range error"); |
|
54 static T foo; |
|
55 return foo; |
|
56 } |
|
57 return Array<T>::elem (d1*j+i); |
|
58 } |
|
59 |
|
60 template <class T> |
|
61 T |
|
62 Array2<T>::elem (int i, int j) const |
|
63 { |
|
64 return Array<T>::elem (d1*j+i); |
|
65 } |
|
66 |
|
67 template <class T> |
|
68 T |
|
69 Array2<T>::checkelem (int i, int j) const |
|
70 { |
|
71 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
72 { |
|
73 (*current_liboctave_error_handler) ("range error"); |
|
74 T foo; |
|
75 static T *bar = &foo; |
|
76 return foo; |
|
77 } |
|
78 return Array<T>::elem (d1*j+i); |
|
79 } |
|
80 |
|
81 template <class T> |
|
82 T |
|
83 Array2<T>::operator () (int i, int j) const |
|
84 { |
|
85 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
86 { |
|
87 (*current_liboctave_error_handler) ("range error"); |
|
88 T foo; |
|
89 static T *bar = &foo; |
|
90 return foo; |
|
91 } |
|
92 return Array<T>::elem (d1*j+i); |
|
93 } |
|
94 |
|
95 template <class T> |
|
96 void |
|
97 Array2<T>::resize (int r, int c) |
|
98 { |
|
99 if (r < 0 || c < 0) |
|
100 { |
|
101 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
|
102 return; |
|
103 } |
|
104 |
|
105 if (r == dim1 () && c == dim2 ()) |
|
106 return; |
|
107 |
|
108 ArrayRep *old_rep = rep; |
|
109 const T *old_data = data (); |
|
110 |
|
111 int old_d1 = dim1 (); |
|
112 int old_d2 = dim2 (); |
|
113 int old_len = length (); |
|
114 |
|
115 rep = new ArrayRep (r*c); |
|
116 |
|
117 d1 = r; |
|
118 d2 = c; |
|
119 |
|
120 if (old_data && old_len > 0) |
|
121 { |
|
122 int min_r = old_d1 < r ? old_d1 : r; |
|
123 int min_c = old_d2 < c ? old_d2 : c; |
|
124 |
|
125 for (int j = 0; j < min_c; j++) |
|
126 for (int i = 0; i < min_r; i++) |
|
127 xelem (i, j) = old_data[old_d1*j+i]; |
|
128 } |
|
129 |
|
130 if (--old_rep->count <= 0) |
|
131 delete old_rep; |
|
132 } |
|
133 |
|
134 template <class T> |
|
135 void |
|
136 Array2<T>::resize (int r, int c, const T& val) |
|
137 { |
|
138 if (r < 0 || c < 0) |
|
139 { |
|
140 (*current_liboctave_error_handler) ("can't resize to negative dimension"); |
|
141 return; |
|
142 } |
|
143 |
|
144 if (r == dim1 () && c == dim2 ()) |
|
145 return; |
|
146 |
|
147 ArrayRep *old_rep = rep; |
|
148 const T *old_data = data (); |
|
149 int old_d1 = dim1 (); |
|
150 int old_d2 = dim2 (); |
|
151 int old_len = length (); |
|
152 |
|
153 rep = new ArrayRep (r*c); |
|
154 |
|
155 d1 = r; |
|
156 d2 = c; |
|
157 |
|
158 int min_r = old_d1 < r ? old_d1 : r; |
|
159 int min_c = old_d2 < c ? old_d2 : c; |
|
160 |
|
161 if (old_data && old_len > 0) |
|
162 { |
|
163 for (int j = 0; j < min_c; j++) |
|
164 for (int i = 0; i < min_r; i++) |
|
165 xelem (i, j) = old_data[old_d1*j+i]; |
|
166 } |
|
167 |
|
168 for (int j = 0; j < min_c; j++) |
|
169 for (int i = min_r; i < r; i++) |
|
170 xelem (i, j) = val; |
|
171 |
|
172 for (int j = min_c; j < c; j++) |
|
173 for (int i = 0; i < r; i++) |
|
174 xelem (i, j) = val; |
|
175 |
|
176 if (--old_rep->count <= 0) |
|
177 delete old_rep; |
|
178 } |
|
179 |
|
180 template <class T> |
|
181 Array2<T>& |
|
182 Array2<T>::insert (const Array2<T>& a, int r, int c) |
|
183 { |
|
184 int a_rows = a.rows (); |
|
185 int a_cols = a.cols (); |
|
186 |
|
187 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
|
188 { |
|
189 (*current_liboctave_error_handler) ("range error for insert"); |
|
190 return *this; |
|
191 } |
|
192 |
|
193 for (int j = 0; j < a_cols; j++) |
|
194 for (int i = 0; i < a_rows; i++) |
|
195 elem (r+i, c+j) = a.elem (i, j); |
|
196 |
|
197 return *this; |
|
198 } |
|
199 |
|
200 /* |
|
201 ;;; Local Variables: *** |
|
202 ;;; mode: C++ *** |
|
203 ;;; End: *** |
|
204 */ |