无内存分配的单链表和双链表的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