⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlistimp.h

📁 C++编译器,通过便编译多种文件的格式来定义出一种文件.
💻 H
📖 第 1 页 / 共 2 页
字号:
template <class T> class _CLASSTYPE BI_SDoubleListImp :
    public BI_DoubleListImp<T>
{

public:

    void add( T );

protected:

    virtual BI_DoubleListElement<T> _FAR *findDetach( T );
    virtual BI_DoubleListElement<T> _FAR *findPred( T );

};

template <class T> void BI_SDoubleListImp<T>::add( T t )
{
    new BI_DoubleListElement<T>( t, findPred(t) );
}

template <class T>
BI_DoubleListElement<T> _FAR *BI_SDoubleListImp<T>::findDetach( T t )
{
    BI_DoubleListElement<T> _FAR *res = findPred(t);
    if( res->next->data == t )
        return res;
    else
        return &tail;
}

template <class T>
BI_DoubleListElement<T> _FAR *BI_SDoubleListImp<T>::findPred( T t )
{
    tail.data = t;
    BI_DoubleListElement<T> _FAR *cursor = &head;
    while( cursor->next->data < t )
        cursor = cursor->next;
    return cursor;
}

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_DoubleListIteratorImp                     */
/*                                                                        */
/*  Implements a double list iterator.  This iterator works with any      */
/*  direct double list.  For indirect lists, see                          */
/*  BI_IDoubleListIteratorImp.                                            */
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T> class _CLASSTYPE BI_DoubleListIteratorImp
{

public:

    BI_DoubleListIteratorImp( const BI_DoubleListImp<T> _FAR &l )
        {
        list = &l;
        cur = list->head.next;
        }


    operator int()
        {
        return cur != &(list->tail);
        }

    T current()
        {
        return cur->data;
        }

    T operator ++ ( int )
        {
        BI_DoubleListElement<T> _FAR *temp = cur;
        cur = cur->next;
        return temp->data;
        }

    T operator ++ ()
        {
        cur = cur->next;
        return cur->data;
        }

    void restart()
        {
        cur = list->head.next;
        }


private:

    const BI_DoubleListImp<T> _FAR *list;
    BI_DoubleListElement<T> _FAR *cur;

};

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_InternalIDoubleListImp                    */
/*                                                                        */
/*  Implements a double-linked list of pointers to objects of type T.     */
/*  This is implemented through the form of BI_DoubleListImp specified by */
/*  List.  Since pointers always have meaningful copy semantics,          */
/*  this class can handle any type of object.                             */
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T, class List> class _CLASSTYPE BI_InternalIDoubleListImp :
    public List
{

public:

    T _FAR *peekHead() const { return (T _FAR *)head.next->data; }
    T _FAR *peekTail() const { return (T _FAR *)tail.prev->data; }

    void add( T _FAR *t ) { List::add( t ); }
    void addAtTail( T _FAR *t ) { List::addAtTail( t ); }
    void detach( T _FAR *t, int del = 0 ) { List::detach( t, del ); }

    void forEach( void (_FAR *)(T _FAR &, void _FAR *), void _FAR * );
    T _FAR *firstThat( int (_FAR *)(const T _FAR &, void _FAR *),
                       void _FAR *
                     ) const;
    T _FAR *lastThat( int (_FAR *)(const T _FAR &, void _FAR *),
                      void _FAR *
                    ) const;

protected:

    virtual BI_DoubleListElement<void _FAR*> _FAR*findPred( void _FAR* ) = 0;

private:

    virtual void removeData( BI_DoubleListElement<void _FAR *> _FAR *block )
        {
        delete (T _FAR *)(block->data);
        }

};

template <class T, class List>
void BI_InternalIDoubleListImp<T,List>::forEach( void (_FAR *f)(T _FAR &, void _FAR *),
                                                 void _FAR *args
                                               )
{
    BI_DoubleListElement<void _FAR *> _FAR *cur = head.next;
    while( cur->next != cur )
        {
        f( *(T _FAR *)cur->data, args );
        cur = cur->next;
        }
}

template <class T, class List>
T _FAR *BI_InternalIDoubleListImp<T,List>::firstThat( int (_FAR *cond)(const T _FAR &, void _FAR *),
                                                 void _FAR *args
                                               ) const
{
    BI_DoubleListElement<void _FAR *> _FAR *cur = head.next;
    while( cur->next != cur )
        if( cond( *(T _FAR *)(cur->data), args ) != 0 )
            return (T _FAR *)cur->data;
        else
            cur = cur->next;
    return 0;
}

template <class T, class List>
T _FAR *BI_InternalIDoubleListImp<T,List>::lastThat( int (_FAR *cond)(const T _FAR &, void _FAR *),
                                                void _FAR *args
                                              ) const
{
    T _FAR *res = 0;
    BI_DoubleListElement<void _FAR *> _FAR *cur = head.next;
    while( cur->next != cur )
        {
        if( cond( *(T _FAR *)(cur->data), args ) != 0 )
            res = (T _FAR *)(cur->data);
        cur = cur->next;
        }
    return res;
}

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_IDoubleListImp                            */
/*                                                                        */
/*  Implements a double-linked list of pointers to objects of             */
/*  type T.  This is implemented through the template                     */
/*  BI_InternalIDoubleListImp. Since pointers always have meaningful      */
/*  copy semantics, this class can handle any type of object.             */
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T> class _CLASSTYPE BI_IDoubleListImp :
    public BI_InternalIDoubleListImp<T, BI_DoubleListImp<void _FAR *> >
{

protected:

    virtual BI_DoubleListElement<void _FAR *> _FAR *findPred( void _FAR * );

};

template <class T>
BI_DoubleListElement<void _FAR *> _FAR *BI_IDoubleListImp<T>::findPred( void _FAR *t )
{
    tail.data = t;
    BI_DoubleListElement<void _FAR *> _FAR *cursor = &head;
    while( !(*(T _FAR *)t == *(T _FAR *)(cursor->next->data)) )
        cursor = cursor->next;
    return cursor;
}

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_ISDoubleListImp                           */
/*                                                                        */
/*  Implements a sorted double-linked list of pointers to objects of      */
/*  type T.  This is implemented through the template                     */
/*  BI_InternalIDoubleListImp. Since pointers always have meaningful      */
/*  copy semantics, this class can handle any type of object.             */
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T> class _CLASSTYPE BI_ISDoubleListImp :
    public BI_InternalIDoubleListImp<T, BI_SDoubleListImp<void _FAR *> >
{

protected:

    virtual BI_DoubleListElement<void _FAR *> _FAR *findDetach( void _FAR * );
    virtual BI_DoubleListElement<void _FAR *> _FAR *findPred( void _FAR * );

};

template <class T>
BI_DoubleListElement<void _FAR *> _FAR *BI_ISDoubleListImp<T>::findDetach( void _FAR *t )
{
    BI_DoubleListElement<void _FAR *> _FAR *res = findPred(t);
    if( *(T _FAR *)(res->next->data) == *(T _FAR *)t )
        return res;
    else
        return &tail;
}

template <class T>
BI_DoubleListElement<void _FAR *> _FAR *BI_ISDoubleListImp<T>::findPred( void _FAR *t )
{
    tail.data = t;
    BI_DoubleListElement<void _FAR *> _FAR *cursor = &head;
    while( *(T _FAR *)(cursor->next->data) < *(T _FAR *)t )
        cursor = cursor->next;
    return cursor;
}

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_IDoubleListIteratorImp                    */
/*                                                                        */
/*  Implements a double list iterator.  This iterator works with any      */
/*  indirect double list.  For direct lists, see BI_DoubleListIteratorImp.*/
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T>
class _CLASSTYPE BI_IDoubleListIteratorImp :
    public BI_DoubleListIteratorImp<void _FAR *>
{

public:

    BI_IDoubleListIteratorImp( const BI_DoubleListImp<void _FAR*> _FAR &l ) :
        BI_DoubleListIteratorImp<void _FAR *>(l) {}

    T _FAR *current()
        {
        return (T _FAR *)BI_DoubleListIteratorImp<void _FAR *>::current();
        }

    T _FAR *operator ++ (int)
      {
      return (T _FAR *)BI_DoubleListIteratorImp<void _FAR *>::operator ++ (1);
      }

    T _FAR *operator ++ ()
        {
        return (T _FAR *)BI_DoubleListIteratorImp<void _FAR *>::operator ++ ();
        }


};

#endif  // __DLISTIMP_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -