template <typename T>
ArrayList<T>::ArrayList(ArrayList&& other) noexcept
: arr(nullptr)
, actualSize(0)
, allocatedSize(0);
{
// So you are swapping the random values of this
// object into other.
swap(*this, other);
}
template <typename T>
ArrayList& ArrayList<T>::operator =(ArrayList<T>&& other) noexcept
{ // ^^^^^^^^
swap(*this, other);
return *this; // Add this line.
}
private:
template <typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other, int allocatedSize)
: ArrayList(allocatedSize)
{
try
{
for(size_t i = 0; i<other.actualSize; ++i)
{
add(other.arr[i]);
}
}
catch(...)
{
for(size_t i = actualSize; i > 0; --i)
{
arr[i-1].~T();
}
::operator delete(arr);
throw;
}
}
public:
// public copy constructor uses the private version
// just passing a couple of parameters forward.
template <typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other)
: ArrayList(other, other.allocatedSize)
{}
template <typename T>
void ArrayList<T>::resize()
{
// resize() uses the private copy constructor
// but passes an increased size through.
ArrayList tmp(*this, std::max(allocatedSize * 1.5, 2));
swap(*this, tmp);
}
template <typename T>
void ArrayList<T>::remove(int index)
{
if(index<0 || index >= actualSize)
{
throw std::runtime_error("Invalid Range");
}
for(i = index + 1; i < actualSize; ++i)
{
arr[i-1] = std::move(arr[i]);
}
// Once you have moved all the items down one place
// Then destroy the last item.
arr[actualSize-1].~T();-actualSize;
--actualSize;arr[actualSize].~T();
}
template <typename T>
ArrayList<T>::ArrayList(ArrayList&& other)
: arr(nullptr)
, actualSize(0)
, allocatedSize(0);
{
// So you are swapping the random values of this
// object into other.
swap(*this, other);
}
template <typename T>
ArrayList& ArrayList<T>::operator =(ArrayList<T>&& other)
{
swap(*this, other);
return *this; // Add this line.
}
private:
template <typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other, int allocatedSize)
: ArrayList(allocatedSize)
{
try
{
for(size_t i = 0; i<other.actualSize; ++i)
{
add(other.arr[i]);
}
}
catch(...)
{
for(size_t i = actualSize; i > 0; --i)
{
arr[i-1].~T();
}
::operator delete(arr);
throw;
}
}
public:
// public copy constructor uses the private version
// just passing a couple of parameters forward.
template <typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other)
ArrayList(other, other.allocatedSize)
{}
template <typename T>
void ArrayList<T>::resize()
{
// resize() uses the private copy constructor
// but passes an increased size through.
ArrayList tmp(*this, std::max(allocatedSize * 1.5, 2));
swap(*this, tmp);
}
template <typename T>
void ArrayList<T>::remove(int index)
{
if(index<0 || index >= actualSize)
{
throw std::runtime_error("Invalid Range");
}
for(i = index + 1; i < actualSize; ++i)
{
arr[i-1] = std::move(arr[i]);
}
// Once you have moved all the items down one place
// Then destroy the last item.
arr[actualSize-1].~T();
--actualSize;
}
template <typename T>
ArrayList<T>::ArrayList(ArrayList&& other) noexcept
: arr(nullptr)
, actualSize(0)
, allocatedSize(0);
{
swap(*this, other);
}
template <typename T>
ArrayList& ArrayList<T>::operator =(ArrayList<T>&& other) noexcept
{ // ^^^^^^^^
swap(*this, other);
return *this; // Add this line.
}
private:
template <typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other, int allocatedSize)
: ArrayList(allocatedSize)
{
try
{
for(size_t i = 0; i<other.actualSize; ++i)
{
add(other.arr[i]);
}
}
catch(...)
{
for(size_t i = actualSize; i > 0; --i)
{
arr[i-1].~T();
}
::operator delete(arr);
throw;
}
}
public:
// public copy constructor uses the private version
// just passing a couple of parameters forward.
template <typename T>
ArrayList<T>::ArrayList(const ArrayList<T>& other)
: ArrayList(other, other.allocatedSize)
{}
template <typename T>
void ArrayList<T>::resize()
{
// resize() uses the private copy constructor
// but passes an increased size through.
ArrayList tmp(*this, std::max(allocatedSize * 1.5, 2));
swap(*this, tmp);
}
template <typename T>
void ArrayList<T>::remove(int index)
{
if(index<0 || index >= actualSize)
{
throw std::runtime_error("Invalid Range");
}
for(i = index + 1; i < actualSize; ++i)
{
arr[i-1] = std::move(arr[i]);
}
// Once you have moved all the items down one place
// Then destroy the last item.
--actualSize;
arr[actualSize].~T();
}
Loading
Loading
Loading
lang-cpp