home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / hpp.z / WVECTOR.HPP < prev    next >
C/C++ Source or Header  |  1996-10-18  |  11KB  |  377 lines

  1. #ifndef _WVECTOR_HPP_INCLUDED
  2. #define _WVECTOR_HPP_INCLUDED
  3.  
  4. #ifndef _WNO_PRAGMA_PUSH
  5. #pragma pack(push,8);
  6. #pragma enum int;
  7. #endif
  8.  
  9. #ifndef _WOBJECT_HPP_INCLUDED
  10. #include "wobject.hpp"
  11. #endif
  12.  
  13. //-----------------------------------------------------------------
  14. //
  15. // WVectorBase
  16. //
  17. // The base class for our simple container class.  It holds a list
  18. // of pointers to WObject.
  19. //
  20. //-----------------------------------------------------------------
  21.  
  22. class WCMCLASS WVectorBase {
  23.  
  24.     public:
  25.  
  26.         WVectorBase( WBool destroyObjects=FALSE );
  27.         virtual ~WVectorBase();
  28.  
  29.         // WObject * operator[]( int index ) const;
  30.  
  31.         WObject *AtIndex( int index ) const;
  32.  
  33.         /**********************************************************
  34.          * Properties
  35.          **********************************************************/
  36.  
  37.         // Count
  38.         //
  39.         // Number of pointers stored.
  40.  
  41.         int GetCount() const { return _count; }
  42.  
  43.         // Size
  44.         //
  45.         // Actual real number of pointers that can be stored.
  46.  
  47.         int GetSize() const { return _size; }
  48.  
  49.         /**********************************************************
  50.          * Methods
  51.          **********************************************************/
  52.  
  53.         // Append
  54.         //
  55.         // Add an object to the end.  Returns the index of the new item.
  56.  
  57.         virtual int Append( WObject * );
  58.  
  59.         // Clear
  60.         //
  61.         // Clears out the list of pointers.  Can optionally call
  62.         // delete for each pointer.
  63.  
  64.         virtual void Clear( WBool forceDelete=FALSE );
  65.  
  66.         // Grow
  67.         //
  68.         // Grows the list by a given amount.
  69.  
  70.         WBool Grow( int growBy=10 );
  71.  
  72.         // Index
  73.         //
  74.         // Returns the index of a given pointer.
  75.  
  76.         virtual int Index( WObject * ) const;
  77.  
  78.         // Insert
  79.         //
  80.         // Inserts an object into a given position.
  81.  
  82.         virtual int Insert( int at, WObject * );
  83.  
  84.         // Remove/RemoveAt
  85.         //
  86.         // Removes an object from the vector without deleting it.
  87.         //
  88.         // Note: In the base class we are calling these "BaseRemove"
  89.         // and "BaseRemoveAt".  This is because the WVector classes
  90.         // needs to define Remove/RemoveAt functions that return
  91.         // different types and this causes problems with forward
  92.         // declarations.
  93.  
  94.         virtual WObject * BaseRemove( WObject * );
  95.         virtual WObject * BaseRemoveAt( int index );
  96.  
  97.         /*********************************************************
  98.          * Other (internal)
  99.          ********************************************************/
  100.  
  101.         int  Count() const;
  102.         int  Size() const;
  103.         void ClearAndDestroy();
  104.         int  InsertAt( int at, WObject *o );
  105.  
  106.     protected:
  107.  
  108.         WBool operator==( const WVectorBase& rhs ) const;
  109.  
  110.         typedef WBool WCMDEF (*WVectorBaseIterateRoutine)( const WObject *,
  111.                                                            void * );
  112.  
  113.         typedef int WCMDEF (*WVectorBaseCompareRoutine)( const void *,
  114.                                                          const void * );
  115.  
  116.         typedef int WCMDEF (*WVectorBaseBSearchRoutine)( const WObject *obj,
  117.                                                          void *userData );
  118.  
  119.         virtual void DoSort( WVectorBaseCompareRoutine routine );
  120.         virtual void DoIterate( WVectorBaseIterateRoutine routine,
  121.                                 void *userData );
  122.         virtual WObject *DoBSearch( WVectorBaseBSearchRoutine routine,
  123.                                 void *userData );
  124.  
  125.         virtual WBool Equal( const WObject * l, const WObject * r ) const;
  126.  
  127.         void Copy( const WVectorBase& rhs );
  128.  
  129.     private:
  130.  
  131.         WVectorBase( const WVectorBase& );             // no implementation
  132.         WVectorBase& operator=( const WVectorBase& );  // no implementation
  133.  
  134.     protected:
  135.  
  136.         WObject **              _objs;
  137.         int                     _count;
  138.         int                     _size;
  139.         WBool                   _destroyObjects;
  140. };
  141.  
  142. //-----------------------------------------------------------------
  143. //
  144. // WVector
  145. //
  146. // Templated class for holding pointers to WObject derived classes.
  147. //
  148. //-----------------------------------------------------------------
  149.  
  150. template<class T>
  151. class WCMCLASS WVector : public WVectorBase {
  152.  
  153.     public:
  154.  
  155.         // default constructor
  156.         //
  157.         // Constructs an empty vector. If the parameter is TRUE,
  158.         // the destructor will call 'delete' on the pointers stored
  159.         // in the vector.
  160.  
  161.         WVector( WBool destroyObjects=FALSE );
  162.  
  163.         // copy constructor
  164.         //
  165.         // Makes a copy of the pointers stored in the source vector.
  166.         // The destructor will not call 'delete' on the objects.
  167.  
  168.         WVector( const WVector<T>& );
  169.  
  170.         // assignment operator
  171.         //
  172.         // Makes a copy of the pointers stored in the source vector.
  173.         // The old pointers stored in this vector are discarded but
  174.         // WILL NOT be deleted.
  175.  
  176.         WVector<T>& operator=( const WVector<T>& );
  177.  
  178.         // operator==
  179.         //
  180.         // Compares the elements of the vectors using the method Equal.
  181.         // Returns true if the vectors have same number of elements
  182.         // and each corresponding pair of elements compares equal.
  183.  
  184.         WBool operator==( const WVector<T>& ) const;
  185.  
  186.         WBool operator!=( const WVector<T>& ) const;
  187.  
  188.         /**************************************************************
  189.          * Overrides
  190.          **************************************************************/
  191.  
  192.         T * operator[]( int index ) const
  193.                 { return (T *)WVectorBase::AtIndex( index ); }
  194.  
  195.         T * Remove( WObject * obj );
  196.  
  197.         T * RemoveAt( int index );
  198.  
  199.     protected:
  200.         virtual WBool Equal( const WObject * lhs, const WObject * rhs ) const;
  201. };
  202.  
  203. template<class T>
  204. WVector<T>::WVector( WBool destroyObjects )
  205.     : WVectorBase( destroyObjects ) {
  206. }
  207.  
  208. template<class T>
  209. WVector<T>::WVector( const WVector<T>& rhs )
  210.     : WVectorBase( false ) {
  211.     Copy( rhs );
  212. }
  213.  
  214. template<class T>
  215. WVector<T>& WVector<T>::operator=( const WVector<T>& rhs ) {
  216.     Copy( rhs );
  217.     return *this;
  218. }
  219.  
  220. template<class T>
  221. WBool WVector<T>::operator==( const WVector<T>& rhs ) const {
  222.     return WVectorBase::operator==( rhs );
  223. }
  224.  
  225. template<class T>
  226. WBool WVector<T>::operator!=( const WVector<T>& rhs ) const {
  227.     return !operator==( rhs );
  228. }
  229.  
  230. template<class T>
  231. WBool WVector<T>::Equal( const WObject * lhs, const WObject * rhs ) const {
  232.     return (WBool)( *(T *)lhs == *(T *)rhs );
  233. }
  234.  
  235. template<class T>
  236. T * WVector<T>::Remove( WObject * obj ) {
  237.     return (T *)WVectorBase::BaseRemove( obj );
  238. }
  239.  
  240. template<class T>
  241. T * WVector<T>::RemoveAt( int index ) {
  242.     return (T *)WVectorBase::BaseRemoveAt( index );
  243. }
  244.  
  245. //-----------------------------------------------------------------
  246. //
  247. // WSortableVector
  248. //
  249. // A sortable version of WVector.
  250. //
  251. //-----------------------------------------------------------------
  252.  
  253. template<class T>
  254. class WCMCLASS WSortableVector : public WVector<T> {
  255.  
  256.     public:
  257.         typedef WBool WCMDEF (*WVectorIterateRoutine )( const T *obj,
  258.                                                         void *userData );
  259.  
  260.         typedef int WCMDEF (*WVectorCompareRoutine)( const T *lhs,
  261.                                                      const T *rhs );
  262.  
  263.         typedef int WCMDEF (*WVectorBSearchRoutine)( const T *obj,
  264.                                                      void *userData );
  265.  
  266.     public:
  267.         WSortableVector( WBool destroyObjects=FALSE );
  268.  
  269.         WSortableVector( const WVector<T>& );
  270.  
  271.         WSortableVector<T>& operator=( const WVector<T>& );
  272.  
  273.         WBool operator==( const WVector<T>& ) const;
  274.  
  275.         WBool operator!=( const WVector<T>& ) const;
  276.  
  277.         /**************************************************************
  278.          * Methods
  279.          **************************************************************/
  280.  
  281.         // BSearch
  282.         //
  283.         // Does a binary search the list (must be sorted).  Pass in
  284.         // a user-defined comparison function, which must return -1, 0
  285.         // or 1 (as with the bsearch function). No default behaviour
  286.         // is supplied.
  287.  
  288.         T * BSearch( WVectorBSearchRoutine routine, void *userData );
  289.  
  290.         // Iterate
  291.         //
  292.         // Iterate through the list, calling a user-defined function
  293.         // each time through.  The user-defined function should return
  294.         // TRUE if the iteration should continue.
  295.  
  296.         void Iterate( WVectorIterateRoutine routine, void *userData=NULL );
  297.  
  298.         // Sort
  299.         //
  300.         // Sorts the vector.  A user-supplied comparison routine can
  301.         // be used (should return -1, 0 or 1 as used with qsort) or
  302.         // if none is provided comparison is done using the
  303.         // CompareToSelf method of the objects.
  304.  
  305.         void Sort( WVectorCompareRoutine routine=NULL );
  306.  
  307.     protected:
  308.         static int DefaultCompare( const T *lhs, const T *rhs );
  309. };
  310.  
  311. template<class T>
  312. WSortableVector<T>::WSortableVector( WBool destroyObjects )
  313.     : WVector( destroyObjects ) {
  314. }
  315.  
  316. template<class T>
  317. WSortableVector<T>::WSortableVector( const WVector<T>& rhs )
  318.     : WVector( rhs ) {
  319. }
  320.  
  321. template<class T>
  322. WSortableVector<T>& WSortableVector<T>::operator=( const WVector<T>& rhs ) {
  323.     Copy( rhs );
  324.     return *this;
  325. }
  326.  
  327. template<class T>
  328. WBool WSortableVector<T>::operator==( const WVector<T>& rhs ) const {
  329.     return WVector<T>::operator==( rhs );
  330. }
  331.  
  332. template<class T>
  333. WBool WSortableVector<T>::operator!=( const WVector<T>& rhs ) const {
  334.     return !operator==( rhs );
  335. }
  336.  
  337. template<class T>
  338. T * WSortableVector<T>::BSearch( WVectorBSearchRoutine routine,
  339.                                  void *userData ) {
  340.     if( !routine ) return NULL;
  341.     return (T *) DoBSearch( (WVectorBaseBSearchRoutine) routine,
  342.                              userData );
  343. }
  344.  
  345. template<class T>
  346. void WSortableVector<T>::Iterate( WVectorIterateRoutine routine,
  347.                                   void *userData ) {
  348.     if( !routine ) return;
  349.     DoIterate( (WVectorBaseIterateRoutine) routine, userData );
  350. }
  351.  
  352. template<class T>
  353. void WSortableVector<T>::Sort( WVectorCompareRoutine routine ) {
  354.     if( !routine ) routine = DefaultCompare;
  355.     DoSort( (WVectorBaseCompareRoutine) routine );
  356. }
  357.  
  358. template<class T>
  359. static int WSortableVector<T>::DefaultCompare( const T *lhp, const T *rhp )
  360. {
  361.     return (*lhp).CompareToSelf( *rhp );
  362. }
  363.  
  364. //
  365. // Declare a basic vector
  366. //
  367.  
  368. extern template WVector<WObject>;
  369.  
  370. #ifndef _WNO_PRAGMA_PUSH
  371. #pragma enum pop;
  372. #pragma pack(pop);
  373. #endif
  374.  
  375. #endif // _WVECTOR_HPP_INCLUDED
  376.  
  377.