TheAlgorithms/C++ 1.0.0
All the 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.

Definition at line 16 of file chaining.cpp.

Member Typedef Documentation

◆ Node

using hash_chain::Node
private
Initial value:
struct Node {
int data{};
std::shared_ptr<struct Node> next;
}
int data[MAX]
test data

Define a linked node.

Definition at line 21 of file chaining.cpp.

21 {
22 int data{};
23 std::shared_ptr<struct Node> next;
24 };

Constructor & Destructor Documentation

◆ hash_chain()

hash_chain::hash_chain ( int mod)
inlineexplicit

Construct a new chain object.

Parameters
modmodulus of the chain

Definition at line 35 of file chaining.cpp.

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

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

Definition at line 45 of file chaining.cpp.

45 {
46 std::shared_ptr<Node> curr;
47 std::shared_ptr<Node> temp(new Node);
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)

◆ display()

void hash_chain::display ( )
inline

Display the chain.

Definition at line 63 of file chaining.cpp.

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;
77 std::cout << std::endl;
78 }
79 }
80 }

◆ 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

Definition at line 101 of file chaining.cpp.

101 {
102 std::shared_ptr<Node> temp = head[h];
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 }

◆ 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.

Definition at line 91 of file chaining.cpp.

91{ return x % _mod; }

Member Data Documentation

◆ _mod

int hash_chain::_mod
private

modulus of the class

Definition at line 27 of file chaining.cpp.

◆ head

std::vector<std::shared_ptr<Node> > hash_chain::head
private

array of nodes

Definition at line 26 of file chaining.cpp.


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