home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / classinc.pak / BAGS.H < prev    next >
C/C++ Source or Header  |  1997-07-23  |  14KB  |  478 lines

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