home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / OTHERUTI / TCPP30-1.ZIP / CLASSINC.ZIP / BAGS.H < prev    next >
C/C++ Source or Header  |  1992-02-18  |  14KB  |  517 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  BAGS.H                                                                */
  4. /*                                                                        */
  5. /*  Copyright Borland International 1991                                  */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( __BAGS_H )
  11. #define __BAGS_H
  12.  
  13. #if !defined( __CHECKS_H )
  14. #include <Checks.h>
  15. #endif    // __CHECKS_H
  16.  
  17. #if !defined( __SHDDEL_H )
  18. #include <ShdDel.h>
  19. #endif    // __SHDDEL_H
  20.  
  21. #if !defined( __VECTIMP_H )
  22. #include <VectImp.h>
  23. #endif    // __VECTIMP_H
  24.  
  25. #if !defined( __RESOURCE_H )
  26. #include <Resource.h>
  27. #endif    // __RESOURCE_H
  28.  
  29. #if !defined( __COLLECT_H )
  30. #include <Collect.h>
  31. #endif    // __COLLECT_H
  32.  
  33. /*------------------------------------------------------------------------*/
  34. /*                                                                        */
  35. /*  template <class Vect, class T> class BI_BagAsVectorImp                */
  36. /*                                                                        */
  37. /*  Implements a bag, using a vector as the underlying implementation.    */
  38. /*  The type Vect specifies the form of the vector, either a              */
  39. /*  BI_CVectorImp<T0> or a BI_ICVectorImp<T0>.  The type T specifies the  */
  40. /*  type of the objects to be put in the bag.  When using                 */
  41. /*  BI_VectorImp<T0>, T should be the same as T0. When using              */
  42. /*  BI_IVectorImp<T0>, T should be of type pointer to T0.  See            */
  43. /*  BI_BagAsVector and BI_IBagAsVector for examples.                      */
  44. /*                                                                        */
  45. /*------------------------------------------------------------------------*/
  46.  
  47. template <class Vect, class T> class _CLASSTYPE BI_BagAsVectorImp
  48. {
  49.  
  50. public:
  51.  
  52.     BI_BagAsVectorImp( unsigned sz = DEFAULT_BAG_SIZE ) :
  53.         data(sz,1)
  54.         {
  55.         }
  56.  
  57.     void add( T t )
  58.         {
  59.         data.add( t );
  60.         }
  61.  
  62.     void detach( T t, TShouldDelete::DeleteType = TShouldDelete::NoDelete )
  63.         {
  64.         data.detach( t );
  65.         }
  66.  
  67.     void flush( TShouldDelete::DeleteType = TShouldDelete::DefDelete )
  68.         {
  69.         data.flush();
  70.         }
  71.  
  72.     int hasMember( T t ) const
  73.         {
  74.         return data.find(t) != UINT_MAX;
  75.         }
  76.  
  77.     T findMember( T t ) const
  78.         {
  79.         PRECONDITION( hasMember(t) );
  80.         return data[data.find(t)];
  81.         }
  82.  
  83.     int isEmpty() const
  84.         {
  85.         return data.isEmpty();
  86.         }
  87.  
  88.     int isFull() const
  89.         { return 0;
  90.         }
  91.  
  92.     int getItemsInContainer() const
  93.         {
  94.         return data.top();
  95.         }
  96.  
  97. protected:
  98.  
  99.     Vect data;
  100.  
  101. };
  102.  
  103. /*------------------------------------------------------------------------*/
  104. /*                                                                        */
  105. /*  template <class T> class BI_BagAsVector                               */
  106. /*                                                                        */
  107. /*  Implements a bag of objects of type T, using a vector as              */
  108. /*  the underlying implementation.                                        */
  109. /*                                                                        */
  110. /*------------------------------------------------------------------------*/
  111.  
  112. template <class T> class _CLASSTYPE BI_BagAsVector :
  113.     public BI_BagAsVectorImp<BI_CVectorImp<T>,T>
  114. {
  115.  
  116. public:
  117.  
  118.     friend class _CLASSTYPE BI_BagAsVectorIterator<T>;
  119.  
  120.     BI_BagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  121.         BI_BagAsVectorImp<BI_CVectorImp<T>,T>( sz )
  122.         {
  123.         }
  124.  
  125.     void forEach( void (_FAR *f)(T _FAR &, void _FAR *), void _FAR *args )
  126.         {
  127.         data.forEach( f, args, 0, data.top() );
  128.         }
  129.  
  130.     T _FAR *firstThat( int (_FAR *f)(const T _FAR &, void _FAR *),
  131.                        void _FAR *args
  132.                      ) const
  133.         {
  134.         return data.firstThat( f, args, 0, data.top() );
  135.         }
  136.  
  137.     T _FAR *lastThat( int (_FAR *f)(const T _FAR &, void _FAR *),
  138.                       void _FAR *args
  139.                     ) const
  140.         {
  141.         return data.lastThat( f, args, 0, data.top() );
  142.         }
  143.  
  144. protected:
  145.  
  146.     virtual T _FAR *find( T ) const;
  147.  
  148. };
  149.  
  150. template <class T> T _FAR *BI_BagAsVector<T>::find( T t ) const
  151. {
  152.     if( isEmpty() )
  153.         return 0;
  154.     for( int index = 0; index < data.top(); index++ )
  155.         if( data[index] == t )
  156.             return &(data[index]);
  157.     return 0;
  158. }
  159.  
  160. template <class T> class _CLASSTYPE BI_BagAsVectorIterator :
  161.     public BI_VectorIteratorImp<T>
  162. {
  163.  
  164. public:
  165.  
  166.     BI_BagAsVectorIterator( const BI_BagAsVector<T> _FAR & b ) :
  167.         BI_VectorIteratorImp<T>(b.data,0,b.data.top()) {}
  168.  
  169. };
  170.  
  171. /*------------------------------------------------------------------------*/
  172. /*                                                                        */
  173. /*  template <class T> class BI_IBagAsVector                              */
  174. /*                                                                        */
  175. /*  Implements a bag of pointers to objects of type T,                    */
  176. /*  using a vector as the underlying implementation.                      */
  177. /*                                                                        */
  178. /*------------------------------------------------------------------------*/
  179.  
  180. template <class T> class _CLASSTYPE BI_IBagAsVector :
  181.     public BI_BagAsVectorImp<BI_ICVectorImp<T>,T _FAR *>,
  182.     public virtual TShouldDelete
  183. {
  184.  
  185. public:
  186.  
  187.     friend class _CLASSTYPE BI_IBagAsVectorIterator<T>;
  188.  
  189.     BI_IBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  190.         BI_BagAsVectorImp<BI_ICVectorImp<T>,T _FAR *>(sz)
  191.         {
  192.         }
  193.  
  194.     ~BI_IBagAsVector()
  195.         {
  196.         flush();
  197.         }
  198.  
  199.     void add( T _FAR *t )
  200.         {
  201.         BI_BagAsVectorImp<BI_ICVectorImp<T>,T _FAR *>::add(t);
  202.         }
  203.  
  204.     void detach( T _FAR *t, DeleteType dt = NoDelete )
  205.         {
  206.         data.detach( t, delObj(dt) );
  207.         }
  208.  
  209.     void flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
  210.         {
  211.         data.flush( delObj(dt), data.top(), 0 );
  212.         }
  213.  
  214.     T _FAR *findMember( T _FAR *t ) const
  215.         {
  216.         unsigned loc = data.find(t);
  217.         return (T _FAR *)( loc == UINT_MAX ? 0 : data[loc] );
  218.         }
  219.  
  220.     void forEach( void (_FAR *f)(T _FAR &, void _FAR *), void _FAR *args )
  221.         {
  222.         data.forEach( f, args, 0, data.top() );
  223.         }
  224.  
  225.     T _FAR *firstThat( int (_FAR *f)(const T _FAR &, void _FAR *),
  226.                        void _FAR *args
  227.                      ) const
  228.         {
  229.         return data.firstThat( f, args, 0, data.top() );
  230.         }
  231.  
  232.     T _FAR *lastThat( int (_FAR *f)(const T _FAR &, void _FAR *),
  233.                       void _FAR *args
  234.                     ) const
  235.         {
  236.         return data.lastThat( f, args, 0, data.top() );
  237.         }
  238.  
  239. };
  240.  
  241. template <class T> class _CLASSTYPE BI_IBagAsVectorIterator :
  242.     public BI_IVectorIteratorImp<T>
  243. {
  244.  
  245. public:
  246.  
  247.     BI_IBagAsVectorIterator( const BI_IBagAsVector<T> _FAR & s ) :
  248.         BI_IVectorIteratorImp<T>(s.data,0,s.data.top()) {}
  249.  
  250. };
  251.  
  252. /*------------------------------------------------------------------------*/
  253. /*                                                                        */
  254. /*  class BI_OBagAsVector                                                 */
  255. /*                                                                        */
  256. /*  Implements a bag of pointers to Object,                               */
  257. /*  using a vector as the underlying implementation.                      */
  258. /*                                                                        */
  259. /*------------------------------------------------------------------------*/
  260.  
  261. class _CLASSTYPE BI_OBagAsVector
  262. {
  263.  
  264. public:
  265.  
  266.     friend class _CLASSTYPE BI_OBagAsVectorIterator;
  267.  
  268.     BI_OBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  269.         obag( sz )
  270.         {
  271.         }
  272.  
  273.     void add( Object _FAR *o )
  274.         {
  275.         obag.add(o);
  276.         }
  277.  
  278.     void detach( Object _FAR *o,
  279.                  TShouldDelete::DeleteType dt = TShouldDelete::NoDelete
  280.                )
  281.         {
  282.         obag.detach( o, dt );
  283.         }
  284.  
  285.     void flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
  286.         {
  287.         obag.flush( dt );
  288.         }
  289.  
  290.     int hasMember( Object _FAR *o ) const
  291.         {
  292.         return obag.hasMember(o);
  293.         }
  294.  
  295.     Object _FAR *findMember( Object _FAR *o ) const
  296.         {
  297.         Object _FAR *obj = obag.findMember( o );
  298.         return obj != 0 ? obj : 0;
  299.         }
  300.  
  301.     int isEmpty() const
  302.         {
  303.         return obag.isEmpty();
  304.         }
  305.  
  306.     int isFull() const
  307.         {
  308.         return obag.isFull();
  309.         }
  310.  
  311.     void forEach( void (_FAR*f)(Object _FAR &, void _FAR*), void _FAR*args )
  312.         {
  313.         obag.forEach( f, args );
  314.         }
  315.  
  316.     Object _FAR *firstThat( int (_FAR *f)(const Object _FAR &, void _FAR *),
  317.                             void _FAR *args
  318.                           ) const
  319.         {
  320.         return obag.firstThat( f, args );
  321.         }
  322.  
  323.     Object _FAR *lastThat( int (_FAR *f)(const Object _FAR &, void _FAR *),
  324.                            void _FAR *args
  325.                          ) const
  326.         {
  327.         return obag.lastThat( f, args );
  328.         }
  329.  
  330.     int getItemsInContainer() const
  331.         {
  332.         return obag.getItemsInContainer();
  333.         }
  334.  
  335.     int ownsElements()
  336.         {
  337.         return obag.ownsElements();
  338.         }
  339.  
  340.     void ownsElements( int del )
  341.         {
  342.         obag.ownsElements( del );
  343.         }
  344.  
  345. protected:
  346.  
  347.     BI_IBagAsVector<Object> obag;
  348.  
  349. };
  350.  
  351. class _CLASSTYPE BI_OBagAsVectorIterator :
  352.     public BI_IBagAsVectorIterator<Object>
  353. {
  354.  
  355. public:
  356.  
  357.     BI_OBagAsVectorIterator( const BI_OBagAsVector _FAR & b ) :
  358.         BI_IBagAsVectorIterator<Object>(b.obag)
  359.         {
  360.         }
  361.  
  362. };
  363.  
  364. /*------------------------------------------------------------------------*/
  365. /*                                                                        */
  366. /*  class BI_TCBagAsVector                                                */
  367. /*                                                                        */
  368. /*  Implements an Object bag, with the full semantics of                  */
  369. /*  the BC 2.0 style Bag, using a vector as the underlying                */
  370. /*  implementation.                                                       */
  371. /*                                                                        */
  372. /*------------------------------------------------------------------------*/
  373.  
  374. class _CLASSTYPE BI_TCBagAsVector : public Collection
  375. {
  376.  
  377. public:
  378.  
  379.     friend class _CLASSTYPE BI_TCBagAsVectorIterator;
  380.  
  381.     BI_TCBagAsVector( int sz = DEFAULT_BAG_SIZE ) :
  382.         bag(sz)
  383.         {
  384.         }
  385.  
  386.     virtual void add( Object _FAR & o )
  387.         {
  388.         bag.add( &o );
  389.         }
  390.  
  391.     virtual void detach( Object _FAR & o,
  392.                     TShouldDelete::DeleteType dt = TShouldDelete::NoDelete )
  393.         {
  394.         bag.detach( &o, dt );
  395.         }
  396.  
  397.     virtual void flush(TShouldDelete::DeleteType dt=TShouldDelete::DefDelete )
  398.         {
  399.         bag.flush( dt );
  400.         }
  401.  
  402.     virtual int isEmpty() const
  403.         {
  404.         return bag.isEmpty();
  405.         }
  406.  
  407.     virtual countType getItemsInContainer() const
  408.         {
  409.         return bag.getItemsInContainer();
  410.         }
  411.  
  412.     void forEach( void (_FAR*f)(Object _FAR &, void _FAR*), void _FAR*args )
  413.         {
  414.         bag.forEach( f, args );
  415.         }
  416.  
  417.     Object _FAR & firstThat( int (_FAR *f)(const Object _FAR &, void _FAR *),
  418.                              void _FAR *args
  419.                            ) const
  420.         {
  421.         return ptrToRef(bag.firstThat( f, args ));
  422.         }
  423.  
  424.     Object _FAR & lastThat( int (_FAR *f)(const Object _FAR &, void _FAR *),
  425.                             void _FAR *args
  426.                           ) const
  427.         {
  428.         return ptrToRef(bag.lastThat( f, args ));
  429.         }
  430.  
  431.     virtual int hasMember( Object _FAR & o ) const
  432.         {
  433.         return bag.hasMember( &o );
  434.         }
  435.  
  436.     virtual Object _FAR & findMember( Object _FAR & o ) const
  437.         {
  438.         return ptrToRef(bag.findMember(&o));
  439.         }
  440.  
  441.     virtual ContainerIterator _FAR & initIterator() const;
  442.  
  443.     virtual classType isA() const
  444.         {
  445.         return bagClass;
  446.         }
  447.  
  448.     virtual char _FAR *nameOf() const
  449.         {
  450.         return "BI_TCBagAsVector";
  451.         }
  452.  
  453.     int ownsElements()
  454.         {
  455.         return bag.ownsElements();
  456.         }
  457.  
  458.     void ownsElements( int del )
  459.         {
  460.         bag.ownsElements( del );
  461.         }
  462.  
  463. protected:
  464.  
  465.     BI_OBagAsVector bag;
  466.  
  467. };
  468.  
  469. class _CLASSTYPE BI_TCBagAsVectorIterator : public ContainerIterator
  470. {
  471.  
  472. public:
  473.  
  474.     BI_TCBagAsVectorIterator( const BI_TCBagAsVector _FAR &b ) :
  475.         iter(b.bag)
  476.         {
  477.         }
  478.  
  479.     virtual operator int()
  480.         {
  481.         return int(iter);
  482.         }
  483.  
  484.     virtual Object _FAR & current()
  485.         {
  486.         return Object::ptrToRef(iter.current());
  487.         }
  488.  
  489.     virtual Object _FAR & operator ++ ( int )
  490.         {
  491.         return Object::ptrToRef(iter++);
  492.         }
  493.  
  494.     virtual Object _FAR & operator ++ ()
  495.         {
  496.         return Object::ptrToRef(++iter);
  497.         }
  498.  
  499.     virtual void restart()
  500.         {
  501.         iter.restart();
  502.         }
  503.  
  504. private:
  505.  
  506.     BI_OBagAsVectorIterator iter;
  507.  
  508. };
  509.  
  510. inline ContainerIterator _FAR & BI_TCBagAsVector::initIterator() const
  511. {
  512.     return *new BI_TCBagAsVectorIterator( *this );
  513. }
  514.  
  515. #endif  // __BAGS_H
  516.  
  517.