array2D::resize() is not optimal
Walter Daems <[email protected]> Wed, 21 Apr 2004 14:45:58 +0200
| Newsgroups | gmane.comp.lib.mtl.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi all,
The function array2D::resize() is not optimal in its current form:
- it ruins sparsity
if the OneD-s of the array2D are sparse.
- it does some initializations that are purposeless
if the OneD-s of the array2D are dense
The patch in attachment solves these problems. The speed in case
of dense OneD-s might be somewhat degraded because of the extra
it.index() < n check, but :
- I guess resizing is to be considered an expensive operation anyway.
- the original function contained already unnecessary overhead
( the third for loop had no purpose whatsoever )
bye,
Walter.
_______________________________________________
This list is archived at http://www.osl.iu.edu/MailArchives/mtl-devel/
patch_resize
(text/plain, 1.5 KB)
*** orig/array2D.h 2004-04-20 13:48:41.000000000 +0200
--- adpt/array2D.h 2004-04-21 14:41:41.000000000 +0200
***************
*** 346,369 ****
/* JGS add sub_matrix */
void resize(size_type m, size_type n) {
rep_ptr newrep = new rep_type(m);
! { for (typename rep_type::iterator i = newrep->begin();
! i != newrep->end(); ++i)
*i = OneD(n);
! }
size_type M = MTL_MIN(m, dim.first());
size_type N = MTL_MIN(n, dim.second());
size_type i, j;
for (i = 0; i < M; ++i)
! for (j = 0; j < N; ++j)
! (*newrep)[i][j] = (*rep)[i][j];
!
! for (; i < m; ++i)
! for (; j < n; ++j)
! (*newrep)[i][j] = T();
rep = newrep;
dim = dim_type(m, n);
}
--- 346,371 ----
/* JGS add sub_matrix */
void resize(size_type m, size_type n) {
rep_ptr newrep = new rep_type(m);
! for (typename rep_type::iterator i = newrep->begin();
! i != newrep->end(); ++i)
*i = OneD(n);
!
size_type M = MTL_MIN(m, dim.first());
size_type N = MTL_MIN(n, dim.second());
size_type i, j;
for (i = 0; i < M; ++i)
! {
! typename OneD::const_iterator it = (*rep)[i].begin();
! const typename OneD::const_iterator endit = (*rep)[i].end();
!
! for ( ; it != endit; ++it )
! if ( it.index() < n )
! (*newrep)[i][it.index()] = *it;
! }
rep = newrep;
dim = dim_type(m, n);
}