comparison liboctave/dim-vector.h @ 12037:a4672b478cef release-3-2-x

dim-vector.h: dim vectors always have two dimensions
author John W. Eaton <jwe@octave.org>
date Wed, 08 Jul 2009 13:49:21 -0400
parents 9a46ba093db4
children b096d11237be
comparison
equal deleted inserted replaced
12036:059ab308074f 12037:a4672b478cef
73 dims[1] = c; 73 dims[1] = c;
74 dims[2] = p; 74 dims[2] = p;
75 } 75 }
76 76
77 dim_vector_rep (const dim_vector_rep& dv) 77 dim_vector_rep (const dim_vector_rep& dv)
78 : dims (dv.ndims > 0 ? new octave_idx_type [dv.ndims] : 0), 78 : dims (new octave_idx_type [dv.ndims]),
79 ndims (dv.ndims > 0 ? dv.ndims : 0), count (1) 79 ndims (dv.ndims), count (1)
80 { 80 {
81 if (dims) 81 if (dims)
82 { 82 {
83 for (int i = 0; i < ndims; i++) 83 for (int i = 0; i < ndims; i++)
84 dims[i] = dv.dims[i]; 84 dims[i] = dv.dims[i];
85 } 85 }
86 } 86 }
87 87
88 dim_vector_rep (octave_idx_type n, const dim_vector_rep *dv, 88 dim_vector_rep (octave_idx_type n, const dim_vector_rep *dv,
89 int fill_value = 0) 89 int fill_value = 0)
90 : dims ((dv && n > 0) ? new octave_idx_type [n] : 0), 90 : dims (new octave_idx_type [n < 2 ? 2 : n]),
91 ndims (n > 0 ? n : 0), count (1) 91 ndims (n < 2 ? 2 : n), count (1)
92 { 92 {
93 if (dims) 93 if (n == 0)
94 {
95 // Result is 0x0.
96 dims[0] = 0;
97 dims[1] = 0;
98 }
99 else if (n == 1)
100 {
101 // Result is a column vector.
102 dims[0] = dv->dims[0];
103 dims[1] = 1;
104 }
105 else
94 { 106 {
95 int dv_ndims = dv ? dv->ndims : 0; 107 int dv_ndims = dv ? dv->ndims : 0;
96 108
97 int min_len = n < dv_ndims ? n : dv_ndims; 109 int min_len = n < dv_ndims ? n : dv_ndims;
98 110
132 } 144 }
133 145
134 void chop_all_singletons (void) 146 void chop_all_singletons (void)
135 { 147 {
136 int j = 0; 148 int j = 0;
149
137 for (int i = 0; i < ndims; i++) 150 for (int i = 0; i < ndims; i++)
138 { 151 {
139 if (dims[i] != 1) 152 if (dims[i] != 1)
140 dims[j++] = dims[i]; 153 dims[j++] = dims[i];
141 } 154 }
142 if (j == 1) dims[1] = 1; 155
156 if (j == 1)
157 dims[1] = 1;
158
143 ndims = j > 2 ? j : 2; 159 ndims = j > 2 ? j : 2;
144 } 160 }
145 161
146 private: 162 private:
147 163
221 { 237 {
222 int len = length (); 238 int len = length ();
223 239
224 if (n != len) 240 if (n != len)
225 { 241 {
226 if (n < 2)
227 {
228 (*current_liboctave_error_handler)
229 ("unable to resize object to fewer than 2 dimensions");
230 return;
231 }
232
233 dim_vector_rep *old_rep = rep; 242 dim_vector_rep *old_rep = rep;
234 243
235 rep = new dim_vector_rep (n, old_rep, fill_value); 244 rep = new dim_vector_rep (n, old_rep, fill_value);
236 245
237 if (--old_rep->count <= 0) 246 if (--old_rep->count <= 0)