Min/Max of An Array of Any DataType, Including Ones That You Create

From Epic Wiki

Overview

Author: ( )

Dear Community,

In 4.3 you now have available two C++ functions from me, which I gave to Epic!

These are template functions for the min and max of an array of any datatype for which the < operator is defined!

You can use my Min/Max of Array with literally any existing UE4 datatype or any datatype that you create, as long as you define operator< for your data type!


Here is my wiki on custom operators / operator overloads in UE4 C++


My functions will optionally return the index of the min/max value if you supply a pointer!

This makes using my Min/Max of Array very flexible for you to use in code, because you dont have to supply the var by reference and so you dont have to supply it at all if you dont want to know the index of the min/max value!

Here's what Max looks like:

/**
* Max of Array
* @param	Array of templated type
* @param	Optional pointer for returning the index of the maximum element, if multiple maximum elements the first index is returned
* @return	The max value found in the array or default value if the array was empty
*/
template< class T >
static FORCEINLINE T Max(const TArray<T>& Values, int32* MaxIndex = NULL)
{
	if (Values.Num() == 0)
	{
		if (MaxIndex)
		{
			*MaxIndex = INDEX_NONE;
		}
		return T();
	}

	T CurMax = Values[0];
	int32 CurMaxIndex = 0;
	for (int32 v = 1; v < Values.Num(); ++v)
	{
		const T Value = Values[v];
		if (CurMax < Value)
		{
			CurMax = Value;
			CurMaxIndex = v;
		}
	}

	if (MaxIndex)
	{
		*MaxIndex = CurMaxIndex;
	}
	return CurMax;
}

GenericPlatformMath.h

You can find my functions in:

 Public\GenericPlatform\GenericPlatformMath.h 

Sample Usage

TArray<YourVarType> MyVars;
TArraY<int32> MyInts;

YourVarType MaxValue = FMath::Max<YourVarType>(MyVars); //dont need to know the index of the max value

int32 MaxIndex;

const int32 MaxValue = FMath::Max<int32>(MyInts,&MaxIndex);

UE_LOG(YourLog,Log,TEXT("index of the max value is is %d"), MaxIndex);

Your Data Type Only Needs the < Operator

Please note how I wrote the test for the Max of Array function!

 if (CurMax < Value)

I wrote it this way so you truly only need to define the operator< for your data type to use it with my template Min/Max array functions!

( )