StaticArray.h
上传用户:tt_chan
上传日期:2009-12-03
资源大小:4523k
文件大小:1k
- #pragma once
- template< class T >
- class CStaticArray
- {
- public:
- // 努贰胶 T绰 酒贰 牢磐其捞胶甫 备泅窍咯具 茄促.
- interface IArrayData
- {
- virtual bool IsEmpty() = 0;
- };
- public:
- T * m_pElements;
- int m_nSize;
- int m_nCursor;
- public:
- CStaticArray( int nSize );
- virtual ~CStaticArray();
- int GetFreeKey();
- T & operator []( int nIndex );
- };
- template< class T >
- CStaticArray< T >::CStaticArray( int nSize )
- {
- m_pElements = new T[ nSize ];
- m_nSize = nSize;
- m_nCursor = 0;
- }
- template< class T >
- CStaticArray< T >::~CStaticArray()
- {
- if ( m_pElements )
- delete[] m_pElements;
- }
- template< class T >
- int CStaticArray< T >::GetFreeKey()
- {
- int nFind;
- for ( nFind = m_nCursor; nFind < m_nSize; nFind++ )
- {
- if ( m_pElements[nFind].IsEmpty() )
- {
- m_nCursor++;
- return nFind;
- }
- }
- for ( nFind = 0; nFind < m_nCursor; nFind++ )
- {
- if ( m_pElements[nFind].IsEmpty() )
- {
- m_nCursor++;
- return nFind;
- }
- }
- return -1;
- }
- template< class T >
- T & CStaticArray< T >::operator []( int nIndex )
- {
- return m_pElements[ nIndex ];
- }