-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathHashTable.h
More file actions
86 lines (68 loc) · 1.56 KB
/
HashTable.h
File metadata and controls
86 lines (68 loc) · 1.56 KB
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
// HashTable.h: interface for the HashTable class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_HASHTABLE_H__28C8E59E_9980_478E_B40B_2D37F41FA886__INCLUDED_)
#define AFX_HASHTABLE_H__28C8E59E_9980_478E_B40B_2D37F41FA886__INCLUDED_
#include "ImageStructs.h"
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class HashEntry
{
public:
HashEntry(){Occupied = false; ent = NULL; index = -1;}
ColorEntry * ent;
int index;
bool Occupied;
};
class HashTable
{
private:
int Hash(colort c, ColorEntry * ent)
{
int hash;
hash = c;
hash = (hash << 1) ^ (int)ent ^ hash;
hash &= 8191;
return hash;
}
public:
int Find(colort c, ColorEntry * ent)
{
int hash = Hash(c,ent);
while(table[hash].Occupied)
{
if(table[hash].ent->c == c && table[hash].ent->next == ent)
return table[hash].index;
hash++;
hash &= 8191;
}
return -1;
}
void Clear()
{for(int i = 0; i < 8192; i++) table[i] = HashEntry();}
void InitTable(ColorEntry ColorMap[4096], int size)
{
int i;
ColorEntry * ce;
for(i = 0, ce = ColorMap;i < size;i++, ce++)
{
InsertEntry(ce,i);
}
}
void InsertEntry(ColorEntry * ce, int i)
{
int hash;
hash = Hash(ce->c, ce->next);
while(table[hash].Occupied)
{
hash++;
hash &= 8191;
}
table[hash].Occupied = true;
table[hash].ent = ce;
table[hash].index = i;
}
HashEntry table[8192];
};
#endif // !defined(AFX_HASHTABLE_H__28C8E59E_9980_478E_B40B_2D37F41FA886__INCLUDED_)