[Scummvm-git-logs] scummvm master -> d99ed4f1d4f9d5742c322697ca24f19c2bb40b19
sev-
noreply at scummvm.org
Sat Jul 20 12:17:19 UTC 2024
This automated email contains information about 3 new commits which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .
Summary:
05c1f81325 SCI: Add move operators for stable pointer arrays
0ad948d51a COMMON: Add uninitialized move functions
d99ed4f1d4 COMMON: Use uninitialized_move when resizing arrays
Commit: 05c1f81325113552579dca37f292822ff910e4a9
https://github.com/scummvm/scummvm/commit/05c1f81325113552579dca37f292822ff910e4a9
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2024-07-20T14:17:15+02:00
Commit Message:
SCI: Add move operators for stable pointer arrays
Changed paths:
engines/sci/graphics/lists32.h
diff --git a/engines/sci/graphics/lists32.h b/engines/sci/graphics/lists32.h
index ffaa5524630..f939264e42e 100644
--- a/engines/sci/graphics/lists32.h
+++ b/engines/sci/graphics/lists32.h
@@ -60,6 +60,13 @@ public:
}
}
}
+ StablePointerArray(StablePointerArray &&other) : _size(other._size) {
+ other._size = 0;
+ for (size_type i = 0; i < _size; ++i) {
+ _items[i] = other._items[i];
+ other._items[i] = nullptr;
+ }
+ }
~StablePointerArray() {
for (size_type i = 0; i < _size; ++i) {
delete _items[i];
@@ -78,6 +85,16 @@ public:
}
}
+ void operator=(StablePointerArray &&other) {
+ clear();
+ _size = other._size;
+ other._size = 0;
+ for (size_type i = 0; i < _size; ++i) {
+ _items[i] = other._items[i];
+ other._items[i] = nullptr;
+ }
+ }
+
T *const &operator[](size_type index) const {
assert(index < _size);
return _items[index];
@@ -217,6 +234,9 @@ public:
}
}
}
+ StablePointerDynamicArray(StablePointerDynamicArray &&other) {
+ _items = Common::move(other._items);
+ }
~StablePointerDynamicArray() {
for (size_type i = 0; i < _items.size(); ++i) {
delete _items[i];
@@ -233,6 +253,10 @@ public:
}
}
}
+ void operator=(StablePointerDynamicArray &&other) {
+ clear();
+ _items = Common::move(other._items);
+ }
T *const &operator[](size_type index) const {
return _items[index];
Commit: 0ad948d51acd117dd5e813ad587335c9aa93ea96
https://github.com/scummvm/scummvm/commit/0ad948d51acd117dd5e813ad587335c9aa93ea96
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2024-07-20T14:17:15+02:00
Commit Message:
COMMON: Add uninitialized move functions
Changed paths:
common/algorithm.h
common/memory.h
common/std/vector.h
diff --git a/common/algorithm.h b/common/algorithm.h
index 08dfb6efabb..ab0e320329a 100644
--- a/common/algorithm.h
+++ b/common/algorithm.h
@@ -90,6 +90,63 @@ Out copy_if(In first, In last, Out dst, Op op) {
return dst;
}
+/**
+ * @}
+ */
+
+/**
+ * @name Move templates
+ * @{
+ */
+
+/**
+ * Move data from the range [first, last) to [dst, dst + (last - first)).
+ *
+ * The function requires the range [dst, dst + (last - first)) to be valid.
+ * It also requires dst not to be in the range [first, last).
+ */
+template<class In, class Out>
+Out move(In first, In last, Out dst) {
+ while (first != last)
+ *dst++ = Common::move(*first++);
+ return dst;
+}
+
+/**
+ * Move data from the range [first, last) to [dst - (last - first), dst).
+ *
+ * The function requires the range [dst - (last - first), dst) to be valid.
+ * It also requires dst not to be in the range [first, last).
+ *
+ * Unlike move, move_backward moves the data from the end to the beginning.
+ */
+template<class In, class Out>
+Out move_backward(In first, In last, Out dst) {
+ while (first != last)
+ *--dst = Common::move(*--last);
+ return dst;
+}
+
+/**
+ * Move data from the range [first, last) to [dst, dst + (last - first)).
+ *
+ * The function requires the range [dst, dst + (last - first)) to be valid.
+ * It also requires dst not to be in the range [first, last).
+ *
+ * Unlike move or move_backward, it does not move all data. It only moves
+ * a data element when operator() of the op parameter returns true for the
+ * passed data element.
+ */
+template<class In, class Out, class Op>
+Out move_if(In first, In last, Out dst, Op op) {
+ while (first != last) {
+ if (op(*first))
+ *dst++ = Common::move(*first);
+ ++first;
+ }
+ return dst;
+}
+
/**
* @}
*/
diff --git a/common/memory.h b/common/memory.h
index 0d9b40ba424..dd91985d75b 100644
--- a/common/memory.h
+++ b/common/memory.h
@@ -22,7 +22,7 @@
#ifndef COMMON_MEMORY_H
#define COMMON_MEMORY_H
-#include "common/scummsys.h"
+#include "common/util.h"
namespace Common {
@@ -62,6 +62,18 @@ Type *uninitialized_copy(In first, In last, Type *dst) {
return dst;
}
+/**
+ * Moves data from the range [first, last) to [dst, dst + (last - first)).
+ * It requires the range [dst, dst + (last - first)) to be valid and
+ * uninitialized.
+ */
+template<class In, class Type>
+Type *uninitialized_move(In first, In last, Type *dst) {
+ while (first != last)
+ new ((void *)dst++) Type(Common::move(*first++));
+ return dst;
+}
+
/**
* Initializes the memory [first, first + (last - first)) with the value x.
* It requires the range [first, first + (last - first)) to be valid and
diff --git a/common/std/vector.h b/common/std/vector.h
index a2c49764bc7..56d609c5b93 100644
--- a/common/std/vector.h
+++ b/common/std/vector.h
@@ -39,16 +39,6 @@
namespace Std {
-template<class In, class Type>
-Type *uninitialized_move(In first, In last, Type *dst) {
- while (first != last) {
- Type &t = *new ((void *)dst++) Type();
- t = Std::move(*first++);
- }
-
- return dst;
-}
-
template<class T>
class vector {
public:
@@ -501,8 +491,8 @@ public:
allocCapacity(newCapacity);
if (oldStorage) {
- // Copy old data
- uninitialized_move(oldStorage, oldStorage + _size, _storage);
+ // Move old data
+ Common::uninitialized_move(oldStorage, oldStorage + _size, _storage);
freeStorage(oldStorage, _size);
}
}
Commit: d99ed4f1d4f9d5742c322697ca24f19c2bb40b19
https://github.com/scummvm/scummvm/commit/d99ed4f1d4f9d5742c322697ca24f19c2bb40b19
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2024-07-20T14:17:15+02:00
Commit Message:
COMMON: Use uninitialized_move when resizing arrays
Changed paths:
common/array.h
common/std/vector.h
diff --git a/common/array.h b/common/array.h
index 7d3e6bb8b3f..83ccb739486 100644
--- a/common/array.h
+++ b/common/array.h
@@ -219,8 +219,8 @@ public:
/** Remove an element at the given position from the array and return the value of that element. */
T remove_at(size_type idx) {
assert(idx < _size);
- T tmp = _storage[idx];
- copy(_storage + idx + 1, _storage + _size, _storage + idx);
+ T tmp = Common::move(_storage[idx]);
+ move(_storage + idx + 1, _storage + _size, _storage + idx);
_size--;
// We also need to destroy the last object properly here.
_storage[_size].~T();
@@ -286,7 +286,7 @@ public:
/** Erase the element at @p pos position and return an iterator pointing to the next element in the array. */
iterator erase(iterator pos) {
- copy(pos + 1, _storage + _size, pos);
+ move(pos + 1, _storage + _size, pos);
_size--;
// We also need to destroy the last object properly here.
_storage[_size].~T();
@@ -295,7 +295,7 @@ public:
/** Erase the elements from @p first to @p last and return an iterator pointing to the next element in the array. */
iterator erase(iterator first, iterator last) {
- copy(last, _storage + _size, first);
+ move(last, _storage + _size, first);
int count = (last - first);
_size -= count;
@@ -361,8 +361,8 @@ public:
allocCapacity(newCapacity);
if (oldStorage) {
- // Copy old data
- uninitialized_copy(oldStorage, oldStorage + _size, _storage);
+ // Move old data
+ uninitialized_move(oldStorage, oldStorage + _size, _storage);
freeStorage(oldStorage, _size);
}
}
@@ -469,30 +469,30 @@ protected:
// storage to avoid conflicts.
allocCapacity(roundUpCapacity(_size + n));
- // Copy the data from the old storage till the position where
+ // Move the data from the old storage till the position where
// we insert new data
- uninitialized_copy(oldStorage, oldStorage + idx, _storage);
+ uninitialized_move(oldStorage, oldStorage + idx, _storage);
// Copy the data we insert
uninitialized_copy(first, last, _storage + idx);
- // Afterwards, copy the old data from the position where we
+ // Afterwards, move the old data from the position where we
// insert.
- uninitialized_copy(oldStorage + idx, oldStorage + _size, _storage + idx + n);
+ uninitialized_move(oldStorage + idx, oldStorage + _size, _storage + idx + n);
freeStorage(oldStorage, _size);
} else if (idx + n <= _size) {
// Make room for the new elements by shifting back
// existing ones.
// 1. Move a part of the data to the uninitialized area
- uninitialized_copy(_storage + _size - n, _storage + _size, _storage + _size);
+ uninitialized_move(_storage + _size - n, _storage + _size, _storage + _size);
// 2. Move a part of the data to the initialized area
- copy_backward(pos, _storage + _size - n, _storage + _size);
+ move_backward(pos, _storage + _size - n, _storage + _size);
// Insert the new elements.
copy(first, last, pos);
} else {
- // Copy the old data from the position till the end to the new
+ // Move the old data from the position till the end to the new
// place.
- uninitialized_copy(pos, _storage + _size, _storage + idx + n);
+ uninitialized_move(pos, _storage + _size, _storage + idx + n);
// Copy a part of the new data to the position inside the
// initialized space.
diff --git a/common/std/vector.h b/common/std/vector.h
index 56d609c5b93..a126e9becdc 100644
--- a/common/std/vector.h
+++ b/common/std/vector.h
@@ -285,8 +285,8 @@ public:
/** Remove an element at the given position from the array and return the value of that element. */
T remove_at(size_type idx) {
assert(idx < _size);
- T tmp = _storage[idx];
- Common::copy(_storage + idx + 1, _storage + _size, _storage + idx);
+ T tmp = Common::move(_storage[idx]);
+ Common::move(_storage + idx + 1, _storage + _size, _storage + idx);
_size--;
// We also need to destroy the last object properly here.
_storage[_size].~T();
@@ -359,15 +359,16 @@ public:
/** Erase the element at @p pos position and return an iterator pointing to the next element in the array. */
iterator erase(iterator pos) {
- Common::copy(pos + 1, _storage + _size, pos);
+ Common::move(pos + 1, _storage + _size, pos);
_size--;
// We also need to destroy the last object properly here.
_storage[_size].~T();
return pos;
}
+ /** Erase the elements from @p first to @p last and return an iterator pointing to the next element in the array. */
iterator erase(iterator first, iterator last) {
- Common::copy(last, this->_storage + this->_size, first);
+ Common::move(last, this->_storage + this->_size, first);
int count = (last - first);
this->_size -= count;
@@ -581,30 +582,30 @@ protected:
// storage to avoid conflicts.
allocCapacity(roundUpCapacity(_size + n));
- // Copy the data from the old storage till the position where
+ // Move the data from the old storage till the position where
// we insert new data
- Common::uninitialized_copy(oldStorage, oldStorage + idx, _storage);
+ Common::uninitialized_move(oldStorage, oldStorage + idx, _storage);
// Copy the data we insert
Common::uninitialized_copy(first, last, _storage + idx);
- // Afterwards, copy the old data from the position where we
+ // Afterwards, move the old data from the position where we
// insert.
- Common::uninitialized_copy(oldStorage + idx, oldStorage + _size, _storage + idx + n);
+ Common::uninitialized_move(oldStorage + idx, oldStorage + _size, _storage + idx + n);
freeStorage(oldStorage, _size);
} else if (idx + n <= _size) {
// Make room for the new elements by shifting back
// existing ones.
// 1. Move a part of the data to the uninitialized area
- Common::uninitialized_copy(_storage + _size - n, _storage + _size, _storage + _size);
+ Common::uninitialized_move(_storage + _size - n, _storage + _size, _storage + _size);
// 2. Move a part of the data to the initialized area
- Common::copy_backward(pos, _storage + _size - n, _storage + _size);
+ Common::move_backward(pos, _storage + _size - n, _storage + _size);
// Insert the new elements.
Common::copy(first, last, pos);
} else {
- // Copy the old data from the position till the end to the new
+ // Move the old data from the position till the end to the new
// place.
- Common::uninitialized_copy(pos, _storage + _size, _storage + idx + n);
+ Common::uninitialized_move(pos, _storage + _size, _storage + idx + n);
// Copy a part of the new data to the position inside the
// initialized space.
More information about the Scummvm-git-logs
mailing list