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

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