00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #if !defined (octave_dim_vector_h)
00025 #define octave_dim_vector_h 1
00026
00027 #include <cassert>
00028
00029 #include <sstream>
00030 #include <string>
00031
00032 #include "lo-error.h"
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 class
00050 dim_vector
00051 {
00052 private:
00053
00054 octave_idx_type *rep;
00055
00056 octave_idx_type& ndims() const
00057 { return rep[-1]; }
00058
00059 octave_idx_type& count() const
00060 { return rep[-2]; }
00061
00062
00063 static octave_idx_type *newrep (int ndims)
00064 {
00065 octave_idx_type *r = new octave_idx_type[ndims + 2];
00066 *r++ = 1; *r++ = ndims;
00067 return r;
00068 }
00069
00070
00071 octave_idx_type *clonerep (void)
00072 {
00073 int l = ndims();
00074 octave_idx_type *r = new octave_idx_type[l + 2];
00075 *r++ = 1; *r++ = l;
00076 for (int i = 0; i < l; i++)
00077 r[i] = rep[i];
00078 return r;
00079 }
00080
00081
00082 octave_idx_type *resizerep (int n, octave_idx_type fill_value)
00083 {
00084 int l = ndims();
00085 if (n < 2) n = 2;
00086 octave_idx_type *r = new octave_idx_type[n + 2];
00087 *r++ = 1; *r++ = n;
00088 if (l > n) l = n;
00089 int j;
00090 for (j = 0; j < l; j++)
00091 r[j] = rep[j];
00092 for (; j < n; j++)
00093 r[j] = fill_value;
00094 return r;
00095 }
00096
00097
00098 void freerep (void)
00099 {
00100 assert (count() == 0);
00101 delete [] (rep - 2);
00102 }
00103
00104 void make_unique (void)
00105 {
00106 if (count() > 1)
00107 {
00108 --count();
00109 rep = clonerep ();
00110 }
00111 }
00112
00113 public:
00114
00115 explicit dim_vector (octave_idx_type n)
00116 : rep (newrep (2))
00117 {
00118 rep[0] = n;
00119 rep[1] = 1;
00120 }
00121
00122 explicit dim_vector (octave_idx_type r, octave_idx_type c)
00123 : rep (newrep (2))
00124 {
00125 rep[0] = r;
00126 rep[1] = c;
00127 }
00128
00129 explicit dim_vector (octave_idx_type r, octave_idx_type c, octave_idx_type p)
00130 : rep (newrep (3))
00131 {
00132 rep[0] = r;
00133 rep[1] = c;
00134 rep[2] = p;
00135 }
00136
00137 octave_idx_type& elem (int i)
00138 {
00139 assert (i >= 0 && i < ndims());
00140 make_unique ();
00141 return rep[i];
00142 }
00143
00144 octave_idx_type elem (int i) const
00145 {
00146 assert (i >= 0 && i < ndims());
00147 return rep[i];
00148 }
00149
00150 void chop_trailing_singletons (void)
00151 {
00152 make_unique ();
00153 int l = ndims();
00154 for (int i = l - 1; i > 1; i--)
00155 {
00156 if (rep[i] == 1)
00157 l--;
00158 else
00159 break;
00160 }
00161 ndims() = l;
00162 }
00163
00164 void chop_all_singletons (void)
00165 {
00166 make_unique ();
00167 int j = 0;
00168 int l = ndims();
00169
00170 for (int i = 0; i < l; i++)
00171 {
00172 if (rep[i] != 1)
00173 rep[j++] = rep[i];
00174 }
00175
00176 if (j == 1)
00177 rep[1] = 1;
00178
00179 ndims() = j > 2 ? j : 2;
00180 }
00181
00182 private:
00183
00184 static octave_idx_type *nil_rep (void)
00185 {
00186 static dim_vector zv (0, 0);
00187 return zv.rep;
00188 }
00189
00190 explicit dim_vector (octave_idx_type *r)
00191 : rep (r) { }
00192
00193 public:
00194
00195 explicit dim_vector (void)
00196 : rep (nil_rep ()) { count()++; }
00197
00198 dim_vector (const dim_vector& dv)
00199 : rep (dv.rep) { count()++; }
00200
00201 static dim_vector alloc (int n)
00202 {
00203 return dim_vector (newrep (n < 2 ? 2 : n));
00204 }
00205
00206 dim_vector& operator = (const dim_vector& dv)
00207 {
00208 if (&dv != this)
00209 {
00210 if (--count() <= 0)
00211 freerep ();
00212
00213 rep = dv.rep;
00214 count()++;
00215 }
00216
00217 return *this;
00218 }
00219
00220 ~dim_vector (void)
00221 {
00222 if (--count() <= 0)
00223 freerep ();
00224 }
00225
00226 int length (void) const { return ndims(); }
00227
00228 octave_idx_type& operator () (int i) { return elem (i); }
00229
00230 octave_idx_type operator () (int i) const { return elem (i); }
00231
00232 void resize (int n, int fill_value = 0)
00233 {
00234 int len = length ();
00235
00236 if (n != len)
00237 {
00238 octave_idx_type *r = resizerep (n, fill_value);
00239
00240 if (--count() <= 0)
00241 freerep ();
00242
00243 rep = r;
00244 }
00245 }
00246
00247 std::string str (char sep = 'x') const
00248 {
00249 std::ostringstream buf;
00250
00251 for (int i = 0; i < length (); i++)
00252 {
00253 buf << elem (i);
00254
00255 if (i < length () - 1)
00256 buf << sep;
00257 }
00258
00259 std::string retval = buf.str ();
00260
00261 return retval;
00262 }
00263
00264 bool all_zero (void) const
00265 {
00266 bool retval = true;
00267
00268 for (int i = 0; i < length (); i++)
00269 {
00270 if (elem (i) != 0)
00271 {
00272 retval = false;
00273 break;
00274 }
00275 }
00276
00277 return retval;
00278 }
00279
00280 bool any_zero (void) const
00281 {
00282 bool retval = false;
00283
00284 for (int i = 0; i < length (); i++)
00285 {
00286 if (elem (i) == 0)
00287 {
00288 retval = true;
00289 break;
00290 }
00291 }
00292
00293 return retval;
00294 }
00295
00296 int
00297 num_ones (void) const
00298 {
00299 int retval = 0;
00300
00301 for (int i = 0; i < length (); i++)
00302 if (elem (i) == 1)
00303 retval++;
00304
00305 return retval;
00306 }
00307
00308 bool
00309 all_ones (void) const
00310 {
00311 return (num_ones () == length ());
00312 }
00313
00314
00315
00316
00317
00318 octave_idx_type numel (int n = 0) const
00319 {
00320 int n_dims = length ();
00321
00322 octave_idx_type retval = 1;
00323
00324 for (int i = n; i < n_dims; i++)
00325 retval *= elem (i);
00326
00327 return retval;
00328 }
00329
00330 bool any_neg (void) const
00331 {
00332 int n_dims = length (), i;
00333 for (i = 0; i < n_dims; i++)
00334 if (elem (i) < 0) break;
00335 return i < n_dims;
00336 }
00337
00338 dim_vector squeeze (void) const
00339 {
00340 dim_vector new_dims = *this;
00341
00342 bool dims_changed = 1;
00343
00344 int k = 0;
00345
00346 for (int i = 0; i < length (); i++)
00347 {
00348 if (elem (i) == 1)
00349 dims_changed = true;
00350 else
00351 new_dims(k++) = elem (i);
00352 }
00353
00354 if (dims_changed)
00355 {
00356 if (k == 0)
00357 new_dims = dim_vector (1, 1);
00358 else if (k == 1)
00359 {
00360
00361
00362
00363 if (elem (0) == 1)
00364 {
00365
00366
00367
00368 octave_idx_type tmp = new_dims(0);
00369
00370 new_dims.resize (2);
00371
00372 new_dims(0) = 1;
00373 new_dims(1) = tmp;
00374 }
00375 else
00376 {
00377
00378
00379
00380 new_dims.resize (2);
00381
00382 new_dims(1) = 1;
00383 }
00384 }
00385 else
00386 new_dims.resize(k);
00387 }
00388
00389 return new_dims;
00390 }
00391
00392 bool concat (const dim_vector& dvb, int dim = 0)
00393 {
00394 if (all_zero ())
00395 {
00396 *this = dvb;
00397 return true;
00398 }
00399
00400 if (dvb.all_zero ())
00401 return true;
00402
00403 int na = length ();
00404 int nb = dvb.length ();
00405
00406
00407 int n_max = na > nb ? na : nb;
00408 int n_min = na < nb ? na : nb;
00409
00410
00411
00412
00413
00414 for (int i = 0; i < n_min; i++)
00415 {
00416 if (elem(i) != dvb(i) && dim != i)
00417 return false;
00418 }
00419
00420
00421 for (int i = n_min; i < n_max; i++)
00422 {
00423 if (na > n_min)
00424 {
00425 if (elem(i) != 1 && dim != i)
00426 return false;
00427 }
00428 else
00429 {
00430 if (dvb(i) != 1 && dim != i)
00431 return false;
00432 }
00433 }
00434
00435
00436
00437
00438
00439 n_max = n_max > (dim + 1) ? n_max : (dim + 1);
00440
00441
00442
00443 if (n_max > na)
00444 resize (n_max, 1);
00445
00446
00447
00448 if (dim >= nb)
00449 elem (dim)++;
00450 else
00451 elem (dim) += dvb(dim);
00452
00453 return true;
00454 }
00455
00456
00457
00458
00459
00460 dim_vector redim (int n) const
00461 {
00462 int n_dims = length ();
00463 if (n_dims == n)
00464 return *this;
00465 else if (n_dims < n)
00466 {
00467 dim_vector retval = alloc (n);
00468
00469 int pad = 0;
00470 for (int i = 0; i < n_dims; i++)
00471 {
00472 retval.rep[i] = rep[i];
00473 if (rep[i] != 0)
00474 pad = 1;
00475 }
00476
00477 for (int i = n_dims; i < n; i++)
00478 retval.rep[i] = pad;
00479
00480 return retval;
00481 }
00482 else
00483 {
00484 if (n < 1) n = 1;
00485
00486 dim_vector retval = alloc (n);
00487
00488 retval.rep[1] = 1;
00489
00490 for (int i = 0; i < n-1; i++)
00491 retval.rep[i] = rep[i];
00492
00493 int k = rep[n-1];
00494 for (int i = n; i < n_dims; i++)
00495 k *= rep[i];
00496
00497 retval.rep[n-1] = k;
00498
00499 return retval;
00500 }
00501 }
00502
00503 bool is_vector (void) const
00504 {
00505 return (length () == 2 && (elem (0) == 1 || elem (1) == 1));
00506 }
00507
00508 int first_non_singleton (int def = 0) const
00509 {
00510 for (int i = 0; i < length (); i++)
00511 {
00512 if (elem (i) != 1)
00513 return i;
00514 }
00515
00516 return def;
00517 }
00518
00519 friend bool operator == (const dim_vector& a, const dim_vector& b);
00520 };
00521
00522 inline bool
00523 operator == (const dim_vector& a, const dim_vector& b)
00524 {
00525
00526 if (a.rep == b.rep)
00527 return true;
00528
00529 bool retval = true;
00530
00531 int a_len = a.length ();
00532 int b_len = b.length ();
00533
00534 if (a_len != b_len)
00535 retval = false;
00536 else
00537 {
00538 for (int i = 0; i < a_len; i++)
00539 {
00540 if (a(i) != b(i))
00541 {
00542 retval = false;
00543 break;
00544 }
00545 }
00546 }
00547
00548 return retval;
00549 }
00550
00551 inline bool
00552 operator != (const dim_vector& a, const dim_vector& b)
00553 {
00554 return ! operator == (a, b);
00555 }
00556
00557 #endif
00558
00559
00560
00561
00562
00563