0

I am import CSV data and I am trying to create a Hash Table from it. I am having trouble setting a conditional statement to check if the bucket is empty. I can place information in a bucket, but right now it overwrites itself. Can anyone help me figure out a conditional statement so I can insert bids into my hash? This is for school, otherwise I would be using the standard library.

const unsigned int DEFAULT_SIZE = 179;

// forward declarations
double strToDouble(string str, char ch);

// define a structure to hold bid information
struct Bid {
string bidId; // unique identifier
string title;
string fund;
double amount;
Bid() {
    amount = 0.0;
}
};

/**
 * Default constructor
 */
HashTable::HashTable() {
// Initialize the structures used to hold bids
for (int i = 0; i < DEFAULT_SIZE; i++)
Table[i] = new Node;
} 

unsigned int HashTable::hash(string key, const int DEFAULT_SIZE) {
//Implement logic to calculate a hash value
int index, sum = 0;
for (int i = 0; i < key.length(); i++)
   sum += key[i];
   index = sum % DEFAULT_SIZE;
   return index;

}

void HashTable::Insert(Bid bid) {
// FIXME (5): Implement logic to insert a bid
int i;
Nodeptr newNode = new Node (bid);
int index = hash(bid.bidId, DEFAULT_SIZE);

/* if empty space */
Table[index] = newNode;
/*Else create new node and check if it null*/


}
  • 4
    Why are you writing a hash table, rather than using `std::unordered_map`? – Sneftel Jun 08 '18 at 12:58
  • If you're using this for a professional application, please use STL. The problem you're trying to solve is a very old and well-known problem, so there's no point in doing it yourself. If it's an exercise in pedantry (school), you probably should put that in the question so that you won't get a bunch of answers with people showing you how you can do this using STL. – Matt Messersmith Jun 08 '18 at 13:01
  • @MattMessersmith It's not called STL, it's called C++ Standard Library. More info on the subject in [this SO post](https://stackoverflow.com/questions/5205491/whats-the-difference-between-stl-and-c-standard-library). – Ron Jun 08 '18 at 13:06
  • Hahah, right. Sorry guys. Editing the post right now – chase dougherty Jun 08 '18 at 13:08
  • 1
    In your table constructor, `Table[i] = new Node;` - um. why do that loop unless you already have something to put there? A table full of `nullptr` is a reasonable flag that says "this bucket is empty". Regardless, a custom hashing functor for `bid` and an unordered map would make *all* of this just go away. Unrelated, your indentation in your hash computation for-loop is dreadfully misleading. Probably want to fix that. – WhozCraig Jun 08 '18 at 13:58
  • 2
    @Ron - The meaning of words change over time, especially when people insist on using them "wrong". If the C++ standard library is called STL often enough, that use becomes "right". – Bo Persson Jun 08 '18 at 13:59
  • @BoPersson I guess you are right. I stand corrected. – Ron Jun 08 '18 at 14:25
  • @Ron you're not wrong, but they are used somewhat interchangeably. I'm fully aware they are not the same thing from a purist perspective, but you'll hear people say STL colloquially when they're really referring to stdlib 99% of the time. At least they do at Lockheed Martin, I can't really speak for other tech companies. I'll try to get into the habit of saying stdlib to avoid confusion, though! – Matt Messersmith Jun 08 '18 at 14:26
  • @WhozCraig I will try to use to a customer hashing functor for bid with an unordered map. The indentation is off because I knew this would be a relatively quick fix so I didn't worry about it when I pasted it into stackoverflow. Appreciate your help. – chase dougherty Jun 08 '18 at 14:33

0 Answers0