Array move semantics fix

This commit is contained in:
Mateusz Karbowiak
2024-06-22 15:13:53 +02:00
parent 0d8691045b
commit 02403377cd

View File

@@ -579,7 +579,7 @@ public:
/// Insert the given item at specified index with keeping items order. /// Insert the given item at specified index with keeping items order.
/// </summary> /// </summary>
/// <param name="index">The zero-based index at which item should be inserted.</param> /// <param name="index">The zero-based index at which item should be inserted.</param>
/// <param name="item">The item to insert.</param> /// <param name="item">The item to be inserted by copying.</param>
void Insert(int32 index, const T& item) void Insert(int32 index, const T& item)
{ {
ASSERT(index >= 0 && index <= _count); ASSERT(index >= 0 && index <= _count);
@@ -592,6 +592,23 @@ public:
data[index] = item; data[index] = item;
} }
/// <summary>
/// Insert the given item at specified index with keeping items order.
/// </summary>
/// <param name="index">The zero-based index at which item should be inserted.</param>
/// <param name="item">The item to inserted by moving.</param>
void Insert(int32 index, T&& item)
{
ASSERT(index >= 0 && index <= _count);
EnsureCapacity(_count + 1);
T* data = _allocation.Get();
Memory::ConstructItems(data + _count, 1);
for (int32 i = _count - 1; i >= index; i--)
data[i + 1] = MoveTemp(data[i]);
_count++;
data[index] = MoveTemp(item);
}
/// <summary> /// <summary>
/// Insert the given item at specified index with keeping items order. /// Insert the given item at specified index with keeping items order.
/// </summary> /// </summary>
@@ -772,9 +789,9 @@ public:
/// <summary> /// <summary>
/// Performs pop from stack operation (stack grows at the end of the collection). /// Performs pop from stack operation (stack grows at the end of the collection).
/// </summary> /// </summary>
T Pop() FORCE_INLINE T Pop()
{ {
T item(Last()); T item = MoveTemp(Last());
RemoveLast(); RemoveLast();
return item; return item;
} }
@@ -807,6 +824,15 @@ public:
Add(item); Add(item);
} }
/// <summary>
/// Performs enqueue to queue operation (queue head is in the beginning of queue).
/// </summary>
/// <param name="item">The item to append.</param>
void Enqueue(T&& item)
{
Add(MoveTemp(item));
}
/// <summary> /// <summary>
/// Performs dequeue from queue operation (queue head is in the beginning of queue). /// Performs dequeue from queue operation (queue head is in the beginning of queue).
/// </summary> /// </summary>
@@ -814,7 +840,7 @@ public:
T Dequeue() T Dequeue()
{ {
ASSERT(HasItems()); ASSERT(HasItems());
T item(First()); T item = MoveTemp(_allocation.Get()[0]);
RemoveAtKeepOrder(0); RemoveAtKeepOrder(0);
return item; return item;
} }