⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tvector.h

📁 五行MMORPG引擎系统V1.0
💻 H
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

#ifndef _TVECTOR_H_
#define _TVECTOR_H_

//Includes
#ifndef _PLATFORM_H_
#include "platform/platform.h"
#endif


//-----------------------------------------------------------------------------
// Helper definitions for the vector class.

/// Size of memory blocks to allocate at a time for vectors.
#define VectorBlockSize 16

#ifdef TORQUE_DEBUG_GUARD
extern bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize,
                         const char* fileName,
                         const U32   lineNum);
#else
extern bool VectorResize(U32 *aSize, U32 *aCount, void **arrayPtr, U32 newCount, U32 elemSize);
#endif


/// Use the following macro to bind a vector to a particular line
///  of the owning class for memory tracking purposes
#ifdef TORQUE_DEBUG_GUARD
#define VECTOR_SET_ASSOCIATION(x) x.setFileAssociation(__FILE__, __LINE__)
#else
#define VECTOR_SET_ASSOCIATION(x)
#endif

// =============================================================================
/// A dynamic array class.
///
/// The vector grows as you insert or append
/// elements.  Insertion is fastest at the end of the array.  Resizing
/// of the array can be avoided by pre-allocating space using the
/// reserve() method.
///
/// <b>***WARNING***</b>
///
/// This template does not initialize, construct or destruct any of
/// it's elements.  This means don't use this template for elements
/// (classes) that need these operations.  This template is intended
/// to be used for simple structures that have no constructors or
/// destructors.
///
/// @nosubgrouping
template<class T>
class Vector
{
  protected:
   U32 mElementCount;
   U32 mArraySize;
   T*  mArray;

#ifdef TGE_RPG
	U32 mGrowSize;
#endif

#ifdef TORQUE_DEBUG_GUARD
   const char* mFileAssociation;
   U32         mLineAssociation;
#endif

   bool  resize(U32);

  public:
   Vector(const U32 initialSize = 0);
#ifdef TGE_RPG
   Vector(const U32 initialSize,const U32 growSize);
#endif
   Vector(const U32 initialSize, const char* fileName, const U32 lineNum);
   Vector(const char* fileName, const U32 lineNum);
   Vector(const Vector&);
   ~Vector();

#ifdef TORQUE_DEBUG_GUARD
   void setFileAssociation(const char* file, const U32 line);
#endif

   /// @name STL interface
   /// @{

   typedef T        value_type;
   typedef T&       reference;
   typedef const T& const_reference;

   typedef T*       iterator;
   typedef const T* const_iterator;
   typedef S32    difference_type;
   typedef U32    size_type;

   Vector<T>& operator=(const Vector<T>& p);

   iterator       begin();
   const_iterator begin() const;
   iterator       end();
   const_iterator end() const;

   S32 size() const;
   bool empty() const;

   void insert(iterator, const T&);
   void erase(iterator);

   T&       front();
   const T& front() const;
   T&       back();
   const T& back() const;

   void push_front(const T&);
   void push_back(const T&);
   void pop_front();
   void pop_back();

   T& operator[](U32);
   const T& operator[](U32) const;

   T& operator[](S32 i)              { return operator[](U32(i)); }
   const T& operator[](S32 i ) const { return operator[](U32(i)); }

   void reserve(U32);
   U32 capacity() const;

   /// @}

   /// @name Extended interface
   /// @{
#ifdef TGE_RPG
	U32  search(const T& );
	U32  addExist(const T& dat);
#endif

   U32  memSize() const;
   T*   address() const;
   U32  setSize(U32);
   void increment(U32 = 1);
   void decrement(U32 = 1);
   void insert(U32);
   void erase(U32);
   void erase_fast(U32);
   void erase_fast(iterator);
   void clear();
   void compact();

   T& first();
   T& last();
   const T& first() const;
   const T& last() const;

   void set(void * addr, U32 sz);

   /// Merge another vector into this one.
   ///
   /// @author BJW 8/20/97
   void merge(const Vector& p);

   /// @}
};

template<class T> inline Vector<T>::~Vector()
{
   dFree(mArray);
}

template<class T> inline Vector<T>::Vector(const U32 initialSize)
{
#ifdef TORQUE_DEBUG_GUARD
   mFileAssociation = NULL;
   mLineAssociation = 0;
#endif

   mArray        = 0;
   mElementCount = 0;
   mArraySize    = 0;
#ifdef TGE_RPG
	mGrowSize	  = 1;
#endif
   if(initialSize)
      reserve(initialSize);
}

#ifdef TGE_RPG
template<class T> inline Vector<T>::Vector(const U32 initialSize,const U32 growSize)
{
#ifdef TORQUE_DEBUG_GUARD
   mFileAssociation = NULL;
   mLineAssociation = 0;
#endif

   mArray        = 0;
   mElementCount = 0;
   mArraySize    = 0;
	mGrowSize	  = growSize;
   if(initialSize)
      reserve(initialSize);
}
#endif


template<class T> inline Vector<T>::Vector(const U32 initialSize,
                                           const char* fileName,
                                           const U32   lineNum)
{
#ifdef TORQUE_DEBUG_GUARD
   mFileAssociation = fileName;
   mLineAssociation = lineNum;
#else
   fileName;
   lineNum;
#endif

   mArray        = 0;
   mElementCount = 0;
   mArraySize    = 0;
#ifdef TGE_RPG
	mGrowSize	  = 1;
#endif
   if(initialSize)
      reserve(initialSize);
}

template<class T> inline Vector<T>::Vector(const char* fileName,
                                           const U32   lineNum)
{
#ifdef TORQUE_DEBUG_GUARD
   mFileAssociation = fileName;
   mLineAssociation = lineNum;
#else
   fileName;
   lineNum;
#endif

   mArray        = 0;
   mElementCount = 0;
   mArraySize    = 0;
#ifdef TGE_RPG
	mGrowSize	  = 1;
#endif
}

template<class T> inline Vector<T>::Vector(const Vector& p)
{
#ifdef TORQUE_DEBUG_GUARD
   mFileAssociation = p.mFileAssociation;
   mLineAssociation = p.mLineAssociation;
#endif

#ifdef TGE_RPG
	mGrowSize	  = p.mGrowSize;
#endif
   mArray = 0;
   resize(p.mElementCount);
   if (p.mElementCount)
      dMemcpy(mArray,p.mArray,mElementCount * sizeof(value_type));
}


#ifdef TORQUE_DEBUG_GUARD
template<class T> inline void Vector<T>::setFileAssociation(const char* file,
                                                            const U32   line)
{
   mFileAssociation = file;
   mLineAssociation = line;
}
#endif


#ifdef TGE_RPG
template<class T> inline U32 Vector<T>::search(const T& dat)
{
	for(U32 n=0; n<mElementCount; n++)
	{
		if(dat == mArray[n])
			return n;
	}
	return -1;
}

template<class T> U32 Vector<T>::addExist(const T& dat)
{
	U32 nIndex;
	nIndex = search(dat);
	if(nIndex != -1)
		return nIndex;
	nIndex = size();
	push_back(dat);
	return nIndex;
}

#endif

template<class T> inline U32 Vector<T>::memSize() const
{
   return capacity() * sizeof(T);
}

template<class T> inline T* Vector<T>::address() const
{
   return mArray;
}

template<class T> inline U32 Vector<T>::setSize(U32 size)
{
   if (size > mArraySize)
      resize(size);
   else
      mElementCount = size;
   return mElementCount;
}

template<class T> inline void Vector<T>::increment(U32 delta)
{
   if ((mElementCount += delta) > mArraySize)
      resize(mElementCount);
}

template<class T> inline void Vector<T>::decrement(U32 delta)
{
   if (mElementCount > delta)
      mElementCount -= delta;
   else
      mElementCount = 0;
}

template<class T> inline void Vector<T>::insert(U32 index)
{
   // Assert: index >= 0 && index < mElementCount
#ifdef TGE_RPG
   increment(mGrowSize);
#else
   increment();
#endif
   dMemmove(&mArray[index + 1],
                    &mArray[index],
                    (mElementCount - index - 1) * sizeof(value_type));
}

template<class T> inline void Vector<T>::erase(U32 index)
{
   // Assert: index >= 0 && index < mElementCount
   dMemmove(&mArray[index],
                    &mArray[index + 1],
                    (mElementCount - index - 1) * sizeof(value_type));
   decrement();
}

template<class T> inline void Vector<T>::erase_fast(U32 index)
{
   // CAUTION: this operator does NOT maintain list order
   // Copy the last element into the deleted 'hole' and decrement the
   //   size of the vector.
   // Assert: index >= 0 && index < mElementCount
   if (index < (mElementCount - 1))
      dMemmove(&mArray[index], &mArray[mElementCount - 1], sizeof(value_type));
   decrement();
}

template<class T> inline T& Vector<T>::first()
{
   return mArray[0];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -