How to Use - LBeghini/Hash-Functions GitHub Wiki

Functions

Creating tables:

The first thing asked by the program is the size of the hash table, and has some stricted rules:

  • Integer
  • Greater than 1

Example

Table size: 12

โ•’โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ••
โ”‚ 0   โ”‚ 1   โ”‚ 2   โ”‚ 3   โ”‚ 4   โ”‚ 5   โ”‚ 6   โ”‚ 7   โ”‚ 8   โ”‚ 9   โ”‚ 10   โ”‚ 11   โ”‚
โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ก
โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚      โ”‚      โ”‚
โ•˜โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•›

According to the table size, the rule of the hash function changes.

If the table size is a prime number, then the rule is set to be the mod of the table size:

h(x) = x mod TABLE.size

If the table size is not a prime number, then rule is set to be the mod of the previous table size prime number:

h(x) = x mod previous_prime(TABLE.size)

Inserting

The insertion function receives an integer, and according to the defined hash function, it will try to put that number in its correct index. If a key is taking its place, it will try to place it in the nearest neighbors, until it finds an empty place or until the entire table has already been searched, meaning that it is full.

Commands

-i, --insert

Key

Any integer positive number

Structure

<COMMAND> [key]

Return

Returns 0 if hash table is full Returns -1 if the key already exists in table

Example

OPERATION: -i 12

or

OPERATION: --insert 12

Result

โ•’โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ••
โ”‚ 0   โ”‚ 1   โ”‚ 2   โ”‚ 3   โ”‚ 4   โ”‚ 5   โ”‚ 6   โ”‚ 7   โ”‚ 8   โ”‚ 9   โ”‚ 10   โ”‚ 11   โ”‚
โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ก
โ”‚  12 โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚      โ”‚      โ”‚
โ•˜โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•›

Searching

The search function receives a key and will try to find the key in its correct index. If that index already has an key that is not that key that's been searched, it will try to find it in the nearest neighbors, until the entire table has been searched, meaning that the key is not there.

Commands

-s, --search

Key

Any integer positive number

Structure

<COMMAND> [key]

Return

Returns the index of the searched key
Returns the error Not found if key not found

Example

OPERATION: -s 12

or

OPERATION: --search 12

Result

RESULT: 0

Deleting

The delete function receives a key integer and will use the search function to get the index. If the index is found, it will remove the key and reset the table. Reset means that it will remove all keys in the table and insert it again, to fix the positions.

Commands

-d, --delete

Key

Any integer positive number

Structure

<COMMAND> [key]

Return

Returns 0 if key not found

Example

OPERATION: -d 12

or

OPERATION: --delete 12

Result

โ•’โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ•คโ•โ•โ•โ•โ•โ•โ••
โ”‚ 0   โ”‚ 1   โ”‚ 2   โ”‚ 3   โ”‚ 4   โ”‚ 5   โ”‚ 6   โ”‚ 7   โ”‚ 8   โ”‚ 9   โ”‚ 10   โ”‚ 11   โ”‚
โ•žโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ชโ•โ•โ•โ•โ•โ•โ•ก
โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚     โ”‚      โ”‚      โ”‚
โ•˜โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•งโ•โ•โ•โ•โ•โ•โ•›

Hash

The hash function returns the structure of the function that is being used to map to the table.

Commands

-h, --hash

Structure

<COMMAND>

Return

Returns the structure of the function being used to map in the hash table

Example

OPERATION: -h

or

OPERATION: --hash

Result

h(x) = x mod 11
โš ๏ธ **GitHub.com Fallback** โš ๏ธ