home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 12.ddi / CLASSINC.PAK / DLISTIMP.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  33.6 KB  |  1,139 lines

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