-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpf_hashtable.cpp
More file actions
62 lines (57 loc) · 1.3 KB
/
Copy pathpf_hashtable.cpp
File metadata and controls
62 lines (57 loc) · 1.3 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
#include "pf_internal.h"
#include "pf_hashtable.h"
PFHashTable::PFHashTable(uint capacity)
: capacity_(capacity)
, table_(capacity)
{
}
//
// search - 在表中搜寻这样的三元组,找到了,返回即可,否则的话,返回-1
//
int PFHashTable::search(int fd, Page num)
{
int key = calcHash(fd, num);
if (key < 0) return -1;
list<Triple>& lst = table_[key];
list<Triple>::const_iterator it;
for (it = lst.begin(); it != lst.end(); it++) {
if ((it->fd == fd) && (it->num == num))
return it->slot;
}
return -1;
}
//
// insert - 往hash表中插入一个元素
//
bool PFHashTable::insert(int fd, Page num, int slot)
{
int key = calcHash(fd, num);
if (key < 0) return false;
list<Triple>& lst = table_[key];
list<Triple>::const_iterator it;
// table中不能已经存在这样的entry
for (it = lst.begin(); it != lst.end(); it++) {
if ((it->fd == fd) && (it->num == num))
return false;
}
lst.push_back(Triple(fd, num, slot));
return true;
}
//
// remove - 从hash表中移除掉一个元素
//
bool PFHashTable::remove(int fd, Page num)
{
int key = calcHash(fd, num);
if (key < 0) return false;
list<Triple>& lst = table_[key];
list<Triple>::iterator it;
// table中不能已经存在这样的entry
for (it = lst.begin(); it != lst.end(); it++) {
if ((it->fd == fd) && (it->num == num)) {
lst.erase(it);
return true;
}
}
return false;
}