高性能流媒体服务器-nebula之数据结构(6)--无节点内存分配的单、双链表

无内存分配的单链表和双链表的c++代码实现,由于不用内存分配给节点,效率很高,这stl中的list要高效得多,使用时要求每一个存储的节点包含一个Entry的实例,要存储节点必须在堆中分配内存。

// -*- C++ -*-

#ifndef _SDLIST_H_
#define _SDLIST_H_

#include 
   
   
    
    
#include 
    
    
     
     
/**
 * @class SLList
 * @brief A "memory neutral" singly-linked list,
 *
 * Uses the free space in objects to store
 * the pointers.
 */
namespace anysee {
    /**
     * @class SLList
     * @brief A "memory neutral" singly-linked list.
     */

    class SLList {
    public:
        
        INLINE SLList (void) {
            clear();
        }
        
        class Entry;
        
        /// Clear the list.
        INLINE void clear (void) {
            head.next = NULL;
        }
        
        /// Is the list empty?
        INLINE bool isEmpty (void) const {
            return (head.next == NULL);
        }
        
        /// Get the head of the list.
        INLINE Entry * get (void) {
            const Entry * e = head.next;
            if (e == NULL) {
                return NULL;
            }
            head.next = e->next;
            return (Entry *) e;
        }
        
    private:
        
        /**
         * @brief Remove one item from the list.
         * @warning This method aborts the program if called.
         */
        INLINE void remove (Entry *) {
            abort();
        }
        
    public:
        
        /// Inserts an entry into the head of the list.
        INLINE void insert (void * ePtr) {
            Entry * e = (Entry *) ePtr;
            e->next = head.next;
            head.next = e;
        }
        
        /// An entry in the list.
        class Entry {
        public:
            INLINE Entry (void)
            : next (NULL)
            {}
            //  private:
            //    Entry * prev;
        public:
            Entry * next;
        };
        
    private:
        
        /// The head of the list.
        Entry head;
        
    };
    
    /**
     *
     * @class DLList
     * @brief A "memory neutral" doubly-linked list.
     */

    class DLList {
    public:
        
        INLINE DLList (void) : mLocker(NULL) {
            clear();
        }
        INLINE DLList(SpinLockType* l) : mLocker(l) {
            clear();
        }
        DLList(const DLList& rhs) {
            clear();
            if(!rhs.isEmpty()) {
                Entry* e = rhs.head.getNext();
                e->prev = &head;
                head.next = e;
            }
            
        }
        
        DLList& operator = (const DLList& rhs)
        {
            if(this != &rhs) {
                clear();
                if (!rhs.isEmpty()) {
                    Entry* e = rhs.head.getNext();
                    e->prev = &head;
                    head.next = e;
                }
            }
            return *this;
        }
        
        class Entry;
        
        /// Clear the list.
        INLINE void clear (void) {
            head.setPrev (&head);
            head.setNext (&head);
        }
        
        /// Is the list empty?
        INLINE bool isEmpty (void) const {
            return (head.getNext() == &head);
        }
        
        INLINE Entry* begin() {
            return head.next;
        }
        
        INLINE bool isEnd(const Entry * e) const {
            return  (e == &head) ;
        }
        
        /// Get the head of the list.
        INLINE Entry * get (void) {
            const Entry * e = head.next;
            if (e == &head) {
                return NULL;
            }
            head.next = e->next;
            head.next->prev = &head;
            return (Entry *) e;
        }

        /// Remove one item from the list.
        INLINE void remove (Entry * e) {
            e->remove();
        }
        
        /// Insert an entry into the head of the list.
        INLINE void insert (Entry * e) {
            e->insert (&head, head.next);
        }
        INLINE  SpinLockType* locker() {
            return mLocker;
        }
        /// An entry in the list.
        class Entry {
        public:
            //  private:
            INLINE void setPrev (Entry * p) { assert (p != NULL); prev = p; }
            INLINE void setNext (Entry * p) { assert (p != NULL); next = p; }
            INLINE Entry * getPrev (void) const { return prev; }
            INLINE Entry * getNext (void) const { return next; }
            INLINE void remove (void) const {
                prev->setNext(next);
                next->setPrev(prev);
            }
            INLINE void insert (Entry * p, Entry * n) {
                prev = p;
                next = n;
                p->setNext (this);
                n->setPrev (this);
            }
            Entry * prev;
            Entry * next;
        };
        
    private:
        
        /// The head of the list.
        Entry            head;
        SpinLockType*    mLocker;
    };

}
#endif

    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值