Opened 11 years ago

Closed 11 years ago

#267 closed task (fixed)

There is a problem with vector sizes in this code snippet

Reported by: jari Owned by: jari
Priority: major Milestone: 0.6.4
Component: core Version: 0.6.3
Keywords: Cc:

Description

template <typename T >
struct VectorPlus : public std::binary_function<std::vector<T>,std::vector<T>,std::vector<T> >
{
	std::vector<T> operator()(const std::vector<T>& u, const std::vector<T>& v) const 
	{
		if ( u.size() > v.size() ){
			std::vector<T> res(u.size());
			transform(u.begin(), u.end(), v.begin(), res.begin(), std::plus<T>());
			copy(u.begin()+v.size(), u.end(), res.begin()+v.size());
			return res;
		}
	
		std::vector<T> res(v.size());
		transform(v.begin(), v.end(), u.begin(), res.begin(), std::plus<T>());
		if ( v.size() > u.size() )
			copy(v.begin()+u.size(), v.end(), res.begin()+u.size());
		return res;
	}

};

Change History (7)

comment:1 Changed 11 years ago by jari

  • Status changed from new to assigned

comment:2 Changed 11 years ago by jari

  • Version changed from trunk to 0.6.3

This is also a problem in the trunk. I'll fix it manually, i.e., no merge will be done since utility.h has been split in the trunk.

comment:3 Changed 11 years ago by jari

Fixed in changeset:478 for the 0.6 branch.

comment:4 Changed 11 years ago by peter

Just a comment on this code. In my taste it is preferable to use reserve and back_inserter iterator rather than resize and normal iterator.

That is irrelavant for this bug, though.

comment:5 Changed 11 years ago by jari

There is no resize here. The res vector is allocated in proper size directly. However, using the back_inserter may be somewhat quicker since the res vector get initialized and later on copied.

comment:6 Changed 11 years ago by peter

Yes, sorry, my point is that the default constructor for T is called u.size() times, which could be avoided using a back_inserter. But you already understood that.

comment:7 Changed 11 years ago by peter

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [485]) Fixes #267 in trunk. This ChangeSet?? should be equivalent with [478], but using a back_insert_iterator to speed up it slightly.

Note: See TracTickets for help on using tickets.