00001 /* 00002 // $Id: //open/dev/fennel/common/IntrusiveDList.h#10 $ 00003 // Fennel is a library of data storage and processing components. 00004 // Copyright (C) 2005-2009 The Eigenbase Project 00005 // Copyright (C) 2005-2009 SQLstream, Inc. 00006 // Copyright (C) 2005-2009 LucidEra, Inc. 00007 // Portions Copyright (C) 1999-2009 John V. Sichi 00008 // 00009 // This program is free software; you can redistribute it and/or modify it 00010 // under the terms of the GNU General Public License as published by the Free 00011 // Software Foundation; either version 2 of the License, or (at your option) 00012 // any later version approved by The Eigenbase Project. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with this program; if not, write to the Free Software 00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 */ 00023 00024 #ifndef Fennel_IntrusiveDList_Included 00025 #define Fennel_IntrusiveDList_Included 00026 00027 FENNEL_BEGIN_NAMESPACE 00028 00029 // TODO: clean this up to follow the IntrusiveList pattern 00030 00034 class FENNEL_COMMON_EXPORT IntrusiveDListNode 00035 { 00036 IntrusiveDListNode *pPrev; 00037 IntrusiveDListNode *pNext; 00038 00039 public: 00040 explicit IntrusiveDListNode() 00041 { 00042 pPrev = pNext = NULL; 00043 } 00044 00045 IntrusiveDListNode *getNext() const 00046 { 00047 return pNext; 00048 } 00049 00050 IntrusiveDListNode *getPrev() const 00051 { 00052 return pPrev; 00053 } 00054 00055 void detach() 00056 { 00057 if (pNext) { 00058 pNext->pPrev = pPrev; 00059 } 00060 if (pPrev) { 00061 pPrev->pNext = pNext; 00062 } 00063 pPrev = pNext = NULL; 00064 } 00065 00066 void insertBefore(IntrusiveDListNode &newNext) 00067 { 00068 pNext = &newNext; 00069 pPrev = pNext->pPrev; 00070 pNext->pPrev = this; 00071 if (pPrev) { 00072 pPrev->pNext = this; 00073 } 00074 } 00075 00076 void insertAfter(IntrusiveDListNode &newPrev) 00077 { 00078 pPrev = &newPrev; 00079 pNext = pPrev->pNext; 00080 pPrev->pNext = this; 00081 if (pNext) { 00082 pNext->pPrev = this; 00083 } 00084 } 00085 }; 00086 00090 template <class T> 00091 class IntrusiveDListIter 00092 { 00093 T *curr; 00094 public: 00095 00096 explicit IntrusiveDListIter() 00097 { 00098 curr = NULL; 00099 } 00100 00101 explicit IntrusiveDListIter(T *currInit) 00102 { 00103 curr = currInit; 00104 } 00105 00106 void restart(T *currInit) 00107 { 00108 curr = currInit; 00109 } 00110 00111 void operator ++ () 00112 { 00113 curr = static_cast<T *>(curr->getNext()); 00114 } 00115 00116 void operator -- () 00117 { 00118 curr = static_cast<T *>(curr->getPrev()); 00119 } 00120 00121 T *operator -> () const 00122 { 00123 return curr; 00124 } 00125 00126 operator T * () const 00127 { 00128 return curr; 00129 } 00130 00131 T & operator * () const 00132 { 00133 return *curr; 00134 } 00135 00136 bool operator == (IntrusiveDListIter const &other) const 00137 { 00138 return curr == other.curr; 00139 } 00140 }; 00141 00152 template <class ElementT, class ReturnT> 00153 class IntrusiveTwoDListIter 00154 { 00158 ElementT *curr; 00159 00163 ElementT *next; 00164 00168 bool processingNext; 00169 00170 protected: 00171 00180 virtual ReturnT *getReturnElement(ElementT *element) const = 0; 00181 00182 public: 00183 00184 explicit IntrusiveTwoDListIter() 00185 { 00186 curr = NULL; 00187 next = NULL; 00188 processingNext = false; 00189 } 00190 00191 explicit IntrusiveTwoDListIter(ElementT *list1, ElementT *list2) 00192 { 00193 if (list1 == NULL) { 00194 curr = list2; 00195 processingNext = true; 00196 } else { 00197 curr = list1; 00198 next = list2; 00199 processingNext = false; 00200 } 00201 } 00202 00203 virtual ~IntrusiveTwoDListIter() 00204 { 00205 } 00206 00207 void operator ++ () 00208 { 00209 curr = static_cast<ElementT *>(curr->getNext()); 00210 if (curr == NULL && !processingNext) { 00211 curr = next; 00212 processingNext = true; 00213 } 00214 } 00215 00216 ReturnT *operator -> () const 00217 { 00218 return getReturnElement(curr); 00219 } 00220 00221 operator ReturnT * () const 00222 { 00223 return getReturnElement(curr); 00224 } 00225 00226 ReturnT & operator * () const 00227 { 00228 return *(getReturnElement(curr)); 00229 } 00230 00231 bool operator == (IntrusiveTwoDListIter const &other) const 00232 { 00233 return curr == other.curr; 00234 } 00235 }; 00236 00237 FENNEL_END_NAMESPACE 00238 00239 #endif 00240 00241 // End IntrusiveDList.h