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