Notes From CS Undergrad Courses FSU
This project is maintained by awa03
Index = hash(key) % table_size;
![[Firefox_Screenshot_2023-11-17T13-55-38.899Z.png]]
hash(k) = k % 10
array[i]
, 0 ≤ i < size, is a listindex = hash(X)
array[index]
to see if X is in it.index = hash(X)
array[index]
index = hash(X)
array[index]
template <typename HashedObj>
class HashTable
{
public:
explicit HashTable(int size=101);
bool contains(HashedObj& x) const;
void makeEmpty();
bool insert(const HashedObj& x);
bool insert(HashedObj&& x);
bool remove(const HashedObj& x);
private:
vector<list<HashedObj>> theLists;
int currSize;
void rehash();
size_t myhash(const HashObj& x) const;
}
template <typename key>
class Hash {
public:
size_t operator()(const key& k) const;
};
template <>
class Hash<string>{ // Implementation
public:
size_t operator()(const string& key){
// ...
}
};
class Employee{
public:
const string& getName() const{
return name;
}
bool operator==(const Employee& rhs) const
{
return getName() == rhs.getName();
}
bool operator!=(const Employee& rhs)const {
return !(*this == rhs);
}
private:
string name;
double salary;
int seniority;
// Aditional private members
};
template<>
class hash<Employee>{
public:
size_t operator()(const Employee& item){
static hash<string> hf;
return hf(item.getName());
}
};
// Separate Chaining
size_t myhash(const HashedObj& x){
static hash<HashedObj> hf;
return hf(x) % theList.size();
}
// Separate Chaining Cont'd
// More Function Definitions
void makeEmpty(){
for(auto& theList: theList){
theList.clear();
}
}
bool contains(const HashedObj& x) const{
auto & whichList = theList[myhash(x)];
return find(begin(whichList), end(whichList) != end(whichList));
}
bool remove(const HashObj& x){
auto& whichList = theList[myhash(x)];
auto itr = find(begin(whichList), end(whichList), x);
if(itr == end(whichList)){
return false;
}
whichList.erase(itr);
--currentSize;
return true;
}
bool insert(const HashedObj& x){
auto& whichList = theList[myhash(x)];
if(find(begin(whichList), end(whichList), x) != end(whichList)){
return false;
}
whichList.push_back(x);
// rehash...
if(++currentsize > theList.size()){
rehash();
}
return true;
}
f(i) = i
- Try hash(x), hash(x) + 1, hash(x) + 2, ...
Insert (assume no duplicate keys)
Index = hash(key) % table_size;
table[index]
is empty, put informations (key and others) in entry table[index]
table[index]
is not empty then, index++; index = index % table_size; goto 2
Search (key)
Index = hash(key) % table_size;
table[index]
is empty) return -1 (not found)Else if (table[index].key == key) return index;
index++; index = index % table_size; goto 2;
![[Firefox_Screenshot_2023-11-17T23-08-09.550Z.png]]
Delete
![[Firefox_Screenshot_2023-11-17T23-10-36.771Z.png]]
// Nested within Hash Table class
enum EntryType{
ACTIVE,
EMPTY,
DELETED
};
struct HashEntry{
HashedObj element;
EntryType info;
HashedEntry(const HashedObj& e = HashedObj{}, EntryType != EMPTY)
: element{e}, info{|}{}
HashEntry(HashedObj&& e, EntryType != EMPTY)
: element{std::move(e)}, info{|}{}
};