Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Proposal: add Vector::InsertPick(int i, pick_ T&)
Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21187] Fri, 08 May 2009 09:48 Go to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Let`s imagine we have some Moveable class M. And we make it`s vector with Vector<M>. So it is possible to use it without any additional constructors if we add new items with Vector<M>::AddPick. But when we need to add something somewhere inside vector and try to call Vector<M>::Insert, compiler requires M to have deep copy ctor. Actually it is not really necessary to have deep copy in this case, as picking would be sufficient. Isn`t it? So I propose adding InsertPick(int i, pick_ T&).
Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21188 is a reply to message #21187] Fri, 08 May 2009 10:07 Go to previous messageGo to next message
mrjt is currently offline  mrjt
Messages: 705
Registered: March 2007
Location: London
Contributor
Well, perhaps it should be added for symmetry, but doesn't this:
Vector<M> v;
M a;
..
v.Insert(0) = a;

Do exactly the same thing?
Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21189 is a reply to message #21188] Fri, 08 May 2009 10:30 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Actually it doesn`t. Differences are:
- you require M to have default constructor, which is not strong requirement for Vector element
- you have one redundant call (ctor and operator= instead of ctor), which sometimes is crucial (picking was made for efficiency, right?)

And IMO this code is little bit less clear than plain v.InsertPick(a).
Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21194 is a reply to message #21189] Fri, 08 May 2009 21:21 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Mindtraveller wrote on Fri, 08 May 2009 04:30

Actually it doesn`t. Differences are:
- you require M to have default constructor, which is not strong requirement for Vector element



Does it?

Quote:


- you have one redundant call (ctor and operator= instead of ctor), which sometimes is crucial (picking was made for efficiency, right?)

And IMO this code is little bit less clear than plain v.InsertPick(a).



While I can agree with symmetry point and clarity issue, I do not see how adding InsertPick should solve above problem.

Maybe you can rewrite mrjt's example with InsertPick?

Mirek
Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21198 is a reply to message #21194] Sat, 09 May 2009 00:05 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

luzr wrote on Fri, 08 May 2009 23:21

Mindtraveller wrote on Fri, 08 May 2009 04:30

Actually it doesn`t. Differences are:
- you require M to have default constructor, which is not strong requirement for Vector element



1. Does it?

Quote:


- you have one redundant call (ctor and operator= instead of ctor), which sometimes is crucial (picking was made for efficiency, right?)

And IMO this code is little bit less clear than plain v.InsertPick(a).



2. While I can agree with symmetry point and clarity issue, I do not see how adding InsertPick should solve above problem.

3. Maybe you can rewrite mrjt's example with InsertPick?

Mirek

1. I`m not quite shure I understand what you mean by this question.
If I understand correctly and the question is about default ctor requirement, TheIDE help says:
Quote:

General requirement: T is required to be moveable and must have either deep copy constructor, pick constructor or default constructor.

AddPick requirement: void AddPick(pick_ T& x)
Adds new element to Vector and picks value of parameter to it.
Requires T to have pick constructor.

-- so it is not necessary for M to have default /deep copy ctor to have Vector<M> and do AddPick.

2. I hope that InsertPick won`t make me writing default constructors or optional deep copy to insert element into queue. This should solve the problem with deep copy where I want to pick only.

3.
Vector<M> v;
M a;
..
v.InsertPick(a);

Or maybe you mean InsertPick(int i, pick_ Vector<M> &) ?

[Updated on: Sat, 09 May 2009 00:09]

Report message to a moderator

Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21200 is a reply to message #21198] Sat, 09 May 2009 09:02 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Yes, my mistake, you are right.

I will add InsertPick ASAP.

Mirek
Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21201 is a reply to message #21200] Sat, 09 May 2009 09:06 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
This should do the trick:

template <class T>
void Vector<T>::InsertPick(int i, pick_ T& x)
{
	if(!count) return;
	ASSERT(&x < vector || &x > vector + items);
	RawInsert(q, count);
	::new(vector[q]) T(x);
}

template <class T>
void Array<T>::InsertPick(int i, pick_ T& x)
{
	vector.InsertN(i, 1);
	vector[i] = new T(x);
}


Mirek

[Updated on: Sat, 09 May 2009 09:13]

Report message to a moderator

Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21210 is a reply to message #21201] Sat, 09 May 2009 21:52 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Thank you very much! U++ is great at it`s support.
Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21246 is a reply to message #21201] Sun, 10 May 2009 19:56 Go to previous messageGo to next message
hans is currently offline  hans
Messages: 44
Registered: March 2006
Location: Germany
Member
Hi,
I think
template <class T>
void Vector<T>::InsertPick(int i, pick_ T& x)
{
	if(!count) return;
	ASSERT(&x < vector || &x > vector + items);
	RawInsert(q, count);
	::new(vector[q]) T(x);
}

is wrong, should be:

template <class T>
void Vector<T>::InsertPick(int q, pick_ T& x)
{
	ASSERT(&x < vector || &x > vector + items);
	RawInsert(q, 1);
	::new(vector[q]) T(x);
}


Thanks, Hans.

[Updated on: Sun, 10 May 2009 19:57]

Report message to a moderator

Re: Proposal: add Vector::InsertPick(int i, pick_ T&) [message #21255 is a reply to message #21246] Sun, 10 May 2009 23:11 Go to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
hans wrote on Sun, 10 May 2009 13:56

Hi,
I think
template <class T>
void Vector<T>::InsertPick(int i, pick_ T& x)
{
	if(!count) return;
	ASSERT(&x < vector || &x > vector + items);
	RawInsert(q, count);
	::new(vector[q]) T(x);
}

is wrong, should be:

template <class T>
void Vector<T>::InsertPick(int q, pick_ T& x)
{
	ASSERT(&x < vector || &x > vector + items);
	RawInsert(q, 1);
	::new(vector[q]) T(x);
}


Thanks, Hans.


Thank you, you are right, of course.

Mirek
Previous Topic: Incorrect ASSERT in Value::Serialize
Next Topic: XmlParser and <![CDATA[
Goto Forum:
  


Current Time: Tue Apr 16 23:11:36 CEST 2024

Total time taken to generate the page: 0.01912 seconds