home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / C_C++ / VisualProgrammingArmoury / data1.cab / MyFileGroup / INCLUDE / Array.hpp next >
Encoding:
C/C++ Source or Header  |  1999-06-23  |  15.0 KB  |  615 lines

  1. #ifndef _INC_ARRAY_HPP
  2. #define _INC_ARRAY_HPP
  3. #ifdef __GNUG__
  4.  
  5. #ifdef BUILD_TCCORE
  6. #define TCCORELIB    dllexport
  7. #else
  8. #ifdef BUILD_LOCAL
  9. #define TCCORELIB
  10. #else
  11. #define TCCORELIB    dllimport
  12. #endif
  13. #endif
  14.  
  15. #else
  16.  
  17. #ifdef BUILD_TCCORE
  18. #define TCCORELIB    __declspec(dllexport)
  19. #else
  20. #ifdef BUILD_LOCAL
  21. #define TCCORELIB
  22. #else
  23. #define TCCORELIB    __declspec(dllimport)
  24. #endif
  25. #endif
  26.  
  27. #endif
  28.  
  29. // **********************************************************************
  30. class TCCORELIB TC_C_Array 
  31. {
  32. private:  char* pData ;
  33. private:  int m_CountUsed ;
  34. private:  int m_CountAlloc ;
  35. private:  int m_ItemSize ;
  36. private:  int m_Grow ;
  37. private:  void _Realloc (int new_num)  ;
  38. public:  void _ReplaceItem (int idx,void*ptr)  ;
  39. public:  void _AddMore (void *ptr,int add_itms)  ;
  40. public:  void _SetBound (int n_items)  ;
  41. public:  void _AddPtr (void *ptr)  ;
  42. public:  int _IndexOfPtr (void *p)  ;
  43. public:  int _InsertPtr (int idx, void *ptr)  ;
  44. public:  void _Remove (int idx)  ;
  45. public:  void _Truncate ()  ;
  46. public:   TC_C_Array (int item_size)  ;
  47. public: virtual  ~TC_C_Array ()  ;
  48. public:  void SetGrow (int grow)  ;
  49. public:  BOOL Swap (int dst,int src,int num=1)  ;
  50. public:  BOOL ValidIndex (int idx) 
  51. {
  52. return ( ((UINT)idx) < (UINT)m_CountUsed );
  53. } // end of func
  54. // **********************************************************************
  55.  
  56. public:  char* _PData () 
  57. {
  58. return pData;
  59. } // end of func
  60. // **********************************************************************
  61.  
  62. public:  int Count () const
  63. {
  64. return m_CountUsed;
  65. } // end of func
  66. // **********************************************************************
  67.  
  68. public:  int SizeOfItem ()  ;
  69.  
  70. }; // end of class TC_C_Array
  71.  
  72. // **********************************************************************
  73.  
  74. // **********************************************************************
  75.  template <class T>
  76. class TC_TArrayPTR 
  77.     : public TC_C_Array
  78. {
  79. public:  BOOL bDeleteAll ;
  80. public:   TC_TArrayPTR () 
  81. : TC_C_Array( sizeof(void*) )
  82. {
  83. bDeleteAll = 0;
  84. } // end of func
  85. // **********************************************************************
  86.  
  87. public: virtual  ~TC_TArrayPTR () 
  88. {
  89. if( bDeleteAll ) DeleteAll();
  90. } // end of func
  91. // **********************************************************************
  92.  
  93. public:  int IndexOf (T *ptr,int start=0) 
  94. {
  95. for( int idx=start; idx < Count(); idx++ )
  96.  if( Get(idx) == ptr )
  97.   return idx;
  98. return -1;
  99. } // end of func
  100. // **********************************************************************
  101.  
  102. public:  int Add (T *ptr) 
  103. {
  104. _AddPtr( (ptr) ? (void*)&ptr : 0  );
  105. return Count()-1;
  106. } // end of func
  107. // **********************************************************************
  108.  
  109. public:  int AddUnq (T *ptr) 
  110. {
  111. int idx = IndexOf(ptr);
  112. if( idx >= 0 ) return idx;
  113. Add(ptr);
  114. return Count()-1;
  115. } // end of func
  116. // **********************************************************************
  117.  
  118. public:  int Insert (int idx, T *ptr) 
  119. {
  120. return _InsertPtr( idx, (ptr) ? (void*)&ptr : 0 );
  121. } // end of func
  122. // **********************************************************************
  123.  
  124. public:  T* Get (int idx) 
  125. {
  126. if( ValidIndex(idx) ) return (T*)(((void**)_PData())[idx]);
  127. return 0;
  128. } // end of func
  129. // **********************************************************************
  130.  
  131. public:  void Remove (int idx) 
  132. {
  133. _Remove( idx );
  134. } // end of func
  135. // **********************************************************************
  136.  
  137. public:  void Remove (T *ptr) 
  138. {
  139. _Remove( IndexOf(ptr) );
  140. } // end of func
  141. // **********************************************************************
  142.  
  143. public:  void Replace (int idx, T *ptr) 
  144. {
  145. if( !ValidIndex(idx) ) return;
  146. ((void**)_PData())[idx] = (void*)ptr;
  147. } // end of func
  148. // **********************************************************************
  149.  
  150. public:  void Delete (int idx) 
  151. {
  152. if( !ValidIndex(idx) ) return;
  153. T * ptr = ((T**)_PData())[idx];
  154. if(ptr) delete ptr;
  155. if( ((T**)_PData())[idx] == ptr ) Remove(idx);
  156. } // end of func
  157. // **********************************************************************
  158.  
  159. public:  void Delete (T *ptr) 
  160. {
  161. Delete( IndexOf(ptr) );
  162. } // end of func
  163. // **********************************************************************
  164.  
  165. public:  void DeleteAll () 
  166. {
  167. while( Count() ) Delete( Count()-1 );
  168. } // end of func
  169. // **********************************************************************
  170.  
  171. public:  void Truncate () 
  172. {
  173. _Truncate();
  174. } // end of func
  175. // **********************************************************************
  176.  
  177. public:  T* PLast () 
  178. {
  179. return Get(Count()-1);
  180. } // end of func
  181. // **********************************************************************
  182.  
  183. public:  T& operator [] (int idx) 
  184. {
  185. T * ptr = Get(idx);
  186. assert( "TC_TArrayPTR::operator []()" && ptr != 0 );
  187. return *ptr;
  188. } // end of func
  189. // **********************************************************************
  190.  
  191.  
  192. }; // end of class TC_TArrayPTR
  193.  
  194. // **********************************************************************
  195.  
  196. // **********************************************************************
  197.  template <class T>
  198. class TC_TArrayC 
  199.     : public TC_C_Array
  200. {
  201. public:  BOOL bAutoExpand ;
  202. public:   TC_TArrayC () 
  203. : TC_C_Array( sizeof(T) )
  204. {
  205. bAutoExpand = 0;
  206. } // end of func
  207. // **********************************************************************
  208.  
  209. public:  TC_TArrayC <T> & operator = (const TC_TArrayC <T> & src_) 
  210. {
  211. TC_TArrayC <T> * src = (TC_TArrayC <T> *)&src_;
  212. if( src == this ) return (*this);
  213. Truncate();
  214. _SetBound( src->Count() );
  215. memcpy( _PData(), src->_PData(), Count() * sizeof(T) );
  216. return (*this);
  217. } // end of func
  218. // **********************************************************************
  219.  
  220. public:  T& operator [] (int idx) 
  221. {
  222. if( idx < 0 )
  223. {
  224.     assert("TC_TArrayC::operator []()" && idx >= 0 );
  225.     return ((T*)0)[0];
  226. }
  227.  
  228. if( idx >= Count() )
  229. {
  230.     if( !bAutoExpand )
  231.     {
  232.         assert("TC_TArrayC::operator []()" && idx < Count() );
  233.         return ((T*)0)[0];
  234.     }
  235.     _SetBound(idx+1);
  236. }
  237.  
  238. return ((T*)_PData())[idx];
  239. } // end of func
  240. // **********************************************************************
  241.  
  242. public:  int IndexOf (T val) 
  243. {
  244. return _IndexOfPtr(&val);
  245. /*
  246. for( int ret = 0; ret < Count(); ret++ )
  247. {
  248.     if( ((T*)_PData())[ret] == val ) return ret;
  249.     //if( !memcmp( ((T*)_PData())+ret, &val, sizeof(T) ) ) return ret;
  250. }
  251. return -1;
  252. */
  253. } // end of func
  254. // **********************************************************************
  255.  
  256. public:  T& Last () 
  257. {
  258. assert( "TC_TArrayC::Last()" && Count() > 0 );
  259. return ((T*)_PData())[Count()-1];
  260. } // end of func
  261. // **********************************************************************
  262.  
  263. public:  int Add (T val) 
  264. {
  265. _AddPtr( &val );
  266. return Count()-1;
  267. } // end of func
  268. // **********************************************************************
  269.  
  270. public:  int Add () 
  271. {
  272. _AddPtr( 0 );
  273. return Count()-1;
  274. } // end of func
  275. // **********************************************************************
  276.  
  277. public:  void AddUnq (T val) 
  278. {
  279. if( IndexOf(val) < 0 ) Add(val);
  280. } // end of func
  281. // **********************************************************************
  282.  
  283. public:  int Insert (int idx, T val) 
  284. {
  285. return _InsertPtr( idx, &val );
  286. } // end of func
  287. // **********************************************************************
  288.  
  289. public:  int Insert (int idx) 
  290. {
  291. return _InsertPtr( idx, NULL );
  292. } // end of func
  293. // **********************************************************************
  294.  
  295. public:  void SetVals (T val) 
  296. {
  297. for( int cnt=0; cnt < Count(); cnt++ ) ((T*)_PData())[cnt] = val;
  298. } // end of func
  299. // **********************************************************************
  300.  
  301. public:  void Remove (int idx) 
  302. {
  303. _Remove(idx);
  304. } // end of func
  305. // **********************************************************************
  306.  
  307. public:  void Truncate () 
  308. {
  309. _Truncate();
  310. } // end of func
  311. // **********************************************************************
  312.  
  313. public:  void SetBound (int size) 
  314. {
  315. if( size < 0 ) size = 0;
  316. if( size == Count() ) return;
  317. if( size < Count() )
  318. {
  319.     while( size < Count() ) Remove(Count()-1);
  320.     return;
  321. }
  322.  
  323. while( size > Count() ) Add();
  324. } // end of func
  325. // **********************************************************************
  326.  
  327.  
  328. }; // end of class TC_TArrayC
  329.  
  330. // **********************************************************************
  331. class _TC_CSandwichObject
  332. {
  333. public:
  334. void * ptr;
  335.     _TC_CSandwichObject( void *p ) { ptr = p; }
  336. };
  337.  
  338. inline void * operator new ( size_t, _TC_CSandwichObject & s )
  339. {
  340.     return s.ptr;
  341. }
  342.  
  343. // **********************************************************************
  344.  template <class T>
  345. class TC_TArrayCPP 
  346.     : public TC_C_Array
  347. {
  348. public:  BOOL bAutoExpand ;
  349. public:   TC_TArrayCPP () 
  350. : TC_C_Array( sizeof(T) )
  351. {
  352. bAutoExpand = 0;
  353. } // end of func
  354. // **********************************************************************
  355.  
  356. public:  TC_TArrayCPP <T> & operator = (const TC_TArrayCPP <T> & src_) 
  357. {
  358. TC_TArrayCPP <T> * src = (TC_TArrayCPP <T> *)&src_;
  359. if( src == this ) return (*this);
  360. DeleteAll();
  361.  
  362. for( int cnt=0; cnt < src->Count(); cnt++ )
  363. {
  364.     Add();
  365.     Last() = (*src)[cnt];
  366. }
  367.  
  368. return (*this);
  369. } // end of func
  370. // **********************************************************************
  371.  
  372. public:   ~TC_TArrayCPP () 
  373. {
  374. DeleteAll();
  375. } // end of func
  376. // **********************************************************************
  377.  
  378. public:  T& operator [] (int idx) 
  379. {
  380. if( idx < 0 )
  381. {
  382.     assert("TC_TArrayCPP::operator []()" && idx >= 0 );
  383.     return ((T*)0)[0];
  384. }
  385.  
  386. if( idx >= Count() )
  387. {
  388.     if( !bAutoExpand )
  389.     {
  390.         assert("TC_TArrayCPP::operator []()" && idx < Count() );
  391.         return ((T*)0)[0];
  392.     }
  393.     while( Count() <= idx ) Add();
  394. }
  395.  
  396. return ((T*)_PData())[idx];
  397. } // end of func
  398. // **********************************************************************
  399.  
  400. public:  T& Last () 
  401. {
  402. assert( "TC_TArrayCPP::Last()" && Count() > 0 );
  403. return ((T*)_PData())[Count()-1];
  404. } // end of func
  405. // **********************************************************************
  406.  
  407. public:  int Add () 
  408. {
  409. /*_AddPtr( _GetObject() );
  410. return Count()-1;*/
  411.  
  412. _AddPtr( 0 );
  413. _TC_CSandwichObject d( _PData() + sizeof(T)*(Count()-1) );
  414. new (d) T();
  415. return Count()-1;
  416. } // end of func
  417. // **********************************************************************
  418.  
  419. public:  int Insert (int idx) 
  420. {
  421. /*return _InsertPtr( idx, _GetObject() );*/
  422.  
  423. idx = _InsertPtr( idx, 0 );
  424. _TC_CSandwichObject d( _PData() + sizeof(T)*idx );
  425. new (d) T();
  426. return idx;
  427. } // end of func
  428. // **********************************************************************
  429.  
  430. public:  void Delete (int idx) 
  431. {
  432. if( !ValidIndex(idx) ) return;
  433. /*{ T obj; memcpy( &obj, ((T*)_PData()) + idx, sizeof(T) ); }*/
  434.  
  435. ((T*)(_PData() + idx*sizeof(T))) -> ~T();
  436.  
  437. _Remove(idx);
  438. } // end of func
  439. // **********************************************************************
  440.  
  441. public:  void DeleteAll () 
  442. {
  443. while( Count() ) Delete( Count()-1 );
  444. } // end of func
  445. // **********************************************************************
  446.  
  447. public:  void SetBound (int size) 
  448. {
  449. if( size < 0 ) size = 0;
  450. if( size == Count() ) return;
  451. if( size < Count() )
  452. {
  453.     while( size < Count() ) Delete(Count()-1);
  454.     return;
  455. }
  456.  
  457. while( size > Count() ) Add();
  458. } // end of func
  459. // **********************************************************************
  460.  
  461.  
  462. }; // end of class TC_TArrayCPP
  463.  
  464. // **********************************************************************
  465.  
  466. // **********************************************************************
  467. class TCCORELIB TC_CArrayString 
  468.     : public TC_TArrayCPP <TC_CString>
  469. {
  470. public:  int Add (LPCSTR str)  ;
  471. public:  int Add ()  ;
  472. public:  void Insert (int idx, LPCSTR str)  ;
  473. public:  int Insert (int idx)  ;
  474. public:  int IndexOf (LPCSTR str)  ;
  475. public:  int IndexOf_I (LPCSTR str)  ;
  476. public:  int AddUnq (LPCSTR str)  ;
  477. public:  int AddUnq_I (LPCSTR str)  ;
  478.  
  479. }; // end of class TC_CArrayString
  480.  
  481. // **********************************************************************
  482. typedef TC_TArrayC <signed char>    TCArrayCHAR;
  483. typedef TC_TArrayC <unsigned char>    TCArrayBYTE;
  484.  
  485. typedef TC_TArrayC <int>            TCArrayINT;
  486. typedef TC_TArrayC <unsigned>        TCArrayUINT;
  487.  
  488. typedef TC_TArrayC <short> TCArraySHORT;
  489. typedef TC_TArrayC <unsigned short> TCArrayUSHORT;
  490.  
  491. typedef TC_TArrayC <long> TCArrayLONG;
  492. typedef TC_TArrayC <unsigned long> TCArrayULONG;
  493.  
  494. typedef TC_TArrayC <float> TCArrayFLOAT;
  495. typedef TC_TArrayC <double> TCArrayDOUBLE;
  496.  
  497. typedef TC_TArrayC <char*> TCArrayPCHAR;
  498. typedef TC_TArrayC <void*> TCArrayPVOID;
  499.  
  500.  
  501. #define TCArrayUCHAR    TCArrayBYTE
  502. #define TCArrayDWORD    TCArrayULONG
  503. //**************************************************** void* + void*
  504. struct TC_PtrPtr
  505. {
  506.     void * p1;
  507.     void * p2;
  508.     TC_PtrPtr() { p1=p2=0; }
  509. };
  510. typedef TC_TArrayCPP <TC_PtrPtr> TCArrayPtrPtr;
  511. //**************************************************** Str + Str
  512. struct TC_StrStr
  513. {
  514.     TC_CString s1;
  515.     TC_CString s2;
  516.     TC_StrStr& operator = (const TC_StrStr& src)
  517.     {
  518.         s1 = src.s1;
  519.         s2 = src.s2;
  520.         return *this;
  521.     }
  522. };
  523. typedef TC_TArrayCPP <TC_StrStr> TCArrayStrStr;
  524. //**************************************************** Str + Str + Str
  525. struct TC_StrStrStr
  526. {
  527.     TC_CString s1;
  528.     TC_CString s2;
  529.     TC_CString s3;
  530.     TC_StrStrStr& operator = (const TC_StrStrStr& src)
  531.     {
  532.         s1 = src.s1;
  533.         s2 = src.s2;
  534.         s3 = src.s3;
  535.         return *this;
  536.     }
  537. };
  538. typedef TC_TArrayCPP <TC_StrStrStr> TCArrayStrStrStr;
  539. //**************************************************** Str + Int
  540. struct TC_StrNum
  541. {
  542.     TC_CString str;
  543.     int num;
  544.     TC_StrNum() { num = 0; }
  545.     TC_StrNum& operator = (const TC_StrNum& src)
  546.     {
  547.         str = src.str;
  548.         num = src.num;
  549.         return *this;
  550.     }
  551. };
  552. typedef TC_TArrayCPP <TC_StrNum> TCArrayStrNum;
  553. //**************************************************** Str + Str + Int
  554. struct TC_StrStrNum
  555. {
  556.     TC_CString s1;
  557.     TC_CString s2;
  558.     int  num;
  559.     TC_StrStrNum() { num = 0; }
  560.     TC_StrStrNum& operator = (const TC_StrStrNum& src)
  561.     {
  562.         s1 = src.s1;
  563.         s2 = src.s2;
  564.         num = src.num;
  565.         return *this;
  566.     }
  567. };
  568. typedef TC_TArrayCPP <TC_StrStrNum> TCArrayStrStrNum;
  569. //**************************************************** Str + Int + Int
  570. struct TC_StrNumNum
  571. {
  572.     TC_CString str;
  573.     int  n1;
  574.     int  n2;
  575.     TC_StrNumNum() { n1 = n2 = 0; }
  576.     TC_StrNumNum& operator = (const TC_StrNumNum& src)
  577.     {
  578.         str = src.str;
  579.         n1 = src.n1;
  580.         n2 = src.n2;
  581.         return *this;
  582.     }
  583. };
  584. typedef TC_TArrayCPP <TC_StrNumNum> TCArrayStrNumNum;
  585. //**************************************************** Str + char[]
  586. struct TC_StrArr
  587. {
  588.     TC_CString str;
  589.     TCArrayCHAR arr;
  590.     TC_StrArr& operator = (const TC_StrArr& src)
  591.     {
  592.         str = src.str;
  593.         arr = src.arr;
  594.         return *this;
  595.     }
  596. };
  597. typedef TC_TArrayCPP <TC_StrArr> TCArrayStrArr;
  598. //**************************************************** Str + Str[]
  599. struct TC_StrArrStr
  600. {
  601.     TC_CString str;
  602.     TC_CArrayString as;
  603.     TC_StrArrStr& operator = (const TC_StrArrStr& src)
  604.     {
  605.         str = src.str;
  606.         as = src.as;
  607.         return *this;
  608.     }
  609. };
  610. typedef TC_TArrayCPP <TC_StrArrStr> TCArrayStrArrStr;
  611. //****************************************************
  612. TCCORELIB int tcTokenizeString (LPCSTR src, LPCSTR schr, TC_CArrayString &dst, BOOL trim=FALSE)  ;
  613.  
  614. #endif // _INC_ARRAY_HPP
  615.