From 602ea8f723f788999fab50b4d1072f110c8d6216 Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Sat, 10 Nov 2018 10:43:35 +0100
Subject: [PATCH] - made some minor changes to TArray after finding out that
 "new int()" is not the same as "new int".

With parentheses this initializes to 0 which created needless initialization code in a few places.
---
 src/tarray.h | 44 ++++++++++++++++++++++++--------------------
 1 file changed, 24 insertions(+), 20 deletions(-)

diff --git a/src/tarray.h b/src/tarray.h
index 7e8c53f06..8543637cd 100644
--- a/src/tarray.h
+++ b/src/tarray.h
@@ -164,7 +164,7 @@ public:
 		Array = (T *)M_Malloc (sizeof(T)*max);
 		if (reserve)
 		{
-			for (unsigned i = 0; i < Count; i++) ::new(&Array[i]) T();
+			ConstructEmpty(0, Count - 1);
 		}
 	}
 	TArray (const TArray<T,TT> &other)
@@ -362,17 +362,6 @@ public:
 		}
 	}
 
-	// Reserves a range of entries in the middle of the array, shifting elements as needed
-	void ReserveAt(unsigned int index, unsigned int amount)
-	{
-		Grow(amount);
-		memmove(&Array[index + amount], &Array[index], sizeof(T)*(Count - index - amount));
-		for (unsigned i = 0; i < amount; i++)
-		{
-			::new ((void *)&Array[index + i]) T();
-		}
-	}
-
 	void ShrinkToFit ()
 	{
 		if (Most > Count)
@@ -411,10 +400,7 @@ public:
 		{
 			// Adding new entries
 			Grow (amount - Count);
-			for (unsigned int i = Count; i < amount; ++i)
-			{
-				::new((void *)&Array[i]) T;
-			}
+			ConstructEmpty(Count, amount - 1);
 		}
 		else if (Count != amount)
 		{
@@ -423,6 +409,18 @@ public:
 		}
 		Count = amount;
 	}
+	// Ensures that the array has at most amount entries.
+	// Useful in cases where the initial allocation may be larger than the final result.
+	// Resize would create a lot of unneeded code in those cases.
+	void Clamp(unsigned int amount)
+	{
+		if (Count > amount)
+		{
+			// Deleting old entries
+			DoDelete(amount, Count - 1);
+			Count = amount;
+		}
+	}
 	void Alloc(unsigned int amount)
 	{
 		// first destroys all content and then rebuilds the array.
@@ -438,10 +436,7 @@ public:
 		Grow (amount);
 		unsigned int place = Count;
 		Count += amount;
-		for (unsigned int i = place; i < Count; ++i)
-		{
-			::new((void *)&Array[i]) T;
-		}
+		ConstructEmpty(place, Count - 1);
 		return place;
 	}
 	unsigned int Size () const
@@ -501,6 +496,15 @@ private:
 			Array[i].~T();
 		}
 	}
+
+	void ConstructEmpty(unsigned int first, unsigned int last)
+	{
+		assert(last != ~0u);
+		for (unsigned int i = first; i <= last; ++i)
+		{
+			::new(&Array[i]) T;
+		}
+	}
 };
 
 // TDeletingArray -----------------------------------------------------------