Updated CDTestFramework with the OPCODE Array SAP test.
Thanks Pierre Terdiman for the latest update.
This commit is contained in:
@@ -1,19 +1,3 @@
|
||||
/*
|
||||
* ICE / OPCODE - Optimized Collision Detection
|
||||
* http://www.codercorner.com/Opcode.htm
|
||||
*
|
||||
* Copyright (c) 2001-2008 Pierre Terdiman, pierre@codercorner.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Contains a simple container class.
|
||||
@@ -38,7 +22,7 @@ subject to the following restrictions:
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Precompiled Header
|
||||
#include "Stdafx.h"
|
||||
#include "StdAfx.h"
|
||||
|
||||
using namespace IceCore;
|
||||
|
||||
@@ -46,6 +30,7 @@ using namespace IceCore;
|
||||
#ifdef CONTAINER_STATS
|
||||
udword Container::mNbContainers = 0;
|
||||
udword Container::mUsedRam = 0;
|
||||
LinkedList Container::mContainers;
|
||||
#endif
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
@@ -58,6 +43,7 @@ Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGr
|
||||
#ifdef CONTAINER_STATS
|
||||
mNbContainers++;
|
||||
mUsedRam+=sizeof(Container);
|
||||
mContainers.AddElem(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -66,11 +52,13 @@ Container::Container() : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGr
|
||||
* Constructor. Also allocates a given number of entries.
|
||||
*/
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
Container::Container(udword size, float growth_factor) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null), mGrowthFactor(growth_factor)
|
||||
Container::Container(udword size, float growth_factor) : mMaxNbEntries(0), mCurNbEntries(0), mEntries(null)
|
||||
{
|
||||
SetGrowthFactor(growth_factor);
|
||||
#ifdef CONTAINER_STATS
|
||||
mNbContainers++;
|
||||
mUsedRam+=sizeof(Container);
|
||||
mContainers.AddElem(this);
|
||||
#endif
|
||||
SetSize(size);
|
||||
}
|
||||
@@ -85,6 +73,7 @@ Container::Container(const Container& object) : mMaxNbEntries(0), mCurNbEntries(
|
||||
#ifdef CONTAINER_STATS
|
||||
mNbContainers++;
|
||||
mUsedRam+=sizeof(Container);
|
||||
mContainers.AddElem(this);
|
||||
#endif
|
||||
*this = object;
|
||||
}
|
||||
@@ -100,9 +89,25 @@ Container::~Container()
|
||||
#ifdef CONTAINER_STATS
|
||||
mNbContainers--;
|
||||
mUsedRam-=GetUsedRam();
|
||||
mContainers.RemElem(this);
|
||||
#endif
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Initializes the container so that it uses an external memory buffer. The container doesn't own the memory, resizing is disabled.
|
||||
* \param max_entries [in] max number of entries in the container
|
||||
* \param entries [in] external memory buffer
|
||||
*/
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void Container::InitSharedBuffers(udword max_entries, udword* entries)
|
||||
{
|
||||
Empty(); // Make sure everything has been released
|
||||
mMaxNbEntries = max_entries;
|
||||
mEntries = entries;
|
||||
mGrowthFactor = -1.0f; // Negative growth ==> resize is disabled
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Clears the container. All stored values are deleted, and it frees used ram.
|
||||
@@ -115,7 +120,7 @@ Container& Container::Empty()
|
||||
#ifdef CONTAINER_STATS
|
||||
mUsedRam-=mMaxNbEntries*sizeof(udword);
|
||||
#endif
|
||||
DELETEARRAY(mEntries);
|
||||
if(mGrowthFactor>=0.0f) ICE_FREE(mEntries); // Release memory if we own it
|
||||
mCurNbEntries = mMaxNbEntries = 0;
|
||||
return *this;
|
||||
}
|
||||
@@ -129,6 +134,13 @@ Container& Container::Empty()
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
bool Container::Resize(udword needed)
|
||||
{
|
||||
// Check growth is allowed
|
||||
if(mGrowthFactor<=0.0f)
|
||||
{
|
||||
ASSERT(!"Invalid operation - trying to resize a static buffer!");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
// Subtract previous amount of bytes
|
||||
mUsedRam-=mMaxNbEntries*sizeof(udword);
|
||||
@@ -139,7 +151,7 @@ bool Container::Resize(udword needed)
|
||||
if(mMaxNbEntries<mCurNbEntries + needed) mMaxNbEntries = mCurNbEntries + needed;
|
||||
|
||||
// Get some bytes for new entries
|
||||
udword* NewEntries = new udword[mMaxNbEntries];
|
||||
udword* NewEntries = (udword*)ICE_ALLOC(sizeof(udword)*mMaxNbEntries);
|
||||
CHECKALLOC(NewEntries);
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
@@ -151,7 +163,7 @@ bool Container::Resize(udword needed)
|
||||
if(mCurNbEntries) CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
|
||||
|
||||
// Delete old data
|
||||
DELETEARRAY(mEntries);
|
||||
ICE_FREE(mEntries);
|
||||
|
||||
// Assign new pointer
|
||||
mEntries = NewEntries;
|
||||
@@ -178,7 +190,7 @@ bool Container::SetSize(udword nb)
|
||||
mMaxNbEntries = nb;
|
||||
|
||||
// Get some bytes for new entries
|
||||
mEntries = new udword[mMaxNbEntries];
|
||||
mEntries = (udword*)ICE_ALLOC(sizeof(udword)*mMaxNbEntries);
|
||||
CHECKALLOC(mEntries);
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
@@ -196,6 +208,13 @@ bool Container::SetSize(udword nb)
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
bool Container::Refit()
|
||||
{
|
||||
// Check refit is allowed
|
||||
if(mGrowthFactor<=0.0f)
|
||||
{
|
||||
ASSERT(!"Invalid operation - trying to refit a static buffer!");
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
// Subtract previous amount of bytes
|
||||
mUsedRam-=mMaxNbEntries*sizeof(udword);
|
||||
@@ -206,7 +225,7 @@ bool Container::Refit()
|
||||
if(!mMaxNbEntries) return false;
|
||||
|
||||
// Get just enough bytes
|
||||
udword* NewEntries = new udword[mMaxNbEntries];
|
||||
udword* NewEntries = (udword*)ICE_ALLOC(sizeof(udword)*mMaxNbEntries);
|
||||
CHECKALLOC(NewEntries);
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
@@ -218,7 +237,7 @@ bool Container::Refit()
|
||||
CopyMemory(NewEntries, mEntries, mCurNbEntries*sizeof(udword));
|
||||
|
||||
// Delete old data
|
||||
DELETEARRAY(mEntries);
|
||||
ICE_FREE(mEntries);
|
||||
|
||||
// Assign new pointer
|
||||
mEntries = NewEntries;
|
||||
@@ -226,6 +245,36 @@ bool Container::Refit()
|
||||
return true;
|
||||
}
|
||||
|
||||
// Same as Refit but more efficient
|
||||
bool Container::Shrink()
|
||||
{
|
||||
if(!mEntries) return false;
|
||||
if(!mCurNbEntries)
|
||||
{
|
||||
Empty();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Try to shrink the pointer
|
||||
if(!GetAllocator()->shrink(mEntries, sizeof(udword)*mCurNbEntries))
|
||||
return false;
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
// Subtract previous amount of bytes
|
||||
mUsedRam-=mMaxNbEntries*sizeof(udword);
|
||||
#endif
|
||||
|
||||
// Get just enough entries
|
||||
mMaxNbEntries = mCurNbEntries;
|
||||
|
||||
#ifdef CONTAINER_STATS
|
||||
// Add current amount of bytes
|
||||
mUsedRam+=mMaxNbEntries*sizeof(udword);
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/**
|
||||
* Checks whether the container already contains a given value.
|
||||
@@ -353,6 +402,19 @@ udword Container::GetUsedRam() const
|
||||
return sizeof(Container) + mMaxNbEntries * sizeof(udword);
|
||||
}
|
||||
|
||||
float Container::GetGrowthFactor() const
|
||||
{
|
||||
return mGrowthFactor;
|
||||
}
|
||||
|
||||
void Container::SetGrowthFactor(float growth)
|
||||
{
|
||||
// Negative growths are reserved for internal usages
|
||||
if(growth<0.0f) growth = 0.0f;
|
||||
mGrowthFactor = growth;
|
||||
}
|
||||
|
||||
//! Operator for "Container A = Container B"
|
||||
void Container::operator=(const Container& object)
|
||||
{
|
||||
SetSize(object.GetNbEntries());
|
||||
|
||||
Reference in New Issue
Block a user