home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 10 / ioProg_10.iso / soft / optima / hpp.z / WVECTOR.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-30  |  10.4 KB  |  369 lines

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