Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

624 lines
15 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: N-way tree container class
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #ifndef UTLNTREE_H
  9. #define UTLNTREE_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "basetypes.h"
  14. #include "utlmemory.h"
  15. #include "tier0/dbg.h"
  16. #define INVALID_NTREE_IDX ((I)~0)
  17. //-----------------------------------------------------------------------------
  18. // class CUtlNTree:
  19. // description:
  20. // A lovely index-based linked list! T is the class type, I is the index
  21. // type, which usually should be an unsigned short or smaller.
  22. //-----------------------------------------------------------------------------
  23. template <class T, class I = unsigned short>
  24. class CUtlNTree
  25. {
  26. public:
  27. typedef T ElemType_t;
  28. typedef I IndexType_t;
  29. // constructor, destructor
  30. CUtlNTree( int growSize = 0, int initSize = 0 );
  31. CUtlNTree( void *pMemory, int memsize );
  32. ~CUtlNTree( );
  33. // gets particular elements
  34. T& Element( I i );
  35. const T& Element( I i ) const;
  36. T& operator[]( I i );
  37. const T& operator[]( I i ) const;
  38. // Make sure we have a particular amount of memory
  39. void EnsureCapacity( int num );
  40. // Clears the tree, doesn't deallocate memory
  41. void RemoveAll();
  42. // Memory deallocation
  43. void Purge();
  44. // Allocation/deallocation methods
  45. I Alloc( );
  46. void Free( I elem );
  47. void FreeSubTree( I elem );
  48. // list modification
  49. void SetRoot( I root );
  50. void LinkChildBefore( I parent, I before, I elem );
  51. void LinkChildAfter( I parent, I after, I elem );
  52. void Unlink( I elem );
  53. // Alloc + link combined
  54. I InsertChildBefore( I parent, I before );
  55. I InsertChildAfter( I parent, I after );
  56. I InsertChildBefore( I parent, I before, const T &elem );
  57. I InsertChildAfter( I parent, I after, const T &elem );
  58. // Unlink + free combined
  59. void Remove( I elem );
  60. void RemoveSubTree( I elem );
  61. // invalid index
  62. inline static I InvalidIndex() { return INVALID_NTREE_IDX; }
  63. inline static size_t ElementSize() { return sizeof(Node_t); }
  64. // list statistics
  65. int Count() const;
  66. I MaxElementIndex() const;
  67. // Traversing the list
  68. I Root() const;
  69. I FirstChild( I i ) const;
  70. I PrevSibling( I i ) const;
  71. I NextSibling( I i ) const;
  72. I Parent( I i ) const;
  73. // Are nodes in the list or valid?
  74. bool IsValidIndex( I i ) const;
  75. bool IsInTree( I i ) const;
  76. protected:
  77. // What the linked list element looks like
  78. struct Node_t
  79. {
  80. T m_Element;
  81. I m_Parent;
  82. I m_FirstChild;
  83. I m_PrevSibling;
  84. I m_NextSibling;
  85. private:
  86. // No copy constructor for these...
  87. Node_t( const Node_t& );
  88. };
  89. // constructs the class
  90. void ConstructList();
  91. // Allocates the element, doesn't call the constructor
  92. I AllocInternal();
  93. // Gets at the node element....
  94. Node_t& InternalNode( I i ) { return m_Memory[i]; }
  95. const Node_t& InternalNode( I i ) const { return m_Memory[i]; }
  96. void ResetDbgInfo()
  97. {
  98. m_pElements = m_Memory.Base();
  99. }
  100. // copy constructors not allowed
  101. CUtlNTree( CUtlNTree<T, I> const& tree ) { Assert(0); }
  102. CUtlMemory<Node_t> m_Memory;
  103. I m_Root;
  104. I m_FirstFree;
  105. I m_ElementCount; // The number actually in the tree
  106. I m_MaxElementIndex; // The max index we've ever assigned
  107. // For debugging purposes;
  108. // it's in release builds so this can be used in libraries correctly
  109. Node_t *m_pElements;
  110. };
  111. //-----------------------------------------------------------------------------
  112. // constructor, destructor
  113. //-----------------------------------------------------------------------------
  114. template <class T, class I>
  115. CUtlNTree<T,I>::CUtlNTree( int growSize, int initSize ) :
  116. m_Memory(growSize, initSize)
  117. {
  118. ConstructList();
  119. ResetDbgInfo();
  120. }
  121. template <class T, class I>
  122. CUtlNTree<T,I>::CUtlNTree( void* pMemory, int memsize ) :
  123. m_Memory(pMemory, memsize/sizeof(T))
  124. {
  125. ConstructList();
  126. ResetDbgInfo();
  127. }
  128. template <class T, class I>
  129. CUtlNTree<T,I>::~CUtlNTree( )
  130. {
  131. RemoveAll();
  132. }
  133. template <class T, class I>
  134. void CUtlNTree<T,I>::ConstructList()
  135. {
  136. m_Root = InvalidIndex();
  137. m_FirstFree = InvalidIndex();
  138. m_ElementCount = m_MaxElementIndex = 0;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // gets particular elements
  142. //-----------------------------------------------------------------------------
  143. template <class T, class I>
  144. inline T& CUtlNTree<T,I>::Element( I i )
  145. {
  146. return m_Memory[i].m_Element;
  147. }
  148. template <class T, class I>
  149. inline const T& CUtlNTree<T,I>::Element( I i ) const
  150. {
  151. return m_Memory[i].m_Element;
  152. }
  153. template <class T, class I>
  154. inline T& CUtlNTree<T,I>::operator[]( I i )
  155. {
  156. return m_Memory[i].m_Element;
  157. }
  158. template <class T, class I>
  159. inline const T& CUtlNTree<T,I>::operator[]( I i ) const
  160. {
  161. return m_Memory[i].m_Element;
  162. }
  163. //-----------------------------------------------------------------------------
  164. // list statistics
  165. //-----------------------------------------------------------------------------
  166. template <class T, class I>
  167. inline int CUtlNTree<T,I>::Count() const
  168. {
  169. return m_ElementCount;
  170. }
  171. template <class T, class I>
  172. inline I CUtlNTree<T,I>::MaxElementIndex() const
  173. {
  174. return m_MaxElementIndex;
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Traversing the list
  178. //-----------------------------------------------------------------------------
  179. template <class T, class I>
  180. inline I CUtlNTree<T,I>::Root() const
  181. {
  182. return m_Root;
  183. }
  184. template <class T, class I>
  185. inline I CUtlNTree<T,I>::FirstChild( I i ) const
  186. {
  187. Assert( IsInTree(i) );
  188. return InternalNode(i).m_FirstChild;
  189. }
  190. template <class T, class I>
  191. inline I CUtlNTree<T,I>::PrevSibling( I i ) const
  192. {
  193. Assert( IsInTree(i) );
  194. return InternalNode(i).m_PrevSibling;
  195. }
  196. template <class T, class I>
  197. inline I CUtlNTree<T,I>::NextSibling( I i ) const
  198. {
  199. Assert( IsInTree(i) );
  200. return InternalNode(i).m_NextSibling;
  201. }
  202. template <class T, class I>
  203. inline I CUtlNTree<T,I>::Parent( I i ) const
  204. {
  205. Assert( IsInTree(i) );
  206. return InternalNode(i).m_Parent;
  207. }
  208. //-----------------------------------------------------------------------------
  209. // Are nodes in the list or valid?
  210. //-----------------------------------------------------------------------------
  211. template <class T, class I>
  212. inline bool CUtlNTree<T,I>::IsValidIndex( I i ) const
  213. {
  214. return (i < m_MaxElementIndex) && (i >= 0);
  215. }
  216. template <class T, class I>
  217. inline bool CUtlNTree<T,I>::IsInTree( I i ) const
  218. {
  219. return (i < m_MaxElementIndex) && (i >= 0) && (InternalNode(i).m_PrevSibling != i);
  220. }
  221. //-----------------------------------------------------------------------------
  222. // Makes sure we have enough memory allocated to store a requested # of elements
  223. //-----------------------------------------------------------------------------
  224. template< class T, class I >
  225. void CUtlNTree<T, I>::EnsureCapacity( int num )
  226. {
  227. MEM_ALLOC_CREDIT_CLASS();
  228. m_Memory.EnsureCapacity(num);
  229. ResetDbgInfo();
  230. }
  231. //-----------------------------------------------------------------------------
  232. // Deallocate memory
  233. //-----------------------------------------------------------------------------
  234. template <class T, class I>
  235. void CUtlNTree<T,I>::Purge()
  236. {
  237. RemoveAll();
  238. m_Memory.Purge( );
  239. m_FirstFree = InvalidIndex();
  240. m_MaxElementIndex = 0;
  241. ResetDbgInfo();
  242. }
  243. //-----------------------------------------------------------------------------
  244. // Node allocation/deallocation
  245. //-----------------------------------------------------------------------------
  246. template <class T, class I>
  247. I CUtlNTree<T,I>::AllocInternal( )
  248. {
  249. I elem;
  250. if ( m_FirstFree == INVALID_NTREE_IDX )
  251. {
  252. // Nothing in the free list; add.
  253. // Since nothing is in the free list, m_MaxElementIndex == total # of elements
  254. // the list knows about.
  255. if ((int)m_MaxElementIndex == m_Memory.NumAllocated())
  256. {
  257. MEM_ALLOC_CREDIT_CLASS();
  258. m_Memory.Grow();
  259. }
  260. Assert( m_MaxElementIndex != INVALID_NTREE_IDX );
  261. elem = (I)m_MaxElementIndex;
  262. ++m_MaxElementIndex;
  263. if ( elem == InvalidIndex() )
  264. {
  265. Error("CUtlNTree overflow!\n");
  266. }
  267. }
  268. else
  269. {
  270. elem = m_FirstFree;
  271. m_FirstFree = InternalNode( m_FirstFree ).m_NextSibling;
  272. }
  273. Node_t &node = InternalNode( elem );
  274. node.m_NextSibling = node.m_PrevSibling = node.m_Parent = node.m_FirstChild = INVALID_NTREE_IDX;
  275. ResetDbgInfo();
  276. // one more element baby
  277. ++m_ElementCount;
  278. return elem;
  279. }
  280. template <class T, class I>
  281. I CUtlNTree<T,I>::Alloc( )
  282. {
  283. I elem = AllocInternal();
  284. Construct( &Element(elem) );
  285. return elem;
  286. }
  287. template <class T, class I>
  288. void CUtlNTree<T,I>::Free( I elem )
  289. {
  290. Assert( IsInTree( elem ) );
  291. Unlink( elem );
  292. // If there's children, this will result in leaks. Use FreeSubTree instead.
  293. Assert( FirstChild( elem ) == INVALID_NTREE_IDX );
  294. Node_t &node = InternalNode( elem );
  295. Destruct( &node.m_Element );
  296. node.m_NextSibling = m_FirstFree;
  297. node.m_PrevSibling = elem; // Marks it as being in the free list
  298. node.m_Parent = node.m_FirstChild = INVALID_NTREE_IDX;
  299. m_FirstFree = elem;
  300. // one less element baby
  301. --m_ElementCount;
  302. }
  303. template <class T, class I>
  304. void CUtlNTree<T,I>::FreeSubTree( I elem )
  305. {
  306. Assert( IsValidIndex( elem ) );
  307. I child = FirstChild( elem );
  308. while ( child != INVALID_NTREE_IDX )
  309. {
  310. I next = NextSibling( child );
  311. FreeSubTree( child );
  312. child = next;
  313. }
  314. Free( elem );
  315. }
  316. //-----------------------------------------------------------------------------
  317. // Clears the tree
  318. //-----------------------------------------------------------------------------
  319. template <class T, class I>
  320. void CUtlNTree<T,I>::RemoveAll()
  321. {
  322. if ( m_MaxElementIndex == 0 )
  323. return;
  324. // Put everything into the free list (even unlinked things )
  325. I prev = InvalidIndex();
  326. for (int i = (int)m_MaxElementIndex; --i >= 0; prev = (I)i )
  327. {
  328. Node_t &node = InternalNode( i );
  329. if ( IsInTree( i ) )
  330. {
  331. Destruct( &node.m_Element );
  332. }
  333. node.m_NextSibling = prev;
  334. node.m_PrevSibling = (I)i; // Marks it as being in the free list
  335. node.m_Parent = node.m_FirstChild = INVALID_NTREE_IDX;
  336. }
  337. // First free points to the first element
  338. m_FirstFree = 0;
  339. // Clear everything else out
  340. m_Root = INVALID_NTREE_IDX;
  341. m_ElementCount = 0;
  342. }
  343. //-----------------------------------------------------------------------------
  344. // list modification
  345. //-----------------------------------------------------------------------------
  346. template <class T, class I>
  347. void CUtlNTree<T,I>::SetRoot( I root )
  348. {
  349. // Resetting the root while it's got stuff in it is bad...
  350. Assert( m_Root == InvalidIndex() );
  351. m_Root = root;
  352. }
  353. //-----------------------------------------------------------------------------
  354. // Links a node after a particular node
  355. //-----------------------------------------------------------------------------
  356. template <class T, class I>
  357. void CUtlNTree<T,I>::LinkChildAfter( I parent, I after, I elem )
  358. {
  359. Assert( IsInTree(elem) );
  360. // Unlink it if it's in the list at the moment
  361. Unlink(elem);
  362. Node_t& newElem = InternalNode(elem);
  363. newElem.m_Parent = parent;
  364. newElem.m_PrevSibling = after;
  365. if ( after != INVALID_NTREE_IDX )
  366. {
  367. Node_t& prevSiblingNode = InternalNode( after );
  368. newElem.m_NextSibling = prevSiblingNode.m_NextSibling;
  369. prevSiblingNode.m_NextSibling = elem;
  370. }
  371. else
  372. {
  373. if ( parent != INVALID_NTREE_IDX )
  374. {
  375. Node_t& parentNode = InternalNode( parent );
  376. newElem.m_NextSibling = parentNode.m_FirstChild;
  377. parentNode.m_FirstChild = elem;
  378. }
  379. else
  380. {
  381. newElem.m_NextSibling = m_Root;
  382. if ( m_Root != INVALID_NTREE_IDX )
  383. {
  384. Node_t& rootNode = InternalNode( m_Root );
  385. rootNode.m_PrevSibling = elem;
  386. }
  387. m_Root = elem;
  388. }
  389. }
  390. if ( newElem.m_NextSibling != INVALID_NTREE_IDX )
  391. {
  392. Node_t& nextSiblingNode = InternalNode( newElem.m_NextSibling );
  393. nextSiblingNode.m_PrevSibling = elem;
  394. }
  395. }
  396. //-----------------------------------------------------------------------------
  397. // Links a node before a particular node
  398. //-----------------------------------------------------------------------------
  399. template <class T, class I>
  400. void CUtlNTree<T,I>::LinkChildBefore( I parent, I before, I elem )
  401. {
  402. Assert( IsValidIndex(elem) );
  403. if ( before != INVALID_NTREE_IDX )
  404. {
  405. LinkChildAfter( parent, InternalNode( before ).m_PrevSibling, elem );
  406. return;
  407. }
  408. // NOTE: I made the choice to do an O(n) operation here
  409. // instead of store more data per node (LastChild).
  410. // This might not be the right choice. Revisit if we get perf problems.
  411. I after;
  412. if ( parent != INVALID_NTREE_IDX )
  413. {
  414. after = InternalNode( parent ).m_FirstChild;
  415. }
  416. else
  417. {
  418. after = m_Root;
  419. }
  420. if ( after == INVALID_NTREE_IDX )
  421. {
  422. LinkChildAfter( parent, after, elem );
  423. return;
  424. }
  425. I next = InternalNode( after ).m_NextSibling;
  426. while ( next != InvalidIndex() )
  427. {
  428. after = next;
  429. next = InternalNode( next ).m_NextSibling;
  430. }
  431. LinkChildAfter( parent, after, elem );
  432. }
  433. //-----------------------------------------------------------------------------
  434. // Unlinks a node from the tree
  435. //-----------------------------------------------------------------------------
  436. template <class T, class I>
  437. void CUtlNTree<T,I>::Unlink( I elem )
  438. {
  439. Assert( IsInTree(elem) );
  440. Node_t *pOldNode = &InternalNode( elem );
  441. // If we're the first guy, reset the head
  442. // otherwise, make our previous node's next pointer = our next
  443. if ( pOldNode->m_PrevSibling != INVALID_NTREE_IDX )
  444. {
  445. InternalNode( pOldNode->m_PrevSibling ).m_NextSibling = pOldNode->m_NextSibling;
  446. }
  447. else
  448. {
  449. if ( pOldNode->m_Parent != INVALID_NTREE_IDX )
  450. {
  451. InternalNode( pOldNode->m_Parent ).m_FirstChild = pOldNode->m_NextSibling;
  452. }
  453. else if ( m_Root == elem )
  454. {
  455. m_Root = pOldNode->m_NextSibling;
  456. }
  457. }
  458. // If we're the last guy, reset the tail
  459. // otherwise, make our next node's prev pointer = our prev
  460. if ( pOldNode->m_NextSibling != INVALID_NTREE_IDX )
  461. {
  462. InternalNode( pOldNode->m_NextSibling ).m_PrevSibling = pOldNode->m_PrevSibling;
  463. }
  464. // Unlink everything except children
  465. pOldNode->m_Parent = pOldNode->m_PrevSibling = pOldNode->m_NextSibling = INVALID_NTREE_IDX;
  466. }
  467. //-----------------------------------------------------------------------------
  468. // Alloc + link combined
  469. //-----------------------------------------------------------------------------
  470. template <class T, class I>
  471. I CUtlNTree<T,I>::InsertChildBefore( I parent, I before )
  472. {
  473. I elem = AllocInternal();
  474. Construct( &Element( elem ) );
  475. LinkChildBefore( parent, before, elem );
  476. return elem;
  477. }
  478. template <class T, class I>
  479. I CUtlNTree<T,I>::InsertChildAfter( I parent, I after )
  480. {
  481. I elem = AllocInternal();
  482. Construct( &Element( elem ) );
  483. LinkChildAfter( parent, after, elem );
  484. return elem;
  485. }
  486. template <class T, class I>
  487. I CUtlNTree<T,I>::InsertChildBefore( I parent, I before, const T &data )
  488. {
  489. I elem = AllocInternal();
  490. CopyConstruct( &Element( elem ), data );
  491. LinkChildBefore( parent, before, elem );
  492. return elem;
  493. }
  494. template <class T, class I>
  495. I CUtlNTree<T,I>::InsertChildAfter( I parent, I after, const T &data )
  496. {
  497. I elem = AllocInternal();
  498. CopyConstruct( &Element( elem ), data );
  499. LinkChildAfter( parent, after, elem );
  500. return elem;
  501. }
  502. //-----------------------------------------------------------------------------
  503. // Unlink + free combined
  504. //-----------------------------------------------------------------------------
  505. template <class T, class I>
  506. void CUtlNTree<T,I>::Remove( I elem )
  507. {
  508. Unlink( elem );
  509. Free( elem );
  510. }
  511. template <class T, class I>
  512. void CUtlNTree<T,I>::RemoveSubTree( I elem )
  513. {
  514. UnlinkSubTree( elem );
  515. Free( elem );
  516. }
  517. #endif // UTLNTREE_H