Hash table-Application in VoIP user information storage

Source: Internet
Author: User
Document directory
  • For users of hash tables, this is an instant. The hash table operation is very fast. In computer programs, if you need to search for thousands of records within one second, a hash table (such as the spelling checker) is usually used) hash Tables are much faster than trees. Tree operations usually require O (n) time. Hash Tables are not only fast, but also easy to implement.
  • The hash table also has some disadvantages. It is array-based. It is difficult to expand the array after it is created. When some hash tables are basically filled up, the performance is very degraded, therefore, although the program must know how much data will be stored in the table (or it is time-consuming to regularly transfer data to a larger hash table ).
  • In addition, there is no easy way to traverse data items in any order (for example, from small to large. If this capability is required, you can only select other data structures.
  • However, if you do not need to traverse data in sequence, you can predict the size of data in advance. Hash Tables are unparalleled in terms of speed and ease of use.
  • The following hash table is used to store the information of the previous user in the SIP Phone (the user information is already stored in a global variable linked list during initialization, which is described in this article, online users need to interact with the console at all times (such as calls and busy lines), which requires high speed). If you don't need to talk about it, analyze the Code directly ~
A hash table is a data structure that provides fast insert and search operations. When I first came into contact with a hash table, its advantages were incredible. Regardless of the amount of data in the hash table, insertion and deletion (sometimes including side division) only require time close to the constant, that is, the time level of 0 (1. In fact, this only requires several machine commands. For users of hash tables, this is an instant. The hash table operation is very fast. In computer programs, if you need to search for thousands of records within one second, a hash table (such as the spelling checker) is usually used) hash Tables are much faster than trees. Tree operations usually require O (n) time. Hash Tables are not only fast, but also easy to implement. The hash table also has some disadvantages. It is array-based. It is difficult to expand the array after it is created. When some hash tables are basically filled up, the performance is very degraded, therefore, although the program must know how much data will be stored in the table (or it is time-consuming to regularly transfer data to a larger hash table ). In addition, there is no easy way to traverse data items in any order (for example, from small to large. If this capability is required, you can only select other data structures. However, if you do not need to traverse data in sequence, you can predict the size of data in advance. Hash Tables are unparalleled in terms of speed and ease of use. The following hash table is used to store the information of the previous user in the SIP Phone (the user information is already stored in a global variable linked list during initialization, which is described in this article, online users need to interact with the console at all times (such as calls and busy lines), which requires high speed). If you don't need to talk about it, analyze the Code directly ~

Header file:

# Ifndef _ hash_h # DEFINE _ hash_h # define OK 1 # define error 0 # define hash_size 256 typedef struct _ hashnode/* using a bidirectional linked list for storage */{struct _ hashnode * next; struct _ hashnode * Prev;} thashnode; thashnode * vsipuserlocation [hash_size];/* slot */# endif

Hash Table Algorithm (string processing)

Int hashstring (const char * Str) {unsigned int h = 0; int Len; int I; Len = strlen (STR); If (LEN <= 0 | Len> hash_size) return-1; for (I = 0; I <Len; I ++) {H = (H <5)-H + * (STR + I );} return H % hash_size;} initialize int hashinit (thashnode ** plist, unsigned int size) {If (! Plist) Return Error; memset (plist, '\ 0', size); Return OK ;}
Insert the int hashaddnode (thashnode ** plist, thashnode * pnode, char * hash, func pcmpfunc) {int index; thashnode * tempnode = NULL; int ret; If (! Plist |! Pnode |! Hash) Return Error; Index = hashstring (hash); If (index <0) Return Error;/* First node */tempnode = plist [Index]; If (! Tempnode) {plist [Index] = pnode; pnode-> next = NULL; pnode-> Prev = NULL; Return OK;}/* traverse whether there is the same node function pcmpfunc () is to compare whether the Pointer Points to the same memory address */If (pcmpfunc) {for (; tempnode! = NULL; tempnode = tempnode-> next) {If (ret = pcmpfunc (tempnode, pnode, 0) = 0) break;} If (ret = 0) return OK;}/* to locate the same node, insert it to the first node position */tempnode = plist [Index]; plist [Index] = pnode; pnode-> next = tempnode; pnode-> Prev = NULL; tempnode-> Prev = pnode; Return OK ;}
To insert a parameter application to a node, call the function in the Code as follows:
typedef struct _Callee {    tHashNode pNode;    char name[24];    struct _Callee *next;}Callee
Callee * P; P points to a node in a global variable (linked list, which stores the information of all users set by the system) and saves the user information (I modified it here, only show part of the content ). The hash table stores the current online users. When registering a user, you need to add it to the hash table. Hashaddnode (vsipuserlocation, (thashnode *) p, p-> name, issamecallee)

The following describes how to communicate with others:

A:

I also encountered this problem because tsipcallee * P points to the memory address that stores the tsipcalle structure. The address points to the memory as a continuous space, for example, the size is n, then we can randomly take a portion of the n Bytes, that is, sizeof (thashnode) <= sizeof (callee), put the first M (<= N) in N) bytes are treated as thashnode content, that is, the M bytes can be considered as a thashnode struct.
However, if sizeof (thashnode)> sizeof (callee) is used, the access may be out of bounds. B: The thasgnode pnode is included when the struct is defined;
Too abstract ~

A: Yes. I will give you a picture and you will understand B: It seems that you have seen a lot of it. At the beginning, I am confused at (thashnode *) P and forced type conversion, I won't overwrite the first two bytes of P! A: Look at this structure. The tsipcallee storage method in the memory, forced conversion means "I" only wants to use the first byte: sizeof (thashnode), and I don't care what is next. Search nodes through data

Thashnode * hashgetnode (thashnode ** plist, char * hash, func pcmpfunc, void * cmparg) {int index; thashnode * tempnode = NULL; If (! Plist |! Hash |! Pcmpfunc) return NULL; Index = hashstring (hash); If (index <0) return NULL; tempnode = plist [Index]; while (tempnode) {/* check whether the cmoarg parameter is saved in the node tempnode.) If (pcmpfunc (tempnode, null, cmparg) = 0) {break;} tempnode = tempnode-> next ;} return tempnode ;}

Obtain the content of the current column:

tHashNode * hashGetSlot(tHashNode ** pList, const char *hash ){    int index;    if ( !pList || !hash )        return NULL;    index = hashString( hash );    if( index < 0 )        return NULL;    return pList[ index ];}

Delete a node

Int hashremovenode (thashnode ** plist, thashnode * pnode, char * hash) {int index; If (! Plist |! Pnode |! Hash) Return Error; Index = hashstring (hash); If (index <0) Return Error; If (! Pnode-> PREV) {/* First node */plist [Index] = pnode-> next; If (pnode-> next) pnode-> next-> Prev = NULL;} else if (! Pnode-> next) {/* next node */pnode-> Prev-> next = NULL;} else {pnode-> Prev-> next = pnode-> next; pnode-> next-> Prev = pnode-> Prev;} Return OK ;}

Hash Tables are widely used to process large amounts of data because of their fast processing speed. But it is not hard to understand. Only by carefully reading the code,

 

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.