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. More...
void | add (int x, int h)
| create and add a new node with a give value and at a given height More...
void | display ()
| Display the chain.
virtual int | hash (int x) const
| Compute the hash of a value for current chain. More...
bool | find (int x, int h) const
| Find if a value and corresponding hash exist. More...

## Private Types

using | Node = struct Node { int data{}
| data stored in the node

## Private Attributes

std::shared_ptr< struct Node > | next
| pointer to the next node
std::vector< std::shared_ptr< Node > > | head
| array of nodes
int | _mod
| modulus of the class

## Detailed Description

Chain class with a given modulus.

## Constructor & Destructor Documentation

## hash_chain()

hash_chain::hash_chain | ( | int | mod | ) |

inlineexplicit

Construct a new chain object.

Parameters

  mod | modulus of the chain

: _mod(mod) {
while (mod--) head.push_back(nullptr);
}
hash_chain::head
std::vector< std::shared_ptr< Node > > head
array of nodes
Definition: chaining.cpp:24
hash_chain::_mod
int _mod
modulus of the class
Definition: chaining.cpp:27
std::vector::push_back
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

  x | value at the new node
  h | height of the node

{
std::shared_ptr<Node> curr;
std::shared_ptr<Node> temp(new Node);
temp->data = x;
temp->next = nullptr;
if (!head[h]) {
head[h] = temp;
curr = head[h];
} else {
curr = head[h];
while (curr->next) curr = curr->next;
curr->next = temp;
}
}
h
int h(int key)
Definition: hash_search.cpp:45
std::shared_ptr< Node >
Node
Definition: linkedlist_implentation_usingarray.cpp:14
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

  x | value to search for
  h | corresponding hash key

Returns
  `true` if element found
  `false` if element not found

{
std::shared_ptr<Node> temp = head[h];
if (!head[h]) {
// index does not exist!
std::cout << "Element not found";
return false;
}
// scan for data value
while (temp->data != x && temp->next) temp = temp->next;
if (temp->next) {
std::cout << "Element 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;
}
std::cout
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

  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.

{ return x % _mod; }

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

- hashing/chaining.cpp