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

📄 dlistimp.h

📁 C++编译器,通过便编译多种文件的格式来定义出一种文件.
💻 H
📖 第 1 页 / 共 2 页
字号:
/*------------------------------------------------------------------------*/
/*                                                                        */
/*  DLISTIMP.H                                                            */
/*                                                                        */
/*  Copyright Borland International 1991                                  */
/*  All Rights Reserved                                                   */
/*                                                                        */
/*------------------------------------------------------------------------*/

#if !defined( __DLISTIMP_H )
#define __DLISTIMP_H

#if !defined( __MEMMGR_H )
#include <MemMgr.h>
#endif  // __MEMMGR_H

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_DoubleListElement                         */
/*                                                                        */
/*  Node for templates BI_DoubleListImp<T> and BI_IDoubleListImp<T>       */
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T> class _CLASSTYPE BI_DoubleListImp;

template <class T> class _CLASSTYPE BI_DoubleListBlockInitializer
{

protected:

    BI_DoubleListBlockInitializer()
        {
        PRECONDITION( count != UINT_MAX );
        if( count++ == 0 )
            BI_DoubleListElement<T>::mgr = 
                new MemBlocks( sizeof(BI_DoubleListElement<T>), 20 );
        }

    ~BI_DoubleListBlockInitializer()
        {
        PRECONDITION( count != 0 );
        if( --count == 0 )
            {
            delete BI_DoubleListElement<T>::mgr;
            BI_DoubleListElement<T>::mgr = 0;
            }
        }

    static unsigned count;

};

template <class T> unsigned BI_DoubleListBlockInitializer<T>::count = 0;

template <class T> class _CLASSTYPE BI_DoubleListElement
{

public:

    BI_DoubleListElement( T t, BI_DoubleListElement<T> _FAR *p ) :
        data(t)
        {
        next = p->next;
        prev = p;
        p->next = this;
        next->prev = this;
        }

    BI_DoubleListElement();

    BI_DoubleListElement<T> _FAR *next;
    BI_DoubleListElement<T> _FAR *prev;
    T data;

    void _FAR *operator new( size_t sz );
    void operator delete( void _FAR * );

private:

    friend class BI_DoubleListBlockInitializer<T>;

    static MemBlocks _FAR *mgr;

};

template <class T> MemBlocks _FAR *BI_DoubleListElement<T>::mgr = 0;

template <class T> inline BI_DoubleListElement<T>::BI_DoubleListElement()
{
    next = prev = 0;
}

template <class T>
void _FAR *BI_DoubleListElement<T>::operator new( size_t sz )
{
    PRECONDITION( mgr != 0 );
    return mgr->allocate( sz );
}

template <class T>
void BI_DoubleListElement<T>::operator delete( void _FAR *b )
{
    PRECONDITION( mgr != 0 );
    mgr->free( b );
}

inline BI_DoubleListElement<void _FAR *>::BI_DoubleListElement()
{
    next = prev = 0;
    data = 0;
}

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_DoubleListImp                             */
/*                                                                        */
/*  Implements a double-linked list of objects of type T.  Assumes that   */
/*  T has meaningful copy semantics and a default constructor.            */
/*                                                                        */
/*------------------------------------------------------------------------*/

template <class T> class _CLASSTYPE BI_DoubleListIteratorImp;

template <class T> class _CLASSTYPE BI_DoubleListImp :
    private BI_DoubleListBlockInitializer<T>
{

public:

    friend class BI_DoubleListIteratorImp<T>;

    BI_DoubleListImp()
        {
        initList();
        }

    ~BI_DoubleListImp()
        {
        flush();
        }

    T peekHead() const
        {
        return head.next->data;
        }

    T peekTail() const
        {
        return tail.prev->data;
        }

    void add( T );
    void addAtTail( T );
    void detach( T, int = 0 );
    void flush( int = 0 );

    int isEmpty() const
        {
        return head.next == &tail;
        }

    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:

    BI_DoubleListElement<T> head, tail;

    virtual BI_DoubleListElement<T> _FAR *findDetach( T t )
        {
        return findPred(t);
        }

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

private:

    virtual void removeData( BI_DoubleListElement<T> _FAR * )
        {
        }

    void initList();

};

template <class T> void BI_DoubleListImp<T>::initList()
{
    head.next = &tail;
    head.prev = &head;
    tail.prev = &head;
    tail.next = &tail;
}

template <class T> void BI_DoubleListImp<T>::add( T toAdd )
{
    new BI_DoubleListElement<T>( toAdd, &head );
}

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

template <class T> void BI_DoubleListImp<T>::addAtTail( T toAdd )
{
    new BI_DoubleListElement<T>( toAdd, tail.prev );
}

template <class T> void BI_DoubleListImp<T>::detach( T toDetach, int del )
{
    BI_DoubleListElement<T> _FAR *pred = findDetach( toDetach );
    BI_DoubleListElement<T> _FAR *item = pred->next;
    if( item != &tail )
        {
        pred->next = pred->next->next;
        pred->next->prev = pred;
        if( del != 0 )
            removeData( item );
        delete item;
        }
}

template <class T> void BI_DoubleListImp<T>::flush( int del )
{
    BI_DoubleListElement<T> _FAR *current = head.next;
    while( current != &tail )
        {
        BI_DoubleListElement<T> _FAR *temp = current;
        current = current->next;
        if( del != 0 )
            removeData( temp );
        delete temp;
        }
    initList();
}

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

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

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

/*------------------------------------------------------------------------*/
/*                                                                        */
/*  template <class T> class BI_SDoubleListImp                            */
/*                                                                        */
/*  Implements a sorted double-linked list of objects of type T.          */
/*  Assumes that T has meaningful copy semantics, a meaningful            */
/*  < operator, and a default constructor.                                */
/*                                                                        */
/*------------------------------------------------------------------------*/

⌨️ 快捷键说明

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