#ifndef __javaC_h__ #define __javaC_h__ // #ifndef __NEW ********* 11.27.2k #ifdef WIN32 #include #include // #include #endif #include #include #include #include #include "typedef.h" // *** ???? #include "errormsg.h" void error_mem( int iNewSize ) {}; // *** ???? #define _MALLOC_USE_ /************************************************************************/ #ifndef __NEW #ifndef _CORBA_CLIENT_ // #include "new" inline void *__cdecl operator new(size_t, void *_P) {return (_P);} #else #include "tao/corba.h" #endif #endif /************************************************************************/ #ifndef max //+ template inline const T& max(const T& x, const T& y) { return x > y ? x : y; } #endif #ifndef min //+ template inline const T& min(const T& x, const T& y) { return x < y ? x : y; } #endif #ifndef swap template inline void swap( T& x, T& y) { T tmp=x; x=y; y=tmp; } #endif #ifndef sign template inline int sign(T& x) { return ((x) > 0 ? 1 : (x) < 0 ? -1 : 0); } #endif /************************************************************************/ class deleted { public: virtual ~deleted() {}; }; /************************************************************************/ template inline void ConstructElements(TYPE* pElements, int nCount) { // first do bit-wise zero initialization memset((void*)pElements, 0, nCount * sizeof(TYPE)); // then call the constructor(s) for (; nCount--; pElements++) { new ((void*)pElements) TYPE; } } template inline void DestructElements(TYPE* pElements, int nCount) { // call the destructor(s) for (; nCount--; pElements++) pElements->~TYPE(); } template inline void CopyElements(TYPE* pDest, const TYPE* pSrc, int nCount) { // default is element-copy using assignment while (nCount--) *pDest++ = *pSrc++; } /************************************************************************/ #define ARRAY(x) _array_ /************************************************************************/ template class _array_ { TYPE* s; int realLength; ///////////////////////////////////////////////////////// public: ///////////////////////////////////////////////////////// int length; int len() const { return length; } ///////////////////////////////////////////////////////// // operator TYPE* () { return s; } operator const TYPE* () const { return s; } ///////////////////////////////////////////////////////// BOOL reset( int len); void setSize( int nNewSize ) { reset(nNewSize); } ///////////////////////////////////////////////////////// void reset( const ARRAY(TYPE)& from ) { *this = from; } ///////////////////////////////////////////////////////// void empty() { DestructElements(s, (int)realLength); #ifdef _MALLOC_USE_ if (s) free(s); #else delete [] (char*)s; #endif realLength=length=0; s = ( TYPE* )NULL; } ///////////////////////////////////////////////////////// _array_( int _len=0 ) { realLength=length=0; s=( TYPE* )NULL; reset(_len); } ///////////////////////////////////////////////////////// _array_( const ARRAY(TYPE)& from ) { realLength=length=0; s=( TYPE* )NULL; (*this) += from; } ///////////////////////////////////////////////////////// ~_array_() { empty(); } ///////////////////////////////////////////////////////// void remove( int nIndex ) // remove element s[nIndex] { if ( nIndex >= length ) return; DestructElements( s+nIndex,1); // copy new data from old memcpy(s+nIndex, s+nIndex+1, (length-nIndex-1) * sizeof(TYPE)); length--; ConstructElements(s+length, 1 ); } ///////////////////////////////////////////////////////// ARRAY(TYPE)& operator = ( const ARRAY(TYPE)& from ) { empty(); return (*this) += from; } ///////////////////////////////////////////////////////// ARRAY(TYPE)& operator < ( ARRAY(TYPE)& from ) { empty(); s = from.s; realLength= from.realLength; length = from.length; from.s = ( TYPE* )NULL; from.empty(); return *this; } ///////////////////////////////////////////////////////// TYPE* gets( ) { return s; } ///////////////////////////////////////////////////////// ARRAY(TYPE)& operator += ( const ARRAY(TYPE)& addStr ) { int len1 = length; int len2 = addStr.length; reset(len1 + len2); CopyElements(s+len1, addStr.s, len2 ); return *this; } ///////////////////////////////////////////////////////// ARRAY(TYPE)& operator + ( const ARRAY(TYPE)& add ) const { ARRAY(TYPE)* news = new ARRAY(TYPE)(*this); return *news += add; } ///////////////////////////////////////////////////////// BOOL append( TYPE sym ) { int _len = length; if ( !reset( _len+1 ) ) return false; *(gets()+_len) = sym; return true; } ///////////////////////////////////////////////////////// ARRAY(TYPE)* subArray(int beginIndex, int endIndex ); // [beginIndex, endIndex) /////////////////////////////////////////////////////////////////// void set( TYPE sym ) { for ( int i = length; i-- > 0;) s[i] = sym; } ///////////////////////////////////////////////////////// }; template inline BOOL ARRAY(TYPE)::reset( int _len) { // grow array if ( realLength < _len ) { // heuristically determine growth when nGrowBy == 0 // (this avoids heap fragmentation in many situations) int nGrowBy = 0; if ( realLength > 24 ) { nGrowBy = realLength / 8; nGrowBy = (nGrowBy < 4) ? 4 : ((nGrowBy > 1024) ? 1024 : nGrowBy); } int newLen = max(_len,realLength+nGrowBy); int newSize = newLen * sizeof(TYPE); #ifdef _MALLOC_USE_ s = (TYPE*)(s ? _realloc(s,newSize) : _malloc(newSize)); // construct remaining elements ConstructElements(&s[length], newLen-length ); #else TYPE* pNewData = (TYPE*) new char[newSize]; if ( !pNewData ) error_mem( newSize ); // copy new data from old memcpy(pNewData, s, length * sizeof(TYPE)); // construct remaining elements ConstructElements(&pNewData[length], newLen-length ); // get rid of old stuff (note: no destructors called) delete [] (char*)s; s = pNewData; #endif realLength = newLen; } length = s ? _len : 0; return length==_len ? true : false; } template inline ARRAY(TYPE)* ARRAY(TYPE)::subArray(int beginIndex, int endIndex ) { int _len = length; if ( endIndex > _len ) endIndex = _len; if ( beginIndex < 0 ) beginIndex = 0; int ret_len = endIndex - beginIndex; if ( ret_len < 0 ) ret_len = 0; ARRAY(TYPE)* ret = new ARRAY(TYPE)( ret_len ); CopyElements(ret->gets(),gets()+beginIndex, (int)ret->length ); return ret; } ///////////////////////////////////////////////////////// /************************************************************************/ template inline int __cdecl TypeCompareR(const TYPE *elem1, const TYPE *elem2 ) { return *elem1 - *elem2; } template inline int __cdecl TypeCompareL(const TYPE *elem1, const TYPE *elem2 ) { return *elem2 - *elem1; } template inline void sortR( ARRAY(TYPE)& current ) // make current[i]<=current[j] for i inline void sortL( ARRAY(TYPE)& current ) // make current[i]>=current[j] for i inline void add( ARRAY(TYPE)& current, TYPE value ) { for ( int i=current.length; i-->0; ) { if ( current[i] == value ) return; } current.append( value ); } /************************************************************************/ template class BasicString : ARRAY(TYPE) { public: ///////////////////////////////////////////////////////// int len() const { return ARRAY(TYPE)::len()-1; } ///////////////////////////////////////////////////////// // operator TYPE* () { return (ARRAY(TYPE)&)*this; } operator const TYPE* () const { return *(const ARRAY(TYPE)*)this; } ///////////////////////////////////////////////////////// // TYPE* seq() { return *this; } const TYPE* seq() const { return *this; } ///////////////////////////////////////////////////////// BasicString() : ARRAY(TYPE)(1) { gets()[0] = 0;} BasicString( const BasicString& from ) : ARRAY(TYPE)() { *this = from; } ///////////////////////////////////////////////////////// bool setLen( int _len ) { reset( _len + 1 ); gets()[_len] = 0;; return true; } ///////////////////////////////////////////////////////// void free() { setLen(0); } ///////////////////////////////////////////////////////// BasicString& operator += ( const TYPE* str ) { if ( str ) { int addLen = strlen(str); int oldLen = len(); setLen( oldLen + addLen ); memcpy( gets()+oldLen, str, addLen*sizeof(TYPE) ); } return *this; } ///////////////////////////////////////////////////////// BasicString& operator = ( const TYPE* str ) { setLen( 0 ); (*this) += str; return *this; } ///////////////////////////////////////////////////////// BasicString& set( const TYPE* str, int _len, int from = 0 ) { if ( str ) { setLen( from + _len ); memcpy( gets() + from, str, _len ); } else setLen( from ); return *this; } ///////////////////////////////////////////////////////// void set( int _index, TYPE value ) { // if ( len() < _index ) setLen( _index ); gets()[ _index ] = value; } ///////////////////////////////////////////////////////// BasicString& operator + ( const TYPE* str ) { BasicString news; news = *this; return news += str; } ///////////////////////////////////////////////////////// BOOL append( TYPE sym ) { int l = len(); setLen( l + 1 ); gets()[l] = sym; return true; } ///////////////////////////////////////////////////////// }; /************************************************************************/ typedef BasicString CharString; /************************************************************************/ #endif