[C++-sig] Re: Optimize vector_indexing_suite set_slice
David Abrahams
dave at boost-consulting.com
Thu Aug 19 23:14:45 CEST 2004
"Neal D. Becker" <ndbecker2 at verizon.net> writes:
> How does this patch look?
>
> This attempts to optimize for the case in set_slice that the slice being
> replaced is the same size as the replacement, in which case a simple copy
> is performed rather than erase/insert.
>
> Index: vector_indexing_suite.hpp
> ===================================================================
> RCS file: /cvsroot/boost/boost/boost/python/suite/indexing/vector_indexing_suite.hpp,v
> retrieving revision 1.8
> diff -c -r1.8 vector_indexing_suite.hpp
> *** vector_indexing_suite.hpp 25 Jul 2004 17:00:37 -0000 1.8
> --- vector_indexing_suite.hpp 19 Aug 2004 19:25:06 -0000
> ***************
> *** 114,121 ****
> --- 114,126 ----
> container.insert(container.begin()+from, first, last);
> }
> else {
> + if (to - from == std::distance (first, last)) {
> + std::copy (first, last, container.begin()+from);
> + }
> + else {
> container.erase(container.begin()+from, container.begin()+to);
> container.insert(container.begin()+from, first, last);
> + }
> }
> }
Shouldn't you generalize it? There's no need to do erase + insert.
Suppose you're replacing 100 items with 99 or 101 items?
If the sizes don't match should either be insert + copy or copy[_backward] +
erase (where the erase happens at the end of the container).
Some handwaving here, but it should be easy enough to figure out how
to make it optimal.
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
More information about the Cplusplus-sig
mailing list