Chain class with a given modulus.
More...
|
| 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.
|
|
|
using | Node |
| Define a linked node.
|
|
|
std::vector< std::shared_ptr< Node > > | head |
| array of nodes
|
|
int | _mod |
| modulus of the class
|
|
Chain class with a given modulus.
Definition at line 16 of file chaining.cpp.
◆ Node
Initial value:
std::shared_ptr<struct Node> next;
}
Define a linked node.
Definition at line 21 of file chaining.cpp.
21 {
23 std::shared_ptr<struct Node> next;
24 };
◆ hash_chain()
hash_chain::hash_chain |
( |
int | mod | ) |
|
|
inlineexplicit |
Construct a new chain object.
- Parameters
-
Definition at line 35 of file chaining.cpp.
36 while (mod--)
head.push_back(
nullptr);
37 }
std::vector< std::shared_ptr< Node > > head
array of nodes
int _mod
modulus of the class
◆ 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
-
x | value at the new node |
h | height 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;
53 } else {
55 while (curr->next) curr = curr->next;
56 curr->next = temp;
57 }
58 }
◆ 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++) {
68 std::cout << "Key " << i << " is empty" << std::endl;
69 } else {
70 std::cout << "Key " << i << " has values = " << std::endl;
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
-
x | value to search for |
h | corresponding 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];
104
105 std::cout << "Element not found" << std::endl;
106 return false;
107 }
108
109
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
118
119 if (temp->data == x) {
120 std::cout << "Element found" << std::endl;
121 return true;
122 }
123
124
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
-
x | value 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.
◆ _mod
◆ head
std::vector<std::shared_ptr<Node> > hash_chain::head |
|
private |
The documentation for this class was generated from the following file: