/*==LICENSE==*
CyanWorlds.com Engine - MMOG client, server and tools
Copyright (C) 2011 Cyan Worlds, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see .
Additional permissions under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with any of RAD Game Tools Bink SDK, Autodesk 3ds Max SDK,
NVIDIA PhysX SDK, Microsoft DirectX SDK, OpenSSL library, Independent
JPEG Group JPEG library, Microsoft Windows Media SDK, or Apple QuickTime SDK
(or a modified version of those libraries),
containing parts covered by the terms of the Bink SDK EULA, 3ds Max EULA,
PhysX SDK EULA, DirectX SDK EULA, OpenSSL and SSLeay licenses, IJG
JPEG Library README, Windows Media SDK EULA, or QuickTime SDK EULA, the
licensors of this Program grant you additional
permission to convey the resulting work. Corresponding Source for a
non-source form of such a combination shall include the source code for
the parts of OpenSSL and IJG JPEG Library used as well as that of the covered
work.
You can contact Cyan Worlds, Inc. by email legal@cyan.com
or by snail mail at:
Cyan Worlds, Inc.
14617 N Newport Hwy
Mead, WA 99021
*==LICENSE==*/
#ifndef hsBiExpander_inc
#define hsBiExpander_inc
#include "hsMemory.h"
#include "hsTemplates.h"
///////////////////////////////////////////////////////////////////////////////
////////////// Expander ///////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
template class hsExpander {
private:
int32_t fNumPost;
int32_t fNumPostAlloc;
T* fArray;
int32_t fGrowBy; // default = 0, to double
int32_t fMinSize; // default = 1, min == 1
int32_t fCurrent;
hsExpander(const hsExpander& x); // make it passed as ref or pointer
void IExpand(int newSize);
public:
enum { kMissingIndex = -1 };
hsExpander(int32_t minSize = 1, int32_t growBy = 0);
virtual ~hsExpander();
hsExpander& operator=(const hsExpander&orig) { return Copy(orig); }
hsExpander& Copy(const hsExpander& orig);
void SetCount(int cnt) { if( cnt >= fNumPostAlloc )IExpand(cnt); fNumPost = cnt; }
int32_t GetCount() const { return fNumPost; }
hsBool Empty() const { return GetCount() == 0; }
const T& Get(int32_t index) const;
int32_t Get(int32_t index, int32_t count, T data[]) const;
int32_t Find(const T&) const; // returns kMissingIndex if not found
void SetArray(T* a, int32_t cnt);
T* GetArray() { return fArray; }
T& operator[]( int32_t index );
int32_t Append(const T&); // returns t's index
T* Append();
int32_t Push(const T& t) { return Append(t); }
T* Push() { return Append(); }
T* Top() { return fNumPost ? fArray + fNumPost-1 : nil; }
int32_t Pop(T* t); // returns count of remaining
int32_t Pop();
void Reset(); // clears out everything
T& Head() { return fArray[0]; }
T& Tail() { return fArray[fNumPost-1]; }
T& Current() { return fArray[fCurrent]; }
void First();
void Last();
void Plus() { ++fCurrent; }
hsBool More() { return (fCurrent < fNumPost); }
};
template
hsExpander& hsExpander::Copy(const hsExpander& orig)
{
SetCount(orig.GetCount());
int i;
for( i = 0; i < GetCount(); i++ )
fArray[i] = orig.fArray[i];
return *this;
}
template
void hsExpander::SetArray(T* a, int32_t cnt)
{
delete [] fArray;
if( a )
fArray = a;
fNumPost = fNumPostAlloc = cnt;
}
template
void hsExpander::IExpand(int newSize)
{
int32_t newPostAlloc = fNumPostAlloc;
if( !newPostAlloc )
newPostAlloc++;
while( newPostAlloc <= newSize )
newPostAlloc = fGrowBy ? newPostAlloc + fGrowBy : newPostAlloc << 1;
T* newArray = new T[newPostAlloc];
int i;
for( i = 0; i < fNumPost; i++ )
newArray[i] = fArray[i];
delete [] (fArray);
fArray = newArray;
fNumPostAlloc = newPostAlloc;
}
template
hsExpander::hsExpander(int32_t minSize, int32_t growBy)
{
hsThrowIfBadParam(minSize < 0);
hsThrowIfBadParam(growBy < 0);
fMinSize = minSize+1;
fGrowBy = growBy;
fArray = new T[fMinSize];
fNumPostAlloc = fMinSize;
fNumPost = 0;
}
template
hsExpander::~hsExpander()
{
delete [] fArray;
}
template
void hsExpander::First()
{
fCurrent = 0;
}
template
void hsExpander::Last()
{
fCurrent = fNumPost-1;
}
template
T& hsExpander::operator[]( int32_t index )
{
hsDebugCode(hsThrowIfBadParam((index < 0)||(index >= fNumPost));)
return fArray[index];
}
template
const T& hsExpander::Get( int32_t index ) const
{
hsDebugCode(hsThrowIfBadParam((index < 0)||(index >= fNumPost));)
return fArray[index];
}
template
int32_t hsExpander::Get(int32_t index, int32_t count, T data[]) const
{
if( count > 0 )
{ hsThrowIfNilParam(data);
hsThrowIfBadParam((index < 0)||(index >= fNumPost));
if (index + count > fNumPost)
count = fNumPost - index;
for (int i = 0; i < count; i++)
data[i] = fArray[i + index];
}
return count;
}
template
int32_t hsExpander::Find(const T& obj) const
{
for (int i = 0; i < fNumPost; i++)
if (fArray[i] == obj)
return i;
return kMissingIndex;
}
template
int32_t hsExpander::Append(const T& obj)
{
hsAssert(!(fNumPost >= fNumPostAlloc), "Must be less");
if( fNumPost == fNumPostAlloc-1 )
IExpand(fNumPostAlloc);
fArray[fNumPost] = obj;
return fNumPost++;
}
template
T* hsExpander::Append()
{
hsAssert(!(fNumPost >= fNumPostAlloc), "Must be less");
if( fNumPost == fNumPostAlloc-1 )
IExpand(fNumPostAlloc);
return fArray + fNumPost++;
}
template
int32_t hsExpander::Pop(T*t)
{
hsThrowIfBadParam(Empty());
--fNumPost;
if( t )
*t = fArray[fNumPost];
return GetCount();
}
template
int32_t hsExpander::Pop()
{
hsThrowIfBadParam(Empty());
--fNumPost;
return GetCount();
}
template
void hsExpander::Reset()
{
fNumPost = 0;
}
///////////////////////////////////////////////////////////////////////////////
////////////// BiExpander /////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
template class hsBiExpander {
private:
int32_t fNumPre;
int32_t fNumPost;
int32_t fNumPreAlloc;
int32_t fNumPostAlloc;
T* fArray;
int32_t fGrowBy; // default = 0, to double
int32_t fMinSize; // default = 1, min == 1
int32_t fCurrent;
hsBiExpander& operator=(const hsBiExpander&); // don't allow assignment
hsBiExpander(const hsBiExpander&); // make it passed as ref or pointer
void IExpand(int newSize, hsBool towardEnd = true);
public:
enum { kMissingIndex = -1 };
hsBiExpander(int32_t minSize = 1, int32_t growBy = 0);
virtual ~hsBiExpander();
int32_t GetFirst() const { return -fNumPre; }
int32_t GetCount() const { return fNumPre + fNumPost; }
hsBool Empty() const { return GetCount() == 0; }
const T& Get(int32_t index) const;
int32_t Get(int32_t index, int32_t count, T data[]) const;
int32_t Find(const T&) const; // returns kMissingIndex if not found
void SetArray(T* a, int32_t cnt, int32_t numPre=0);
T** GetArray() { return fArray - fNumPre; }
T& operator[]( int32_t index );
T* Append(); // returns t's index
T* Push(); // returns t's index
int32_t Append(const T&); // returns t's index
int32_t Push(const T&); // returns t's index
int32_t Pop(T*t = nil) { return PopHead(t); } // returns count of remaining
int32_t PopHead(T*t = nil); // returns count of remaining
int32_t PopTail(T*t = nil); // returns count of remaining
void Reset(); // clears out everything
T& Head() { return fArray[-fNumPre]; }
T& Tail() { return fArray[fNumPost-1]; }
T& Current() { return fArray[fCurrent]; }
void First();
void Last();
void Plus() { ++fCurrent; }
void Minus() { --fCurrent; }
hsBool More() { return (fCurrent < fNumPost)&&(fCurrent >= -fNumPre); }
};
template
void hsBiExpander::SetArray(T* a, int32_t cnt, int32_t numPre)
{
if( !numPre )
Reset();
else
{
fNumPreAlloc = fNumPre = numPre;
fNumPostAlloc = fNumPost = cnt - numPre;
fArray = a + numPre;
}
}
template
void hsBiExpander::IExpand(int newSize, hsBool towardEnd)
{
int32_t newPreAlloc = fNumPreAlloc;
int32_t newPostAlloc = fNumPostAlloc;
if( towardEnd )
{
if( !newPostAlloc )
newPostAlloc++;
while( newPostAlloc <= newSize )
newPostAlloc = fGrowBy ? newPostAlloc + fGrowBy : newPostAlloc << 1;
}
else
{
if( !newPreAlloc )
newPreAlloc++;
while( newPreAlloc <= newSize )
newPreAlloc = fGrowBy ? newPreAlloc + fGrowBy : newPreAlloc << 1;
}
T* newArray = new T[newPreAlloc + newPostAlloc];
newArray += newPreAlloc;
int i;
for( i = -fNumPre; i < fNumPost; i++ )
newArray[i] = fArray[i];
// HSMemory::BlockMove(fArray-fNumPre, newArray-fNumPre,
// (fNumPre+fNumPost)*sizeof(*fArray));
delete [] (fArray-fNumPreAlloc);
fArray = newArray;
fNumPreAlloc = newPreAlloc;
fNumPostAlloc = newPostAlloc;
}
template
hsBiExpander::hsBiExpander(int32_t minSize, int32_t growBy)
{
hsThrowIfBadParam(minSize < 0);
hsThrowIfBadParam(growBy < 0);
fMinSize = minSize+1;
fGrowBy = growBy;
fArray = new T[fMinSize << 1];
fNumPreAlloc = fNumPostAlloc = fMinSize;
fArray += fNumPreAlloc;
fNumPre = fNumPost = 0;
}
template
hsBiExpander::~hsBiExpander()
{
delete [] (fArray - fNumPreAlloc);
}
template
void hsBiExpander::First()
{
fCurrent = -fNumPre;
}
template
void hsBiExpander::Last()
{
fCurrent = fNumPost-1;
}
template
T& hsBiExpander::operator[]( int32_t index )
{
hsDebugCode(hsThrowIfBadParam((index < -fNumPre)||(index >= fNumPost));)
return fArray[index];
}
template
const T& hsBiExpander::Get( int32_t index ) const
{
hsDebugCode(hsThrowIfBadParam((index < -fNumPre)||(index >= fNumPost));)
return fArray[index];
}
template
int32_t hsBiExpander::Get(int32_t index, int32_t count, T data[]) const
{
if( count > 0 )
{ hsThrowIfNilParam(data);
hsThrowIfBadParam((index < -fNumPre)||(index >= fNumPost));
if (index + count > fNumPost)
count = fNumPost - index;
for (int i = 0; i < count; i++)
data[i] = fArray[i + index];
}
return count;
}
template
int32_t hsBiExpander::Find(const T& obj) const
{
for (int i = -fNumPre; i < fNumPost; i++)
if (fArray[i] == obj)
return i;
return kMissingIndex;
}
template
T* hsBiExpander::Append()
{
hsAssert(!(fNumPost >= fNumPostAlloc), "Must be less");
if( fNumPost == fNumPostAlloc-1 )
IExpand(fNumPostAlloc, true);
return fArray + fNumPost++;
}
template
T* hsBiExpander::Push()
{
hsAssert(!(fNumPre >= fNumPreAlloc), "Must be less");
if( ++fNumPre == fNumPreAlloc )
IExpand(fNumPreAlloc, false);
return fArray - fNumPre;
}
template
int32_t hsBiExpander::Append(const T& obj)
{
hsAssert(!(fNumPost >= fNumPostAlloc), "Must be less");
if( fNumPost == fNumPostAlloc-1 )
IExpand(fNumPostAlloc, true);
fArray[fNumPost] = obj;
return fNumPost++;
}
template
int32_t hsBiExpander::Push(const T& obj)
{
hsAssert(!(fNumPre >= fNumPreAlloc), "Must be less");
if( ++fNumPre == fNumPreAlloc )
IExpand(fNumPreAlloc, false);
fArray[-fNumPre] = obj;
return -fNumPre;
}
template
int32_t hsBiExpander::PopHead(T*t)
{
hsThrowIfBadParam(Empty());
if( t )
*t = fArray[-fNumPre];
--fNumPre;
return GetCount();
}
template
int32_t hsBiExpander::PopTail(T*t)
{
hsThrowIfBadParam(Empty());
--fNumPost;
if( t )
*t = fArray[fNumPost];
return GetCount();
}
template
void hsBiExpander::Reset()
{
fNumPre = fNumPost = 0;
}
#endif // hsBiExpander_inc