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.

93 lines
1.8 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose: hashed intrusive linked list.
  4. //
  5. // $NoKeywords: $
  6. //
  7. // Serialization/unserialization buffer
  8. //=============================================================================//
  9. #ifndef UTLNODEHASH_H
  10. #define UTLNODEHASH_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "tier1/utlmemory.h"
  15. #include "tier1/byteswap.h"
  16. #include "tier1/utlintrusivelist.h"
  17. #include <stdarg.h>
  18. // to use this class, your list node class must have a Key() function defined which returns an
  19. // integer type. May add this class to main utl tier when i'm happy w/ it.
  20. template<class T, int HASHSIZE = 7907, class K = int > class CUtlNodeHash
  21. {
  22. int m_nNumNodes;
  23. public:
  24. CUtlIntrusiveDList<T> m_HashChains[HASHSIZE];
  25. CUtlNodeHash( void )
  26. {
  27. m_nNumNodes = 0;
  28. }
  29. T *FindByKey(K nMatchKey, int *pChainNumber = NULL)
  30. {
  31. unsigned int nChain=(unsigned int) nMatchKey ;
  32. nChain %= HASHSIZE;
  33. if ( pChainNumber )
  34. *( pChainNumber ) = nChain;
  35. for( T * pNode = m_HashChains[ nChain ].m_pHead; pNode; pNode = pNode->m_pNext )
  36. if ( pNode->Key() == nMatchKey )
  37. return pNode;
  38. return NULL;
  39. }
  40. void Add( T * pNode )
  41. {
  42. unsigned int nChain=(unsigned int) pNode->Key();
  43. nChain %= HASHSIZE;
  44. m_HashChains[ nChain ].AddToHead( pNode );
  45. m_nNumNodes++;
  46. }
  47. void Purge( void )
  48. {
  49. m_nNumNodes = 0;
  50. // delete all nodes
  51. for( int i=0; i < HASHSIZE; i++)
  52. m_HashChains[i].Purge();
  53. }
  54. int Count( void ) const
  55. {
  56. return m_nNumNodes;
  57. }
  58. void DeleteByKey( K nMatchKey )
  59. {
  60. int nChain;
  61. T *pSearch = FindByKey( nMatchKey, &nChain );
  62. if ( pSearch )
  63. {
  64. m_HashChains[ nChain ].RemoveNode( pSearch );
  65. m_nNumNodes--;
  66. }
  67. }
  68. ~CUtlNodeHash( void )
  69. {
  70. // delete all lists
  71. Purge();
  72. }
  73. };
  74. #endif