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

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  LISTIMP.H                                                             */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1994 Borland International                        */
  6. /*  All Rights Reserved                                                   */
  7. /*                                                                        */
  8. /*------------------------------------------------------------------------*/
  9.  
  10. #if !defined( CLASSLIB_LISTIMP_H )
  11. #define CLASSLIB_LISTIMP_H
  12.  
  13. #if !defined( __LIMITS_H )
  14. #include <limits.h>
  15. #endif
  16.  
  17. #if !defined( __CHECKS_H )
  18. #include <checks.h>
  19. #endif
  20.  
  21. #if !defined( CLASSLIB_DEFS_H )
  22. #include <classlib/defs.h>
  23. #endif
  24.  
  25. #if !defined( CLASSLIB_MEMMGR_H )
  26. #include <classlib/memmgr.h>
  27. #endif
  28.  
  29. #if !defined( CLASSLIB_ALLOCTR_H )
  30. #include <classlib/alloctr.h>
  31. #endif
  32.  
  33. #if !defined( CLASSLIB_VOIDP_H )
  34. #include <classlib/voidp.h>
  35. #endif
  36.  
  37. #pragma option -Vo-
  38. #if defined( BI_CLASSLIB_NO_po )
  39. #pragma option -po-
  40. #endif
  41.  
  42. /*------------------------------------------------------------------------*/
  43. /*                                                                        */
  44. /*  template <class T> class TMListElement                                */
  45. /*                                                                        */
  46. /*  Node for templates TMListImp<T,Alloc> and TMIListImp<T,Alloc>         */
  47. /*                                                                        */
  48. /*------------------------------------------------------------------------*/
  49.  
  50. template <class T,class Alloc> class TMListImp;
  51. template <class T,class Alloc> class TMListElement;
  52.  
  53. template <class T,class Alloc> class TMListBlockInitializer :
  54.     public Alloc
  55. {
  56.  
  57. protected:
  58.  
  59.     TMListBlockInitializer();
  60.     ~TMListBlockInitializer();
  61.  
  62.     static unsigned Count;
  63.  
  64. };
  65.  
  66. #if defined( BI_OLDNAMES )
  67. #define BI_MListBlockInitializer TMListBlockInitializer
  68. #endif
  69.  
  70. template <class T,class Alloc>
  71. TMListBlockInitializer<T,Alloc>::TMListBlockInitializer()
  72. {
  73.     PRECONDITION( Count != UINT_MAX );
  74.     if( Count++ == 0 )
  75.         TMListElement<T,Alloc>::Mgr =
  76.             new(*this)TMMemBlocks<Alloc>( sizeof(TMListElement<T,Alloc>), 20 );
  77. }
  78.  
  79. template <class T,class Alloc>
  80. TMListBlockInitializer<T,Alloc>::~TMListBlockInitializer()
  81. {
  82.     PRECONDITION( Count != 0 );
  83.     if( --Count == 0 )
  84.         {
  85.         delete TMListElement<T,Alloc>::Mgr;
  86.         TMListElement<T,Alloc>::Mgr = 0;
  87.         }
  88. }
  89.  
  90. template <class T,class Alloc>
  91. unsigned TMListBlockInitializer<T,Alloc>::Count = 0;
  92.  
  93. template <class T,class Alloc> class TMListElement
  94. {
  95.  
  96. public:
  97.  
  98.     TMListElement( const T& t, TMListElement<T,Alloc> *p ) :
  99.         Data(t)
  100.         {
  101.         Next = p->Next;
  102.         p->Next = this;
  103.         }
  104.  
  105.     TMListElement();
  106.  
  107.     TMListElement<T,Alloc> *Next;
  108.     T Data;
  109.  
  110.     void *operator new( size_t sz );
  111.     void operator delete( void * );
  112.  
  113. private:
  114.  
  115.     friend TMListBlockInitializer<T,Alloc>;
  116.  
  117.     static TMMemBlocks<Alloc> *Mgr;
  118.  
  119. };
  120.  
  121. #if defined( BI_OLDNAMES )
  122. #define BI_MListElement TMListElement
  123. #endif
  124.  
  125. template <class T,class Alloc> TMMemBlocks<Alloc> *TMListElement<T,Alloc>::Mgr = 0;
  126.  
  127. template <class T,class Alloc>
  128. inline TMListElement<T,Alloc>::TMListElement()
  129. {
  130.     Next = 0;
  131. }
  132.  
  133. template <class T,class Alloc>
  134. void *TMListElement<T,Alloc>::operator new( size_t sz )
  135. {
  136.     PRECONDITION( Mgr != 0 );
  137.     return Mgr->Allocate( sz );
  138. }
  139.  
  140. template <class T,class Alloc>
  141. void TMListElement<T,Alloc>::operator delete( void *b )
  142. {
  143.     PRECONDITION( Mgr != 0 );
  144.     Mgr->Free( b );
  145. }
  146.  
  147. /*------------------------------------------------------------------------*/
  148. /*                                                                        */
  149. /*  template <class T,class Alloc> class TMListImp                        */
  150. /*                                                                        */
  151. /*  Implements a managed list of objects of type T.  Assumes that         */
  152. /*  T has meaningful copy semantics and a default constructor.            */
  153. /*                                                                        */
  154. /*------------------------------------------------------------------------*/
  155.  
  156. template <class T,class Alloc> class TMListIteratorImp;
  157.  
  158. template <class T,class Alloc> class TMListImp :
  159.     private TMListBlockInitializer<T,Alloc>
  160. {
  161.  
  162.     typedef TMListBlockInitializer<T,Alloc> Parent;
  163.  
  164. public:
  165.  
  166.     typedef void (*IterFunc)(T &, void *);
  167.     typedef int  (*CondFunc)(const T &, void *);
  168.  
  169.     friend TMListIteratorImp<T,Alloc>;
  170.  
  171.     TMListImp()
  172.         {
  173.         InitList();
  174.         }
  175.  
  176.     ~TMListImp()
  177.         {
  178.         Flush();
  179.         }
  180.  
  181.     const T& PeekHead() const
  182.         {
  183.         return Head.Next->Data;
  184.         }
  185.  
  186.     int Add( const T& t );
  187.  
  188.     int Detach( const T& t )
  189.         {
  190.         return DoDetach( t, 0 );
  191.         }
  192.  
  193.     int DetachAtHead()
  194.         {
  195.         return DoDetachAtHead(0);
  196.         }
  197.  
  198.     T *Find( const const T& t );
  199.  
  200.     void Flush()
  201.         {
  202.         DoFlush();
  203.         }
  204.  
  205.     int IsEmpty() const
  206.         {
  207.         return ItemsInContainer == 0;
  208.         }
  209.  
  210.     int GetItemsInContainer() const
  211.         {
  212.         return ItemsInContainer;
  213.         }
  214.  
  215.     void ForEach( IterFunc iter, void *args );
  216.     T *FirstThat( CondFunc cond, void *args ) const;
  217.     T *LastThat( CondFunc cond, void *args ) const;
  218.  
  219.     Parent::operator delete;
  220.     Parent::operator delete [];
  221.  
  222. #if defined( BI_OLDNAMES )
  223.     const T& peekHead() const { return PeekHead(); }
  224.     void add( const T& t ) { Add(t); }
  225.     void detach( const T& t, int del = 0 ) { DoDetach( t, del ); }
  226.     void flush( int = 0 ) { Flush(); }
  227.     int isEmpty() const { return IsEmpty(); }
  228.     void forEach( IterFunc iter, void *args )
  229.         { ForEach( iter, args ); }
  230.     T *firstThat( CondFunc cond, void *args ) const
  231.         { return FirstThat( cond, args ); }
  232.     T *lastThat( CondFunc cond, void *args ) const
  233.         { return LastThat( cond, args ); }
  234. #endif  // BI_OLDNAMES
  235.  
  236. protected:
  237.  
  238.     int DoDetach( const T& t, int del = 0 )
  239.         {
  240.         return DetachElement(FindDetach(t),del);
  241.         }
  242.  
  243.     int DoDetachAtHead( int del = 0 )
  244.         {
  245.         return DetachElement(&Head,del);
  246.         }
  247.  
  248.     void DoFlush( int del = 0 );
  249.  
  250.     TMListElement<T,Alloc> Head, Tail;
  251.  
  252.     virtual TMListElement<T,Alloc> *FindDetach( const T& t )
  253.         {
  254.         return FindPred(t);
  255.         }
  256.  
  257.     virtual TMListElement<T,Alloc> *FindPred( const T& );
  258.  
  259.     int ItemsInContainer;
  260.  
  261. private:
  262.  
  263.     virtual void RemoveData( TMListElement<T,Alloc> * )
  264.         {
  265.         }
  266.  
  267.     void InitList();
  268.  
  269.     int DetachElement( TMListElement<T,Alloc> *pred, int del = 0 );
  270.  
  271. };
  272.  
  273. #if defined( BI_OLDNAMES )
  274. #define BI_MListImp TMListImp
  275. #endif
  276.  
  277. template <class T,class Alloc> void TMListImp<T,Alloc>::InitList()
  278. {
  279.     Head.Next = &Tail;
  280.     Tail.Next = &Tail;
  281.     ItemsInContainer = 0;
  282. }
  283.  
  284. template <class T,class Alloc> int TMListImp<T,Alloc>::Add( const T& toAdd )
  285. {
  286.     new TMListElement<T,Alloc>( toAdd, &Head );
  287.     ItemsInContainer++;
  288.     return 1;
  289. }
  290.  
  291. template <class T,class Alloc>
  292. TMListElement<T,Alloc> *TMListImp<T,Alloc>::FindPred( const T& t )
  293. {
  294.     Tail.Data = t;
  295.     TMListElement<T,Alloc> *cursor = &Head;
  296.     while( !(t == cursor->Next->Data) )
  297.         cursor = cursor->Next;
  298.     Tail.Data = T();
  299.     return cursor;
  300. }
  301.  
  302. template <class T,class Alloc>
  303. int TMListImp<T,Alloc>::DetachElement( TMListElement<T,Alloc> *pred,
  304.                                        int del )
  305. {
  306.     TMListElement<T,Alloc> *item = pred->Next;
  307.     if( item == &Tail )
  308.         return 0;
  309.     else
  310.         {
  311.         pred->Next = pred->Next->Next;
  312.         if( del != 0 )
  313.             RemoveData( item );
  314.         delete item;
  315.         ItemsInContainer--;
  316.         return 1;
  317.         }
  318. }
  319.  
  320. template <class T,class Alloc> T *TMListImp<T,Alloc>::Find( const T& t )
  321. {
  322.     TMListElement<T,Alloc> *cursor = FindPred(t)->Next;
  323.     if( cursor == &Tail )
  324.         return 0;
  325.     else
  326.         return &cursor->Data;
  327. }
  328.  
  329. template <class T,class Alloc> void TMListImp<T,Alloc>::DoFlush( int del )
  330. {
  331.     TMListElement<T,Alloc> *current = Head.Next;
  332.     while( current != &Tail )
  333.         {
  334.         TMListElement<T,Alloc> *temp = current;
  335.         current = current->Next;
  336.         if( del != 0 )
  337.             RemoveData( temp );
  338.         delete temp;
  339.         }
  340.     InitList();
  341. }
  342.  
  343. template <class T,class Alloc>
  344. void TMListImp<T,Alloc>::ForEach( IterFunc iter, void *args )
  345. {
  346.     TMListElement<T,Alloc> *cur = Head.Next;
  347.     while( cur->Next != cur )
  348.         {
  349.         iter( cur->Data, args );
  350.         cur = cur->Next;
  351.         }
  352. }
  353.  
  354. template <class T,class Alloc>
  355. T *TMListImp<T,Alloc>::FirstThat( CondFunc cond, void *args ) const
  356. {
  357.     TMListElement<T,Alloc> *cur = Head.Next;
  358.     while( cur->Next != cur )
  359.         if( cond( cur->Data, args ) != 0 )
  360.             return &(cur->Data);
  361.         else
  362.             cur = cur->Next;
  363.     return 0;
  364. }
  365.  
  366. template <class T,class Alloc>
  367. T *TMListImp<T,Alloc>::LastThat( CondFunc cond, void *args ) const
  368. {
  369.     T *res = 0;
  370.     TMListElement<T,Alloc> *cur = Head.Next;
  371.     while( cur->Next != cur )
  372.         {
  373.         if( cond( cur->Data, args ) != 0 )
  374.             res = &(cur->Data);
  375.         cur = cur->Next;
  376.         }
  377.     return res;
  378. }
  379.  
  380. /*------------------------------------------------------------------------*/
  381. /*                                                                        */
  382. /*  template <class T,class Alloc> class TMListIteratorImp                */
  383. /*                                                                        */
  384. /*  Implements a list iterator.  This iterator works with any direct      */
  385. /*  managed list.  For indirect lists, see TMIListIteratorImp.            */
  386. /*                                                                        */
  387. /*------------------------------------------------------------------------*/
  388.  
  389. template <class T,class Alloc> class TMListIteratorImp
  390. {
  391.  
  392. public:
  393.  
  394.     TMListIteratorImp( const TMListImp<T,Alloc>& l )
  395.         {
  396.         List = &l;
  397.         Cur = List->Head.Next;
  398.         }
  399.  
  400.     TMListIteratorImp( const TMSListImp<T,Alloc>& l );
  401.  
  402.     operator int()
  403.         {
  404.         return Cur != &(List->Tail);
  405.         }
  406.  
  407.     const T& Current()
  408.         {
  409.         PRECONDITION( int(*this) != 0 );
  410.         return Cur->Data;
  411.         }
  412.  
  413.     const T& operator ++ ( int )
  414.         {
  415.         PRECONDITION( Cur != &(List->Tail) );
  416.         TMListElement<T,Alloc> *temp = Cur;
  417.         Cur = Cur->Next;
  418.         return temp->Data;
  419.         }
  420.  
  421.     const T& operator ++ ()
  422.         {
  423.         PRECONDITION( Cur->Next != &(List->Tail) );
  424.         Cur = Cur->Next;
  425.         return Cur->Data;
  426.         }
  427.  
  428.     void Restart()
  429.         {
  430.         Cur = List->Head.Next;
  431.         }
  432.  
  433. #if defined( BI_OLDNAMES )
  434.     const T& current() { return Current(); }
  435.     void restart() { Restart(); }
  436. #endif  // BI_OLDNAMES
  437.  
  438. private:
  439.  
  440.     const TMListImp<T,Alloc> *List;
  441.     TMListElement<T,Alloc> *Cur;
  442.  
  443. };
  444.  
  445. #if defined( BI_OLDNAMES )
  446. #define BI_MListIteratorImp TMListIteratorImp
  447. #endif
  448.  
  449. /*------------------------------------------------------------------------*/
  450. /*                                                                        */
  451. /*  template <class T> class TListImp                                     */
  452. /*  template <class T> class TListIteratorImp                             */
  453. /*                                                                        */
  454. /*  Implements a list of objects of type T using TStandardAllocator as    */
  455. /*  its memory manager.  Assumes that T has meaningful copy semantics     */
  456. /*  and a default constructor.                                            */
  457. /*                                                                        */
  458. /*------------------------------------------------------------------------*/
  459.  
  460. template <class T> class TListImp :
  461.     public TMListImp<T,TStandardAllocator>
  462. {
  463. };
  464.  
  465. template <class T> class TListIteratorImp :
  466.     public TMListIteratorImp<T,TStandardAllocator>
  467. {
  468.  
  469. public:
  470.  
  471.     TListIteratorImp( const TMListImp<T,TStandardAllocator>& l ) :
  472.         TMListIteratorImp<T,TStandardAllocator>( l ) {}
  473.  
  474.     TListIteratorImp( const TMSListImp<T,TStandardAllocator>& l ) :
  475.         TMListIteratorImp<T,TStandardAllocator>( l ) {}
  476.  
  477. };
  478.  
  479. #if defined( BI_OLDNAMES )
  480. #define BI_ListImp TListImp
  481. #define BI_ListIteratorImp TListIteratorImp
  482. #endif
  483.  
  484. /*------------------------------------------------------------------------*/
  485. /*                                                                        */
  486. /*  template <class T,class Alloc> class TMSListImp                       */
  487. /*                                                                        */
  488. /*  Implements a managed, sorted list of objects of type T.  Assumes that */
  489. /*  T has meaningful copy semantics, a meaningful < operator, and a       */
  490. /*  default constructor.                                                  */
  491. /*                                                                        */
  492. /*------------------------------------------------------------------------*/
  493.  
  494. template <class T,class Alloc> class TMSListImp :
  495.     private TMListImp<T,Alloc>
  496. {
  497.  
  498.     typedef TMListImp<T,Alloc> Parent;
  499.  
  500. public:
  501.  
  502.     friend TMListIteratorImp<T,Alloc>;
  503.  
  504.     int Add( const T& t );
  505.  
  506.     Parent::IterFunc;
  507.     Parent::CondFunc;
  508.     Parent::PeekHead;
  509.     Parent::Detach;
  510.     Parent::DetachAtHead;
  511.     Parent::Find;
  512.     Parent::Flush;
  513.     Parent::IsEmpty;
  514.     Parent::GetItemsInContainer;
  515.     Parent::ForEach;
  516.     Parent::FirstThat;
  517.     Parent::LastThat;
  518.     Parent::operator delete;
  519.     Parent::operator delete [];
  520.  
  521. #if defined( BI_OLDNAMES )
  522.     void add( const T& t ) { Add(t); }
  523. #endif  // BI_OLDNAMES
  524.  
  525. protected:
  526.  
  527.     Parent::Head;
  528.     Parent::Tail;
  529.     Parent::ItemsInContainer;
  530.     Parent::DoDetach;
  531.     Parent::DoDetachAtHead;
  532.     Parent::DoFlush;
  533.  
  534.     virtual TMListElement<T,Alloc> *FindDetach( const T& t );
  535.     virtual TMListElement<T,Alloc> *FindPred( const T& );
  536.  
  537. };
  538.  
  539. template <class T,class Alloc> class TMSListIteratorImp :
  540.     public TMListIteratorImp<T,Alloc>
  541. {
  542.  
  543. public:
  544.  
  545.     TMSListIteratorImp( const TMSListImp<T,Alloc>& l ) :
  546.         TMListIteratorImp<T,Alloc>( l ) {}
  547.  
  548. };
  549.  
  550. #if defined( BI_OLDNAMES )
  551. #define BI_MSListImp TMSListImp
  552. #define BI_MSListIteratorImp TMSListIteratorImp
  553. #endif
  554.  
  555. template <class T,class Alloc> int TMSListImp<T,Alloc>::Add( const T& t )
  556. {
  557.     new TMListElement<T,Alloc>( t, FindPred(t) );
  558.     ItemsInContainer++;
  559.     return 1;
  560. }
  561.  
  562. template <class T,class Alloc>
  563. TMListElement<T,Alloc> *TMSListImp<T,Alloc>::FindDetach( const T& t )
  564. {
  565.     TMListElement<T,Alloc> *res = FindPred(t);
  566.     if( res != 0 &&
  567.         res->Next->Data == t )
  568.         return res;
  569.     else
  570.         return &Tail;
  571. }
  572.  
  573. template <class T,class Alloc>
  574. TMListElement<T,Alloc> *TMSListImp<T,Alloc>::FindPred( const T& t )
  575. {
  576.     Tail.Data = t;
  577.     TMListElement<T,Alloc> *cursor = &Head;
  578.     while( cursor->Next->Data < t )
  579.         cursor = cursor->Next;
  580.     return cursor;
  581. }
  582.  
  583. /*------------------------------------------------------------------------*/
  584. /*                                                                        */
  585. /*  template <class T> class TSListImp                                    */
  586. /*  template <class T> class TSListIteratorImp                            */
  587. /*                                                                        */
  588. /*  Implements a sorted list of objects of type T using                   */
  589. /*  TStandardAllocator as its memory manager.  Assumes that               */
  590. /*  T has meaningful copy semantics, a meaningful < operator, and a       */
  591. /*  default constructor.                                                  */
  592. /*                                                                        */
  593. /*------------------------------------------------------------------------*/
  594.  
  595. template <class T> class TSListImp :
  596.     public TMSListImp<T,TStandardAllocator>
  597. {
  598. };
  599.  
  600. template <class T> class TSListIteratorImp :
  601.     public TMSListIteratorImp<T,TStandardAllocator>
  602. {
  603.  
  604. public:
  605.  
  606.     TSListIteratorImp( const TSListImp<T>& l ) :
  607.         TMSListIteratorImp<T,TStandardAllocator>( l ) {}
  608.  
  609. };
  610.  
  611. // constructor for TMListIteratorImp
  612. template <class T, class Alloc>
  613. TMListIteratorImp<T,Alloc>::TMListIteratorImp( const TMSListImp<T,Alloc>& l )
  614. {
  615.     List = &l;
  616.     Cur = List->Head.Next;
  617. }
  618.  
  619. #if defined( BI_OLDNAMES )
  620. #define BI_SListImp TSListImp
  621. #define BI_SListIteratorImp TSListIteratorImp
  622. #endif
  623.  
  624. /*------------------------------------------------------------------------*/
  625. /*                                                                        */
  626. /*  template <class T,class List,class Alloc> class TMInternalIListImp    */
  627. /*                                                                        */
  628. /*  Implements a managed list of pointers to objects of type T.           */
  629. /*  This is implemented through the form of TMListImp specified by List.  */
  630. /*  Since pointers always have meaningful copy semantics, this class      */
  631. /*  can handle any type of object.                                        */
  632. /*                                                                        */
  633. /*------------------------------------------------------------------------*/
  634.  
  635. template <class T,class List,class Alloc> class TMInternalIListImp :
  636.     public List
  637. {
  638.  
  639.     typedef List Parent;
  640.  
  641. public:
  642.  
  643.     typedef void (*IterFunc)(T&, void *);
  644.     typedef int  (*CondFunc)(const T&, void *);
  645.  
  646.     T *PeekHead() const
  647.         {
  648.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::PeekHead()));
  649.         }
  650.  
  651.     int Add( T *t )
  652.         {
  653.         return Parent::Add( t );
  654.         }
  655.  
  656.     int Detach( T *t, int del = 0 )
  657.         {
  658.         return Parent::DoDetach( t, del );
  659.         }
  660.  
  661.     int DetachAtHead( int del = 0 )
  662.         {
  663.         return Parent::DoDetachAtHead( del );
  664.         }
  665.  
  666.     void Flush( int del = 0 )
  667.         {
  668.         Parent::DoFlush(del);
  669.         }
  670.  
  671.     T *Find( const T *t );
  672.  
  673.     void ForEach( IterFunc iter, void * );
  674.     T *FirstThat( CondFunc cond, void * ) const;
  675.     T *LastThat( CondFunc cond, void * ) const;
  676.  
  677. #if defined( BI_OLDNAMES )
  678.     T *peekHead() const { return PeekHead(); }
  679.     void add( T *t ) { Add(t); }
  680.     void detach( T *t, int del = 0 ) { Detach( t, del ); }
  681.     void forEach( IterFunc iter, void *args )
  682.         { ForEach( iter, args ); }
  683.     T *firstThat( CondFunc cond, void *args ) const
  684.         { return FirstThat( cond, args ); }
  685.     T *lastThat( CondFunc cond, void *args ) const
  686.         { return LastThat( cond, args ); }
  687. #endif  // BI_OLDNAMES
  688.  
  689. protected:
  690.  
  691.     virtual TMListElement<TVoidPointer,Alloc> *
  692.                           FindPred( const TVoidPointer& ) = 0;
  693.  
  694. private:
  695.  
  696.     virtual void RemoveData( TMListElement<TVoidPointer,Alloc> *block )
  697.         {
  698.         delete STATIC_CAST(T *,STATIC_CAST(void *,block->Data));
  699.         }
  700. };
  701.  
  702. #if defined( BI_OLDNAMES )
  703. #define BI_MInternalIListImp TMInternalIListImp
  704. #endif
  705.  
  706. template <class T,class List,class Alloc>
  707. T *TMInternalIListImp<T,List,Alloc>::Find( const T *t )
  708. {
  709.     TMListElement<TVoidPointer,Alloc> *cur = Head.Next;
  710.     Tail.Data = t;
  711.     while( !(*STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)) == *t) )
  712.         cur = cur->Next;
  713.     Tail.Data = TVoidPointer();
  714.     if( cur == &Tail )
  715.         return 0;
  716.     else
  717.         return STATIC_CAST(T *,STATIC_CAST(void *,cur->Data));
  718. }
  719.  
  720. template <class T,class List,class Alloc>
  721. void TMInternalIListImp<T,List,Alloc>::ForEach( IterFunc iter,
  722.                                                 void *args )
  723. {
  724.     TMListElement<TVoidPointer,Alloc> *cur = Head.Next;
  725.     while( cur->Next != cur )
  726.         {
  727.         iter( *STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)), args );
  728.         cur = cur->Next;
  729.         }
  730. }
  731.  
  732. template <class T,class List,class Alloc>
  733. T *TMInternalIListImp<T,List,Alloc>::FirstThat( CondFunc cond,
  734.                                                 void *args ) const
  735. {
  736.     TMListElement<TVoidPointer,Alloc> *cur = Head.Next;
  737.     while( cur->Next != cur )
  738.         if( cond( *STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)), args ) != 0 )
  739.             return STATIC_CAST(T *,STATIC_CAST(void *,cur->Data));
  740.         else
  741.             cur = cur->Next;
  742.     return 0;
  743. }
  744.  
  745. template <class T,class List,class Alloc>
  746. T *TMInternalIListImp<T,List,Alloc>::LastThat( CondFunc cond,
  747.                                                void *args ) const
  748. {
  749.     T *res = 0;
  750.     TMListElement<TVoidPointer,Alloc> *cur = Head.Next;
  751.     while( cur->Next != cur )
  752.         {
  753.         if( cond( *STATIC_CAST(T *,STATIC_CAST(void *,cur->Data)), args ) != 0 )
  754.             res = STATIC_CAST(T *,STATIC_CAST(void *,cur->Data));
  755.         cur = cur->Next;
  756.         }
  757.     return res;
  758. }
  759.  
  760. /*------------------------------------------------------------------------*/
  761. /*                                                                        */
  762. /*  template <class T,class Alloc> class TMIListImp                       */
  763. /*  template <class T,class Alloc> class TMIListIteratorImp               */
  764. /*                                                                        */
  765. /*  Implements a managed list of pointers to objects of type T.           */
  766. /*  This is implemented through the template TMInternalIListImp.  Since   */
  767. /*  pointers always have meaningful copy semantics, this class            */
  768. /*  can handle any type of object.                                        */
  769. /*                                                                        */
  770. /*------------------------------------------------------------------------*/
  771.  
  772. template <class T,class Alloc> class TMIListImp :
  773.     public TMInternalIListImp<T, TMListImp<TVoidPointer,Alloc>, Alloc >
  774. {
  775.  
  776.     typedef TMInternalIListImp<T, TMListImp<TVoidPointer,Alloc>, Alloc > Parent;
  777.  
  778. public:
  779.  
  780.     friend TMIListIteratorImp<T,Alloc>;
  781.  
  782.     void Flush( int del = 0 )
  783.         {
  784.         Parent::DoFlush(del);
  785.         }
  786.  
  787.     Parent::IterFunc;
  788.     Parent::CondFunc;
  789.     Parent::PeekHead;
  790.     Parent::Add;
  791.     Parent::Detach;
  792.     Parent::DetachAtHead;
  793.     Parent::Find;
  794.     Parent::IsEmpty;
  795.     Parent::GetItemsInContainer;
  796.     Parent::ForEach;
  797.     Parent::FirstThat;
  798.     Parent::LastThat;
  799.     Parent::operator delete;
  800.     Parent::operator delete [];
  801.  
  802. protected:
  803.  
  804.     Parent::Head;
  805.     Parent::Tail;
  806.     Parent::ItemsInContainer;
  807.  
  808.     virtual TMListElement<TVoidPointer,Alloc> *FindPred( const TVoidPointer& );
  809.  
  810. };
  811.  
  812. template <class T,class Alloc> class TMIListIteratorImp :
  813.     private TMListIteratorImp<TVoidPointer,Alloc>
  814. {
  815.  
  816.     typedef TMListIteratorImp<TVoidPointer,Alloc> Parent;
  817.  
  818. public:
  819.  
  820.     TMIListIteratorImp( const TMIListImp<T,Alloc>& l ) :
  821.         TMListIteratorImp<TVoidPointer,Alloc>(l) {}
  822.  
  823.     T *Current()
  824.         {
  825.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::Current()));
  826.         }
  827.  
  828.     T *operator ++ (int)
  829.         {
  830.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++(1)));
  831.         }
  832.  
  833.     T *operator ++ ()
  834.         {
  835.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++()));
  836.         }
  837.  
  838.     Parent::Restart;
  839.     Parent::operator int;
  840.  
  841. #if defined( BI_OLDNAMES )
  842.     T *current() { return Current(); }
  843.     void restart() { Restart(); }
  844. #endif  // BI_OLDNAMES
  845.  
  846. };
  847.  
  848. template <class T,class Alloc>
  849. TMListElement<TVoidPointer,Alloc> *
  850.     TMIListImp<T,Alloc>::FindPred( const TVoidPointer& t )
  851. {
  852.     Tail.Data = t;
  853.     TMListElement<TVoidPointer,Alloc> *cursor = &Head;
  854.     while( !(*STATIC_CAST(T *,STATIC_CAST(void *,t)) ==
  855.              *STATIC_CAST(T *,STATIC_CAST(void *,cursor->Next->Data)) ))
  856.         cursor = cursor->Next;
  857.     Tail.Data = TVoidPointer();
  858.     return cursor;
  859. }
  860.  
  861. #if defined( BI_OLDNAMES )
  862. #define BI_MIListImp TMIListImp
  863. #define BI_MIListIteratorImp TMIListIteratorImp
  864. #endif
  865.  
  866. /*------------------------------------------------------------------------*/
  867. /*                                                                        */
  868. /*  template <class T> class TIListImp                                    */
  869. /*  template <class T> class TIListIteratorImp                            */
  870. /*                                                                        */
  871. /*  Implements a list of pointers to objects of type T using              */
  872. /*  TStandardAllocator as its memory manager. This is implemented         */
  873. /*  through the template TMInternalIListImp.  Since                       */
  874. /*  pointers always have meaningful copy semantics, this class            */
  875. /*  can handle any type of object.                                        */
  876. /*                                                                        */
  877. /*------------------------------------------------------------------------*/
  878.  
  879. template <class T> class TIListImp :
  880.     public TMIListImp<T,TStandardAllocator>
  881. {
  882. };
  883.  
  884. template <class T> class TIListIteratorImp :
  885.     public TMIListIteratorImp<T,TStandardAllocator>
  886. {
  887.  
  888. public:
  889.  
  890.     TIListIteratorImp( const TIListImp<T>& l ) :
  891.         TMIListIteratorImp<T,TStandardAllocator>( l ) {}
  892.  
  893. };
  894.  
  895. #if defined( BI_OLDNAMES )
  896. #define BI_IListImp TIListImp
  897. #define BI_IListIteratorImp TIListIteratorImp
  898. #endif
  899.  
  900. /*------------------------------------------------------------------------*/
  901. /*                                                                        */
  902. /*  template <class T,class Alloc> class TMISListImp                      */
  903. /*  template <class T,class Alloc> class TMISListIteratorImp              */
  904. /*                                                                        */
  905. /*  Implements a managed sorted list of pointers to objects of type T.    */
  906. /*  This is implemented through the template TInternalIListImp.  Since    */
  907. /*  pointers always have meaningful copy semantics, this class            */
  908. /*  can handle any type of object.                                        */
  909. /*                                                                        */
  910. /*------------------------------------------------------------------------*/
  911.  
  912. template <class T,class Alloc> class TMISListImp :
  913.     private TMInternalIListImp<T, TMSListImp<TVoidPointer,Alloc>, Alloc >
  914. {
  915.  
  916.     typedef TMInternalIListImp<T, TMSListImp<TVoidPointer,Alloc>, Alloc > Parent;
  917.  
  918. public:
  919.  
  920.     friend TMISListIteratorImp<T,Alloc>;
  921.  
  922.     Parent::IterFunc;
  923.     Parent::CondFunc;
  924.     Parent::PeekHead;
  925.     Parent::Add;
  926.     Parent::Detach;
  927.     Parent::DetachAtHead;
  928.     Parent::Find;
  929.     Parent::Flush;
  930.     Parent::IsEmpty;
  931.     Parent::GetItemsInContainer;
  932.     Parent::ForEach;
  933.     Parent::FirstThat;
  934.     Parent::LastThat;
  935.     Parent::operator delete;
  936.     Parent::operator delete [];
  937.  
  938. protected:
  939.  
  940.     Parent::Head;
  941.     Parent::Tail;
  942.     Parent::ItemsInContainer;
  943.  
  944.     virtual TMListElement<TVoidPointer,Alloc> *FindDetach( const TVoidPointer& );
  945.     virtual TMListElement<TVoidPointer,Alloc> *FindPred( const TVoidPointer& );
  946.  
  947. };
  948.  
  949. template <class T,class Alloc> class TMISListIteratorImp :
  950.     private TMListIteratorImp<TVoidPointer,Alloc>
  951. {
  952.  
  953.     typedef TMListIteratorImp<TVoidPointer,Alloc> Parent;
  954.  
  955. public:
  956.  
  957.     TMISListIteratorImp( const TMISListImp<T,Alloc>& l ) :
  958.         TMListIteratorImp<TVoidPointer,Alloc>(l) {}
  959.  
  960.     T *Current()
  961.         {
  962.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::Current()));
  963.         }
  964.  
  965.     T *operator ++ (int)
  966.         {
  967.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++(1)));
  968.         }
  969.  
  970.     T *operator ++ ()
  971.         {
  972.         return STATIC_CAST(T *,STATIC_CAST(void *,Parent::operator++()));
  973.         }
  974.  
  975.     Parent::Restart;
  976.     Parent::operator int;
  977.  
  978. #if defined( BI_OLDNAMES )
  979.     T *current() { return Current(); }
  980. #endif  // BI_OLDNAMES
  981.  
  982. };
  983.  
  984. #if defined( BI_OLDNAMES )
  985. #define BI_MISListImp TMISListImp
  986. #define BI_MISListIteratorImp TMISListIteratorImp
  987. #endif
  988.  
  989. template <class T,class Alloc>
  990. TMListElement<TVoidPointer,Alloc> *
  991.     TMISListImp<T,Alloc>::FindDetach( const TVoidPointer& t )
  992. {
  993.     TMListElement<TVoidPointer,Alloc> *res = FindPred(t);
  994.     if( res == 0 || res->Next == &Tail )
  995.         return &Tail;
  996.     else if(
  997.         *STATIC_CAST(T *,STATIC_CAST(void *,res->Next->Data)) ==
  998.         *STATIC_CAST(T *,STATIC_CAST(void *,t)) )
  999.         return res;
  1000.     else
  1001.         return &Tail;
  1002. }
  1003.  
  1004. template <class T,class Alloc>
  1005. TMListElement<TVoidPointer,Alloc> *
  1006.     TMISListImp<T,Alloc>::FindPred( const TVoidPointer& t )
  1007. {
  1008.     Tail.Data = t;
  1009.     TMListElement<TVoidPointer,Alloc> *cursor = &Head;
  1010.     while( *STATIC_CAST(T *,STATIC_CAST(void *,cursor->Next->Data)) <
  1011.            *STATIC_CAST(T *,STATIC_CAST(void *,t)) )
  1012.         cursor = cursor->Next;
  1013.     Tail.Data = TVoidPointer();
  1014.     return cursor;
  1015. }
  1016.  
  1017. /*------------------------------------------------------------------------*/
  1018. /*                                                                        */
  1019. /*  template <class T> class TISListImp                                   */
  1020. /*  template <class T> class TISListIteratorImp                           */
  1021. /*                                                                        */
  1022. /*  Implements a sorted list of pointers to objects of type T using       */
  1023. /*  TStandardAllocator as its memory manager.                             */
  1024. /*  This is implemented through the template TInternalIListImp.  Since    */
  1025. /*  pointers always have meaningful copy semantics, this class            */
  1026. /*  can handle any type of object.                                        */
  1027. /*                                                                        */
  1028. /*------------------------------------------------------------------------*/
  1029.  
  1030. template <class T> class TISListImp :
  1031.     public TMISListImp<T,TStandardAllocator>
  1032. {
  1033. };
  1034.  
  1035. template <class T> class TISListIteratorImp :
  1036.     public TMISListIteratorImp<T,TStandardAllocator>
  1037. {
  1038.  
  1039. public:
  1040.  
  1041.     TISListIteratorImp( const TISListImp<T>& l ) :
  1042.         TMISListIteratorImp<T,TStandardAllocator>( l ) {}
  1043.  
  1044. };
  1045.  
  1046. #if defined( BI_OLDNAMES )
  1047. #define BI_ISListImp TISListImp
  1048. #endif
  1049.  
  1050. #if defined( BI_CLASSLIB_NO_po )
  1051. #pragma option -po.
  1052. #endif
  1053.  
  1054. #pragma option -Vo.
  1055.  
  1056. #endif  // CLASSLIB_LISTIMP_H
  1057.  
  1058.