comparison liboctave/Sparse.h @ 11586:12df7854fa7c

strip trailing whitespace from source files
author John W. Eaton <jwe@octave.org>
date Thu, 20 Jan 2011 17:24:59 -0500
parents fd0a3ac60b0e
children a21a3875ca83
comparison
equal deleted inserted replaced
11585:1473d0cf86d2 11586:12df7854fa7c
73 SparseRep (void) : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0), 73 SparseRep (void) : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0),
74 ncols (0), count (1) { c[0] = 0; } 74 ncols (0), count (1) { c[0] = 0; }
75 75
76 SparseRep (octave_idx_type n) : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n), 76 SparseRep (octave_idx_type n) : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n),
77 ncols (n), count (1) 77 ncols (n), count (1)
78 { 78 {
79 for (octave_idx_type i = 0; i < n + 1; i++) 79 for (octave_idx_type i = 0; i < n + 1; i++)
80 c[i] = 0; 80 c[i] = 0;
81 } 81 }
82 82
83 SparseRep (octave_idx_type nr, octave_idx_type nc) : d (0), r (0), c (new octave_idx_type [nc+1]), nzmx (0), 83 SparseRep (octave_idx_type nr, octave_idx_type nc) : d (0), r (0), c (new octave_idx_type [nc+1]), nzmx (0),
84 nrows (nr), ncols (nc), count (1) 84 nrows (nr), ncols (nc), count (1)
85 { 85 {
86 for (octave_idx_type i = 0; i < nc + 1; i++) 86 for (octave_idx_type i = 0; i < nc + 1; i++)
87 c[i] = 0; 87 c[i] = 0;
88 } 88 }
89 89
90 SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) : d (new T [nz]), 90 SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) : d (new T [nz]),
91 r (new octave_idx_type [nz]), c (new octave_idx_type [nc+1]), nzmx (nz), nrows (nr), 91 r (new octave_idx_type [nz]), c (new octave_idx_type [nc+1]), nzmx (nz), nrows (nr),
92 ncols (nc), count (1) 92 ncols (nc), count (1)
93 { 93 {
94 for (octave_idx_type i = 0; i < nc + 1; i++) 94 for (octave_idx_type i = 0; i < nc + 1; i++)
95 c[i] = 0; 95 c[i] = 0;
96 } 96 }
97 97
98 SparseRep (const SparseRep& a) 98 SparseRep (const SparseRep& a)
99 : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]), 99 : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]),
100 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1) 100 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1)
101 { 101 {
102 octave_idx_type nz = a.nnz (); 102 octave_idx_type nz = a.nnz ();
103 copy_or_memcpy (nz, a.d, d); 103 copy_or_memcpy (nz, a.d, d);
104 copy_or_memcpy (nz, a.r, r); 104 copy_or_memcpy (nz, a.r, r);
105 copy_or_memcpy (ncols + 1, a.c, c); 105 copy_or_memcpy (ncols + 1, a.c, c);
106 } 106 }
107 107
108 ~SparseRep (void) { delete [] d; delete [] r; delete [] c; } 108 ~SparseRep (void) { delete [] d; delete [] r; delete [] c; }
109 109
110 octave_idx_type length (void) const { return nzmx; } 110 octave_idx_type length (void) const { return nzmx; }
111 111
112 octave_idx_type nnz (void) const { return c [ncols]; } 112 octave_idx_type nnz (void) const { return c [ncols]; }
176 176
177 Sparse (void) 177 Sparse (void)
178 : rep (nil_rep ()), dimensions (dim_vector(0,0)) { } 178 : rep (nil_rep ()), dimensions (dim_vector(0,0)) { }
179 179
180 explicit Sparse (octave_idx_type n) 180 explicit Sparse (octave_idx_type n)
181 : rep (new typename Sparse<T>::SparseRep (n)), 181 : rep (new typename Sparse<T>::SparseRep (n)),
182 dimensions (dim_vector (n, n)) { } 182 dimensions (dim_vector (n, n)) { }
183 183
184 explicit Sparse (octave_idx_type nr, octave_idx_type nc) 184 explicit Sparse (octave_idx_type nr, octave_idx_type nc)
185 : rep (new typename Sparse<T>::SparseRep (nr, nc)), 185 : rep (new typename Sparse<T>::SparseRep (nr, nc)),
186 dimensions (dim_vector (nr, nc)) { } 186 dimensions (dim_vector (nr, nc)) { }
187 187
188 explicit Sparse (octave_idx_type nr, octave_idx_type nc, T val); 188 explicit Sparse (octave_idx_type nr, octave_idx_type nc, T val);
189 189
190 Sparse (const dim_vector& dv, octave_idx_type nz) 190 Sparse (const dim_vector& dv, octave_idx_type nz)
194 Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) 194 Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz)
195 : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)), 195 : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)),
196 dimensions (dim_vector (nr, nc)) { } 196 dimensions (dim_vector (nr, nc)) { }
197 197
198 // Type conversion case. Preserves capacity (). 198 // Type conversion case. Preserves capacity ().
199 template <class U> 199 template <class U>
200 Sparse (const Sparse<U>& a) 200 Sparse (const Sparse<U>& a)
201 : rep (new typename Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols, a.rep->nzmx)), 201 : rep (new typename Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols, a.rep->nzmx)),
202 dimensions (a.dimensions) 202 dimensions (a.dimensions)
203 { 203 {
204 octave_idx_type nz = a.nnz (); 204 octave_idx_type nz = a.nnz ();
205 std::copy (a.rep->d, a.rep->d + nz, rep->d); 205 std::copy (a.rep->d, a.rep->d + nz, rep->d);
206 copy_or_memcpy (nz, a.rep->r, rep->r); 206 copy_or_memcpy (nz, a.rep->r, rep->r);
207 copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c); 207 copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c);
208 } 208 }
238 octave_idx_type capacity (void) const { return nzmax (); } 238 octave_idx_type capacity (void) const { return nzmax (); }
239 octave_idx_type nnz (void) const { return rep->nnz (); } 239 octave_idx_type nnz (void) const { return rep->nnz (); }
240 240
241 // Querying the number of elements (incl. zeros) may overflow the index type, 241 // Querying the number of elements (incl. zeros) may overflow the index type,
242 // so don't do it unless you really need it. 242 // so don't do it unless you really need it.
243 octave_idx_type numel (void) const 243 octave_idx_type numel (void) const
244 { 244 {
245 return dimensions.safe_numel (); 245 return dimensions.safe_numel ();
246 } 246 }
247 247
248 octave_idx_type nelem (void) const { return capacity (); } 248 octave_idx_type nelem (void) const { return capacity (); }
249 octave_idx_type length (void) const { return numel (); } 249 octave_idx_type length (void) const { return numel (); }
250 250
262 while (cidx(ret+1) < k) 262 while (cidx(ret+1) < k)
263 ret++; 263 ret++;
264 return ret; 264 return ret;
265 } 265 }
266 266
267 size_t byte_size (void) const 267 size_t byte_size (void) const
268 { 268 {
269 return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type) 269 return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type)
270 + static_cast<size_t> (capacity ()) * (sizeof (T) + sizeof (octave_idx_type))); 270 + static_cast<size_t> (capacity ()) * (sizeof (T) + sizeof (octave_idx_type)));
271 } 271 }
272 272
273 dim_vector dims (void) const { return dimensions; } 273 dim_vector dims (void) const { return dimensions; }
274 274
275 Sparse<T> squeeze (void) const { return *this; } 275 Sparse<T> squeeze (void) const { return *this; }
276 276
277 octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const; 277 octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const;
278 278
279 T range_error (const char *fcn, octave_idx_type n) const; 279 T range_error (const char *fcn, octave_idx_type n) const;
280 T& range_error (const char *fcn, octave_idx_type n); 280 T& range_error (const char *fcn, octave_idx_type n);
281 281
285 T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const; 285 T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const;
286 T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx); 286 T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx);
287 287
288 // No checking, even for multiple references, ever. 288 // No checking, even for multiple references, ever.
289 289
290 T& xelem (octave_idx_type n) 290 T& xelem (octave_idx_type n)
291 { 291 {
292 octave_idx_type i = n % rows (), j = n / rows(); 292 octave_idx_type i = n % rows (), j = n / rows();
293 return xelem (i, j); 293 return xelem (i, j);
294 } 294 }
295 295
296 T xelem (octave_idx_type n) const 296 T xelem (octave_idx_type n) const
297 { 297 {
298 octave_idx_type i = n % rows (), j = n / rows(); 298 octave_idx_type i = n % rows (), j = n / rows();
299 return xelem (i, j); 299 return xelem (i, j);
300 } 300 }
301 301
302 T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); } 302 T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); }
303 T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); } 303 T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); }
304 304
305 T& xelem (const Array<octave_idx_type>& ra_idx) 305 T& xelem (const Array<octave_idx_type>& ra_idx)
306 { return xelem (compute_index (ra_idx)); } 306 { return xelem (compute_index (ra_idx)); }
348 { 348 {
349 make_unique (); 349 make_unique ();
350 return xelem (n); 350 return xelem (n);
351 } 351 }
352 352
353 T& elem (octave_idx_type i, octave_idx_type j) 353 T& elem (octave_idx_type i, octave_idx_type j)
354 { 354 {
355 make_unique (); 355 make_unique ();
356 return xelem (i, j); 356 return xelem (i, j);
357 } 357 }
358 358
359 T& elem (const Array<octave_idx_type>& ra_idx) 359 T& elem (const Array<octave_idx_type>& ra_idx)
360 { return Sparse<T>::elem (compute_index (ra_idx)); } 360 { return Sparse<T>::elem (compute_index (ra_idx)); }
361 361
410 T operator () (octave_idx_type n) const { return elem (n); } 410 T operator () (octave_idx_type n) const { return elem (n); }
411 T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); } 411 T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); }
412 T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); } 412 T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); }
413 #endif 413 #endif
414 414
415 Sparse<T> maybe_compress (bool remove_zeros = false) 415 Sparse<T> maybe_compress (bool remove_zeros = false)
416 { 416 {
417 if (remove_zeros) 417 if (remove_zeros)
418 make_unique (); // Needs to unshare because elements are removed. 418 make_unique (); // Needs to unshare because elements are removed.
419 419
420 rep->maybe_compress (remove_zeros); 420 rep->maybe_compress (remove_zeros);
421 return (*this); 421 return (*this);
422 } 422 }
423 423
424 Sparse<T> reshape (const dim_vector& new_dims) const; 424 Sparse<T> reshape (const dim_vector& new_dims) const;
425 425
426 Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const; 426 Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const;
432 432
433 void resize (octave_idx_type r, octave_idx_type c); 433 void resize (octave_idx_type r, octave_idx_type c);
434 434
435 void resize (const dim_vector& dv); 435 void resize (const dim_vector& dv);
436 436
437 void change_capacity (octave_idx_type nz) 437 void change_capacity (octave_idx_type nz)
438 { 438 {
439 if (nz < nnz ()) 439 if (nz < nnz ())
440 make_unique (); // Unshare now because elements will be truncated. 440 make_unique (); // Unshare now because elements will be truncated.
441 rep->change_length (nz); 441 rep->change_length (nz);
442 } 442 }
443 443
444 Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c); 444 Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c);
445 Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx); 445 Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx);
446 446
524 524
525 if (f_zero != 0.) 525 if (f_zero != 0.)
526 { 526 {
527 octave_idx_type nr = rows (); 527 octave_idx_type nr = rows ();
528 octave_idx_type nc = cols (); 528 octave_idx_type nc = cols ();
529 529
530 result = Sparse<U> (nr, nc, f_zero); 530 result = Sparse<U> (nr, nc, f_zero);
531 531
532 for (octave_idx_type j = 0; j < nc; j++) 532 for (octave_idx_type j = 0; j < nc; j++)
533 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) 533 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++)
534 { 534 {
599 octave_idx_type jtmp; 599 octave_idx_type jtmp;
600 octave_idx_type iold = 0; 600 octave_idx_type iold = 0;
601 octave_idx_type jold = 0; 601 octave_idx_type jold = 0;
602 octave_idx_type ii = 0; 602 octave_idx_type ii = 0;
603 T tmp; 603 T tmp;
604 604
605 a.cidx (0) = 0; 605 a.cidx (0) = 0;
606 for (octave_idx_type i = 0; i < nz; i++) 606 for (octave_idx_type i = 0; i < nz; i++)
607 { 607 {
608 itmp = 0; jtmp = 0; 608 itmp = 0; jtmp = 0;
609 is >> itmp; 609 is >> itmp;
652 652
653 iold = itmp; 653 iold = itmp;
654 jold = jtmp; 654 jold = jtmp;
655 655
656 tmp = read_fcn (is); 656 tmp = read_fcn (is);
657 657
658 if (is) 658 if (is)
659 { 659 {
660 a.data (ii) = tmp; 660 a.data (ii) = tmp;
661 a.ridx (ii++) = itmp; 661 a.ridx (ii++) = itmp;
662 } 662 }
665 } 665 }
666 666
667 for (octave_idx_type j = jold; j < nc; j++) 667 for (octave_idx_type j = jold; j < nc; j++)
668 a.cidx(j+1) = ii; 668 a.cidx(j+1) = ii;
669 } 669 }
670 670
671 done: 671 done:
672 672
673 return is; 673 return is;
674 } 674 }
675 675