Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
DynamicArray.h
Package: LegendOfMir2_Server020418.zip [view]
Upload User: szopptop
Upload Date: 2013-04-23
Package Size: 1047k
Code Size: 3k
Category:
Game Server Simulator
Development Platform:
Visual C++
- #define _MAX_USER_ARRAY 5000
- /*
- typedef struct tag_TDYNAMICARRAY
- {
- CSessionInfo* pInfo;
- } _TDYNAMICARRAY, *_LPTDYNAMICARRAY;
- */
- template <class T> class CWHDynamicArray
- {
- protected:
- T* m_Elements[_MAX_USER_ARRAY];
- BOOL m_IsAvElements[_MAX_USER_ARRAY];
- int m_nLastRemoveIndex;
- int m_nLastInsertIndex;
- public:
- CWHDynamicArray();
- ~CWHDynamicArray();
- int AttachData(T* pElement);
- void DettachData(int nIndex);
- int GetAvailablePosition();
- T* GetEmptyElement(int nIndex);
- BOOL SetEmptyElement(int nIndex, T* pElement);
- __inline T* GetData(int nIndex) { if (nIndex >= 0 && nIndex <= _MAX_USER_ARRAY) return m_Elements[nIndex]; return NULL; }
- };
- template <class T> CWHDynamicArray<T>::CWHDynamicArray()
- {
- m_nLastRemoveIndex = 0;
- m_nLastInsertIndex = 0;
- for (int i = 0; i < _MAX_USER_ARRAY; i++)
- {
- m_Elements[i] = NULL;
- m_IsAvElements[i] = FALSE;
- }
- }
- template <class T> CWHDynamicArray<T>::~CWHDynamicArray()
- {
- for (int i = 0; i < _MAX_USER_ARRAY; i++)
- {
- if (m_Elements[i])
- {
- delete m_Elements[i];
- m_Elements[i] = NULL;
- }
- m_IsAvElements[i] = FALSE;
- }
- }
- template <class T> int CWHDynamicArray<T>::GetAvailablePosition()
- {
- int nIndex = -1;
- nIndex = m_nLastRemoveIndex;
- if (m_IsAvElements[nIndex] == FALSE)
- return nIndex;
- for (int i = m_nLastInsertIndex; i < _MAX_USER_ARRAY; i++)
- {
- if (m_IsAvElements[i] == FALSE)
- return i;
- }
- for (i = 0; i < m_nLastInsertIndex; i++)
- {
- if (m_IsAvElements[i] == FALSE)
- return i;
- }
- return -1;
- }
- template <class T> T* CWHDynamicArray<T>::GetEmptyElement(int nIndex)
- {
- if (m_IsAvElements[nIndex] == FALSE)
- {
- if (!m_Elements[nIndex])
- m_Elements[nIndex] = new T;
- m_IsAvElements[nIndex] = TRUE;
- return m_Elements[nIndex];
- }
- return NULL;
- }
- template <class T> BOOL CWHDynamicArray<T>::SetEmptyElement(int nIndex, T* pElement)
- {
- if (m_IsAvElements[nIndex] == TRUE && m_Elements[nIndex] == pElement)
- {
- m_IsAvElements[nIndex] = FALSE;
- return TRUE;
- }
- return FALSE;
- }
- template <class T> int CWHDynamicArray<T>::AttachData(T* pElement)
- {
- int nIndex = -1;
- nIndex = m_nLastRemoveIndex;
- if (m_Elements[nIndex] == NULL && m_IsAvElements[nIndex] == FALSE)
- {
- m_Elements[nIndex] = pElement;
- m_IsAvElements[nIndex] = TRUE;
- return nIndex;
- }
- for (int i = m_nLastInsertIndex; i < _MAX_USER_ARRAY; i++)
- {
- if (m_Elements[i] == NULL && m_IsAvElements[i] == FALSE)
- {
- m_Elements[i] = pElement;
- m_IsAvElements[i] = TRUE;
- m_nLastInsertIndex = i;
- nIndex = i;
- return nIndex;
- }
- }
- for (i = 0; i < m_nLastInsertIndex; i++)
- {
- if (m_Elements[i] == NULL && m_IsAvElements[i] == FALSE)
- {
- m_Elements[i] = pElement;
- m_IsAvElements[i] = TRUE;
- m_nLastInsertIndex = i;
- nIndex = i;
- return nIndex;
- }
- }
- }
- template <class T> void CWHDynamicArray<T>::DettachData(int nIndex)
- {
- if (nIndex >= 0 && nIndex <= _MAX_USER_ARRAY)
- {
- m_Elements[nIndex] = NULL;
- m_IsAvElements[nIndex] = FALSE;
- m_nLastRemoveIndex = nIndex;
- }
- }