Home » U++ Library support » U++ Core » Vector<T>::Set(int i, T&& x) proposal
Vector<T>::Set(int i, T&& x) proposal [message #49119] |
Tue, 19 December 2017 23:30  |
Novo
Messages: 1430 Registered: December 2006
|
Ultimate Contributor |
|
|
I propose to add method below to the Vector class.
template <class T>
T& Vector<T>::Set(int i, T&& x) {
ASSERT(i >= 0);
const int count = GetCount();
if (i == count)
return Add(pick(x));
else if (i > count) {
At(i - 1);
return Add(pick(x));
}
T* addr = vector + i;
addr->~T();
::new(addr) T(pick(x));
return *addr;
}
Or it can be implemented like this:
template <class T>
T& Vector<T>::Set(int i, T&& x) {
ASSERT(i >= 0);
At(i);
T* addr = vector + i;
addr->~T();
::new(addr) T(pick(x));
return *addr;
}
Second implementation has less memory allocation stuff. It is hard to tell which one is better.
I believe there is no need to check that x is already contained in Vector because it is impossible to get an rvalue of a Vectors's element.
Regards,
Novo
|
|
|
 |
|
Vector<T>::Set(int i, T&& x) proposal
By: Novo on Tue, 19 December 2017 23:30
|
 |
|
Re: Vector<T>::Set(int i, T&& x) proposal
By: mirek on Sun, 28 January 2018 22:47
|
 |
|
Re: Vector<T>::Set(int i, T&& x) proposal
By: Novo on Tue, 30 January 2018 03:30
|
 |
|
Re: Vector<T>::Set(int i, T&& x) proposal
By: mirek on Wed, 31 January 2018 19:34
|
 |
|
Re: Vector<T>::Set(int i, T&& x) proposal
By: Novo on Wed, 31 January 2018 20:01
|
 |
|
Re: Vector<T>::Set(int i, T&& x) proposal
By: mirek on Thu, 01 February 2018 18:53
|
 |
|
Re: Vector<T>::Set(int i, T&& x) proposal
By: Novo on Thu, 01 February 2018 19:03
|
Goto Forum:
Current Time: Fri Apr 25 18:58:09 CEST 2025
Total time taken to generate the page: 0.02461 seconds
|