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.










share|improve this question







New contributor




Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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.










    share|improve this question







    New contributor




    Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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.










      share|improve this question







      New contributor




      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question







      New contributor




      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 10 mins ago









      Songg Tùng

      1




      1




      New contributor




      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Songg Tùng is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Songg Tùng is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          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






























          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.










          draft saved

          draft discarded


















          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python