[Original] Space change speed, C realize the hashtable of the high performance data storage

Source: Internet
Author: User
Tags array length int size strcmp netbeans
In the process of implementing a Web server system, a number of public places to use a special Hashtabke, the previous release of the C implementation of the Hashtable has an important drawback is the need to dynamically allocate data containers for each item, which will lead to a large amount of time spent on memory allocation, Today, I see the design concept of. NET Java again on the internet and found JAVA2. The new dictionary container was introduced in 0, but the Java implementation method is two separate containers, which will add a memory allocation, we have a better method for C.
c is not checking the array out of bounds, which is both a disadvantage and an advantage, when you are sure that the allocated memory is large enough to set a 1-length array of items in the structure, the actual use of the usual data on the normal. So you can combine two arrays into one structure, and my Hashtable definition is this:
typedef struct _HASHNODE Hashnode;
typedef struct _HASHTABLE hashTable;
struct _hashnode{//hash Table node type
char * key;
void * value; This class relies on the application
Hashnode *next;//The first table's linked table pointer
Hashnode *pt;//A separate second hashindex item
};

struct _hashtable
{
unsigned int size;
unsigned int current;
Hashnode Items[1];
};
This can be malloc once in the process of allocating hash structure questions, such as:
HashTable *hs= (HashTable *) calloc (1,hashdefaultlength* (sizeof (Hashnode)) +sizeof (HashTable)) to achieve dynamic length allocation, The organization of the array is increased by this hashnode line, Hashnode *next;//the table pointer in the first table Hashnode *pt;//a separate second Hashindex item pointer so that Hashtable can be implemented in a struct. PT record the first Hashcode value, encountered the conflict will point pt to the entity Hashnode items[1] Index, multiple conflicts are recorded in the next pointer, that is, PT acts as a conflict head pointer address, followed by a linked list (before the time is required to increase Add a table memory allocation), the table is maintained with the next pointer, in fact, the PT can be considered as a new array column, he and the main hashnode array length exactly the same.
Of course, in Java dictionary load Factor = 1, but I implemented a Hashtable, the principle is roughly consistent, but still use 0. 75 as the load Factor, this will reduce the length of the linked list, increasing the addressing speed.
The following is my implementation code, after a simple stress test, 5 million times to increase the read time space within 1 seconds, performance is satisfactory
/*
* File: hashtable.cImplementation of hash table
* Author:netpet
* Flower NET Server
* About this program: in order to avoid the error caused by the release of stack memory, this requires that all name and key must be heap memory
* The hash function of the hash table is the hash function of Java, so the growth factor is also 0.75 of the hash
* This program is for the integration of Web server products dedicated design, with part of the code for the specific product optimization and does not represent the universality of the characteristics
* Program in Linux 2.46 debugging through, editing tools NetBeans 6.1 for C
* Contact: email:netpetboy@163.com qq:51977431
* Created on June 3, 2008, 4:14
*/
#include "hashtable.h"
#include <stdio.h>
unsigned int gethashvalue (char *key)
{
unsigned int hash;
unsigned char *p;
For (hash=0, p = (unsigned char *) key; *p; p++)
hash = * hash + *p;
Hash=hash & 0x7fffffff;
return hash;
}

/*
* Function: Specify the size of a new hash table
* Parameter: Length: Create a Hashtable table with the given length
* Return: Hash table
*/
HashTable * hashtablenew (int length)
{
HashTable *hs= (HashTable *) calloc (1,length* (sizeof (Hashnode)) +sizeof (HashTable));
hs->size=length;
hs->current=0;
return HS;
}
/*
* Function: Fixed the initial size of the hash table
* Parameters: None
* Return: Hash table that returns the default volume of the system
*/
HashTable * newhashtable (void)
{
HashTable *hs= (HashTable *) calloc (1,hashdefaultlength* (sizeof (Hashnode)) +sizeof (HashTable));
hs->size=hashdefaultlength;
hs->current=0;
return HS;
}
/*
* Function: Get the value of the given key
* Parameter: T:hash table pointer key: name string
* Return: null if not present
*/
void * Hashget (HashTable *t,char * key)
{
unsigned int hash=gethashvalue (key);
int index=hash%t->size;
Hashnode *node=t->items[index].pt;
while (node) {
if (!strcmp (key, Node->key))
Return node->value;
node=node->next;
}
return NULL;
}
/*
* Function: Sets an item, does not determine whether the item already exists, if exists it overwrites
* Parameter: T:hash table pointer address key: Name Value: Value
* Return: void
*/
void HashSet (HashTable **to,char * key,void *value)
{
HashTable * T=*TO;
if ((t->size*75) < (t->current*100))/** is greater than the boundary 0.75 extension * *
Hashexpend (to);
T=*to;
}

unsigned int hash=gethashvalue (key);
int index=hash%t->size;
Hashnode *node=t->items[index].pt;
if (!node)
t->items[index].pt=&t->items[t->current];
else {

while (node) {
if (!strcmp (key, Node->key)) {
node->value=value;
Return
}
if (Node->next)
node=node->next;
Else
Break
}
node->next=&t->items[t->current];
node=node->next;
node->next=null;

}
t->items[t->current].key=key;
t->items[t->current].value=value;
t->current++;
}
/*
* Features: Add a new item that may cause duplication, but faster
* Parameter: T:hash table pointer address key: Name Value: Value
* Return: void
*/
void Hashadd (HashTable **to,char * key,void *value)
{
HashTable * T=*TO;
if ((t->size*75) < (t->current*100))/** is greater than the boundary 0.75 extension * *
Hashexpend (to);
T=*to;
}
unsigned int hash=gethashvalue (key);
int index=hash%t->size;
Hashnode *node;
t->items[t->current].key=key;
t->items[t->current].value=value;
if (t->items[index].pt)
{
node=t->items[index].pt;
while (Node->next)
node=node->next;
node->next=&t->items[t->current];
node=node->next;
node->next=null;
}
Else
t->items[index].pt=&t->items[t->current];
t->current++;
}
/*
* Function: Remove the specified item
* Parameter: T:hash table pointer key: name to remove
* Return: void
*/
void Hashremove (HashTable *t, char * key) {
unsigned int hash=gethashvalue (key);
int index=hash%t->size;
Hashnode *node=t->items[index].pt, *node1;
Node1=node;
while (node) {
if (!strcmp (key, Node->key)) {
node->key=null;
node->value=null;
if (node==t->items[index].pt)
t->items[index].pt=null;
Else
node1->next=node->next;
Return
}
Node1=node;
node=node->next;
}
}
/*
* Function: Whether to include the specified item
* Parameter: T:hash table pointer key: Name
* Return: void
*/
int Hashcontainkey (HashTable *t,char * key)
{
unsigned int hash=gethashvalue (key);
int index=hash%t->size;
Hashnode *node=t->items[index].pt;
while (node) {
if (!strcmp (key, Node->key))
return 1;
node=node->next;
}
return 0;
}
/** copy two hash table * *
void Hashcopy (HashTable **tn,hashtable *to)
{
unsigned int i;
HashTable *t=*tn;
Hashnode * node=t->items;
Hashnode * nodet=to->items;
for (i=0;i<to->size;i++)
{
if (Nodet[i].key)
{
Hashadd (Tn,nodet[i].key,nodet[i].value);
}
}
}
/*
* Function: Extend existing table
* Parameter: T;hash table pointer address
* Return: Hash table
*/
HashTable * Hashexpend (HashTable * * to)
{
HashTable *t=*to;
unsigned int length = (t->current) * 2 + 1;
HashTable *hs= (HashTable *) calloc (1, length* (sizeof (Hashnode)) +sizeof (HashTable));
hs->size=length;
hs->current=0;
Hashcopy (&AMP;HS, T);

Free (*to);
*TO=HS;
return HS;
}
/*
* Function: Print hash table
* Parameter: T:hash table pointer
* Return: void
*/
void Printhash (HashTable *t)
{
Hashnode *node=t->items,*node1;
int i;
for (i=0;i<t->size;i++) {
if (Node[i].key)
printf ("Current-generated loops:%d:________________________/n", i);
printf ("%d this item: key:%spt:%p,next:%p,%p/n", I, node[i].key,node[i].value,node[i].pt,node[i].next,node[i]);
node1=node[i].pt;
while (Node1)
{
printf ("%d contains items: key:%s/tpt:%p,/tnext:%p,%p/n", I, Node1->key,node1->value,node1->pt,node1->next,node1) ;
node1=node1->next;
}
}
}
/*
* Function: Release a hash table
* Parameter: T:hash table pointer
* Return: void
*/
void Hashfree (HashTable *t)
{
Free (T);

}



/*
* File: hashtable.hDefinition of a hash table
* Author:netpet
* Flower NET Server
* About this program: in order to avoid the error caused by the release of stack memory, this requires that all name and key must be heap memory
* The hash function of the hash table is the hash function of Java, so the growth factor is also 0.75 of the hash
* This program is for the integration of Web server products dedicated design, with part of the code for the specific product optimization and does not represent the universality of the characteristics
* Program in Linux 2.46 debugging through, editing tools NetBeans 6.1 for C
* Contact: email:netpetboy@163.com qq:51977431
* Created on June 3, 2008, 4:14
*/

#ifndef _hashtable_h
#define _hashtable_h

#ifdef __cplusplus
extern "C" {
#endif

#define NIL-1//Empty node tags are dependent on the keyword type, this section assumes that the keyword is a non-negative integer
#define HASHDEFAULTLENGTH 11//table length depends on the application, but generally should be based on. Determine m as a prime
#include <stdlib.h>
typedef struct _HASHNODE Hashnode;
typedef struct _HASHTABLE hashTable;
struct _hashnode{//hash Table node type
char * key;
void * value; This class relies on the application
Hashnode *next;//The first table's linked table pointer
Hashnode *pt;//A separate second hashindex item
};

struct _hashtable
{
unsigned int size;
unsigned int current;
Hashnode Items[1];
};
/*
* Function: hash function
* Parameter: str: string to convert
* return: Converted unsigned int value result
*/
extern unsigned int gethashvalue (char *key);
/*
* Function: Get the value of the given key
* Parameter: T:hash table pointer key: name string
* Return: void
*/
extern void * Hashget (HashTable *t,char * key);//hashsearch
/*
* Function: Sets an item, does not determine whether the item already exists, if exists it overwrites
* Parameter: T:hash table pointer address key: Name Value: Value
* Return: void
*/
extern void HashSet (HashTable **to,char * key,void *value);
/*
* Function: Add a certain is a new item, faster, without checking, may cause duplication
* Parameter: T:hash table pointer address key: Name Value: Value
* Return: void
*/
extern void Hashadd (HashTable **to,char * key,void *value);
/*
* Function: Remove the specified item
* Parameter: T:hash table pointer key: name to remove
* Return: void
*/
void Hashremove (HashTable *t,char * key);
/*
* Function: Whether to include the specified item
* Parameter: T:hash table pointer key: Name
* Return: void
*/
int Hashcontainkey (HashTable *t,char * key);
/*
* Function: Specify the size of a new hash table
* Parameter: Length: Create a Hashtable table with the given length
* Return: Hash table
*/
HashTable * hashtablenew (int length);
/*
* Function: Fixed the initial size of the hash table
* Parameters: None
* Return: Hash table that returns the default volume of the system
*/
HashTable * newhashtable (void);
/*
* Function: Extend existing table
* Parameter: T;hash table pointer address
* Return: Hash table
*/
HashTable * Hashexpend (HashTable * * T);
/*
* Function: Print hash table
* Parameter: T:hash table pointer
* Return: void
*/
void Printhash (HashTable *t);
/*
* Function: Release a hash table
* Parameter: T:hash table pointer
* Return: void
*/
void Hashfree (HashTable *t);


#ifdef __cplusplus
}
#endif

#endif/* _hashtable_h * *

If you encounter a problem.
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.