4513
|
1 /* |
|
2 |
|
3 Copyright (C) 2003 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_dim_vector_h) |
|
24 #define octave_dim_vector_h 1 |
|
25 |
|
26 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
|
27 #pragma interface |
|
28 #endif |
|
29 |
|
30 #include <cassert> |
4543
|
31 #include <string> |
|
32 |
|
33 #include "lo-sstream.h" |
4513
|
34 |
|
35 class |
|
36 dim_vector |
|
37 { |
|
38 public: |
|
39 |
|
40 dim_vector (void) : ndims (0), dims (0) { } |
|
41 |
|
42 dim_vector (int n) : ndims (1), dims (new int [1]) { dims[0] = n; } |
|
43 |
|
44 dim_vector (int r, int c) |
|
45 : ndims (2), dims (new int [2]) { dims[0] = r; dims[1] = c; } |
|
46 |
|
47 dim_vector (int r, int c, int p) |
|
48 : ndims (3), dims (new int [3]) { dims[0] = r; dims[1] = c; dims[2] = p; } |
|
49 |
|
50 dim_vector (const dim_vector& dv) |
|
51 : ndims (dv.ndims) |
|
52 { |
|
53 if (dv.dims) |
|
54 { |
|
55 dims = new int [ndims]; |
|
56 |
|
57 for (int i = 0; i < ndims; i++) |
|
58 dims[i] = dv.dims[i]; |
|
59 } |
|
60 else |
|
61 dims = 0; |
|
62 } |
|
63 |
|
64 dim_vector& operator = (const dim_vector& dv) |
|
65 { |
|
66 if (&dv != this) |
|
67 { |
|
68 ndims = dv.ndims; |
|
69 |
|
70 if (dv.dims) |
|
71 { |
|
72 dims = new int [ndims]; |
|
73 |
|
74 for (int i = 0; i < ndims; i++) |
|
75 dims[i] = dv.dims[i]; |
|
76 } |
|
77 } |
|
78 |
|
79 return *this; |
|
80 } |
|
81 |
|
82 ~dim_vector (void) { delete [] dims; } |
|
83 |
|
84 int length (void) const { return ndims; } |
|
85 |
|
86 int& elem (int i) |
|
87 { |
|
88 if (i >= ndims) |
|
89 resize (i+1); |
|
90 |
|
91 return dims[i]; |
|
92 } |
|
93 |
|
94 int elem (int i) const { return i < ndims ? dims[i] : -1; } |
|
95 |
|
96 int& operator () (int i) { return elem (i); } |
|
97 |
|
98 int operator () (int i) const { return elem (i); } |
|
99 |
|
100 void resize (int n) |
|
101 { |
|
102 if (n > ndims) |
|
103 { |
|
104 int *new_dims = new int [n]; |
|
105 |
|
106 for (int i = 0; i < ndims; i++) |
|
107 new_dims[i] = dims[i]; |
|
108 |
|
109 for (int i = ndims; i < n; i++) |
|
110 new_dims[i] = 0; |
|
111 |
|
112 delete [] dims; |
|
113 |
|
114 dims = new_dims; |
|
115 |
|
116 ndims = n; |
|
117 } |
|
118 else |
|
119 ndims = n; |
|
120 } |
|
121 |
4543
|
122 std::string str (void) const |
|
123 { |
|
124 OSSTREAM buf; |
|
125 |
|
126 for (int i = 0; i < ndims; i++) |
|
127 { |
|
128 buf << dims[i]; |
|
129 |
|
130 if (i < ndims - 1) |
|
131 buf << "x"; |
|
132 } |
|
133 |
|
134 buf << OSSTREAM_ENDS; |
|
135 |
|
136 std::string retval = OSSTREAM_STR (buf); |
|
137 |
|
138 OSSTREAM_FREEZE (buf); |
|
139 |
|
140 return retval; |
|
141 } |
|
142 |
|
143 bool all_zero (void) const |
|
144 { |
|
145 bool retval = true; |
|
146 |
|
147 for (int i = 0; i < ndims; i++) |
|
148 { |
|
149 if (dims[i] != 0) |
|
150 { |
|
151 retval = false; |
|
152 break; |
|
153 } |
|
154 } |
|
155 |
|
156 return retval; |
|
157 } |
|
158 |
4513
|
159 private: |
|
160 |
|
161 int ndims; |
|
162 int *dims; |
|
163 }; |
|
164 |
4543
|
165 static inline bool |
|
166 operator == (const dim_vector& a, const dim_vector& b) |
|
167 { |
|
168 bool retval = true; |
|
169 |
|
170 int a_len = a.length (); |
|
171 int b_len = b.length (); |
|
172 |
|
173 if (a_len != b_len) |
|
174 retval = false; |
|
175 else |
|
176 { |
|
177 for (int i = 0; i < a_len; i++) |
|
178 { |
|
179 if (a(i) != b(i)) |
|
180 { |
|
181 retval = false; |
|
182 break; |
|
183 } |
|
184 } |
|
185 } |
|
186 |
|
187 return retval; |
|
188 } |
|
189 |
|
190 static inline bool |
|
191 operator != (const dim_vector& a, const dim_vector& b) |
|
192 { |
|
193 return ! operator == (a, b); |
|
194 } |
|
195 |
4513
|
196 #endif |
|
197 |
|
198 /* |
|
199 ;;; Local Variables: *** |
|
200 ;;; mode: C++ *** |
|
201 ;;; End: *** |
|
202 */ |
|
203 |