| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- /************************************************************************************
- PublicHeader: OVR
- Filename : OVR_List.h
- Content : Template implementation for doubly-connected linked List
- Created : September 19, 2012
- Notes :
- Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
- Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
- you may not use the Oculus VR Rift SDK except in compliance with the License,
- which is provided at the time of installation or download, or which
- otherwise accompanies this software in either electronic or hard copy form.
- You may obtain a copy of the License at
- http://www.oculusvr.com/licenses/LICENSE-3.2
- Unless required by applicable law or agreed to in writing, the Oculus VR SDK
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ************************************************************************************/
- #ifndef OVR_List_h
- #define OVR_List_h
- #include "OVR_Types.h"
- namespace OVR {
- //-----------------------------------------------------------------------------------
- // ***** ListNode
- //
- // Base class for the elements of the intrusive linked list.
- // To store elements in the List do:
- //
- // class MyData : ListNode<MyData>
- // {
- // . . .
- // };
- template<class T>
- class ListNode
- {
- private:
- ListNode<T>* pPrev;
- ListNode<T>* pNext;
- template<class X, class B> friend class List;
- #ifdef OVR_BUILD_DEBUG
- bool marker; // Is this a marker node (rather than an actual data node)?
- #endif
- public:
- #ifdef OVR_BUILD_DEBUG
- T* GetPrev() const
- {
- if (pPrev && !pPrev->marker)
- {
- return (T*)(pPrev);
- }
- else
- {
- OVR_FAIL_M("Unstable call to ListNode<>::GetPrev() without first checking List<>IsFirst()");
- return nullptr;
- }
- }
- T* GetNext() const
- {
- if (pNext && !pNext->marker)
- {
- return (T*)(pNext);
- }
- else
- {
- OVR_FAIL_M("Unstable call to ListNode<>::GetNext() without first checking List<>IsLast()");
- return nullptr;
- }
- }
- #else
- T* GetPrev() const { return (T*)(pPrev); }
- T* GetNext() const { return (T*)(pNext); }
- #endif
- ListNode()
- {
- #ifdef OVR_BUILD_DEBUG
- marker = false; // Most nodes are data nodes, so that is the default.
- #endif
- pPrev = nullptr;
- pNext = nullptr;
- }
- bool IsInList()
- {
- return (pNext != nullptr);
- }
- void RemoveNode()
- {
- pPrev->pNext = pNext;
- pNext->pPrev = pPrev;
- pPrev = nullptr;
- pNext = nullptr;
- }
- // Removes us from the list and inserts pnew there instead.
- void ReplaceNodeWith(T* pnew)
- {
- pPrev->pNext = pnew;
- pNext->pPrev = pnew;
- pnew->pPrev = pPrev;
- pnew->pNext = pNext;
- pPrev = nullptr;
- pNext = nullptr;
- }
- // Inserts the argument linked list node after us in the list.
- void InsertNodeAfter(T* p)
- {
- p->pPrev = pNext->pPrev; // this
- p->pNext = pNext;
- pNext->pPrev = p;
- pNext = p;
- }
- // Inserts the argument linked list node before us in the list.
- void InsertNodeBefore(T* p)
- {
- p->pNext = pNext->pPrev; // this
- p->pPrev = pPrev;
- pPrev->pNext = p;
- pPrev = p;
- }
- void Alloc_MoveTo(ListNode<T>* pdest)
- {
- pdest->pNext = pNext;
- pdest->pPrev = pPrev;
- pPrev->pNext = pdest;
- pNext->pPrev = pdest;
- pPrev = nullptr;
- pNext = nullptr;
- }
- };
- //------------------------------------------------------------------------
- // ***** List
- //
- // Doubly linked intrusive list.
- // The data type must be derived from ListNode.
- //
- // Adding: PushFront(), PushBack().
- // Removing: Remove() - the element must be in the list!
- // Moving: BringToFront(), SendToBack() - the element must be in the list!
- //
- // Iterating:
- // MyData* data = MyList.GetFirst();
- // while (!MyList.IsNull(data))
- // {
- // . . .
- // data = MyList.GetNext(data);
- // }
- //
- // Removing:
- // MyData* data = MyList.GetFirst();
- // while (!MyList.IsNull(data))
- // {
- // MyData* next = MyList.GetNext(data);
- // if (ToBeRemoved(data))
- // MyList.Remove(data);
- // data = next;
- // }
- //
- // List<> represents a doubly-linked list of T, where each T must derive
- // from ListNode<B>. B specifies the base class that was directly
- // derived from ListNode, and is only necessary if there is an intermediate
- // inheritance chain.
- template<class T, class B = T> class List
- {
- public:
- typedef T ValueType;
- List()
- {
- Root.pNext = Root.pPrev = &Root;
- #ifdef OVR_BUILD_DEBUG
- Root.marker = true; // This is a marker node.
- #endif
- }
- void Clear()
- {
- Root.pNext = Root.pPrev = &Root;
- }
- size_t GetSize() const
- {
- size_t n = 0;
- for(const ListNode<B>* pNode = Root.pNext; pNode != &Root; pNode = pNode->pNext)
- ++n;
- return n;
- }
- const ValueType* GetFirst() const { return IsEmpty() ? nullptr : (const ValueType*)Root.pNext; }
- const ValueType* GetLast () const { return IsEmpty() ? nullptr : (const ValueType*)Root.pPrev; }
- ValueType* GetFirst() { return IsEmpty() ? nullptr : (ValueType*)Root.pNext; }
- ValueType* GetLast () { return IsEmpty() ? nullptr : (ValueType*)Root.pPrev; }
- // Determine if list is empty (i.e.) points to itself.
- // Go through void* access to avoid issues with strict-aliasing optimizing out the
- // access after RemoveNode(), etc.
- bool IsEmpty() const { return Root.pNext == &Root; }
- bool IsFirst(const ValueType* p) const { return p == Root.pNext; }
- bool IsLast (const ValueType* p) const { return p == Root.pPrev; }
- bool IsNull (const ListNode<B>* p) const { return p == nullptr || p == &Root; }
- inline const ValueType* GetPrev(const ValueType* p) const { return IsNull(p->pPrev) ? nullptr : (const ValueType*)p->pPrev; }
- inline const ValueType* GetNext(const ValueType* p) const { return IsNull(p->pNext) ? nullptr : (const ValueType*)p->pNext; }
- inline ValueType* GetPrev( ValueType* p) { return IsNull(p->pPrev) ? nullptr : (ValueType*)p->pPrev; }
- inline ValueType* GetNext( ValueType* p) { return IsNull(p->pNext) ? nullptr : (ValueType*)p->pNext; }
- void PushFront(ValueType* p)
- {
- p->pNext = Root.pNext;
- p->pPrev = &Root;
- Root.pNext->pPrev = p;
- Root.pNext = p;
- }
- void PushBack(ValueType* p)
- {
- p->pPrev = Root.pPrev;
- p->pNext = &Root;
- Root.pPrev->pNext = p;
- Root.pPrev = p;
- }
- static void Remove(ValueType* p)
- {
- p->pPrev->pNext = p->pNext;
- p->pNext->pPrev = p->pPrev;
- p->pPrev = nullptr;
- p->pNext = nullptr;
- }
- void BringToFront(ValueType* p)
- {
- Remove(p);
- PushFront(p);
- }
- void SendToBack(ValueType* p)
- {
- Remove(p);
- PushBack(p);
- }
- // Appends the contents of the argument list to the front of this list;
- // items are removed from the argument list.
- void PushListToFront(List<T>& src)
- {
- if (!src.IsEmpty())
- {
- ValueType* pfirst = src.GetFirst();
- ValueType* plast = src.GetLast();
- src.Clear();
- plast->pNext = Root.pNext;
- pfirst->pPrev = &Root;
- Root.pNext->pPrev = plast;
- Root.pNext = pfirst;
- }
- }
- void PushListToBack(List<T>& src)
- {
- if (!src.IsEmpty())
- {
- ValueType* pfirst = src.GetFirst();
- ValueType* plast = src.GetLast();
- src.Clear();
- plast->pNext = &Root;
- pfirst->pPrev = Root.pPrev;
- Root.pPrev->pNext = pfirst;
- Root.pPrev = plast;
- }
- }
- // Removes all source list items after (and including) the 'pfirst' node from the
- // source list and adds them to out list.
- void PushFollowingListItemsToFront(List<T>& src, ValueType *pfirst)
- {
- if (pfirst != &src.Root)
- {
- ValueType *plast = src.Root.pPrev;
- // Remove list remainder from source.
- pfirst->pPrev->pNext = &src.Root;
- src.Root.pPrev = pfirst->pPrev;
- // Add the rest of the items to list.
- plast->pNext = Root.pNext;
- pfirst->pPrev = &Root;
- Root.pNext->pPrev = plast;
- Root.pNext = pfirst;
- }
- }
- // Removes all source list items up to but NOT including the 'pend' node from the
- // source list and adds them to out list.
- void PushPrecedingListItemsToFront(List<T>& src, ValueType *ptail)
- {
- if (src.GetFirst() != ptail)
- {
- ValueType *pfirst = src.Root.pNext;
- ValueType *plast = ptail->pPrev;
- // Remove list remainder from source.
- ptail->pPrev = &src.Root;
- src.Root.pNext = ptail;
- // Add the rest of the items to list.
- plast->pNext = Root.pNext;
- pfirst->pPrev = &Root;
- Root.pNext->pPrev = plast;
- Root.pNext = pfirst;
- }
- }
- // Removes a range of source list items starting at 'pfirst' and up to, but not including 'pend',
- // and adds them to out list. Note that source items MUST already be in the list.
- void PushListItemsToFront(ValueType *pfirst, ValueType *pend)
- {
- if (pfirst != pend)
- {
- ValueType *plast = pend->pPrev;
- // Remove list remainder from source.
- pfirst->pPrev->pNext = pend;
- pend->pPrev = pfirst->pPrev;
- // Add the rest of the items to list.
- plast->pNext = Root.pNext;
- pfirst->pPrev = &Root;
- Root.pNext->pPrev = plast;
- Root.pNext = pfirst;
- }
- }
- void Alloc_MoveTo(List<T>* pdest)
- {
- if (IsEmpty())
- pdest->Clear();
- else
- {
- pdest->Root.pNext = Root.pNext;
- pdest->Root.pPrev = Root.pPrev;
- Root.pNext->pPrev = &pdest->Root;
- Root.pPrev->pNext = &pdest->Root;
- }
- }
- private:
- // Copying is prohibited
- List(const List<T>&);
- const List<T>& operator = (const List<T>&);
- ListNode<B> Root;
- };
- //------------------------------------------------------------------------
- // ***** FreeListElements
- //
- // Remove all elements in the list and free them in the allocator
- template<class List, class Allocator>
- void FreeListElements(List& list, Allocator& allocator)
- {
- typename List::ValueType* self = list.GetFirst();
- while(!list.IsNull(self))
- {
- typename List::ValueType* next = list.GetNext(self);
- allocator.Free(self);
- self = next;
- }
- list.Clear();
- }
- } // OVR
- #endif
|