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.

159 lines
3.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // The copyright to the contents herein is the property of Valve, L.L.C.
  4. // The contents may be used and/or copied only with the written permission of
  5. // Valve, L.L.C., or in accordance with the terms and conditions stipulated in
  6. // the agreement/contract under which the contents have been supplied.
  7. //
  8. //*****************************************************************************
  9. //
  10. // Contents:
  11. //
  12. // CSimpleBitString
  13. //
  14. // Authors:
  15. //
  16. // Target restrictions:
  17. //
  18. // Tool restrictions:
  19. //
  20. // Things to do:
  21. //
  22. //
  23. //
  24. //*****************************************************************************
  25. #ifndef INCLUDED_COMMON_SIMPLEBITSTRING_H
  26. #define INCLUDED_COMMON_SIMPLEBITSTRING_H
  27. #if defined(_MSC_VER) && (_MSC_VER > 1000)
  28. #pragma once
  29. #endif
  30. //*****************************************************************************
  31. //
  32. // Include files required by this header.
  33. //
  34. // Note: Do NOT place any 'using' directives or declarations in header files -
  35. // put them at the top of the source files that require them.
  36. // Use fully-qualified names in header files.
  37. //
  38. //*****************************************************************************
  39. // All precompiled headers (Stable.h) include this first to give use a common
  40. // place for including order-dependent library headers (things that need to go first).
  41. class CSimpleBitString
  42. {
  43. public:
  44. explicit CSimpleBitString( uint32 ReserveNumBits = 0 )
  45. :
  46. m_uNumBits( 0 ),
  47. m_vecU8()
  48. {
  49. m_vecU8.EnsureCapacity( (ReserveNumBits / 8) + 1 );
  50. m_vecU8[ m_vecU8.AddToTail() ] = 0x00; // always need 1 byte
  51. }
  52. void clear()
  53. {
  54. m_uNumBits = 0;
  55. m_vecU8.RemoveAll();
  56. m_vecU8[ m_vecU8.AddToTail() ] = 0x00; // always need 1 byte
  57. }
  58. void AppendBits( uint64 data, uint32 NumSignificantLowBitsOfData );
  59. void AppendBits( const uint8 * pData, uint32 NumBitsOfData );
  60. void ReversiblyObfusticateBitsFromStart( uint32 NumBits, const uint8 * pObfusticationData, size_t uSizeOfObfusticationData );
  61. uint8 GetByteChecksumFromStart( uint32 NumBits ) const;
  62. uint GetCurrNumBits() const
  63. {
  64. return m_uNumBits;
  65. }
  66. const uint8 * data() const
  67. {
  68. return & m_vecU8[0];
  69. }
  70. size_t size() const
  71. {
  72. return m_vecU8.Count();
  73. }
  74. private:
  75. uint32 m_uNumBits;
  76. CUtlVector<uint8> m_vecU8;
  77. public:
  78. // Iterator class for retrieving bits
  79. class iterator
  80. {
  81. public:
  82. explicit iterator( const CSimpleBitString & bs )
  83. :
  84. m_rSimpleBitString( bs ),
  85. m_uNextBitIdx( 0 )
  86. {
  87. }
  88. void ResetToStart()
  89. {
  90. m_uNextBitIdx = 0;
  91. }
  92. uint32 GetNumConsumedBits() const
  93. {
  94. return m_uNextBitIdx;
  95. }
  96. uint32 GetNumRemainingBits() const
  97. {
  98. return m_rSimpleBitString.m_uNumBits - m_uNextBitIdx;
  99. }
  100. uint32 GetNextBits( uint32 NumBitsToGet );
  101. uint64 GetNextBits64( uint32 NumBitsToGet );
  102. void SkipNextBits( uint32 NumBitsToSkip )
  103. {
  104. if ( m_uNextBitIdx + NumBitsToSkip > m_rSimpleBitString.m_uNumBits )
  105. {
  106. AssertMsg( false, "Not enough bits in CSimpleBitString" );
  107. NumBitsToSkip = 0;
  108. }
  109. m_uNextBitIdx += NumBitsToSkip;
  110. }
  111. bool operator ==( const iterator & other ) const
  112. {
  113. return (& m_rSimpleBitString == & other.m_rSimpleBitString)
  114. && m_uNextBitIdx == other.m_uNextBitIdx;
  115. }
  116. void DoAssertClassInvariant() const;
  117. private:
  118. const CSimpleBitString & m_rSimpleBitString; //lint !e1725 reference
  119. uint32 m_uNextBitIdx;
  120. };
  121. friend class iterator;
  122. };
  123. #endif