Files
RP412/MUNGA/AVERAGE.h
T
CydandClaude Opus 4.8 4abbf8879f Initial import of Red Planet v4.10 Win32 source
Imports the current Win32 source for the pod-racing game 'Red Planet',
built on the MUNGA engine and its L4 (Win32/DirectX) platform layer:

- MUNGA / MUNGA_L4: cross-platform engine core and Win32 backend
- RP / RP_L4: Red Planet game logic and Win32 application
- DivLoader, Setup1: asset loader and installer project
- lib, MUNGA_L4/openal, MUNGA_L4/sos: third-party audio dependencies

Removed stale Subversion metadata and added .gitignore/.gitattributes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-30 07:59:51 -05:00

299 lines
6.4 KiB
C++

#pragma once
#include "style.h"
//##########################################################################
//########################### AverageOf ##############################
//##########################################################################
template <class T> class AverageOf SIGNATURED
{
public:
//
//-----------------------------------------------------------------------
// Constructor, Destructor, Testing
//-----------------------------------------------------------------------
//
AverageOf();
AverageOf(
size_t size,
T initial=(T)0
);
~AverageOf();
Logical
TestInstance() const;
//
//-----------------------------------------------------------------------
// SetSize
//-----------------------------------------------------------------------
//
void
SetSize(
size_t size,
T initial=(T)0
);
//
//-----------------------------------------------------------------------
// Add
//-----------------------------------------------------------------------
//
void
Add(T value);
//
//-----------------------------------------------------------------------
// CalculateAverage
//-----------------------------------------------------------------------
//
T
CalculateAverage();
//
//-----------------------------------------------------------------------
// CalculateOlympicAverage
//-----------------------------------------------------------------------
//
T
CalculateOlympicAverage();
//
//-----------------------------------------------------------------------
// Calculate Trend of Average
//-----------------------------------------------------------------------
//
Scalar
CalculateTrend();
private:
//
//-----------------------------------------------------------------------
// Private data
//-----------------------------------------------------------------------
//
size_t size;
size_t next;
T *array;
};
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~ AverageOf templates ~~~~~~~~~~~~~~~~~~~~~~~~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T>
AverageOf<T>::AverageOf()
{
array = NULL;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T>
AverageOf<T>::AverageOf(
size_t the_size,
T initial
)
{
array = NULL;
SetSize(the_size, initial);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> void
AverageOf<T>::SetSize(
size_t the_size,
T initial
)
{
if (array != NULL)
{
Unregister_Pointer(array);
delete[] array;
}
size = the_size;
array = new T[size];
Register_Pointer(array);
next = 0;
for (size_t i = 0; i < size; i++)
{
Check_Pointer(array);
Verify(i < size);
array[i] = initial;
Check_Fpu();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T>
AverageOf<T>::~AverageOf()
{
Unregister_Pointer(array);
delete[] array;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> Logical
AverageOf<T>::TestInstance() const
{
Check_Pointer(array);
Verify(next < size);
return True;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> void
AverageOf<T>::Add(T value)
{
Check(this);
Check_Pointer(array);
Verify(next < size);
array[next] = value;
++next;
if (next >= size)
{
Verify(next == size);
next = 0;
}
Check_Fpu();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> T
AverageOf<T>::CalculateAverage()
{
Check(this);
size_t i;
T accumulate;
for (i = 0, accumulate = (T)0; i < size; i++)
{
Check_Pointer(array);
Verify(i < size);
accumulate += array[i];
Check_Fpu();
}
Verify(!Small_Enough((T)size));
return (accumulate / (T)size);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> T
AverageOf<T>::CalculateOlympicAverage()
{
Check(this);
size_t i;
T accumulate, min_value, max_value;
Verify(0 < size);
min_value = array[0];
max_value = array[0];
Check_Fpu();
for (i = 0, accumulate = (T)0; i < size; i++)
{
Check_Pointer(array);
Verify(i < size);
accumulate += array[i];
Check_Fpu();
min_value = Min(array[i], min_value);
max_value = Max(array[i], max_value);
Check_Fpu();
}
accumulate -= min_value;
accumulate -= max_value;
Check_Fpu();
Verify(!Small_Enough((T)(size - 2)));
return (accumulate / (T)(size - 2));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
template <class T> Scalar
AverageOf<T>::CalculateTrend()
{
Check(this);
size_t i;
Scalar f = 0.0f;
Scalar fx = 0.0f;
Scalar x1 = size;
Scalar x2 = x1*x1/2.0f;
Scalar x3 = x2 + x1*x1*x1/3.0f + x1/6.0f;
x2 += x1/2.0f;
i = next;
Scalar t = 1.0f;
do
{
f += array[i];
fx += t*array[i];
t += 1.0f;
if (++i == size)
{
i = 0;
}
} while (i != next);
return (x1*fx - x2*f) / (x1*x3 - x2*x2);
}
//##########################################################################
//######################## StaticAverageOf ###########################
//##########################################################################
template <class T> class StaticAverageOf SIGNATURED
{
public:
//
//-----------------------------------------------------------------------
// Constructor, Destructor, Testing
//-----------------------------------------------------------------------
//
StaticAverageOf()
{size = (T)0; total = (T)0;}
~StaticAverageOf()
{}
Logical
TestInstance() const
{return True;}
//
//-----------------------------------------------------------------------
// Add
//-----------------------------------------------------------------------
//
void
Add(T value)
{Check(this); size++; total += value;}
//
//-----------------------------------------------------------------------
// CalculateAverage
//-----------------------------------------------------------------------
//
T
CalculateAverage()
{Check(this); return (size == 0) ? ((T)0) : (total / (T)size);}
//
//-----------------------------------------------------------------------
// GetSize
//-----------------------------------------------------------------------
//
size_t
GetSize()
{Check(this); return size;}
private:
//
//-----------------------------------------------------------------------
// Private data
//-----------------------------------------------------------------------
//
size_t size;
T total;
};