comparison liboctave/Sparse.h @ 7602:7bfaa9611558

Rewrite sparse mappers in terms of a functor template function
author David Bateman <dbateman@free.fr>
date Wed, 19 Mar 2008 10:38:33 -0400
parents ada435261879
children 36594d5bbe13
comparison
equal deleted inserted replaced
7601:8a939b217863 7602:7bfaa9611558
519 octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); } 519 octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); }
520 520
521 Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; 521 Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
522 Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, 522 Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
523 sortmode mode = ASCENDING) const; 523 sortmode mode = ASCENDING) const;
524
525 template <class U, class F>
526 Sparse<U>
527 map (F fcn) const
528 {
529 Sparse<U> result;
530 U f_zero = fcn (0.);
531
532 if (f_zero != 0.)
533 {
534 octave_idx_type nr = rows ();
535 octave_idx_type nc = cols ();
536
537 result = Sparse<U> (nr, nc, f_zero);
538
539 for (octave_idx_type j = 0; j < nc; j++)
540 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++)
541 {
542 OCTAVE_QUIT;
543 /* Use data instead of elem for better performance. */
544 result.data (ridx (i) + j * nr) = fcn (data(i));
545 }
546
547 result.maybe_compress (true);
548 }
549 else
550 {
551 octave_idx_type nz = nnz ();
552 octave_idx_type nr = rows ();
553 octave_idx_type nc = cols ();
554
555 result = Sparse<U> (nr, nc, nz);
556 octave_idx_type ii = 0;
557 result.cidx (ii) = 0;
558
559 for (octave_idx_type j = 0; j < nc; j++)
560 {
561 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++)
562 {
563 U val = fcn (data (i));
564 if (val != 0.0)
565 {
566 result.data (ii) = val;
567 result.ridx (ii++) = ridx (i);
568 }
569 OCTAVE_QUIT;
570 }
571 result.cidx (j+1) = ii;
572 }
573
574 result.maybe_compress (false);
575 }
576
577 return result;
578 }
524 }; 579 };
525 580
526 // NOTE: these functions should be friends of the Sparse<T> class and 581 // NOTE: these functions should be friends of the Sparse<T> class and
527 // Sparse<T>::dimensions should be protected, not public, but we can't 582 // Sparse<T>::dimensions should be protected, not public, but we can't
528 // do that because of bugs in gcc prior to 3.3. 583 // do that because of bugs in gcc prior to 3.3.