summaryrefslogtreecommitdiff
path: root/utils/shadercompile/utlnodehash.h
blob: 0e59a0b3a2d0fcdc9376d2bb97a2a3a34436ea6d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: hashed intrusive linked list.
//
// $NoKeywords: $
//
// Serialization/unserialization buffer
//=============================================================================//

#ifndef UTLNODEHASH_H
#define UTLNODEHASH_H

#ifdef _WIN32
#pragma once
#endif

#include "tier1/utlmemory.h"
#include "tier1/byteswap.h"
#include "tier1/utlintrusivelist.h"

#include <stdarg.h>

// to use this class, your list node class must have a Key() function defined which returns an
// integer type. May add this class to main utl tier when i'm happy w/ it.
template<class T, int HASHSIZE = 7907, class K = int > class CUtlNodeHash
{

	int m_nNumNodes;

public:

	CUtlIntrusiveDList<T> m_HashChains[HASHSIZE];

	CUtlNodeHash( void )
	{
		m_nNumNodes = 0;
	}


	T *FindByKey(K nMatchKey, int *pChainNumber = NULL)
	{
		unsigned int nChain=(unsigned int) nMatchKey ;
		nChain %= HASHSIZE;
		if ( pChainNumber )
			*( pChainNumber ) = nChain;
		for( T * pNode = m_HashChains[ nChain ].m_pHead; pNode; pNode = pNode->m_pNext )
			if ( pNode->Key() == nMatchKey )
				return pNode;
		return NULL;
	}

	void Add( T * pNode )
	{
		unsigned int nChain=(unsigned int) pNode->Key();
		nChain %= HASHSIZE;
		m_HashChains[ nChain ].AddToHead( pNode );
		m_nNumNodes++;
	}


	void Purge( void )
	{
		m_nNumNodes = 0;
		// delete all nodes
		for( int i=0; i < HASHSIZE; i++)
			m_HashChains[i].Purge();
	}

	int Count( void ) const
	{
		return m_nNumNodes;
	}

	void DeleteByKey( K nMatchKey )
	{
		int nChain;
		T *pSearch = FindByKey( nMatchKey, &nChain );
		if ( pSearch )
		{
			m_HashChains[ nChain ].RemoveNode( pSearch );
			m_nNumNodes--;
		}
	}

	~CUtlNodeHash( void )
	{
		// delete all lists
		Purge();
	}
};


#endif