Counter Strike : Global Offensive Source Code
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.

176 lines
4.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef UTLQUEUE_H
  8. #define UTLQUEUE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "utlvector.h"
  13. // T is the type stored in the queue
  14. template< class T, class M = CUtlMemory< T > >
  15. class CUtlQueue
  16. {
  17. public:
  18. // constructor: lessfunc is required, but may be set after the constructor with
  19. // SetLessFunc
  20. CUtlQueue( int growSize = 0, int initSize = 0 );
  21. CUtlQueue( T *pMemory, int numElements );
  22. // element access
  23. T& operator[]( int i );
  24. T const& operator[]( int i ) const;
  25. T& Element( int i );
  26. T const& Element( int i ) const;
  27. // return the item from the front of the queue and delete it
  28. T const& RemoveAtHead();
  29. // return the item from the end of the queue and delete it
  30. T const& RemoveAtTail();
  31. // return item at the front of the queue
  32. T const& Head();
  33. // return item at the end of the queue
  34. T const& Tail();
  35. // put a new item on the queue to the tail.
  36. void Insert( T const &element );
  37. // checks if an element of this value already exists on the queue, returns true if it does
  38. bool Check( T const element );
  39. // Returns the count of elements in the queue
  40. int Count() const { return m_heap.Count(); }
  41. // Is element index valid?
  42. bool IsIdxValid( int i ) const;
  43. // doesn't deallocate memory
  44. void RemoveAll() { m_heap.RemoveAll(); }
  45. // Memory deallocation
  46. void Purge() { m_heap.Purge(); }
  47. protected:
  48. CUtlVector<T, M> m_heap;
  49. T m_current;
  50. };
  51. //-----------------------------------------------------------------------------
  52. // The CUtlQueueFixed class:
  53. // A queue class with a fixed allocation scheme
  54. //-----------------------------------------------------------------------------
  55. template< class T, size_t MAX_SIZE >
  56. class CUtlQueueFixed : public CUtlQueue< T, CUtlMemoryFixed<T, MAX_SIZE > >
  57. {
  58. typedef CUtlQueue< T, CUtlMemoryFixed<T, MAX_SIZE > > BaseClass;
  59. public:
  60. // constructor, destructor
  61. CUtlQueueFixed( int growSize = 0, int initSize = 0 ) : BaseClass( growSize, initSize ) {}
  62. CUtlQueueFixed( T* pMemory, int numElements ) : BaseClass( pMemory, numElements ) {}
  63. };
  64. template< class T, class M >
  65. inline CUtlQueue<T, M>::CUtlQueue( int growSize, int initSize ) :
  66. m_heap(growSize, initSize)
  67. {
  68. }
  69. template< class T, class M >
  70. inline CUtlQueue<T, M>::CUtlQueue( T *pMemory, int numElements ) :
  71. m_heap(pMemory, numElements)
  72. {
  73. }
  74. //-----------------------------------------------------------------------------
  75. // element access
  76. //-----------------------------------------------------------------------------
  77. template< class T, class M >
  78. inline T& CUtlQueue<T,M>::operator[]( int i )
  79. {
  80. return m_heap[i];
  81. }
  82. template< class T, class M >
  83. inline T const& CUtlQueue<T,M>::operator[]( int i ) const
  84. {
  85. return m_heap[i];
  86. }
  87. template< class T, class M >
  88. inline T& CUtlQueue<T,M>::Element( int i )
  89. {
  90. return m_heap[i];
  91. }
  92. template< class T, class M >
  93. inline T const& CUtlQueue<T,M>::Element( int i ) const
  94. {
  95. return m_heap[i];
  96. }
  97. //-----------------------------------------------------------------------------
  98. // Is element index valid?
  99. //-----------------------------------------------------------------------------
  100. template< class T, class M >
  101. inline bool CUtlQueue<T,M>::IsIdxValid( int i ) const
  102. {
  103. return (i >= 0) && (i < m_heap.Count());
  104. }
  105. template <class T, class M>
  106. inline T const& CUtlQueue<T, M>::RemoveAtHead()
  107. {
  108. m_current = m_heap[0];
  109. m_heap.Remove((int)0);
  110. return m_current;
  111. }
  112. template <class T, class M>
  113. inline T const& CUtlQueue<T, M>::RemoveAtTail()
  114. {
  115. m_current = m_heap[ m_heap.Count() - 1 ];
  116. m_heap.Remove((int)(m_heap.Count() - 1));
  117. return m_current;
  118. }
  119. template <class T, class M>
  120. inline T const& CUtlQueue<T, M>::Head()
  121. {
  122. m_current = m_heap[0];
  123. return m_current;
  124. }
  125. template <class T, class M>
  126. inline T const& CUtlQueue<T, M>::Tail()
  127. {
  128. m_current = m_heap[ m_heap.Count() - 1 ];
  129. return m_current;
  130. }
  131. template <class T, class M>
  132. void CUtlQueue<T, M>::Insert( T const &element )
  133. {
  134. int index = m_heap.AddToTail();
  135. m_heap[index] = element;
  136. }
  137. template <class T, class M>
  138. bool CUtlQueue<T, M>::Check( T const element )
  139. {
  140. int index = m_heap.Find(element);
  141. return ( index != -1 );
  142. }
  143. #endif // UTLQUEUE_H