From 24cd164dd75c38b46037bb9b4b1f44e24b42cb4b Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Thu, 30 Jul 2020 18:30:02 -0400 Subject: [PATCH] better code structure --- hashing/chaining.cpp | 73 +++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/hashing/chaining.cpp b/hashing/chaining.cpp index 974b10584..7f0216886 100644 --- a/hashing/chaining.cpp +++ b/hashing/chaining.cpp @@ -2,29 +2,27 @@ * @file chaining.cpp * @author [vasutomar](https://github.com/vasutomar) * @author [Krishna Vedala](https://github.com/kvedala) - * @brief Implementation of node chains accessed using keys. + * @brief Implementation of [hash + * chains](https://en.wikipedia.org/wiki/Hash_chain). */ #include #include #include #include -namespace { // keep the code local to this file by assigning them to an unnamed - // namespace -/** - * @{ - * @brief Define a linked node - */ -struct Node { - int data{}; ///< data stored in the node - std::shared_ptr next; ///< pointer to the next node -}; - /** * @brief Chain class with a given modulus */ -class chain { +class hash_chain { private: + /** + * @brief Define a linked node + */ + using Node = struct Node { + int data{}; ///< data stored in the node + std::shared_ptr next; ///< pointer to the next node + }; + std::vector> head; ///< array of nodes int _mod; ///< modulus of the class @@ -34,7 +32,7 @@ class chain { * * @param mod modulus of the chain */ - explicit chain(int mod) : _mod(mod) { + explicit hash_chain(int mod) : _mod(mod) { while (mod--) head.push_back(nullptr); } @@ -46,7 +44,7 @@ class chain { */ void add(int x, int h) { std::shared_ptr curr; - std::shared_ptr temp(new struct Node); + std::shared_ptr temp(new Node); temp->data = x; temp->next = nullptr; if (!head[h]) { @@ -86,30 +84,49 @@ class chain { * * @param x value to compute modulus of * @return modulus of `x` + * @note declared as a + * [`virtual`](https://en.cppreference.com/w/cpp/language/virtual) so that + * custom implementations of the class can modify the hash function. */ - int hash(int x) const { return x % _mod; } + virtual int hash(int x) const { return x % _mod; } - void find(int x, int h) { - auto temp = head[h]; + /** + * @brief Find if a value and corresponding hash exist + * + * @param x value to search for + * @param h corresponding hash key + * @returns `true` if element found + * @returns `false` if element not found + */ + bool find(int x, int h) const { + std::shared_ptr temp = head[h]; if (!head[h]) { + // index does not exist! std::cout << "Element not found"; - return; + return false; } + + // scan for data value while (temp->data != x && temp->next) temp = temp->next; + if (temp->next) { std::cout << "Element found"; - } else { - if (temp->data == x) { - std::cout << "Element found"; - } else { - std::cout << "Element not found"; - } + return true; } + + // implicit else condition + // i.e., temp->next == nullptr + if (temp->data == x) { + std::cout << "Element found"; + return true; + } + + // further implicit else condition + std::cout << "Element not found"; + return false; } }; -} // namespace - /** Main function * @returns `0` always */ @@ -118,7 +135,7 @@ int main() { std::cout << "Enter the size of Hash Table. = "; std::cin >> mod; - chain mychain(mod); + hash_chain mychain(mod); bool loop = true; while (loop) {