OVR_List.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /************************************************************************************
  2. PublicHeader: OVR
  3. Filename : OVR_List.h
  4. Content : Template implementation for doubly-connected linked List
  5. Created : September 19, 2012
  6. Notes :
  7. Copyright : Copyright 2014 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_List_h
  21. #define OVR_List_h
  22. #include "OVR_Types.h"
  23. namespace OVR {
  24. //-----------------------------------------------------------------------------------
  25. // ***** ListNode
  26. //
  27. // Base class for the elements of the intrusive linked list.
  28. // To store elements in the List do:
  29. //
  30. // class MyData : ListNode<MyData>
  31. // {
  32. // . . .
  33. // };
  34. template<class T>
  35. class ListNode
  36. {
  37. private:
  38. ListNode<T>* pPrev;
  39. ListNode<T>* pNext;
  40. template<class X, class B> friend class List;
  41. #ifdef OVR_BUILD_DEBUG
  42. bool marker; // Is this a marker node (rather than an actual data node)?
  43. #endif
  44. public:
  45. #ifdef OVR_BUILD_DEBUG
  46. T* GetPrev() const
  47. {
  48. if (pPrev && !pPrev->marker)
  49. {
  50. return (T*)(pPrev);
  51. }
  52. else
  53. {
  54. OVR_FAIL_M("Unstable call to ListNode<>::GetPrev() without first checking List<>IsFirst()");
  55. return nullptr;
  56. }
  57. }
  58. T* GetNext() const
  59. {
  60. if (pNext && !pNext->marker)
  61. {
  62. return (T*)(pNext);
  63. }
  64. else
  65. {
  66. OVR_FAIL_M("Unstable call to ListNode<>::GetNext() without first checking List<>IsLast()");
  67. return nullptr;
  68. }
  69. }
  70. #else
  71. T* GetPrev() const { return (T*)(pPrev); }
  72. T* GetNext() const { return (T*)(pNext); }
  73. #endif
  74. ListNode()
  75. {
  76. #ifdef OVR_BUILD_DEBUG
  77. marker = false; // Most nodes are data nodes, so that is the default.
  78. #endif
  79. pPrev = nullptr;
  80. pNext = nullptr;
  81. }
  82. bool IsInList()
  83. {
  84. return (pNext != nullptr);
  85. }
  86. void RemoveNode()
  87. {
  88. pPrev->pNext = pNext;
  89. pNext->pPrev = pPrev;
  90. pPrev = nullptr;
  91. pNext = nullptr;
  92. }
  93. // Removes us from the list and inserts pnew there instead.
  94. void ReplaceNodeWith(T* pnew)
  95. {
  96. pPrev->pNext = pnew;
  97. pNext->pPrev = pnew;
  98. pnew->pPrev = pPrev;
  99. pnew->pNext = pNext;
  100. pPrev = nullptr;
  101. pNext = nullptr;
  102. }
  103. // Inserts the argument linked list node after us in the list.
  104. void InsertNodeAfter(T* p)
  105. {
  106. p->pPrev = pNext->pPrev; // this
  107. p->pNext = pNext;
  108. pNext->pPrev = p;
  109. pNext = p;
  110. }
  111. // Inserts the argument linked list node before us in the list.
  112. void InsertNodeBefore(T* p)
  113. {
  114. p->pNext = pNext->pPrev; // this
  115. p->pPrev = pPrev;
  116. pPrev->pNext = p;
  117. pPrev = p;
  118. }
  119. void Alloc_MoveTo(ListNode<T>* pdest)
  120. {
  121. pdest->pNext = pNext;
  122. pdest->pPrev = pPrev;
  123. pPrev->pNext = pdest;
  124. pNext->pPrev = pdest;
  125. pPrev = nullptr;
  126. pNext = nullptr;
  127. }
  128. };
  129. //------------------------------------------------------------------------
  130. // ***** List
  131. //
  132. // Doubly linked intrusive list.
  133. // The data type must be derived from ListNode.
  134. //
  135. // Adding: PushFront(), PushBack().
  136. // Removing: Remove() - the element must be in the list!
  137. // Moving: BringToFront(), SendToBack() - the element must be in the list!
  138. //
  139. // Iterating:
  140. // MyData* data = MyList.GetFirst();
  141. // while (!MyList.IsNull(data))
  142. // {
  143. // . . .
  144. // data = MyList.GetNext(data);
  145. // }
  146. //
  147. // Removing:
  148. // MyData* data = MyList.GetFirst();
  149. // while (!MyList.IsNull(data))
  150. // {
  151. // MyData* next = MyList.GetNext(data);
  152. // if (ToBeRemoved(data))
  153. // MyList.Remove(data);
  154. // data = next;
  155. // }
  156. //
  157. // List<> represents a doubly-linked list of T, where each T must derive
  158. // from ListNode<B>. B specifies the base class that was directly
  159. // derived from ListNode, and is only necessary if there is an intermediate
  160. // inheritance chain.
  161. template<class T, class B = T> class List
  162. {
  163. public:
  164. typedef T ValueType;
  165. List()
  166. {
  167. Root.pNext = Root.pPrev = &Root;
  168. #ifdef OVR_BUILD_DEBUG
  169. Root.marker = true; // This is a marker node.
  170. #endif
  171. }
  172. void Clear()
  173. {
  174. Root.pNext = Root.pPrev = &Root;
  175. }
  176. size_t GetSize() const
  177. {
  178. size_t n = 0;
  179. for(const ListNode<B>* pNode = Root.pNext; pNode != &Root; pNode = pNode->pNext)
  180. ++n;
  181. return n;
  182. }
  183. const ValueType* GetFirst() const { return IsEmpty() ? nullptr : (const ValueType*)Root.pNext; }
  184. const ValueType* GetLast () const { return IsEmpty() ? nullptr : (const ValueType*)Root.pPrev; }
  185. ValueType* GetFirst() { return IsEmpty() ? nullptr : (ValueType*)Root.pNext; }
  186. ValueType* GetLast () { return IsEmpty() ? nullptr : (ValueType*)Root.pPrev; }
  187. // Determine if list is empty (i.e.) points to itself.
  188. // Go through void* access to avoid issues with strict-aliasing optimizing out the
  189. // access after RemoveNode(), etc.
  190. bool IsEmpty() const { return Root.pNext == &Root; }
  191. bool IsFirst(const ValueType* p) const { return p == Root.pNext; }
  192. bool IsLast (const ValueType* p) const { return p == Root.pPrev; }
  193. bool IsNull (const ListNode<B>* p) const { return p == nullptr || p == &Root; }
  194. inline const ValueType* GetPrev(const ValueType* p) const { return IsNull(p->pPrev) ? nullptr : (const ValueType*)p->pPrev; }
  195. inline const ValueType* GetNext(const ValueType* p) const { return IsNull(p->pNext) ? nullptr : (const ValueType*)p->pNext; }
  196. inline ValueType* GetPrev( ValueType* p) { return IsNull(p->pPrev) ? nullptr : (ValueType*)p->pPrev; }
  197. inline ValueType* GetNext( ValueType* p) { return IsNull(p->pNext) ? nullptr : (ValueType*)p->pNext; }
  198. void PushFront(ValueType* p)
  199. {
  200. p->pNext = Root.pNext;
  201. p->pPrev = &Root;
  202. Root.pNext->pPrev = p;
  203. Root.pNext = p;
  204. }
  205. void PushBack(ValueType* p)
  206. {
  207. p->pPrev = Root.pPrev;
  208. p->pNext = &Root;
  209. Root.pPrev->pNext = p;
  210. Root.pPrev = p;
  211. }
  212. static void Remove(ValueType* p)
  213. {
  214. p->pPrev->pNext = p->pNext;
  215. p->pNext->pPrev = p->pPrev;
  216. p->pPrev = nullptr;
  217. p->pNext = nullptr;
  218. }
  219. void BringToFront(ValueType* p)
  220. {
  221. Remove(p);
  222. PushFront(p);
  223. }
  224. void SendToBack(ValueType* p)
  225. {
  226. Remove(p);
  227. PushBack(p);
  228. }
  229. // Appends the contents of the argument list to the front of this list;
  230. // items are removed from the argument list.
  231. void PushListToFront(List<T>& src)
  232. {
  233. if (!src.IsEmpty())
  234. {
  235. ValueType* pfirst = src.GetFirst();
  236. ValueType* plast = src.GetLast();
  237. src.Clear();
  238. plast->pNext = Root.pNext;
  239. pfirst->pPrev = &Root;
  240. Root.pNext->pPrev = plast;
  241. Root.pNext = pfirst;
  242. }
  243. }
  244. void PushListToBack(List<T>& src)
  245. {
  246. if (!src.IsEmpty())
  247. {
  248. ValueType* pfirst = src.GetFirst();
  249. ValueType* plast = src.GetLast();
  250. src.Clear();
  251. plast->pNext = &Root;
  252. pfirst->pPrev = Root.pPrev;
  253. Root.pPrev->pNext = pfirst;
  254. Root.pPrev = plast;
  255. }
  256. }
  257. // Removes all source list items after (and including) the 'pfirst' node from the
  258. // source list and adds them to out list.
  259. void PushFollowingListItemsToFront(List<T>& src, ValueType *pfirst)
  260. {
  261. if (pfirst != &src.Root)
  262. {
  263. ValueType *plast = src.Root.pPrev;
  264. // Remove list remainder from source.
  265. pfirst->pPrev->pNext = &src.Root;
  266. src.Root.pPrev = pfirst->pPrev;
  267. // Add the rest of the items to list.
  268. plast->pNext = Root.pNext;
  269. pfirst->pPrev = &Root;
  270. Root.pNext->pPrev = plast;
  271. Root.pNext = pfirst;
  272. }
  273. }
  274. // Removes all source list items up to but NOT including the 'pend' node from the
  275. // source list and adds them to out list.
  276. void PushPrecedingListItemsToFront(List<T>& src, ValueType *ptail)
  277. {
  278. if (src.GetFirst() != ptail)
  279. {
  280. ValueType *pfirst = src.Root.pNext;
  281. ValueType *plast = ptail->pPrev;
  282. // Remove list remainder from source.
  283. ptail->pPrev = &src.Root;
  284. src.Root.pNext = ptail;
  285. // Add the rest of the items to list.
  286. plast->pNext = Root.pNext;
  287. pfirst->pPrev = &Root;
  288. Root.pNext->pPrev = plast;
  289. Root.pNext = pfirst;
  290. }
  291. }
  292. // Removes a range of source list items starting at 'pfirst' and up to, but not including 'pend',
  293. // and adds them to out list. Note that source items MUST already be in the list.
  294. void PushListItemsToFront(ValueType *pfirst, ValueType *pend)
  295. {
  296. if (pfirst != pend)
  297. {
  298. ValueType *plast = pend->pPrev;
  299. // Remove list remainder from source.
  300. pfirst->pPrev->pNext = pend;
  301. pend->pPrev = pfirst->pPrev;
  302. // Add the rest of the items to list.
  303. plast->pNext = Root.pNext;
  304. pfirst->pPrev = &Root;
  305. Root.pNext->pPrev = plast;
  306. Root.pNext = pfirst;
  307. }
  308. }
  309. void Alloc_MoveTo(List<T>* pdest)
  310. {
  311. if (IsEmpty())
  312. pdest->Clear();
  313. else
  314. {
  315. pdest->Root.pNext = Root.pNext;
  316. pdest->Root.pPrev = Root.pPrev;
  317. Root.pNext->pPrev = &pdest->Root;
  318. Root.pPrev->pNext = &pdest->Root;
  319. }
  320. }
  321. private:
  322. // Copying is prohibited
  323. List(const List<T>&);
  324. const List<T>& operator = (const List<T>&);
  325. ListNode<B> Root;
  326. };
  327. //------------------------------------------------------------------------
  328. // ***** FreeListElements
  329. //
  330. // Remove all elements in the list and free them in the allocator
  331. template<class List, class Allocator>
  332. void FreeListElements(List& list, Allocator& allocator)
  333. {
  334. typename List::ValueType* self = list.GetFirst();
  335. while(!list.IsNull(self))
  336. {
  337. typename List::ValueType* next = list.GetNext(self);
  338. allocator.Free(self);
  339. self = next;
  340. }
  341. list.Clear();
  342. }
  343. } // OVR
  344. #endif