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_Array3_h) |
|
25 #define octave_Array3_h 1 |
|
26 |
|
27 #if defined (__GNUG__) |
|
28 #pragma interface |
|
29 #endif |
|
30 |
|
31 #include <cassert> |
|
32 #include <cstdlib> |
|
33 |
|
34 #include "Array2.h" |
|
35 #include "lo-error.h" |
|
36 |
|
37 class idx_vector; |
|
38 |
|
39 // Three dimensional array class. |
|
40 |
|
41 template <class T> |
3585
|
42 class |
|
43 Array3 : public Array2<T> |
1988
|
44 { |
|
45 protected: |
|
46 |
|
47 int d3; |
|
48 |
3492
|
49 Array3 (T *d, int n, int m, int k) : Array2<T> (d, n, get_size (m, k)) |
1988
|
50 { |
|
51 d2 = m; |
|
52 d3 = k; |
|
53 set_max_indices (3); |
|
54 } |
|
55 |
|
56 public: |
|
57 |
|
58 Array3 (void) : Array2<T> () |
|
59 { |
|
60 d2 = 0; |
|
61 d3 = 0; |
|
62 set_max_indices (3); |
|
63 } |
|
64 |
3492
|
65 Array3 (int n, int m, int k) : Array2<T> (n, get_size (m, k)) |
1988
|
66 { |
|
67 d2 = m; |
|
68 d3 = k; |
|
69 set_max_indices (3); |
|
70 } |
|
71 |
|
72 Array3 (int n, int m, int k, const T& val) : Array2<T> (n, m*k, val) |
|
73 { |
|
74 d2 = m; |
|
75 d3 = k; |
|
76 set_max_indices (3); |
|
77 } |
|
78 |
|
79 Array3 (const Array3<T>& a) : Array2<T> (a) |
|
80 { |
|
81 d2 = a.d2; |
|
82 d3 = a.d3; |
|
83 set_max_indices (3); |
|
84 } |
|
85 |
|
86 ~Array3 (void) { } |
|
87 |
|
88 Array3<T>& operator = (const Array3<T>& a) |
|
89 { |
|
90 if (this != &a && rep != a.rep) |
|
91 { |
|
92 Array<T>::operator = (a); |
|
93 d1 = a.d1; |
|
94 d2 = a.d2; |
|
95 d3 = a.d3; |
|
96 } |
|
97 |
|
98 return *this; |
|
99 } |
|
100 |
|
101 int dim3 (void) const { return d3; } |
|
102 |
2306
|
103 // No checking of any kind, ever. |
1988
|
104 |
|
105 T& xelem (int i, int j, int k) { return Array2<T>::xelem (i, d2*k+j); } |
2306
|
106 T xelem (int i, int j, int k) const { return Array2<T>::xelem (i, d2*k+j); } |
1988
|
107 |
2306
|
108 // Note that the following element selection methods don't use |
|
109 // xelem() because they need to make use of the code in |
|
110 // Array<T>::elem() that checks the reference count. |
|
111 |
|
112 T& checkelem (int i, int j, int k) |
|
113 { |
|
114 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
115 { |
|
116 (*current_liboctave_error_handler) ("range error"); |
|
117 static T foo; |
|
118 return foo; |
|
119 } |
|
120 return Array2<T>::elem (i, d2*k+j); |
|
121 } |
|
122 |
|
123 T& elem (int i, int j, int k) { return Array2<T>::elem (i, d2*k+j); } |
|
124 |
|
125 #if defined (BOUNDS_CHECKING) |
|
126 T& operator () (int i, int j, int k) { return checkelem (i, j, k); } |
|
127 #else |
|
128 T& operator () (int i, int j, int k) { return elem (i, j, k); } |
|
129 #endif |
|
130 |
|
131 T checkelem (int i, int j, int k) const |
|
132 { |
|
133 if (i < 0 || j < 0 || k < 0 || i >= d1 || j >= d2 || k >= d3) |
|
134 { |
|
135 (*current_liboctave_error_handler) ("range error"); |
2440
|
136 return T (); |
2306
|
137 } |
|
138 return Array2<T>::elem (i, d1*k+j); |
|
139 } |
|
140 |
|
141 T elem (int i, int j, int k) const { return Array2<T>::elem (i, d2*k+j); } |
|
142 |
|
143 #if defined (BOUNDS_CHECKING) |
|
144 T operator () (int i, int j, int k) const { return checkelem (i, j, k); } |
|
145 #else |
|
146 T operator () (int i, int j, int k) const { return elem (i, j, k); } |
|
147 #endif |
1988
|
148 |
|
149 void resize (int n, int m, int k); |
|
150 void resize (int n, int m, int k, const T& val); |
|
151 |
|
152 #ifdef HEAVYWEIGHT_INDEXING |
|
153 void maybe_delete_elements (idx_vector& i, idx_vector& j, idx_vector& k); |
|
154 |
|
155 Array3<T> value (void); |
|
156 #endif |
|
157 }; |
|
158 |
|
159 template <class LT, class RT> |
3665
|
160 int |
3836
|
161 assign (Array3<LT>& lhs, const Array3<RT>& rhs, const LT& resize_fill_value); |
|
162 |
|
163 template <class LT, class RT> |
|
164 int |
|
165 assign (Array3<LT>& lhs, const Array3<RT>& rhs) |
|
166 { |
|
167 return assign (lhs, rhs, static_cast<LT> (0)); |
|
168 } |
1988
|
169 |
|
170 #endif |
|
171 |
|
172 /* |
|
173 ;;; Local Variables: *** |
|
174 ;;; mode: C++ *** |
|
175 ;;; End: *** |
|
176 */ |