1988
|
1 // Template array classes -*- C++ -*- |
|
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 #define HEAVYWEIGHT_INDEXING 1 |
|
32 |
|
33 #include <cassert> |
|
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 { |
|
46 protected: |
|
47 |
|
48 Array2 (T *d, int n, int m) : Array<T> (d, n*m) |
|
49 { |
|
50 d1 = n; |
|
51 d2 = m; |
|
52 set_max_indices (2); |
|
53 } |
|
54 |
|
55 public: |
|
56 |
|
57 // These really need to be protected (and they will be in the |
|
58 // future, so don't depend on them being here!), but they can't be |
|
59 // until template friends work correctly in g++. |
|
60 |
|
61 int d1; |
|
62 int d2; |
|
63 |
|
64 Array2 (void) : Array<T> () |
|
65 { |
|
66 d1 = 0; |
|
67 d2 = 0; |
|
68 set_max_indices (2); |
|
69 } |
|
70 |
|
71 Array2 (int n, int m) : Array<T> (n*m) |
|
72 { |
|
73 d1 = n; |
|
74 d2 = m; |
|
75 set_max_indices (2); |
|
76 } |
|
77 |
|
78 Array2 (int n, int m, const T& val) : Array<T> (n*m, val) |
|
79 { |
|
80 d1 = n; |
|
81 d2 = m; |
|
82 set_max_indices (2); |
|
83 } |
|
84 |
|
85 Array2 (const Array2<T>& a) : Array<T> (a) |
|
86 { |
|
87 d1 = a.d1; |
|
88 d2 = a.d2; |
|
89 set_max_indices (2); |
|
90 } |
|
91 |
|
92 Array2 (const Array<T>& a, int n, int m) : Array<T> (a) |
|
93 { |
|
94 d1 = n; |
|
95 d2 = m; |
|
96 set_max_indices (2); |
|
97 } |
|
98 |
|
99 ~Array2 (void) { } |
|
100 |
|
101 Array2<T>& operator = (const Array2<T>& a) |
|
102 { |
|
103 if (this != &a && rep != a.rep) |
|
104 { |
|
105 Array<T>::operator = (a); |
|
106 d1 = a.d1; |
|
107 d2 = a.d2; |
|
108 } |
|
109 |
|
110 return *this; |
|
111 } |
|
112 |
|
113 int dim1 (void) const { return d1; } |
|
114 int dim2 (void) const { return d2; } |
|
115 |
|
116 int rows (void) const { return d1; } |
|
117 int cols (void) const { return d2; } |
|
118 int columns (void) const { return d2; } |
|
119 |
|
120 T& elem (int i, int j) { return Array<T>::elem (d1*j+i); } |
|
121 T& checkelem (int i, int j); |
|
122 T& operator () (int i, int j) { return checkelem (i, j); } |
|
123 |
|
124 T elem (int i, int j) const; |
|
125 T checkelem (int i, int j) const; |
|
126 T operator () (int i, int j) const; |
|
127 |
|
128 // No checking. |
|
129 |
|
130 T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); } |
|
131 T xelem (int i, int j) const { return Array<T>::xelem (d1*j+i); } |
|
132 |
|
133 void resize (int n, int m); |
|
134 void resize (int n, int m, const T& val); |
|
135 |
|
136 Array2<T>& insert (const Array2<T>& a, int r, int c); |
|
137 |
|
138 #ifdef HEAVYWEIGHT_INDEXING |
|
139 void maybe_delete_elements (idx_vector& i, idx_vector& j); |
|
140 |
|
141 Array2<T> value (void); |
|
142 #endif |
|
143 }; |
|
144 |
|
145 template <class LT, class RT> |
|
146 int assign (Array2<LT>& lhs, const Array2<RT>& rhs); |
|
147 |
|
148 #endif |
|
149 |
|
150 /* |
|
151 ;;; Local Variables: *** |
|
152 ;;; mode: C++ *** |
|
153 ;;; page-delimiter: "^/\\*" *** |
|
154 ;;; End: *** |
|
155 */ |