LINUX-C GLib Library Hash Table ghashtable Introduction

Source: Internet
Author: User

Baidu Cloud GLib Link: https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ Password: ol6y

A hash table is a data structure that provides key-value access, with the specified key value providing quick access to the value associated with it. A typical use of a hash table is a dictionary that can quickly find words by the first letter of a word. For a detailed description of the hash table, please refer to the data structure of the relevant books, I only introduce the glib library in the basic usage of the hash table.

To use a hash table you must first create it, and the glib library has two functions that can be used to create the hash table, respectively, G_hash_table_new () and G_hash_table_new_full (), and their prototypes are as follows:

1 ghashtable *g_hash_table_new (ghashfunc hash_func, Gequalfunc key_equal_func); 2 3 ghashtable* g_hash_table_new_full (ghashfunc hash_func,4                                   gequalfunc Key_equal_ Func,5                                   gdestroynotify key_destroy_func,6                                    gdestroynotify value_ DESTROY_FUNC);

Where Hash_func is a function that creates a hash value for key; Key_equal_func is used to compare two keys for equality;
Key_destroy_func when you delete and destroy an entry from the hash table, the glib library automatically calls it to release the memory space that the key occupies,
This is useful for a hash table where key is dynamically allocated memory, and the Value_destroy_func function is similar to Key_destroy_func.
Only it frees the memory space that value occupies.

Here is a snippet of code to illustrate the basic usage of the hash table in the GLib library

1 /***************************************************************************2 file:g_hash.c3 desc: This file is used to demonstrate the use of the hash table in the GLib library4 compile:gcc-o g_hash g_hash.c ' pkg-config--cflags--libs glib-2.0 '5  ***************************************************************************/6 7#include <glib.h>8 9 voidPrint_key_value (gpointer key, Gpointer value, Gpointer user_data);Ten voidDisplay_hash_table (ghashtable *table); One voidFree_key (gpointer data); A voidFree_value (gpointer value); -  - voidPrint_key_value (gpointer key, Gpointer value, Gpointer user_data) the { -printf"%s--->%s/n", key, value); - } -  + voidDisplay_hash_table (ghashtable *table) - { + G_hash_table_foreach (table, Print_key_value, NULL); A } at  - voidFree_key (gpointer data) - { -printf"We free key:%s/n", data); -      Free(data); - } in  - voidFree_value (gpointer data) to { +printf"We Free Value:%s/n", data); -      Free(data); the } *  $ intMainintargcChar*argv[])Panax Notoginseng { -Ghashtable *table =NULL; the  +Table =g_hash_table_new (G_str_hash, g_str_equal); A  theG_hash_table_insert (table,"1"," One"); +G_hash_table_insert (table,"2"," Both"); -G_hash_table_insert (table,"3","three"); $G_hash_table_insert (table,"4"," Four"); $G_hash_table_insert (table,"5","Five"); - display_hash_table (table); -printf"Size of hash table:%d/n", g_hash_table_size (table)); the  -printf"before Replace:3--->%s/n", G_hash_table_lookup (table,"3"));WuyiG_hash_table_replace (table,"3","Third"); theprintf"After replace:3--->%s/n", G_hash_table_lookup (table,"3")); -  WuG_hash_table_remove (table,"2"); - display_hash_table (table); Aboutprintf"Now size of hash table:%d/n", g_hash_table_size (table)); $  - G_hash_table_destroy (table); -  -Table =g_hash_table_new_full (G_str_hash, G_str_equal, Free_key, free_value); AG_hash_table_insert (table, StrDup (" One"), StrDup (" First")); +G_hash_table_insert (table, StrDup (" Both"), StrDup ("Second")); theG_hash_table_insert (table, StrDup ("three"), StrDup ("Third")); -      $printf"Remove an item from hash table:/n"); theG_hash_table_remove (table," Both"); the  theprintf"Destroy Hash Table:/n"); the G_hash_table_destroy (table); -  in     return 0; the}

Through the code we see the glib library as a very simple interface for hash tables. The following is the output of the code

[Email protected] c]$./g_hash
1---> One
2--->
3---> Three
4---> Four
5---> Five
Size of Hash Table:5
Before Replace:3---> Three
After Replace:3---> third
1---> One
3---> Third
4---> Four
5---> Five
Now size of hash table:4
Remove an item from hash table:
We Free Key:two
We Free Value:second
Destroy Hash Table:
We Free Key:three
We Free Value:third
We Free Key:one
We Free Value:first

Description of the code:

1, first we call the G_hash_table_new () to create a hash table, and then use G_hash_table_insert () to insert the entry into the hash list, the inserted entry must be a key-value pair, To see how many entries are available in the hash table, use G_hash_table_size ().
2, with G_hash_table_lookup () through key can find the corresponding value,g_hash_table_replace () can be replaced by a key corresponding to the value.
3, with G_hash_table_foreach () can traverse the hash table each entry, and the corresponding operation of them.
4. Use G_hash_table_remove () to remove an entry from the hash table.
5. Destroy a hash table with G_hash_table_destroy ().
6, for using G_hash_table_new_full () to create and provide key_destroy_func and value_destroy_func hash table, delete the entries in the hash table or destroy the hash table, The library automatically calls these two functions to free up memory, and when the hash table is destroyed, the order of the items destroyed is not always the same as the order in which the entries are inserted. In the code we use StrDup () to dynamically allocate the hash table's key and value within the

LINUX-C GLib Library Hash Table ghashtable Introduction

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.