[Scummvm-git-logs] scummvm master -> 4002c7bdde452cfb02c48344b7605e48f7e8c8c8

bluegr noreply at scummvm.org
Mon Dec 19 12:33:12 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
4002c7bdde COMMON: Add a version of Common::Array::resize with a fill value


Commit: 4002c7bdde452cfb02c48344b7605e48f7e8c8c8
    https://github.com/scummvm/scummvm/commit/4002c7bdde452cfb02c48344b7605e48f7e8c8c8
Author: Cameron Cawley (ccawley2011 at gmail.com)
Date: 2022-12-19T14:33:07+02:00

Commit Message:
COMMON: Add a version of Common::Array::resize with a fill value

Changed paths:
    common/array.h
    engines/ultima/shared/std/containers.h
    test/common/array.h


diff --git a/common/array.h b/common/array.h
index 7e50f2cfec5..ae702e9d9fb 100644
--- a/common/array.h
+++ b/common/array.h
@@ -381,6 +381,21 @@ public:
 		_size = newSize;
 	}
 
+	/** Change the size of the array and initialize new elements that exceed the
+	 *  current array's size with copies of value. */
+	void resize(size_type newSize, const T value) {
+		reserve(newSize);
+
+		T *storage = _storage;
+
+		for (size_type i = newSize; i < _size; ++i)
+			storage[i].~T();
+		if (newSize > _size)
+			uninitialized_fill_n(storage + _size, newSize - _size, value);
+
+		_size = newSize;
+	}
+
 	/** Assign to this array the elements between the given iterators from another array,
 	 *  from @p first included to @p last excluded.
 	 */
diff --git a/engines/ultima/shared/std/containers.h b/engines/ultima/shared/std/containers.h
index 4e903e723c8..7250a473bcf 100644
--- a/engines/ultima/shared/std/containers.h
+++ b/engines/ultima/shared/std/containers.h
@@ -91,12 +91,8 @@ public:
 	typedef const T const_reference;
 
 	vector() : Common::Array<T>() {}
-	vector(size_t newSize) : Common::Array<T>() {
-		Common::Array<T>::resize(newSize);
-	}
-	vector(size_t newSize, const T elem) {
-		resize(newSize, elem);
-	}
+	vector(size_t newSize) : Common::Array<T>(newSize) {}
+	vector(size_t newSize, const T elem) : Common::Array<T>(newSize, elem) {}
 
 	reverse_iterator rbegin() {
 		return reverse_iterator(this, (int)Common::Array<T>::size() - 1);
@@ -115,16 +111,6 @@ public:
 		Common::Array<T>::remove_at(0);
 	}
 
-	void resize(size_t newSize) {
-		Common::Array<T>::resize(newSize);
-	}
-	void resize(size_t newSize, const T elem) {
-		size_t oldSize = Common::Array<T>::size();
-		resize(newSize);
-		for (size_t idx = oldSize; idx < newSize; ++idx)
-			this->operator[](idx) = elem;
-	}
-
 	T at(size_t index) const {
 		return (*this)[index];
 	}
diff --git a/test/common/array.h b/test/common/array.h
index 5cf352cad4b..36f58dceb4e 100644
--- a/test/common/array.h
+++ b/test/common/array.h
@@ -416,6 +416,18 @@ class ArrayTestSuite : public CxxTest::TestSuite
 		TS_ASSERT_EQUALS(array.size(), (unsigned int)2);
 		TS_ASSERT_EQUALS(array[0], -3);
 		TS_ASSERT_EQUALS(array[1], 163);
+
+		array.resize(4, 42);
+		TS_ASSERT_EQUALS(array.size(), (unsigned int)4);
+		TS_ASSERT_EQUALS(array[0], -3);
+		TS_ASSERT_EQUALS(array[1], 163);
+		TS_ASSERT_EQUALS(array[2], 42);
+		TS_ASSERT_EQUALS(array[3], 42);
+
+		array.resize(2, 42);
+		TS_ASSERT_EQUALS(array.size(), (unsigned int)2);
+		TS_ASSERT_EQUALS(array[0], -3);
+		TS_ASSERT_EQUALS(array[1], 163);
 	}
 
 	void test_swap() {




More information about the Scummvm-git-logs mailing list