home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / h.z / WCVECTOR.H < prev    next >
C/C++ Source or Header  |  1996-07-24  |  11KB  |  366 lines

  1. //
  2. //  wcvector.h   Defines the WATCOM Container Vector Classes
  3. //
  4. //  Copyright by WATCOM International Corp. 1988-1996.  All rights reserved.
  5. //
  6. #ifndef _WCVECTOR_H_INCLUDED
  7. #define _WCVECTOR_H_INCLUDED
  8.  
  9. #ifndef __cplusplus
  10. #error wcvector.h is for use with C++
  11. #endif
  12.  
  13. #ifndef _WCDEFS_H_INCLUDED
  14.  #include <wcdefs.h>
  15. #endif
  16. #ifndef _WCEXCEPT_H_INCLUDED
  17.  #include <wcexcept.h>
  18. #endif
  19. #ifndef _STRING_H_INCLUDED
  20.  #include <string.h>
  21. #endif
  22. #ifndef _WCVBASE_H_INCLUDED
  23.  #include <wcvbase.h>
  24. #endif
  25.  
  26.  
  27. #if defined( new ) && defined( _WNEW_OPERATOR )
  28. #  undef new
  29. #endif
  30. #if defined( delete ) && defined( _WDELETE_OPERATOR )
  31. #  undef delete
  32. #endif
  33.  
  34.  
  35. //
  36. //  This defines a basic Value vector with elements of type <Type>
  37. //
  38.  
  39. template <class Type>
  40. class WCValVector : public WCBareVectorBase<Type> {
  41. public:
  42.     inline WCValVector ( size_t length = 0 )
  43.                         : WCBareVectorBase<Type>( length ) {};
  44.     WCValVector ( size_t, const Type& );
  45.     inline WCValVector ( const WCValVector & orig )
  46.                                         : WCBareVectorBase<Type> ( 0 ) {
  47.         base_construct ( &orig );
  48.     };
  49.  
  50.     inline virtual ~WCValVector() {};
  51.  
  52.     inline size_t length () const {
  53.         return( vector_len );
  54.     };
  55.  
  56.     Type& operator[] ( int index );
  57.  
  58.     const Type& operator[] ( int index ) const;
  59.  
  60.     inline WCValVector & operator= ( const WCValVector & orig ) {
  61.         base_assign( &orig );
  62.         return( *this );
  63.     };
  64. };
  65.  
  66.  
  67. template <class Type>
  68. WCValVector<Type>::WCValVector( size_t length, const Type &init_val )
  69.                         : WCBareVectorBase<Type>( length ) {
  70.     for( int i = 0; i < vector_len; i++ ){
  71.         new( &( vector[ i ] ) ) Type( init_val );
  72.     }
  73.     num_init = length;
  74. };
  75.     
  76.  
  77. //
  78. //  The non-const index operator.  If index is greater than the length of
  79. //  the vector and the resize_required exception is disabled, then attempt to
  80. //  resize the vector so that index is the last element.
  81. //
  82.  
  83. template <class Type>
  84. Type &WCValVector<Type>::operator[] ( int index ) {
  85.     if( index < 0 ) {
  86.         base_throw_index_range();
  87.         index = 0;
  88.     }
  89.     if( index >= num_init ) {
  90.         if( index >= vector_len ){
  91.             // attempt to index an element greater than the vector's length
  92.             base_throw_resize_required();
  93.             if( !resize( index + 1 ) ){
  94.                 // resize to perform request failed.  Try and have at least one
  95.                 // valid entry
  96.                 if( vector_len == 0 ) {
  97.                     resize( 1 );
  98.                     index = 0;
  99.                 } else {
  100.                     index = vector_len - 1;
  101.                 }
  102.             }
  103.         }
  104.         // default initialize any unitialized entries upto index
  105.         base_init_upto( index );
  106.     }
  107.     return( vector[ index ] );
  108. };
  109.  
  110.  
  111. //
  112. // The const index operator.  If the vector is empty and exceptions are
  113. // disabled, add a default initialized first element, and return it.
  114. //
  115.  
  116. template <class Type>
  117. const Type &WCValVector<Type>::operator[] ( int index ) const {
  118.     WCValVector<Type> * const non_const_this = ( WCValVector<Type> * )this;
  119.     if( vector_len == 0 ) {
  120.         base_throw_empty_container();
  121.         base_throw_index_range();
  122.         non_const_this->resize( 1 );
  123.         return( vector[ 0 ] );
  124.     } else {
  125.         int new_index = base_index_check( index );
  126.         if( new_index >= num_init ) {
  127.             // Even though this is a non-const operation, the user's view of
  128.             // the vector is not being changed.  Any element the user
  129.             // accesses must be intialized.
  130.             non_const_this->base_init_upto( new_index );
  131.         }
  132.         return( vector[ new_index ] );
  133.     }
  134. };
  135.  
  136.  
  137.  
  138. //
  139. //  This defines a basic Pointer vector with elements of type pointer to <Type>
  140. //
  141. // Implementation note:
  142. // WCPtrVector inherits from WCValVector templated over <void *>.  This saves
  143. // most of the vector code being generated for pointer vectors templated over
  144. // different types, speeding up compile time, and reducing code size.
  145. //
  146.  
  147. template <class Type>
  148. class WCPtrVector : public WCValVector<void *> {
  149. protected:
  150.     typedef Type * __Type_Ptr;
  151.     virtual void base_init_upto( int );
  152. public:
  153.     inline WCPtrVector( size_t length = 0 )
  154.                         : WCValVector( length ) {};
  155.  
  156.     inline WCPtrVector( size_t length, const Type *init_value )
  157.                         : WCValVector( length
  158.                                      , (void *)init_value ) {};
  159.  
  160.     inline WCPtrVector( const WCPtrVector & orig ) : WCValVector( 0 ) {
  161.         base_construct( &orig );
  162.     };
  163.  
  164.     inline virtual ~WCPtrVector() {};
  165.  
  166.     void clearAndDestroy();
  167.  
  168.     inline WCPtrVector & operator=( const WCPtrVector & orig ) {
  169.         base_assign( &orig );
  170.         return( *this );
  171.     };
  172.  
  173.     Type * &operator[] ( int index ) {
  174.         return( (Type * &)WCValVector::operator[]( index ) );
  175.     };
  176.  
  177.     Type * const &operator[] ( int index ) const {
  178.         return( (Type * const &)WCValVector::operator[]( index ) );
  179.     };
  180. };
  181.  
  182.  
  183. template<class Type>
  184. void WCPtrVector<Type>::base_init_upto( int index ) {
  185.     if( index >= num_init ){
  186.         // intialize the vector by NULLing out unitialized elements
  187.         memset( &vector[ num_init ], 0
  188.               , ( index + 1 - num_init ) * sizeof( void * ) );
  189.         num_init = index + 1;
  190.     }
  191. }
  192.  
  193.  
  194. template <class Type>
  195. void WCPtrVector<Type>::clearAndDestroy() {
  196.     for( unsigned i = 0; i < num_init; i++ ) {
  197.         delete( (Type *)( vector[ i ] ) );
  198.     }   
  199.     clear();
  200. };
  201.  
  202.  
  203.  
  204.  
  205. //
  206. //  This defines the Value Ordered vector with elements of type <Type>
  207. //
  208.  
  209. template <class Type>
  210. class WCValOrderedVector : public WCOrderedVectorBase<Type> {
  211. private:
  212.     virtual int base_equivalent( const Type &elem1, const Type &elem2 ) const {
  213.         return( elem1 == elem2 );
  214.     };
  215. public:
  216.     inline WCValOrderedVector( size_t length = WCDEFAULT_VECTOR_LENGTH,
  217.                 unsigned default_grow = WCDEFAULT_VECTOR_RESIZE_GROW )
  218.                         : WCOrderedVectorBase<Type>( length, default_grow ) {};
  219.  
  220.     inline WCValOrderedVector( const WCValOrderedVector& orig )
  221.                         : WCOrderedVectorBase<Type>( 0, 0 ) {
  222.         base_construct( &orig );
  223.     };
  224.  
  225.     inline virtual ~WCValOrderedVector() {};
  226.  
  227.     inline WCValOrderedVector & operator=( const WCValOrderedVector& orig ) {
  228.         base_assign( &orig );
  229.         return( *this );
  230.     };
  231. };
  232.  
  233.  
  234.  
  235.  
  236. //
  237. //  This defines the pointer Ordered vector with elements of type
  238. //  pointer to <Type>
  239. //
  240.  
  241. template <class Type>
  242. class WCPtrOrderedVector
  243.         : public WCPtrVectorBase<Type, WCOrderedVectorBase<void *> > {
  244. private:
  245.     virtual int base_equivalent( const __Stored_Ptr &elem1
  246.                                , const __Stored_Ptr &elem2 ) const {
  247.         return( *(const Type *)elem1 == *(const Type *)elem2 );
  248.     }
  249. public:
  250.     inline WCbool append( Type * new_elem ){
  251.         return( insert( new_elem ) );
  252.     };
  253.  
  254.     inline WCbool insert( Type * new_elem ){
  255.         return( WCOrderedVectorBase::insert( new_elem ) );
  256.     };
  257.  
  258.     inline WCbool insertAt( int index, Type * new_elem ){
  259.         return( WCOrderedVectorBase::insertAt( index, new_elem ) );
  260.     };
  261.  
  262.     inline WCbool prepend( Type * new_elem ){
  263.         return( WCOrderedVectorBase::prepend( new_elem ) );
  264.     };
  265.  
  266.     inline WCPtrOrderedVector( size_t length = WCDEFAULT_VECTOR_LENGTH,
  267.                 unsigned default_grow = WCDEFAULT_VECTOR_RESIZE_GROW )
  268.                         : WCPtrVectorBase( length, default_grow ) {};
  269.  
  270.     inline WCPtrOrderedVector( const WCPtrOrderedVector & orig )
  271.                         : WCPtrVectorBase( 0, 0 ) {
  272.         base_construct( &orig );
  273.     };
  274.  
  275.     inline virtual ~WCPtrOrderedVector() {};
  276.  
  277.     inline WCPtrOrderedVector & operator=( const WCPtrOrderedVector & orig ) {
  278.         base_assign( &orig );
  279.         return( *this );
  280.     };
  281. };
  282.  
  283.  
  284.  
  285.  
  286. //
  287. //  This defines the Value Sorted vector with elements of type <Type>
  288. //
  289.  
  290. template <class Type>
  291. class WCValSortedVector : public WCSortedVectorBase<Type> {
  292. private:
  293.     virtual int base_equivalent( const Type &elem1, const Type &elem2 ) const {
  294.         return( elem1 == elem2 );
  295.     };
  296.     virtual int base_less_than( const Type& elem1, const Type& elem2 ) const {
  297.         return( elem1 < elem2 );
  298.     }
  299. public:
  300.     inline WCValSortedVector( size_t length = WCDEFAULT_VECTOR_LENGTH,
  301.                 unsigned default_grow = WCDEFAULT_VECTOR_RESIZE_GROW )
  302.                         : WCSortedVectorBase<Type>( length, default_grow ) {};
  303.  
  304.     inline WCValSortedVector( const WCValSortedVector & orig )
  305.                         : WCSortedVectorBase<Type>( 0, 0 ) {
  306.         base_construct( &orig );
  307.     };
  308.  
  309.     inline virtual ~WCValSortedVector() {};
  310.  
  311.     inline WCValSortedVector & operator=( const WCValSortedVector & orig ) {
  312.         base_assign( &orig );
  313.         return( *this );
  314.     };
  315. };
  316.  
  317.  
  318.  
  319.  
  320. //
  321. //  This defines the pointer Sorted vector with elements of type
  322. //  pointer to <Type>
  323. //
  324.  
  325. template <class Type>
  326. class WCPtrSortedVector
  327.                 : public WCPtrVectorBase<Type, WCSortedVectorBase<void *> > {
  328. private:
  329.     virtual int base_equivalent( const __Stored_Ptr &elem1
  330.                                , const __Stored_Ptr &elem2 ) const {
  331.         return( *(const Type *)elem1 == *(const Type *)elem2 );
  332.     }
  333.     virtual int base_less_than( const __Stored_Ptr &elem1
  334.                               , const __Stored_Ptr &elem2 ) const {
  335.         return( *(const Type *)elem1 < *(const Type *)elem2 );
  336.     }
  337. public:
  338.     inline WCbool insert( Type * elem ) {
  339.         return( WCSortedVectorBase::insert( elem ) );
  340.     }
  341.  
  342.     inline WCPtrSortedVector( size_t length = WCDEFAULT_VECTOR_LENGTH,
  343.                 unsigned default_grow = WCDEFAULT_VECTOR_RESIZE_GROW )
  344.                         : WCPtrVectorBase( length, default_grow ) {};
  345.  
  346.     inline WCPtrSortedVector( const WCPtrSortedVector & orig )
  347.                         : WCPtrVectorBase( 0, 0 ) {
  348.         base_construct( &orig );
  349.     };
  350.  
  351.     inline virtual ~WCPtrSortedVector() {};
  352.  
  353.     inline WCPtrSortedVector & operator=( const WCPtrSortedVector & orig ) {
  354.         base_assign( &orig );
  355.         return( *this );
  356.     };
  357. };
  358.  
  359. #if defined( _WNEW_OPERATOR )
  360. #  define new _WNEW_OPERATOR
  361. #endif
  362. #if defined( _WDELETE_OPERATOR )
  363. #  define delete _WDELETE_OPERATOR
  364. #endif
  365. #endif
  366.