/
open_addressing.hpp
87 lines (78 loc) · 1.75 KB
/
open_addressing.hpp
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
#ifndef _OPEN_ADDRESSING_HPP
#define _OPEN_ADDRESSING_HPP
#include "../stdafx.h"
template <class T>
class OpenAddressing
{
public:
typedef int (*HashFunction)(int key, int i);
private:
class Element
{
public:
int key_;
T data_;
enum {NIL, DELETED, OCCUPIED} flag_;
Element()
{
flag_ = NIL;
}
};
vector< Element > table_;
HashFunction hash_function_;
int slot_num_;
public:
OpenAddressing(const int slot_num, const HashFunction hash_function)
: table_(slot_num)
{
hash_function_ = hash_function;
slot_num_ = slot_num;
}
int HashInsert(int key, T data)
{
int i, hash;
for (i = 0; i < slot_num_; ++i)
{
hash = hash_function_(key, i);
if (table_[hash].flag_ != Element::OCCUPIED)
{
table_[hash].flag_ = Element::OCCUPIED;
table_[hash].key_ = key;
table_[hash].data_ = data;
return hash;
}
}
return -1;
//throw "hash table overflow";
}
int HashSearch(const int key)
{
int i, hash;
for (i = 0; i < slot_num_; ++i)
{
hash = hash_function_(key, i);
if (table_[hash].flag_ == Element::NIL)
{
return -1;
}
if (table_[hash].flag_ == Element::OCCUPIED &&
table_[hash].key_ == key)
{
return hash;
}
}
return -1;
}
T HashGet(const int hash)
{
return table_[hash].data_;
}
/*
11.4-2
*/
void HashDelete(const int hash)
{
table_[hash].flag_ = Element::DELETED;
}
};
#endif