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> |
|
31 |
|
32 class |
|
33 dim_vector |
|
34 { |
|
35 public: |
|
36 |
|
37 dim_vector (void) : ndims (0), dims (0) { } |
|
38 |
|
39 dim_vector (int n) : ndims (1), dims (new int [1]) { dims[0] = n; } |
|
40 |
|
41 dim_vector (int r, int c) |
|
42 : ndims (2), dims (new int [2]) { dims[0] = r; dims[1] = c; } |
|
43 |
|
44 dim_vector (int r, int c, int p) |
|
45 : ndims (3), dims (new int [3]) { dims[0] = r; dims[1] = c; dims[2] = p; } |
|
46 |
|
47 dim_vector (const dim_vector& dv) |
|
48 : ndims (dv.ndims) |
|
49 { |
|
50 if (dv.dims) |
|
51 { |
|
52 dims = new int [ndims]; |
|
53 |
|
54 for (int i = 0; i < ndims; i++) |
|
55 dims[i] = dv.dims[i]; |
|
56 } |
|
57 else |
|
58 dims = 0; |
|
59 } |
|
60 |
|
61 dim_vector& operator = (const dim_vector& dv) |
|
62 { |
|
63 if (&dv != this) |
|
64 { |
|
65 ndims = dv.ndims; |
|
66 |
|
67 if (dv.dims) |
|
68 { |
|
69 dims = new int [ndims]; |
|
70 |
|
71 for (int i = 0; i < ndims; i++) |
|
72 dims[i] = dv.dims[i]; |
|
73 } |
|
74 } |
|
75 |
|
76 return *this; |
|
77 } |
|
78 |
|
79 ~dim_vector (void) { delete [] dims; } |
|
80 |
|
81 int length (void) const { return ndims; } |
|
82 |
|
83 int& elem (int i) |
|
84 { |
|
85 if (i >= ndims) |
|
86 resize (i+1); |
|
87 |
|
88 return dims[i]; |
|
89 } |
|
90 |
|
91 int elem (int i) const { return i < ndims ? dims[i] : -1; } |
|
92 |
|
93 int& operator () (int i) { return elem (i); } |
|
94 |
|
95 int operator () (int i) const { return elem (i); } |
|
96 |
|
97 void resize (int n) |
|
98 { |
|
99 if (n > ndims) |
|
100 { |
|
101 int *new_dims = new int [n]; |
|
102 |
|
103 for (int i = 0; i < ndims; i++) |
|
104 new_dims[i] = dims[i]; |
|
105 |
|
106 for (int i = ndims; i < n; i++) |
|
107 new_dims[i] = 0; |
|
108 |
|
109 delete [] dims; |
|
110 |
|
111 dims = new_dims; |
|
112 |
|
113 ndims = n; |
|
114 } |
|
115 else |
|
116 ndims = n; |
|
117 } |
|
118 |
|
119 private: |
|
120 |
|
121 int ndims; |
|
122 int *dims; |
|
123 }; |
|
124 |
|
125 #endif |
|
126 |
|
127 /* |
|
128 ;;; Local Variables: *** |
|
129 ;;; mode: C++ *** |
|
130 ;;; End: *** |
|
131 */ |
|
132 |