> Also, I am getting a compile error with the resize function you provided.
when getting an error, provide the error message
also, would appreciate if you update in another post instead of touching the op.
1 2
|
//HashSet::resize(int new_size) {
void resize(int new_size) {
|
forgot the return type, and the HashSet:: preamble is used when defining member functions outside the class (there I was just saying «this is a member function»)
in `add()'
you may insert new elements in positions that are not already taken. Those are marked with EMPTY or NIL
however, you need to be careful of not inserting the same element twice on the set
in pseudocode
1 2 3 4 5 6 7 8 9 10 11 12
|
add(value):
if find(value): //already there, do nothing
return
hash_code = hash(value) % size
//look for a free space
for K in range(0, size):
position = (hash_code + K) % size //adapt to your collision algorithm
if table[position].state in {empty, nil}:
table[position].value = value
table[position].state = full //now this cell does have an element
break
count += 1 //we keep track of how many elements there are in the set
|
mCount holds the number of elements in your set. It should be initialised to 0 in the HashSet constructor and updated on the add() and remove() operations
in your find() function you have
1 2 3 4
|
else if (mTable[hashCode].state != Entry::EMPTY || mTable[hashCode].state == Entry::NIL) { //line 108
//loop traversing the collisions...
return true; //line 118
}
|
if you enter that if, you always return true
when traversing the collisions, if you find an empty cell you should return false.
so there are three breaking conditions:
- you found the element, return true
- you reach an empty cell, return false
- you traversed the full table, return false
1 2 3 4 5 6 7 8 9
|
find(value):
hash_code = hash(value) % size
for K in range(0, size):
position = (hash_code + K) % size
if table[position].state == empty:
return false
if table[position].state == full and table[position].value == value:
return true
return false
|
> One thing that I'm definitely unclear on is what you're suggested code is doing in main()
ok, let's start over
you have a text file and want to analyse the words used on it. For this you'll insert all those words into a set
1 2 3 4
|
//pseudocode
HashSet word_set
while read(word):
word_set.insert(word)
|
you defined your HashSet class like this
1 2
|
template <typename TValue, typename TFunc>
class HashSet
|
it is a template with two parameters
the first parameter indicates the type of the element stored in the set, so you may have a set of integers, a set of strings, a set of animals, whatever
the second parameter is the hash function, which maps each value into a position on your table, for example
1 2 3
|
size_t hash_string(string s){
return s.size();
}
|
that will hash the strings according to their length (expect a lot of collisions)
I don't know what hash function are you expected to use, so suggested std::hash
to create your set then
1 2 3 4 5 6 7
|
HashSet< //want a hash set
std::string, //that stores strings
std::hash<std::string> //using std::hash for the hash function
> word_set{ //variable name
50, //starts with a capacity of 50
std::hash<std::string>{} //and uses std::hash
};
|
now that you have a set you can operate on it
1 2 3 4 5 6 7 8 9
|
word_set.add("hello");
word_set.add("brave");
word_set.add("new");
word_set.add("world");
word_set.remove("brave");
if (word_set.find("hello"))
cout << "nice to meet you\n";
else
cout << "die heretic\n";
|