Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
hash_chain Class Reference

Chain class with a given modulus. More...

Collaboration diagram for hash_chain:
[legend]

Public Member Functions

 hash_chain (int mod)
 Construct a new chain object.
 
void add (int x, int h)
 create and add a new node with a give value and at a given height
 
void display ()
 Display the chain.
 
virtual int hash (int x) const
 Compute the hash of a value for current chain.
 
bool find (int x, int h) const
 Find if a value and corresponding hash exist.
 

Private Types

using Node
 Define a linked node.
 

Private Attributes

std::vector< std::shared_ptr< Node > > head
 array of nodes
 
int _mod
 modulus of the class
 

Detailed Description

Chain class with a given modulus.

Member Typedef Documentation

◆ Node

using hash_chain::Node
private
Initial value:
struct Node {
int data{};
}
int data[MAX]
test data
Definition hash_search.cpp:24
Definition linkedlist_implentation_usingarray.cpp:14

Define a linked node.

21 {
22 int data{}; ///< data stored in the node
23 std::shared_ptr<struct Node> next; ///< pointer to the next node
24 };
T next(T... args)

Constructor & Destructor Documentation

◆ hash_chain()

hash_chain::hash_chain ( int mod)
inlineexplicit

Construct a new chain object.

Parameters
modmodulus of the chain
35 : _mod(mod) {
36 while (mod--) head.push_back(nullptr);
37 }
std::vector< std::shared_ptr< Node > > head
array of nodes
Definition chaining.cpp:26
int _mod
modulus of the class
Definition chaining.cpp:27
T push_back(T... args)
Here is the call graph for this function:

Member Function Documentation

◆ add()

void hash_chain::add ( int x,
int h )
inline

create and add a new node with a give value and at a given height

Parameters
xvalue at the new node
hheight of the node
45 {
48 temp->data = x;
49 temp->next = nullptr;
50 if (!head[h]) {
51 head[h] = temp;
52 curr = head[h];
53 } else {
54 curr = head[h];
55 while (curr->next) curr = curr->next;
56 curr->next = temp;
57 }
58 }
int h(int key)
Definition hash_search.cpp:45
Here is the call graph for this function:

◆ display()

void hash_chain::display ( )
inline

Display the chain.

63 {
64 std::shared_ptr<Node> temp = nullptr;
65 int i = 0;
66 for (i = 0; i < _mod; i++) {
67 if (!head[i]) {
68 std::cout << "Key " << i << " is empty" << std::endl;
69 } else {
70 std::cout << "Key " << i << " has values = " << std::endl;
71 temp = head[i];
72 while (temp->next) {
73 std::cout << temp->data << " " << std::endl;
74 temp = temp->next;
75 }
76 std::cout << temp->data;
78 }
79 }
80 }
T endl(T... args)
Here is the call graph for this function:

◆ find()

bool hash_chain::find ( int x,
int h ) const
inline

Find if a value and corresponding hash exist.

Parameters
xvalue to search for
hcorresponding hash key
Returns
true if element found
false if element not found
101 {
103 if (!head[h]) {
104 // index does not exist!
105 std::cout << "Element not found" << std::endl;
106 return false;
107 }
108
109 // scan for data value
110 while (temp->data != x && temp->next) temp = temp->next;
111
112 if (temp->next) {
113 std::cout << "Element found" << std::endl;
114 return true;
115 }
116
117 // implicit else condition
118 // i.e., temp->next == nullptr
119 if (temp->data == x) {
120 std::cout << "Element found" << std::endl;
121 return true;
122 }
123
124 // further implicit else condition
125 std::cout << "Element not found" << std::endl;
126 return false;
127 }
Here is the call graph for this function:

◆ hash()

virtual int hash_chain::hash ( int x) const
inlinevirtual

Compute the hash of a value for current chain.

Parameters
xvalue to compute modulus of
Returns
modulus of x
Note
declared as a virtual so that custom implementations of the class can modify the hash function.
91{ return x % _mod; }

The documentation for this class was generated from the following file: