Interface definition and implementation analysis of the Hash Table

Source: Internet
Author: User

Interface definition and implementation analysis of the Hash Table
Interface Definition of the open address Hash Function

Basic operations include initializing an open address hash table, destroying an open address hash table, inserting elements, deleting elements, searching elements, and obtaining the number of elements.

Various operations are defined as follows:

Ohtbl_init

Int ohtbl_init (OHTbl * htbl, int positions, int (* h1) (const void * key), int (* h2) (const void * key ),

Int (* match) (const void * key1, const void * key2), void (* destroy) (void * data ));

If the hash table is initialized successfully, 0 is returned; otherwise,-1 is returned.

Description initialize the open address hash table htbl. Before performing other operations on the hash table, you must first initialize the table.

The positions parameter specifies the number of slots in the table. Function pointer h1, h2 is used to specify the User-Defined secondary hash function to complete the double hash process. The function pointer match points to a user-defined function, which is used to determine whether two keys match. Its usage is similar to match in chtbl_init. The function pointer destroy releases the dynamically allocated memory space by calling ohtbl_destroy, which is similar to the usage of parameters in ohtbl_init. If the data in the hash function table does not need to be released, destroy should point to NULL.

Complexity O (m), m is the number of slots in the hash table.

Ohtbl_destroy

Void ohtbl_destroy (OHTbl * htbl );

No returned value.

Description: destroys an open address hash table specified by htbl. You are not allowed to perform other operations after calling ohtbl_destroy unless you initialize it again.

Ohtbl_destroy deletes all elements in the hash table and releases the memory space occupied by Members whose destroy parameter is not NULL in ohtbl_init.

Complexity O (m), m is the number of slots in the hash table.

Ohtbl_insert

Int ohtbl_insert (OHTbl * htbl, const void * data );

If the element is successfully inserted, 0 is returned. If the hash table already contains this element, 1 is returned. Otherwise,-1 is returned.

Description inserts an element into the open address hash table specified by htbl.

The new element contains a pointer to data. Therefore, this pointer remains valid as long as the element still exists in the hash table. Data-related space is managed by the caller of the function.

Complexity O (1 ).

Ohtbl_remove

Int ohtbl_remove (OHTbl * htbl, const void ** data );

If the element is successfully deleted, 0 is returned. Otherwise,-1 is returned.

Description used to delete data matching elements from the open address hash table specified by htbl.

When returned, data points to the data stored in the deleted element. Data-related memory space will be managed by function callers.

Complexity O (1 ).

Ohtbl_lookup

Int ohtbl_lookup (const OHTbl * htbl, const void ** data );

If an element is found in the table, 0 is returned. Otherwise,-1 is returned.

Describes whether the open address hash table specified by htbl has data matching elements.

If found, when the function returns, data points to the data of matched elements in the hash table.

Complexity O (1 ).

Ohtbl_size

Int ohtbl_size (const OHTbl * htbl );

The number of elements in the return value hash table.

Describes the macro used to obtain the number of elements in a hash table.

Complexity O (1 ).

Implementation and Analysis of an open address hash table

The implementation is divided into two files: one is the header file of the address hash table and the other is the implementation file of the abstract data type.

Example 1: header file of the address hash table
/* Ohtbl. h */
# Ifndef OHTBL_H # define OHTBL_H # include <stdlib. h>/* define the data structure of the open address hash table */typedef struct OHTbl _ {int positions;/* 1 specify the number of slots allocated in the hash table */void * vacated; /* 2 points to a special address space. This special address has deleted an element */int (* h1) (const void * key ); /* 3 auxiliary hash function */int (* h2) (const void * key);/* 4 auxiliary hash function */int (* match) (const void * key1, const void * key2);/* 5 determine whether two elements match */void (* destroy) (void * data);/* 6 destroy function */int size; /* 7 Number of existing elements */void ** table;/* 8 array of storage elements */} OHTbl;
/* Function prototype Declaration */
Int ohtbl_init (OHTbl * htbl, int positions, int (* h1) (const void * key), int (* h2) (const void * key ),
Int (* match) (const void * key1, const void * key2), void (* destroy) (void * data ));

Void ohtbl_destroy (OHTbl * htbl );

Int ohtbl_insert (OHTbl * htbl, const void * data );

Int ohtbl_remove (OHTbl * htbl, void ** data );

Int ohtbl_lookup (const OHTbl * htbl, void ** data );

# Define ohtbl_size (htbl)-> size)
# Endif // OHTBL_H
Example 2: Implementation of abstract data types in an on-address hash table
/* Ohtbl. c */# include <stdlib. h>
# Include <string. h>

# Include "ohtbl. h"

/* Reserve a special memory address for the blank element */
Static char vacated;

/* Ohtbl_init initializes the open address hash table specified by htbl */
Int ohtbl_init (OHTbl * htbl, int positions, int (* h1) (const void * key), int (* h2) (const void * key ),
Int (* match) (const void * key1, const void * key2), void (* destroy) (void * data ))
{
Int I;
/* Empty space */
If (htbl-> table = (void **) malloc (positions * sizeof (void *) = NULL)
Return-1;

/* Initialize each slot and set the pointer of each slot to NULL */
Htbl-> positions = positions;
For (I = 0; I Htbl-> table [I] = NULL;

/* Set the empty members to the special memory address reserved for this purpose */
Htbl-> vacated = & vacated;

/* Encapsulate 4 functions */
Htbl-> h1 = h1;
Htbl-> h2 = h2;
Htbl-> match = match;
Htbl-> destroy = destroy;

/* Number of initialization elements */
Htbl-> size = 0;

Return 0;
}

/* Ohtbl_destroy destroy the open address hash table specified by htbl */
Void ohtbl_destroy (OHTbl * htbl)
{
Int I;

If (htbl-> destroy! = NULL)
{
For (I = 0; I {
If (htbl-> table [I]! = NULL & htbl-> table [I]! = Htbl-> vacated)
Htbl-> destroy (htbl-> table [I]);
}
}
/* Release the tablespace */
Free (htbl-> table );

/* Clear the data structure */
Memset (htbl, 0, sizeof (OHTbl );

Return;
}

/* Insert an element to the table using ohtbl_insert */
Int ohtbl_insert (OHTbl * htbl, const void * data)
{
Void * temp;
Int position, I;

/* Because the open address hash table has a fixed size, you must ensure that there is sufficient space for element placement before insertion */
If (htbl-> size = htbl-> positions)
Return-1;

/* Duplicate table insertion is not allowed for the same key. Call htbl_lookup to check whether the same element exists before insertion */
Temp = (void *) data;
If (ohtbl_lookup (htbl, temp) = 0)
Return 1;

/* If the preceding conditions are met, use the double hash method to find unused slots in the table */
For (I = 0; I {
Position = (htbl-> h1 (data) + (I * htbl-> h2 (data) % htbl-> positions;

If (htbl-> table [position] = NULL | htbl-> table [position] = htbl-> vacated)
{
/* Insert an element into the table */
Htbl-> table [position] = (void *) data;
Htbl-> size ++;
Return 0;
}
}
/* Incorrect hash function is selected */
Return-1;
}

/* Ohtbl_remove Delete the elements that match data in the specified htbl table */
Int ohtbl_remove (OHTbl * htbl, void ** data)
{
Int position, I;

/* Locate the position of the element to be deleted through the double hash */
For (I = 0; I {
Position = (htbl-> h1 (* data) + (I * h2 (* data) % htbl-> positions;

If (htbl-> table [position] = NULL)
{
/* No matching data found */
Return-1;
}
Else if (htbl-> table [position] = htbl-> vacated)
{
/* Locate the highlighted position and continue searching */
Continue;
}
Else if (htbl-> match (htbl-> table [position], * data ))
{
/* Direct data to the data being deleted */
* Data = htbl-> table [position];
/* Place the slot address to the vacated member */
Htbl-> [position] = htbl-> vacated;
Htbl-> size --;
Return 0;
}
}
/* If no element is found,-1 */is returned */
Return-1;
}

/* Ohtbl_lookup searches for the elements matching data in the table specified by htbl */
Int ohtbl_lookup (const OHTbl * htbl, void ** data)
{
Int position, I;

For (I = 0; I {
Position = (htbl-> h1 (* data) + (I * htbl-> h2 (* data) % htbl-> positions;

If (htbl-> table [position] = NULL)
{
/* No data found */
Retun-1;
}
Else if (htbl-> match (htbl-> table [position], * data ))
{
/* Direct data to the data found */
* Data = htbl-> table [position];
Return 0;
}
}
Return-1;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.