1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1988
|
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 (octave_Array2_h) |
|
25 #define octave_Array2_h 1 |
|
26 |
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <cassert> |
3473
|
32 #include <climits> |
|
33 #include <cmath> |
1988
|
34 #include <cstdlib> |
|
35 |
|
36 #include "Array.h" |
|
37 #include "lo-error.h" |
|
38 |
|
39 class idx_vector; |
|
40 |
|
41 // Two dimensional array class. |
|
42 |
|
43 template <class T> |
|
44 class Array2 : public Array<T> |
|
45 { |
3473
|
46 private: |
|
47 |
|
48 int get_size (int r, int c) const; |
|
49 |
1988
|
50 protected: |
|
51 |
3473
|
52 Array2 (T *d, int n, int m) : Array<T> (d, get_size (n, m)) |
1988
|
53 { |
|
54 d1 = n; |
|
55 d2 = m; |
|
56 set_max_indices (2); |
|
57 } |
|
58 |
|
59 public: |
|
60 |
|
61 // These really need to be protected (and they will be in the |
|
62 // future, so don't depend on them being here!), but they can't be |
|
63 // until template friends work correctly in g++. |
|
64 |
|
65 int d1; |
|
66 int d2; |
|
67 |
|
68 Array2 (void) : Array<T> () |
|
69 { |
|
70 d1 = 0; |
|
71 d2 = 0; |
|
72 set_max_indices (2); |
|
73 } |
|
74 |
3473
|
75 Array2 (int n, int m) : Array<T> (get_size (n, m)) |
1988
|
76 { |
|
77 d1 = n; |
|
78 d2 = m; |
|
79 set_max_indices (2); |
|
80 } |
|
81 |
3473
|
82 Array2 (int n, int m, const T& val) : Array<T> (get_size (n, m), val) |
1988
|
83 { |
|
84 d1 = n; |
|
85 d2 = m; |
|
86 set_max_indices (2); |
|
87 } |
|
88 |
|
89 Array2 (const Array2<T>& a) : Array<T> (a) |
|
90 { |
|
91 d1 = a.d1; |
|
92 d2 = a.d2; |
|
93 set_max_indices (2); |
|
94 } |
|
95 |
|
96 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
97 { |
|
98 d1 = n; |
|
99 d2 = m; |
|
100 set_max_indices (2); |
|
101 } |
|
102 |
|
103 ~Array2 (void) { } |
|
104 |
|
105 Array2<T>& operator = (const Array2<T>& a) |
|
106 { |
|
107 if (this != &a && rep != a.rep) |
|
108 { |
|
109 Array<T>::operator = (a); |
|
110 d1 = a.d1; |
|
111 d2 = a.d2; |
|
112 } |
|
113 |
|
114 return *this; |
|
115 } |
|
116 |
|
117 int dim1 (void) const { return d1; } |
|
118 int dim2 (void) const { return d2; } |
|
119 |
|
120 int rows (void) const { return d1; } |
|
121 int cols (void) const { return d2; } |
|
122 int columns (void) const { return d2; } |
|
123 |
2109
|
124 // No checking of any kind, ever. |
|
125 |
|
126 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
|
127 T xelem (int i, int j) const { return Array<T>::xelem (d1*j+i); } |
|
128 |
2306
|
129 // Note that the following element selection methods don't use |
|
130 // xelem() because they need to make use of the code in |
|
131 // Array<T>::elem() that checks the reference count. |
2006
|
132 |
|
133 T& checkelem (int i, int j) |
|
134 { |
|
135 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
136 { |
2109
|
137 (*current_liboctave_error_handler) |
|
138 ("T& Array2<T>::checkelem (%d, %d): range error", i, j); |
2006
|
139 static T foo; |
|
140 return foo; |
|
141 } |
|
142 else |
2109
|
143 return Array<T>::elem (d1*j+i); |
2006
|
144 } |
|
145 |
2306
|
146 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
|
147 |
2109
|
148 #if defined (BOUNDS_CHECKING) |
2306
|
149 T& operator () (int i, int j) { return checkelem (i, j); } |
2006
|
150 #else |
2306
|
151 T& operator () (int i, int j) { return elem (i, j); } |
2006
|
152 #endif |
|
153 |
|
154 T checkelem (int i, int j) const |
|
155 { |
|
156 if (i < 0 || j < 0 || i >= d1 || j >= d2) |
|
157 { |
2109
|
158 (*current_liboctave_error_handler) |
|
159 ("T Array2<T>::checkelem (%d, %d): range error", i, j); |
2440
|
160 return T (); |
2006
|
161 } |
|
162 else |
2109
|
163 return Array<T>::elem (d1*j+i); |
2006
|
164 } |
1988
|
165 |
2306
|
166 T elem (int i, int j) const { return Array<T>::elem (d1*j+i); } |
|
167 |
2109
|
168 #if defined (BOUNDS_CHECKING) |
2306
|
169 T operator () (int i, int j) const { return checkelem (i, j); } |
2006
|
170 #else |
2306
|
171 T operator () (int i, int j) const { return elem (i, j); } |
2006
|
172 #endif |
|
173 |
2109
|
174 T range_error (const char *fcn, int i, int j) const; |
|
175 T& range_error (const char *fcn, int i, int j); |
1988
|
176 |
|
177 void resize (int n, int m); |
|
178 void resize (int n, int m, const T& val); |
|
179 |
|
180 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
181 |
2815
|
182 bool is_square (void) const { return (d1 == d2); } |
|
183 |
3225
|
184 Array2<T> transpose (void) const; |
|
185 |
1988
|
186 #ifdef HEAVYWEIGHT_INDEXING |
3472
|
187 void maybe_delete_elements (idx_vector& i); |
|
188 |
1988
|
189 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
190 |
|
191 Array2<T> value (void); |
2382
|
192 |
|
193 Array2<T> index (idx_vector& i) const; |
|
194 |
|
195 Array2<T> index (idx_vector& i, idx_vector& j) const; |
1988
|
196 #endif |
|
197 }; |
|
198 |
|
199 template <class LT, class RT> |
|
200 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
201 |
|
202 #endif |
|
203 |
|
204 /* |
|
205 ;;; Local Variables: *** |
|
206 ;;; mode: C++ *** |
|
207 ;;; End: *** |
|
208 */ |