home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / CLASSINC.PAK / BAGS.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  13.6 KB  |  481 lines

  1. //----------------------------------------------------------------------------
  2. // Borland BIDS Container Library
  3. // Copyright (c) 1991, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   5.7  $
  6. //
  7. //----------------------------------------------------------------------------
  8. #if !defined( CLASSLIB_BAGS_H )
  9. #define CLASSLIB_BAGS_H
  10.  
  11. #if !defined( CLASSLIB_DEFS_H )
  12. # include <classlib/defs.h>
  13. #endif
  14. #if !defined( CLASSLIB_SHDDEL_H )
  15. # include <classlib/shddel.h>
  16. #endif
  17. #if !defined( CLASSLIB_VECTIMP_H )
  18. # include <classlib/vectimp.h>
  19. #endif
  20. #if !defined( SERVICES_CHECKS_H )
  21. # include <services/checks.h>
  22. #endif
  23.  
  24. #pragma option -Vo-
  25. #if defined( BI_CLASSLIB_NO_po )
  26. # pragma option -po-
  27. #endif
  28.  
  29. #if defined(BI_NAMESPACE)
  30. namespace ClassLib {
  31. #endif
  32.  
  33. /*------------------------------------------------------------------------*/
  34. /*                                                                        */
  35. /*  template <class Vect, class T> class TBagAsVectorImp                  */
  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. /*  TCVectorImp<T0> or a TICVectorImp<T0>.  The type T specifies the      */
  40. /*  type of the objects to be put in the bag.  When using                 */
  41. /*  TVectorImp<T0>, T should be the same as T0. When using                */
  42. /*  TIVectorImp<T0>, T should be of type pointer to T0.  See              */
  43. /*  TBagAsVector and TIBagAsVector for examples.                          */
  44. /*                                                                        */
  45. /*------------------------------------------------------------------------*/
  46.  
  47. template <class Vect, class T> class TBagAsVectorImp
  48. {
  49.  
  50. public:
  51.  
  52.     TBagAsVectorImp( unsigned sz = DEFAULT_BAG_SIZE ) :
  53.         Data(sz,1)
  54.         {
  55.         }
  56.  
  57.     int IsEmpty() const
  58.         {
  59.         return Data.IsEmpty();
  60.         }
  61.  
  62.     int IsFull() const
  63.         { return 0;
  64.         }
  65.  
  66.     int GetItemsInContainer() const
  67.         {
  68.         return Data.Top();
  69.         }
  70.  
  71. #if defined( BI_OLDNAMES )
  72.     int isEmpty() const { return IsEmpty(); }
  73.     int isFull() const { return IsFull(); }
  74.     int getItemsInContainer() const { return GetItemsInContainer(); }
  75. #endif
  76.  
  77. protected:
  78.  
  79.     Vect Data;
  80.  
  81. };
  82.  
  83. #if defined( BI_OLDNAMES )
  84. #define BI_BagAsVectorImp TBagAsVectorImp
  85. #endif
  86.  
  87. /*------------------------------------------------------------------------*/
  88. /*                                                                        */
  89. /*  template <class T,class Alloc> class TMBagAsVector                    */
  90. /*  template <class T,class Alloc> class TMBagAsVectorIterator            */
  91. /*                                                                        */
  92. /*  Implements a managed bag of objects of type T, using a vector as      */
  93. /*  the underlying implementation.                                        */
  94. /*                                                                        */
  95. /*------------------------------------------------------------------------*/
  96.  
  97. template <class T, class Alloc> class TMBagAsVectorIterator;
  98.  
  99. template <class T,class Alloc> class TMBagAsVector :
  100.     public TBagAsVectorImp<TMCVectorImp<T,Alloc>,T>
  101. {
  102.  
  103.     typedef TBagAsVectorImp<TMCVectorImp<T,Alloc>,T> Parent;
  104.  
  105. public:
  106.  
  107.     friend class TMBagAsVectorIterator<T,Alloc>;
  108.  
  109.     typedef void (*IterFunc)(T&, void *);
  110.     typedef int  (*CondFunc)(const T&, void *);
  111.  
  112.     TMBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  113.         TBagAsVectorImp<TMCVectorImp<T,Alloc>,T>( sz )
  114.         {
  115.         }
  116.  
  117.     int Add( const T& t )
  118.         {
  119.         return Data.Add( t );
  120.         }
  121.  
  122.     int Detach( const T& t )
  123.         {
  124.         return Data.Detach( t );
  125.         }
  126.  
  127.     int HasMember( const T& t ) const
  128.         {
  129.         return Data.Find(t) != UINT_MAX;
  130.         }
  131.  
  132.     const T *Find( const T& t ) const
  133.         {
  134.         unsigned loc = Data.Find(t);
  135.         return ( loc == UINT_MAX ? 0 : &Data[loc] );
  136.         }
  137.  
  138.     void ForEach( IterFunc iter, void *args )
  139.         {
  140.         Data.ForEach( iter, args, 0, Data.Top() );
  141.         }
  142.  
  143.     T *FirstThat( CondFunc cond, void *args ) const
  144.         {
  145.         return Data.FirstThat( cond, args, 0, Data.Top() );
  146.         }
  147.  
  148.     T *LastThat( CondFunc cond, void *args ) const
  149.         {
  150.         return Data.LastThat( cond, args, 0, Data.Top() );
  151.         }
  152.  
  153.     void Flush()
  154.         {
  155.         Data.Flush();
  156.         }
  157.  
  158. #if defined( BI_OLDNAMES )
  159.     void add( const T& t ) { Add(t); }
  160.     void detach( const T& t,
  161.                  TShouldDelete::DeleteType dt = TShouldDelete::NoDelete )
  162.         { Detach( t ); }
  163.     int hasMember( const T& t ) const { return HasMember(t); }
  164.     T findMember( const T& t ) const
  165.     {
  166.     PRECONDITION( HasMember(t) );
  167.     return *Find(t);
  168.     }
  169.     Parent::isEmpty;
  170.     Parent::isFull;
  171.     Parent::getItemsInContainer;
  172.     void forEach( IterFunc iter, void *args )
  173.         { ForEach( iter, args ); }
  174.     T *firstThat( CondFunc cond, void *args ) const
  175.         { return FirstThat( cond, args ); }
  176.     T *lastThat( CondFunc cond, void *args ) const
  177.         { return LastThat( cond, args ); }
  178.     void flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
  179.         {
  180.         Flush();
  181.         }
  182.  
  183. #endif  // BI_OLDNAMES
  184.  
  185. };
  186.  
  187. template <class T,class Alloc> class TMBagAsVectorIterator :
  188.     private TMVectorIteratorImp<T,Alloc>
  189. {
  190.  
  191.     typedef TMVectorIteratorImp<T,Alloc> Parent;
  192.  
  193. public:
  194.  
  195.     TMBagAsVectorIterator( const TMBagAsVector<T,Alloc>& b ) :
  196.         TMVectorIteratorImp<T,Alloc>(b.Data)
  197.         {
  198.         }
  199.  
  200.     void Restart()
  201.         {
  202.         Parent::Restart();
  203.         }
  204.  
  205.     Parent::operator int;
  206.     Parent::Current;
  207.     Parent::operator ++;
  208.  
  209. #if defined( BI_OLDNAMES )
  210.     Parent::restart;
  211.     Parent::current;
  212. #endif
  213.  
  214. };
  215.  
  216. #if defined( BI_OLDNAMES )
  217. #define BI_MBagAsVector TMBagAsVector
  218. #define BI_MBagAsVectorIterator TMBagAsVectorIterator
  219. #endif
  220.  
  221. /*------------------------------------------------------------------------*/
  222. /*                                                                        */
  223. /*  template <class T> class TBagAsVector                                 */
  224. /*  template <class T> class TBagAsVectorIterator                         */
  225. /*                                                                        */
  226. /*  Implements a bag of objects of type T, using a vector as the          */
  227. /*  underlying implementation and TStandardAllocator as its memory        */
  228. /*  manager.                                                              */
  229. /*                                                                        */
  230. /*------------------------------------------------------------------------*/
  231.  
  232. template <class T> class TBagAsVector :
  233.     public TMBagAsVector<T,TStandardAllocator>
  234. {
  235.  
  236. public:
  237.  
  238.     TBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  239.         TMBagAsVector<T,TStandardAllocator>( sz )
  240.         {
  241.         }
  242.  
  243. };
  244.  
  245. template <class T> class TBagAsVectorIterator :
  246.     public TMBagAsVectorIterator<T,TStandardAllocator>
  247. {
  248.  
  249. public:
  250.  
  251.     TBagAsVectorIterator( const TBagAsVector<T>& b ) :
  252.         TMBagAsVectorIterator<T,TStandardAllocator>(b)
  253.         {
  254.         }
  255.  
  256. };
  257.  
  258. #if defined( BI_OLDNAMES )
  259. #define BI_BagAsVector TBagAsVector
  260. #define BI_BagAsVectorIterator TBagAsVectorIterator
  261. #endif
  262.  
  263. /*------------------------------------------------------------------------*/
  264. /*                                                                        */
  265. /*  template <class T,class Alloc> class TMIBagAsVector                   */
  266. /*  template <class T,class Alloc> class TMIBagAsVectorIterator           */
  267. /*                                                                        */
  268. /*  Implements a managed bag of pointers to objects of type T,            */
  269. /*  using a vector as the underlying implementation.                      */
  270. /*                                                                        */
  271. /*------------------------------------------------------------------------*/
  272.  
  273. template <class T, class Alloc> class TMIBagAsVectorIterator;
  274.  
  275. template <class T,class Alloc> class TMIBagAsVector :
  276.     public TBagAsVectorImp<TMICVectorImp<T,Alloc>,T *>,
  277.     public TShouldDelete
  278. {
  279.  
  280.     typedef TBagAsVectorImp<TMICVectorImp<T,Alloc>,T *> Parent;
  281.  
  282. public:
  283.  
  284.     typedef void (*IterFunc)(T&, void *);
  285.     typedef int  (*CondFunc)(const T&, void *);
  286.  
  287.     friend TMIBagAsVectorIterator<T,Alloc>;
  288.  
  289.     TMIBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  290.         TBagAsVectorImp<TMICVectorImp<T,Alloc>,T *>(sz)
  291.         {
  292.         }
  293.  
  294.     ~TMIBagAsVector()
  295.         {
  296.         Flush();
  297.         }
  298.  
  299.     int Add( T *t )
  300.         {
  301.         return Data.Add( t );
  302.         }
  303.  
  304.     int Detach( T *t, DeleteType dt = NoDelete )
  305.         {
  306.         return Data.Detach( t, dt );
  307.         }
  308.  
  309.     int HasMember( const T *t ) const
  310.         {
  311.         return Data.Find(t) != UINT_MAX;
  312.         }
  313.  
  314.     void Flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
  315.         {
  316.         Data.Flush( DelObj(dt), Data.Top(), 0 );
  317.         }
  318.  
  319.     T *Find( T *t ) const
  320.         {
  321.         unsigned loc = Data.Find(t);
  322.         return ( loc == UINT_MAX ? 0 : STATIC_CAST(T *,Data[loc]) );
  323.         }
  324.  
  325.     void ForEach( IterFunc iter, void *args )
  326.         {
  327.         Data.ForEach( iter, args, 0, Data.Top() );
  328.         }
  329.  
  330.     T *FirstThat( CondFunc cond, void *args ) const
  331.         {
  332.         return Data.FirstThat( cond, args, 0, Data.Top() );
  333.         }
  334.  
  335.     T *LastThat( CondFunc cond, void *args ) const
  336.         {
  337.         return Data.LastThat( cond, args, 0, Data.Top() );
  338.         }
  339.  
  340. #if defined( BI_OLDNAMES )
  341.     void add( T *t ) { Add(t); }
  342.     void detach( T *t, DeleteType dt = NoDelete ) { Detach( t, dt ); }
  343.     void flush( TShouldDelete::DeleteType dt = TShouldDelete::DefDelete )
  344.         { Flush( dt ); }
  345.     T *findMember( T *t ) const { return Find(t); }
  346.     Parent::isEmpty;
  347.     Parent::isFull;
  348.     Parent::getItemsInContainer;
  349.     void forEach( IterFunc iter, void *args )
  350.         { ForEach( iter, args ); }
  351.     T *firstThat( CondFunc cond, void *args ) const
  352.         { return FirstThat( cond, args ); }
  353.     T *lastThat( CondFunc cond, void *args ) const
  354.         { return LastThat( cond, args ); }
  355. #endif  // BI_OLDNAMES
  356.  
  357. };
  358.  
  359. template <class T,class Alloc> class TMIBagAsVectorIterator :
  360.     public TMICVectorIteratorImp<T,Alloc>
  361. {
  362.  
  363. public:
  364.  
  365.     TMIBagAsVectorIterator( const TMIBagAsVector<T,Alloc>& s ) :
  366.         TMICVectorIteratorImp<T,Alloc>(s.Data,0,s.Data.Top()) {}
  367.  
  368. };
  369.  
  370. #if defined( BI_OLDNAMES )
  371. #define BI_MIBagAsVector TMIBagAsVector
  372. #define BI_MIBagAsVectorIterator TMIBagAsVectorIterator
  373. #endif
  374.  
  375. /*------------------------------------------------------------------------*/
  376. /*                                                                        */
  377. /*  template <class T> class TIBagAsVector                                */
  378. /*  template <class T> class TIBagAsVectorIterator                        */
  379. /*                                                                        */
  380. /*  Implements a bag of pointers to objects of type T, using a vector as  */
  381. /*  the underlying implementation and TStandardAllocator as its memory    */
  382. /*  manager.                                                              */
  383. /*                                                                        */
  384. /*------------------------------------------------------------------------*/
  385.  
  386. template <class T> class TIBagAsVector :
  387.     public TMIBagAsVector<T,TStandardAllocator>
  388. {
  389.  
  390. public:
  391.  
  392.     TIBagAsVector( unsigned sz = DEFAULT_BAG_SIZE ) :
  393.         TMIBagAsVector<T,TStandardAllocator>(sz)
  394.         {
  395.         }
  396.  
  397. };
  398.  
  399. template <class T> class TIBagAsVectorIterator :
  400.     private TMIBagAsVectorIterator<T,TStandardAllocator>
  401. {
  402.  
  403.     typedef TMIBagAsVectorIterator<T,TStandardAllocator> Parent;
  404.  
  405. public:
  406.  
  407.     TIBagAsVectorIterator( const TIBagAsVector<T>& s ) :
  408.         TMIBagAsVectorIterator<T,TStandardAllocator>(s)
  409.         {
  410.         }
  411.  
  412.     void Restart()
  413.         {
  414.         Parent::Restart();
  415.         }
  416.  
  417.     Parent::operator int;
  418.     Parent::Current;
  419.     Parent::operator ++;
  420.  
  421. #if defined( BI_OLDNAMES )
  422.     Parent::restart;
  423.     Parent::current;
  424. #endif
  425.  
  426. };
  427.  
  428. #if defined( BI_OLDNAMES )
  429. #define BI_IBagAsVector TIBagAsVector
  430. #define BI_IBagAsVectorIterator TIBagAsVectorIterator
  431. #endif
  432.  
  433. /*------------------------------------------------------------------------*/
  434. /*                                                                        */
  435. /*  template <class T> class TBag                                         */
  436. /*  template <class T> class TBagIterator                                 */
  437. /*                                                                        */
  438. /*  Easy names for TBagAsVector and TBagAsVectorIterator.                 */
  439. /*                                                                        */
  440. /*------------------------------------------------------------------------*/
  441.  
  442. template <class T> class TBag :
  443.     public TBagAsVector<T>
  444. {
  445.  
  446. public:
  447.  
  448.     TBag( unsigned sz = DEFAULT_BAG_SIZE ) :
  449.         TBagAsVector<T>( sz )
  450.         {
  451.         }
  452.  
  453. };
  454.  
  455. template <class T> class TBagIterator :
  456.     public TBagAsVectorIterator<T>
  457. {
  458.  
  459. public:
  460.  
  461.  
  462.     TBagIterator( const TBag<T>& a ) :
  463.         TBagAsVectorIterator<T>(a)
  464.         {
  465.         }
  466.  
  467. };
  468.  
  469. #if defined(BI_NAMESPACE)
  470. }   // namespace ClassLib
  471. #endif
  472.  
  473. #if defined( BI_CLASSLIB_NO_po )
  474. #pragma option -po.
  475. #endif
  476.  
  477. #pragma option -Vo.
  478.  
  479. #endif  // CLASSLIB_BAGS_H
  480.  
  481.