aboutsummaryrefslogtreecommitdiff
path: root/mp/src/public/tier1/utlvector.h
diff options
context:
space:
mode:
Diffstat (limited to 'mp/src/public/tier1/utlvector.h')
-rw-r--r--mp/src/public/tier1/utlvector.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/mp/src/public/tier1/utlvector.h b/mp/src/public/tier1/utlvector.h
index 0ddf4ad0..7313bc99 100644
--- a/mp/src/public/tier1/utlvector.h
+++ b/mp/src/public/tier1/utlvector.h
@@ -23,6 +23,7 @@
#include "tier1/utlmemory.h"
#include "tier1/utlblockmemory.h"
#include "tier1/strtools.h"
+#include "vstdlib/random.h"
#define FOR_EACH_VEC( vecName, iteratorName ) \
for ( int iteratorName = 0; iteratorName < (vecName).Count(); iteratorName++ )
@@ -63,6 +64,8 @@ public:
const T& Head() const;
T& Tail();
const T& Tail() const;
+ T& Random();
+ const T& Random() const;
// STL compatible member functions. These allow easier use of std::sort
// and they are forward compatible with the C++ 11 range-based for loops.
@@ -159,6 +162,8 @@ public:
void Sort( int (__cdecl *pfnCompare)(const T *, const T *) );
+ void Shuffle( IUniformRandomStream* pSteam = NULL );
+
#ifdef DBGFLAG_VALIDATE
void Validate( CValidator &validator, char *pchName ); // Validate our internal structures
#endif // DBGFLAG_VALIDATE
@@ -677,6 +682,37 @@ inline int CUtlVector<T, A>::Size() const
}
template< typename T, class A >
+inline T& CUtlVector<T, A>::Random()
+{
+ Assert( m_Size > 0 );
+ return m_Memory[ RandomInt( 0, m_Size - 1 ) ];
+}
+
+template< typename T, class A >
+inline const T& CUtlVector<T, A>::Random() const
+{
+ Assert( m_Size > 0 );
+ return m_Memory[ RandomInt( 0, m_Size - 1 ) ];
+}
+
+
+//-----------------------------------------------------------------------------
+// Shuffle - Knuth/Fisher-Yates
+//-----------------------------------------------------------------------------
+template< typename T, class A >
+void CUtlVector<T, A>::Shuffle( IUniformRandomStream* pSteam )
+{
+ for ( int i = 0; i < m_Size; i++ )
+ {
+ int j = pSteam ? pSteam->RandomInt( i, m_Size - 1 ) : RandomInt( i, m_Size - 1 );
+ if ( i != j )
+ {
+ V_swap( m_Memory[ i ], m_Memory[ j ] );
+ }
+ }
+}
+
+template< typename T, class A >
inline int CUtlVector<T, A>::Count() const
{
return m_Size;