home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2000 February
/
PCWorld_2000-02_cd.bin
/
Software
/
TemaCD
/
tcvpa
/
data1.cab
/
MyFileGroup
/
INCLUDE
/
Array.hpp
next >
Wrap
C/C++ Source or Header
|
1999-06-03
|
15KB
|
599 lines
#ifndef _INC_ARRAY_HPP
#define _INC_ARRAY_HPP
#ifdef BUILD_TCCORE
#define TCCORELIB __declspec(dllexport)
#else
#ifdef BUILD_LOCAL
#define TCCORELIB
#else
#define TCCORELIB __declspec(dllimport)
#endif
#endif
// **********************************************************************
class TCCORELIB TC_C_Array
{
private: char* pData ;
private: int m_CountUsed ;
private: int m_CountAlloc ;
private: int m_ItemSize ;
private: int m_Grow ;
private: void _Realloc (int new_num) ;
public: void _ReplaceItem (int idx,void*ptr) ;
public: void _AddMore (void *ptr,int add_itms) ;
public: void _SetBound (int n_items) ;
public: void _AddPtr (void *ptr) ;
public: int _IndexOfPtr (void *p) ;
public: int _InsertPtr (int idx, void *ptr) ;
public: void _Remove (int idx) ;
public: void _Truncate () ;
public: TC_C_Array (int item_size) ;
public: virtual ~TC_C_Array () ;
public: void SetGrow (int grow) ;
public: BOOL Swap (int dst,int src,int num=1) ;
public: BOOL ValidIndex (int idx)
{
return ( ((UINT)idx) < (UINT)m_CountUsed );
} // end of func
// **********************************************************************
public: char* _PData ()
{
return pData;
} // end of func
// **********************************************************************
public: int Count () const
{
return m_CountUsed;
} // end of func
// **********************************************************************
public: int SizeOfItem () ;
}; // end of class TC_C_Array
// **********************************************************************
// **********************************************************************
template <class T>
class TC_TArrayPTR
: public TC_C_Array
{
public: BOOL bDeleteAll ;
public: TC_TArrayPTR ()
: TC_C_Array( sizeof(void*) )
{
bDeleteAll = 0;
} // end of func
// **********************************************************************
public: virtual ~TC_TArrayPTR ()
{
if( bDeleteAll ) DeleteAll();
} // end of func
// **********************************************************************
public: int IndexOf (T *ptr,int start=0)
{
for( int idx=start; idx < Count(); idx++ )
if( Get(idx) == ptr )
return idx;
return -1;
} // end of func
// **********************************************************************
public: int Add (T *ptr)
{
_AddPtr( (ptr) ? (void*)&ptr : 0 );
return Count()-1;
} // end of func
// **********************************************************************
public: int AddUnq (T *ptr)
{
int idx = IndexOf(ptr);
if( idx >= 0 ) return idx;
Add(ptr);
return Count()-1;
} // end of func
// **********************************************************************
public: int Insert (int idx, T *ptr)
{
return _InsertPtr( idx, (ptr) ? (void*)&ptr : 0 );
} // end of func
// **********************************************************************
public: T* Get (int idx)
{
if( ValidIndex(idx) ) return (T*)(((void**)_PData())[idx]);
return 0;
} // end of func
// **********************************************************************
public: void Remove (int idx)
{
_Remove( idx );
} // end of func
// **********************************************************************
public: void Remove (T *ptr)
{
_Remove( IndexOf(ptr) );
} // end of func
// **********************************************************************
public: void Replace (int idx, T *ptr)
{
if( !ValidIndex(idx) ) return;
((void**)_PData())[idx] = (void*)ptr;
} // end of func
// **********************************************************************
public: void Delete (int idx)
{
if( !ValidIndex(idx) ) return;
T * ptr = ((T**)_PData())[idx];
if(ptr) delete ptr;
if( ((T**)_PData())[idx] == ptr ) Remove(idx);
} // end of func
// **********************************************************************
public: void Delete (T *ptr)
{
Delete( IndexOf(ptr) );
} // end of func
// **********************************************************************
public: void DeleteAll ()
{
while( Count() ) Delete( Count()-1 );
} // end of func
// **********************************************************************
public: void Truncate ()
{
_Truncate();
} // end of func
// **********************************************************************
public: T* PLast ()
{
return Get(Count()-1);
} // end of func
// **********************************************************************
public: T& operator [] (int idx)
{
T * ptr = Get(idx);
assert( "TC_TArrayPTR::operator []()" && ptr != 0 );
return *ptr;
} // end of func
// **********************************************************************
}; // end of class TC_TArrayPTR
// **********************************************************************
// **********************************************************************
template <class T>
class TC_TArrayC
: public TC_C_Array
{
public: BOOL bAutoExpand ;
public: TC_TArrayC ()
: TC_C_Array( sizeof(T) )
{
bAutoExpand = 0;
} // end of func
// **********************************************************************
public: TC_TArrayC <T> & operator = (const TC_TArrayC <T> & src_)
{
TC_TArrayC <T> * src = (TC_TArrayC <T> *)&src_;
if( src == this ) return (*this);
Truncate();
_SetBound( src->Count() );
memcpy( _PData(), src->_PData(), Count() * sizeof(T) );
return (*this);
} // end of func
// **********************************************************************
public: T& operator [] (int idx)
{
if( idx < 0 )
{
assert("TC_TArrayC::operator []()" && idx >= 0 );
return ((T*)0)[0];
}
if( idx >= Count() )
{
if( !bAutoExpand )
{
assert("TC_TArrayC::operator []()" && idx < Count() );
return ((T*)0)[0];
}
_SetBound(idx+1);
}
return ((T*)_PData())[idx];
} // end of func
// **********************************************************************
public: int IndexOf (T val)
{
return _IndexOfPtr(&val);
/*
for( int ret = 0; ret < Count(); ret++ )
{
if( ((T*)_PData())[ret] == val ) return ret;
//if( !memcmp( ((T*)_PData())+ret, &val, sizeof(T) ) ) return ret;
}
return -1;
*/
} // end of func
// **********************************************************************
public: T& Last ()
{
assert( "TC_TArrayC::Last()" && Count() > 0 );
return ((T*)_PData())[Count()-1];
} // end of func
// **********************************************************************
public: int Add (T val)
{
_AddPtr( &val );
return Count()-1;
} // end of func
// **********************************************************************
public: int Add ()
{
_AddPtr( 0 );
return Count()-1;
} // end of func
// **********************************************************************
public: void AddUnq (T val)
{
if( IndexOf(val) < 0 ) Add(val);
} // end of func
// **********************************************************************
public: int Insert (int idx, T val)
{
return _InsertPtr( idx, &val );
} // end of func
// **********************************************************************
public: int Insert (int idx)
{
return _InsertPtr( idx, NULL );
} // end of func
// **********************************************************************
public: void SetVals (T val)
{
for( int cnt=0; cnt < Count(); cnt++ ) ((T*)_PData())[cnt] = val;
} // end of func
// **********************************************************************
public: void Remove (int idx)
{
_Remove(idx);
} // end of func
// **********************************************************************
public: void Truncate ()
{
_Truncate();
} // end of func
// **********************************************************************
public: void SetBound (int size)
{
if( size < 0 ) size = 0;
if( size == Count() ) return;
if( size < Count() )
{
while( size < Count() ) Remove(Count()-1);
return;
}
while( size > Count() ) Add();
} // end of func
// **********************************************************************
}; // end of class TC_TArrayC
// **********************************************************************
class _TC_CSandwichObject
{
public:
void * ptr;
_TC_CSandwichObject( void *p ) { ptr = p; }
};
inline void * operator new ( size_t, _TC_CSandwichObject & s )
{
return s.ptr;
}
// **********************************************************************
template <class T>
class TC_TArrayCPP
: public TC_C_Array
{
public: BOOL bAutoExpand ;
public: TC_TArrayCPP ()
: TC_C_Array( sizeof(T) )
{
bAutoExpand = 0;
} // end of func
// **********************************************************************
public: TC_TArrayCPP <T> & operator = (const TC_TArrayCPP <T> & src_)
{
TC_TArrayCPP <T> * src = (TC_TArrayCPP <T> *)&src_;
if( src == this ) return (*this);
DeleteAll();
for( int cnt=0; cnt < src->Count(); cnt++ )
{
Add();
Last() = (*src)[cnt];
}
return (*this);
} // end of func
// **********************************************************************
public: ~TC_TArrayCPP ()
{
DeleteAll();
} // end of func
// **********************************************************************
public: T& operator [] (int idx)
{
if( idx < 0 )
{
assert("TC_TArrayCPP::operator []()" && idx >= 0 );
return ((T*)0)[0];
}
if( idx >= Count() )
{
if( !bAutoExpand )
{
assert("TC_TArrayCPP::operator []()" && idx < Count() );
return ((T*)0)[0];
}
while( Count() <= idx ) Add();
}
return ((T*)_PData())[idx];
} // end of func
// **********************************************************************
public: T& Last ()
{
assert( "TC_TArrayCPP::Last()" && Count() > 0 );
return ((T*)_PData())[Count()-1];
} // end of func
// **********************************************************************
public: int Add ()
{
/*_AddPtr( _GetObject() );
return Count()-1;*/
_AddPtr( 0 );
_TC_CSandwichObject d( _PData() + sizeof(T)*(Count()-1) );
new (d) T();
return Count()-1;
} // end of func
// **********************************************************************
public: int Insert (int idx)
{
/*return _InsertPtr( idx, _GetObject() );*/
idx = _InsertPtr( idx, 0 );
_TC_CSandwichObject d( _PData() + sizeof(T)*idx );
new (d) T();
return idx;
} // end of func
// **********************************************************************
public: void Delete (int idx)
{
if( !ValidIndex(idx) ) return;
/*{ T obj; memcpy( &obj, ((T*)_PData()) + idx, sizeof(T) ); }*/
((T*)(_PData() + idx*sizeof(T))) -> ~T();
_Remove(idx);
} // end of func
// **********************************************************************
public: void DeleteAll ()
{
while( Count() ) Delete( Count()-1 );
} // end of func
// **********************************************************************
public: void SetBound (int size)
{
if( size < 0 ) size = 0;
if( size == Count() ) return;
if( size < Count() )
{
while( size < Count() ) Delete(Count()-1);
return;
}
while( size > Count() ) Add();
} // end of func
// **********************************************************************
}; // end of class TC_TArrayCPP
// **********************************************************************
// **********************************************************************
class TCCORELIB TC_CArrayString
: public TC_TArrayCPP <TC_CString>
{
public: int Add (LPCSTR str) ;
public: int Add () ;
public: void Insert (int idx, LPCSTR str) ;
public: int Insert (int idx) ;
public: int IndexOf (LPCSTR str) ;
public: int IndexOf_I (LPCSTR str) ;
public: int AddUnq (LPCSTR str) ;
public: int AddUnq_I (LPCSTR str) ;
}; // end of class TC_CArrayString
// **********************************************************************
typedef TC_TArrayC <signed char> TCArrayCHAR;
typedef TC_TArrayC <unsigned char> TCArrayBYTE;
typedef TC_TArrayC <int> TCArrayINT;
typedef TC_TArrayC <unsigned> TCArrayUINT;
typedef TC_TArrayC <short> TCArraySHORT;
typedef TC_TArrayC <unsigned short> TCArrayUSHORT;
typedef TC_TArrayC <long> TCArrayLONG;
typedef TC_TArrayC <unsigned long> TCArrayULONG;
typedef TC_TArrayC <float> TCArrayFLOAT;
typedef TC_TArrayC <double> TCArrayDOUBLE;
typedef TC_TArrayC <char*> TCArrayPCHAR;
typedef TC_TArrayC <void*> TCArrayPVOID;
#define TCArrayUCHAR TCArrayBYTE
#define TCArrayDWORD TCArrayULONG
//**************************************************** void* + void*
struct TC_PtrPtr
{
void * p1;
void * p2;
TC_PtrPtr() { p1=p2=0; }
};
typedef TC_TArrayCPP <TC_PtrPtr> TCArrayPtrPtr;
//**************************************************** Str + Str
struct TC_StrStr
{
TC_CString s1;
TC_CString s2;
TC_StrStr& operator = (const TC_StrStr& src)
{
s1 = src.s1;
s2 = src.s2;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrStr> TCArrayStrStr;
//**************************************************** Str + Str + Str
struct TC_StrStrStr
{
TC_CString s1;
TC_CString s2;
TC_CString s3;
TC_StrStrStr& operator = (const TC_StrStrStr& src)
{
s1 = src.s1;
s2 = src.s2;
s3 = src.s3;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrStrStr> TCArrayStrStrStr;
//**************************************************** Str + Int
struct TC_StrNum
{
TC_CString str;
int num;
TC_StrNum() { num = 0; }
TC_StrNum& operator = (const TC_StrNum& src)
{
str = src.str;
num = src.num;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrNum> TCArrayStrNum;
//**************************************************** Str + Str + Int
struct TC_StrStrNum
{
TC_CString s1;
TC_CString s2;
int num;
TC_StrStrNum() { num = 0; }
TC_StrStrNum& operator = (const TC_StrStrNum& src)
{
s1 = src.s1;
s2 = src.s2;
num = src.num;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrStrNum> TCArrayStrStrNum;
//**************************************************** Str + Int + Int
struct TC_StrNumNum
{
TC_CString str;
int n1;
int n2;
TC_StrNumNum() { n1 = n2 = 0; }
TC_StrNumNum& operator = (const TC_StrNumNum& src)
{
str = src.str;
n1 = src.n1;
n2 = src.n2;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrNumNum> TCArrayStrNumNum;
//**************************************************** Str + char[]
struct TC_StrArr
{
TC_CString str;
TCArrayCHAR arr;
TC_StrArr& operator = (const TC_StrArr& src)
{
str = src.str;
arr = src.arr;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrArr> TCArrayStrArr;
//**************************************************** Str + Str[]
struct TC_StrArrStr
{
TC_CString str;
TC_CArrayString as;
TC_StrArrStr& operator = (const TC_StrArrStr& src)
{
str = src.str;
as = src.as;
return *this;
}
};
typedef TC_TArrayCPP <TC_StrArrStr> TCArrayStrArrStr;
//****************************************************
TCCORELIB int tcTokenizeString (LPCSTR src, LPCSTR schr, TC_CArrayString &dst, BOOL trim=FALSE) ;
#endif // _INC_ARRAY_HPP