-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashtable.cpp
143 lines (118 loc) · 3.21 KB
/
hashtable.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "hashtable.h"
#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "bankAccount.h"
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
using namespace std;
HashTable::HashTable(int NBuckets)
{
if(NBuckets <= 0)
{
perror("size under 0");
exit(1);
}
nBuckets = NBuckets;
totalSize = 0;
pthread_mutex_init (&mtx, NULL); //arxikopoioume to mutex
buckets = new Bucket*[nBuckets]; //kataskeuazoume ena pinaka apo buckets
for(int i = 0; i < nBuckets; i++)
{
buckets[i] = new Bucket();
}
}
HashTable::~HashTable()
{
bankAccount* tmp;
for(int i = 0; i < nBuckets; i++)
{
tmp = buckets[i]->bucketList->deleteFirstNode();
while(tmp != NULL)
{
delete tmp;
tmp = buckets[i]->bucketList->deleteFirstNode();//diagrafoume ta stoixeia tou bucket
}
delete buckets[i]; //kai meta to bucket
}
delete []buckets;
pthread_mutex_destroy(&mtx);
}
int HashTable::hashFunction(std::string name)
{//djb2 hashfunction
const char *str = name.c_str();
unsigned long hash = 5381;
int c;
while ((c = *str++))
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash % nBuckets;
}
int HashTable::get_size()
{
return totalSize;
}
int HashTable::get_nBuckets()
{
return nBuckets;
}
Node<bankAccount>* HashTable::get_bucket(std::string name)
{ //epistregei thn lista pou antistixei se auto to bucket
return buckets[hashFunction(name)]->bucketList->get_begin();
}
void HashTable::lockHashtable() //kleidwnei to hashtable
{
pthread_mutex_lock(&this->mtx);
}
void HashTable::unlockHashtable() //ksekleidwnei to hashtable
{
pthread_mutex_unlock(&this->mtx);
}
void HashTable::lockBucketOf(std::string& name) //kleidwnei to bucket
{
pthread_mutex_lock(&(buckets[hashFunction(name)]->mtx));
}
void HashTable::unlockBucketOf(std::string& name) //ksekleidwnei to bucket
{
pthread_mutex_lock(&(buckets[hashFunction(name)]->mtx));
}
bankAccount* HashTable::searchNode(std::string name) //psaxnei ena stoixeio tou hashtable
{
Node<bankAccount>* tmp = buckets[hashFunction(name)]->bucketList->get_begin();
while(tmp != NULL)
{
if(*(tmp->get_data()->get_name()) == name)
{
return tmp->get_data();
}
tmp = tmp->get_next();
}
return NULL;
}
/* */
bool HashTable::insertNode(bankAccount* bAccount) //eisagei ena stoixeio sto hashtable
{
if(searchNode(*(bAccount->get_name())) != NULL) return false; //yparxei idi
totalSize++;
buckets[hashFunction(*(bAccount->get_name()))]->bucketList->insertEnd(bAccount);
return true;
}
/*//dn xreaizetai
bool HashTable::deleteNode(std::string name) //dn xreaizetai
{
Node<bankAccount>* tmp = buckets[hashFunction(name)]->get_begin();
while(tmp != NULL)
{
if(tmp->get_data()->get_name() == name)
{
//diagrafei apo opoio bankaccount mporei na exei kanei synallagi me auto
totalSize--;
delete buckets[hashFunction(Id)]->deleteNode(tmp);
return true;
}
tmp = tmp->get_next();
}
return false;
}*/