Rehashing a hash table in c++ with quadratic probing
up vote
0
down vote
favorite
The code below is my attempt at trying to create a hash table. I'm currently stuck with the rehash function as I think it's not efficient enough (I believe it's O(n^2). I'd be grateful if someone could give some comments and suggestions on how I could improve my rehash function.
class Hashtable{
private:
int sze; //size: number of values are currently in the hashtable
int cap; //capacity: the size of the hashtable
struct HashNode{
string value;
};
HashNode** arr; //bucket
//determine whether the number is prime or not
bool IsPrime(int number){
if (number == 2 || number == 3)
return true;
if (number % 2 == 0 || number % 3 == 0)
return false;
int divisor = 6;
while (divisor * divisor - 2 * divisor + 1 <= number)
{
if (number % (divisor - 1) == 0)
return false;
if (number % (divisor + 1) == 0)
return false;
divisor += 6;
}
return true;
}
//find the next prime number that is >= a
int NextPrime(int a){
while (!IsPrime(++a)){ }
return a;
}
int hashing(const string &s) const{
int h = 0;
for (int i = 0; i < s.size(); i++)
{
h += int(s[i]);
}
return h;
}
void rehashing ()
{
int oldCap = cap;
sze = 0;
//Doubling the capacity
cap = NextPrime(cap*2);
HashNode** oldArr = arr;
arr = new HashNode*[cap]();
//moving the values to the new after rehashing
for (int i = 0; i < oldCap; i++){
if (oldArr[i] != nullptr){
for (int j = 0; j < cap; j++){
int index = (hashing(oldArr[i]->value) + j*j) % cap;
if (arr[index] == nullptr){
arr[index] = new HashNode {oldArr[i]->value};
sze++;
break;
} //if
} //for
delete oldArr[i];
oldArr[i] = nullptr;
} //if
} //for
delete oldArr;
}
public:
// Constructor
Hashtable(int ini_cap = 101) : sze(0), cap(ini_cap), arr(new HashNode*[cap]){
for (int i = 0; i < cap; i++)
{
arr[i] = nullptr;
}
}
//Destructor
~Hashtable(){
for (int i = 0; i < cap; i++){
if (arr[i] != nullptr){
delete arr[i];
arr[i] = nullptr;
}
}
delete arr;
}
double load_factor() const {return double(sze)/cap;}
void put(const string& s){
//Initialize a new node for the new input
HashNode* temp = new HashNode{s};
//Insert using quadratic probing
for (int i = 0; i < cap; i++){
int index = (hashing(s) + i*i) % cap;
if (arr[index] == nullptr){
arr[index] = temp;
sze++;
break;
}
}
if (load_factor() > 0.5){
rehashing();
} //if
} //add
};
Ideally, I think i'd be the best If I could make it O(n). So if anyone have any idea on how I could do that please tell me. Thank you.
c++ c++11 hash-table
New contributor
add a comment |
up vote
0
down vote
favorite
The code below is my attempt at trying to create a hash table. I'm currently stuck with the rehash function as I think it's not efficient enough (I believe it's O(n^2). I'd be grateful if someone could give some comments and suggestions on how I could improve my rehash function.
class Hashtable{
private:
int sze; //size: number of values are currently in the hashtable
int cap; //capacity: the size of the hashtable
struct HashNode{
string value;
};
HashNode** arr; //bucket
//determine whether the number is prime or not
bool IsPrime(int number){
if (number == 2 || number == 3)
return true;
if (number % 2 == 0 || number % 3 == 0)
return false;
int divisor = 6;
while (divisor * divisor - 2 * divisor + 1 <= number)
{
if (number % (divisor - 1) == 0)
return false;
if (number % (divisor + 1) == 0)
return false;
divisor += 6;
}
return true;
}
//find the next prime number that is >= a
int NextPrime(int a){
while (!IsPrime(++a)){ }
return a;
}
int hashing(const string &s) const{
int h = 0;
for (int i = 0; i < s.size(); i++)
{
h += int(s[i]);
}
return h;
}
void rehashing ()
{
int oldCap = cap;
sze = 0;
//Doubling the capacity
cap = NextPrime(cap*2);
HashNode** oldArr = arr;
arr = new HashNode*[cap]();
//moving the values to the new after rehashing
for (int i = 0; i < oldCap; i++){
if (oldArr[i] != nullptr){
for (int j = 0; j < cap; j++){
int index = (hashing(oldArr[i]->value) + j*j) % cap;
if (arr[index] == nullptr){
arr[index] = new HashNode {oldArr[i]->value};
sze++;
break;
} //if
} //for
delete oldArr[i];
oldArr[i] = nullptr;
} //if
} //for
delete oldArr;
}
public:
// Constructor
Hashtable(int ini_cap = 101) : sze(0), cap(ini_cap), arr(new HashNode*[cap]){
for (int i = 0; i < cap; i++)
{
arr[i] = nullptr;
}
}
//Destructor
~Hashtable(){
for (int i = 0; i < cap; i++){
if (arr[i] != nullptr){
delete arr[i];
arr[i] = nullptr;
}
}
delete arr;
}
double load_factor() const {return double(sze)/cap;}
void put(const string& s){
//Initialize a new node for the new input
HashNode* temp = new HashNode{s};
//Insert using quadratic probing
for (int i = 0; i < cap; i++){
int index = (hashing(s) + i*i) % cap;
if (arr[index] == nullptr){
arr[index] = temp;
sze++;
break;
}
}
if (load_factor() > 0.5){
rehashing();
} //if
} //add
};
Ideally, I think i'd be the best If I could make it O(n). So if anyone have any idea on how I could do that please tell me. Thank you.
c++ c++11 hash-table
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
The code below is my attempt at trying to create a hash table. I'm currently stuck with the rehash function as I think it's not efficient enough (I believe it's O(n^2). I'd be grateful if someone could give some comments and suggestions on how I could improve my rehash function.
class Hashtable{
private:
int sze; //size: number of values are currently in the hashtable
int cap; //capacity: the size of the hashtable
struct HashNode{
string value;
};
HashNode** arr; //bucket
//determine whether the number is prime or not
bool IsPrime(int number){
if (number == 2 || number == 3)
return true;
if (number % 2 == 0 || number % 3 == 0)
return false;
int divisor = 6;
while (divisor * divisor - 2 * divisor + 1 <= number)
{
if (number % (divisor - 1) == 0)
return false;
if (number % (divisor + 1) == 0)
return false;
divisor += 6;
}
return true;
}
//find the next prime number that is >= a
int NextPrime(int a){
while (!IsPrime(++a)){ }
return a;
}
int hashing(const string &s) const{
int h = 0;
for (int i = 0; i < s.size(); i++)
{
h += int(s[i]);
}
return h;
}
void rehashing ()
{
int oldCap = cap;
sze = 0;
//Doubling the capacity
cap = NextPrime(cap*2);
HashNode** oldArr = arr;
arr = new HashNode*[cap]();
//moving the values to the new after rehashing
for (int i = 0; i < oldCap; i++){
if (oldArr[i] != nullptr){
for (int j = 0; j < cap; j++){
int index = (hashing(oldArr[i]->value) + j*j) % cap;
if (arr[index] == nullptr){
arr[index] = new HashNode {oldArr[i]->value};
sze++;
break;
} //if
} //for
delete oldArr[i];
oldArr[i] = nullptr;
} //if
} //for
delete oldArr;
}
public:
// Constructor
Hashtable(int ini_cap = 101) : sze(0), cap(ini_cap), arr(new HashNode*[cap]){
for (int i = 0; i < cap; i++)
{
arr[i] = nullptr;
}
}
//Destructor
~Hashtable(){
for (int i = 0; i < cap; i++){
if (arr[i] != nullptr){
delete arr[i];
arr[i] = nullptr;
}
}
delete arr;
}
double load_factor() const {return double(sze)/cap;}
void put(const string& s){
//Initialize a new node for the new input
HashNode* temp = new HashNode{s};
//Insert using quadratic probing
for (int i = 0; i < cap; i++){
int index = (hashing(s) + i*i) % cap;
if (arr[index] == nullptr){
arr[index] = temp;
sze++;
break;
}
}
if (load_factor() > 0.5){
rehashing();
} //if
} //add
};
Ideally, I think i'd be the best If I could make it O(n). So if anyone have any idea on how I could do that please tell me. Thank you.
c++ c++11 hash-table
New contributor
The code below is my attempt at trying to create a hash table. I'm currently stuck with the rehash function as I think it's not efficient enough (I believe it's O(n^2). I'd be grateful if someone could give some comments and suggestions on how I could improve my rehash function.
class Hashtable{
private:
int sze; //size: number of values are currently in the hashtable
int cap; //capacity: the size of the hashtable
struct HashNode{
string value;
};
HashNode** arr; //bucket
//determine whether the number is prime or not
bool IsPrime(int number){
if (number == 2 || number == 3)
return true;
if (number % 2 == 0 || number % 3 == 0)
return false;
int divisor = 6;
while (divisor * divisor - 2 * divisor + 1 <= number)
{
if (number % (divisor - 1) == 0)
return false;
if (number % (divisor + 1) == 0)
return false;
divisor += 6;
}
return true;
}
//find the next prime number that is >= a
int NextPrime(int a){
while (!IsPrime(++a)){ }
return a;
}
int hashing(const string &s) const{
int h = 0;
for (int i = 0; i < s.size(); i++)
{
h += int(s[i]);
}
return h;
}
void rehashing ()
{
int oldCap = cap;
sze = 0;
//Doubling the capacity
cap = NextPrime(cap*2);
HashNode** oldArr = arr;
arr = new HashNode*[cap]();
//moving the values to the new after rehashing
for (int i = 0; i < oldCap; i++){
if (oldArr[i] != nullptr){
for (int j = 0; j < cap; j++){
int index = (hashing(oldArr[i]->value) + j*j) % cap;
if (arr[index] == nullptr){
arr[index] = new HashNode {oldArr[i]->value};
sze++;
break;
} //if
} //for
delete oldArr[i];
oldArr[i] = nullptr;
} //if
} //for
delete oldArr;
}
public:
// Constructor
Hashtable(int ini_cap = 101) : sze(0), cap(ini_cap), arr(new HashNode*[cap]){
for (int i = 0; i < cap; i++)
{
arr[i] = nullptr;
}
}
//Destructor
~Hashtable(){
for (int i = 0; i < cap; i++){
if (arr[i] != nullptr){
delete arr[i];
arr[i] = nullptr;
}
}
delete arr;
}
double load_factor() const {return double(sze)/cap;}
void put(const string& s){
//Initialize a new node for the new input
HashNode* temp = new HashNode{s};
//Insert using quadratic probing
for (int i = 0; i < cap; i++){
int index = (hashing(s) + i*i) % cap;
if (arr[index] == nullptr){
arr[index] = temp;
sze++;
break;
}
}
if (load_factor() > 0.5){
rehashing();
} //if
} //add
};
Ideally, I think i'd be the best If I could make it O(n). So if anyone have any idea on how I could do that please tell me. Thank you.
c++ c++11 hash-table
c++ c++11 hash-table
New contributor
New contributor
New contributor
asked 10 mins ago
Songg Tùng
1
1
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Songg Tùng is a new contributor. Be nice, and check out our Code of Conduct.
Songg Tùng is a new contributor. Be nice, and check out our Code of Conduct.
Songg Tùng is a new contributor. Be nice, and check out our Code of Conduct.
Songg Tùng is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208857%2frehashing-a-hash-table-in-c-with-quadratic-probing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown