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.

134 lines
2.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Holds the enumerated list of default cursors
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef DAR_H
  8. #define DAR_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <vgui/VGUI.h>
  15. #include "tier1/utlvector.h"
  16. #include "tier0/memdbgon.h"
  17. namespace vgui
  18. {
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Simple lightweight dynamic array implementation
  21. //-----------------------------------------------------------------------------
  22. template<class ELEMTYPE> class Dar : public CUtlVector< ELEMTYPE >
  23. {
  24. typedef CUtlVector< ELEMTYPE > BaseClass;
  25. public:
  26. Dar()
  27. {
  28. }
  29. Dar(int initialCapacity) :
  30. BaseClass( 0, initialCapacity )
  31. {
  32. }
  33. public:
  34. void SetCount(int count)
  35. {
  36. this->EnsureCount( count );
  37. }
  38. int GetCount()
  39. {
  40. return this->Count();
  41. }
  42. int AddElement(ELEMTYPE elem)
  43. {
  44. return this->AddToTail( elem );
  45. }
  46. void MoveElementToEnd( ELEMTYPE elem )
  47. {
  48. if ( this->Count() == 0 )
  49. return;
  50. // quick check to see if it's already at the end
  51. if ( this->Element( this->Count() - 1 ) == elem )
  52. return;
  53. int idx = this->Find( elem );
  54. if ( idx == this->InvalidIndex() )
  55. return;
  56. this->Remove( idx );
  57. this->AddToTail( elem );
  58. }
  59. // returns the index of the element in the array, -1 if not found
  60. int FindElement(ELEMTYPE elem)
  61. {
  62. return this->Find( elem );
  63. }
  64. bool HasElement(ELEMTYPE elem)
  65. {
  66. if ( this->FindElement(elem) != this->InvalidIndex() )
  67. {
  68. return true;
  69. }
  70. return false;
  71. }
  72. int PutElement(ELEMTYPE elem)
  73. {
  74. int index = this->FindElement(elem);
  75. if (index >= 0)
  76. {
  77. return index;
  78. }
  79. return this->AddElement(elem);
  80. }
  81. // insert element at index and move all the others down 1
  82. void InsertElementAt(ELEMTYPE elem,int index)
  83. {
  84. this->InsertBefore( index, elem );
  85. }
  86. void SetElementAt(ELEMTYPE elem,int index)
  87. {
  88. this->EnsureCount( index + 1 );
  89. this->Element( index ) = elem;
  90. }
  91. void RemoveElementAt(int index)
  92. {
  93. this->Remove( index );
  94. }
  95. void RemoveElementsBefore(int index)
  96. {
  97. if ( index <= 0 )
  98. return;
  99. this->RemoveMultiple( 0, index - 1 );
  100. }
  101. void RemoveElement(ELEMTYPE elem)
  102. {
  103. this->FindAndRemove( elem );
  104. }
  105. void *GetBaseData()
  106. {
  107. return this->Base();
  108. }
  109. void CopyFrom(Dar<ELEMTYPE> &dar)
  110. {
  111. this->CopyArray( dar.Base(), dar.Count() );
  112. }
  113. };
  114. }
  115. #include "tier0/memdbgoff.h"
  116. #endif // DAR_H