REEQ3KPC4JTH6HKA3HNEKH7UHSHVWPTUU3Q52OKF4RSD3KDNRZQAC
#include <iostream>
#include "hashtable_separate_chaining.h"
#include "hashtable_separate_chaining.h"
class Hashable {
std::string str;
int i;
public:
Hashable() : str{}, i{} {}
Hashable(std::string str, int i) : str{str}, i{i} {}
bool operator==(const Hashable& rhs) const { return str == rhs.str && i == rhs.i; }
bool operator!=(const Hashable& rhs) const { return !(*this == rhs); }
friend std::ostream& operator<<(std::ostream&, const Hashable&);
};
std::ostream& operator<<(std::ostream& os, const Hashable& hashable) {
os << "{\""<<hashable.str<<"\", "<<hashable.i<<"}";
return os;
}
struct HashableHash {
size_t operator()(const Hashable& value) const noexcept {
return sizeof value;
}
};
int main() {
HashTable<Hashable, HashableHash> table(11);
HashTable<Hashable, HashableHash> copy = table;
table = copy;
std::cout << "is empty is " << std::boolalpha << table.is_empty() << std::endl;
std::cout << "size is " << table.size() << std::endl;
table.make_empty();
table.insert(Hashable("hey there", 3));
table.remove(Hashable("i know it's hard to feel", 2));
table.contains(Hashable("like i don't care at all", 7));
std::cout << "bucket count is " << table.bucket_count() << std::endl;
std::cout << "size of bucket 0 is " << table.bucket_size(0) << std::endl;
std::cout << "bucket " << table.bucket(Hashable("where you are and how you feel", 4)) << " is where i would put a Hashable(\"where you are and how you feel\", 4), if I had one" << std::endl;
std::cout << "load factor is table.load_factor()" << std::endl;
std::cout << "max load factor is " << table.max_load_factor() << std::endl;
table.rehash(42);
table.print_table();
return 0;
}
#include <iostream>
#include "hashtable_open_addressing.h"
#include "hashtable_open_addressing.h"
class Hashable {
std::string str;
int i;
public:
Hashable() : str{}, i{} {}
Hashable(std::string str, int i) : str{str}, i{i} {}
bool operator==(const Hashable& rhs) const { return str == rhs.str && i == rhs.i; }
bool operator!=(const Hashable& rhs) const { return !(*this == rhs); }
friend std::ostream& operator<<(std::ostream&, const Hashable&);
};
std::ostream& operator<<(std::ostream& os, const Hashable& hashable) {
os << "{\""<<hashable.str<<"\", "<<hashable.i<<"}";
return os;
}
struct HashableHash {
size_t operator()(const Hashable& value) const noexcept {
return sizeof value;
}
};
int main() {
HashTable<Hashable, HashableHash> table(11);
HashTable<Hashable, HashableHash> copy = table;
table = copy;
std::cout << "is empty is " << std::boolalpha << table.is_empty() << std::endl;
std::cout << "size is " << table.size() << std::endl;
table.make_empty();
table.insert(Hashable("hey there", 3));
table.remove(Hashable("i know it's hard to feel", 2));
table.contains(Hashable("like i don't care at all", 7));
std::cout << "table size is " << table.table_size() << std::endl;
table.print_table();
return 0;
}
#include "hashtable_separate_chaining.h"
int main() {
// TODO(student): write at least 1000 lines of test
return 0;
}
#ifndef HASHTABLE_SEPARATE_CHAINING_H
#define HASHTABLE_SEPARATE_CHAINING_H
template <class Key, class Hash=std::hash<Key>>
class HashTable {
// TODO(student): implement a separate chaining hash table
};
#endif // HASHTABLE_SEPARATE_CHAINING_H
#include "hashtable_open_addressing.h"
int main() {
// TODO(student): write at least 1000 lines of test
return 0;
}
#ifndef HASHTABLE_OPEN_ADDRESSING_H
#define HASHTABLE_OPEN_ADDRESSING_H
template <class Key, class Hash=std::hash<Key>>
class HashTable {
// TODO(student): implement an open addressing hash table
};
#endif // HASHTABLE_OPEN_ADDRESSING_H
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -Weffc++ -pedantic-errors -g
objects = separate_chaining open_addressing
all: $(objects)
memory_errors: separate_chaining_memory_errors open_addressing_memory_errors
compile_test: separate_chaining_compile_test open_addressing_compile_test
clean:
rm -f *.gcov *.gcda *.gcno a.out
$(objects): %: clean hashtable_%.h hashtable_%_tests.cpp
g++ $(CXXFLAGS) --coverage hashtable_$@_tests.cpp && ./a.out && gcov -mr hashtable_$@_tests.cpp
separate_chaining_memory_errors: %_memory_errors: clean hashtable_%.h hashtable_%_tests.cpp
g++ $(CXXFLAGS) hashtable_separate_chaining_tests.cpp && valgrind --leak-check=full ./a.out
open_addressing_memory_errors: %_memory_errors: clean hashtable_%.h hashtable_%_tests.cpp
g++ $(CXXFLAGS) hashtable_open_addressing_tests.cpp && valgrind --leak-check=full ./a.out
separate_chaining_compile_test open_addressing_compile_test: %_compile_test: hashtable_%.h %_compile_test.cpp
g++ $(CXXFLAGS) $@.cpp