Linux-c glib library hash table GHashTable introduction

 

Baidu Cloud glib link: https://pan.baidu.com/s/1W9qdlMKWRKIFykenTVuWNQ Password: ol6y

A hash table is a data structure that provides key-value access, and the value associated with it can be quickly accessed through the specified key value. A typical use of a hash table is a dictionary, which can quickly find words by their initials. For a detailed introduction to the hash table, please refer to the related books on data structure. Here I only introduce the basic usage of the hash table in the glib library.

To use a hash table, you must first create it. There are two functions in the glib library that can be used to create a hash table, g_hash_table_new() and g_hash_table_new_full(). 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 the key; key_equal_func is used to compare whether two keys are equal;
key_destroy_func When you delete or destroy an entry from the hash table, the glib library will automatically call it to release the memory occupied by the key space,
which is very useful for the hash table where the key is dynamically allocated memory; the function of value_destroy_func is similar to that of key_destroy_func,
except that it releases the memory space occupied by value.

The following is a piece of code to demonstrate the basic usage of the hash table in the glib library 

1  /* ********************************************* ****************************
 2     file: g_hash.c
 3     desc: This file is used to demonstrate the usage of the hash table in the glib library
 4     compile : gcc -o g_hash g_hash.c `pkg-config --cflags --libs glib-2.0`
 5  *************************** ************************************************* */ 
6  
7 #include <glib.h>
 8  
9  void print_key_value(gpointer key, gpointer value, gpointer user_data);
 10  void display_hash_table(GHashTable * table);
 11  void free_key(gpointer data);
 12  void free_value(gpointer value);
13 
14 void print_key_value(gpointer key, gpointer value, gpointer user_data)
15 {
16     printf("%s ---> %s/n", key, value);
17 }
18 
19 void display_hash_table(GHashTable *table)
20 {
21     g_hash_table_foreach(table, print_key_value, NULL);
22 }
23 
24 void free_key(gpointer data)
25 {
26     printf("We free key: %s /n", data);
27     free(data);
28 }
29 
30 void free_value(gpointer data)
31 {
32     printf("We free value: %s /n", data);
33     free(data);
34 }
35 
36 int main(int argc, char *argv[])
37 {
38     GHashTable *table = NULL;
39 
40     table = g_hash_table_new(g_str_hash, g_str_equal);
41 
42     g_hash_table_insert(table, "1", "one");
43     g_hash_table_insert(table, "2", "two");
44     g_hash_table_insert(table, "3", "three");
45     g_hash_table_insert(table, "4", "four");
46     g_hash_table_insert(table, "5", "five");
47     display_hash_table(table);
48     printf("Size of hash table: %d /n", g_hash_table_size(table));
49 
50     printf("Before replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
51     g_hash_table_replace(table, "3", "third");
52     printf("After replace: 3 ---> %s /n", g_hash_table_lookup(table, "3"));
53 
54     g_hash_table_remove(table, "2");
55     display_hash_table(table);
56     printf("Now size of hash table: %d /n", g_hash_table_size(table));
57 
58     g_hash_table_destroy(table);
59 
60     table = g_hash_table_new_full(g_str_hash, g_str_equal, free_key, free_value);
61     g_hash_table_insert(table, strdup("one"), strdup("first"));
62     g_hash_table_insert(table, strdup("two"),strdup ("second"));
63     g_hash_table_insert(table, strdup("three"), strdup("third"));
64     
65     printf("Remove an item from hash table: /n");
66     g_hash_table_remove(table, "two");
67 
68     printf("Destroy hash table: /n");
69     g_hash_table_destroy(table);
70 
71     return 0;
72 }

 

Through the code, we can see that the glib library provides a very simple interface for the hash table. Here is the output of the code

[plusboy@localhost c]$ ./g_hash
1 ---> one
2 ---> two
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

Explanation of the code:

1. First, we call g_hash_table_new() to create a hash table, and then use g_hash_table_insert() to insert entries into the hash table. The inserted entry must be a key-value pair. To check how many entries are in the hash table, use g_hash_table_size ( ).
2. Use g_hash_table_lookup() to find the value corresponding to it through the key, and g_hash_table_replace() can replace the value corresponding to a key.
3. Use g_hash_table_foreach() to traverse each entry in the hash table and perform corresponding operations on 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 the hash table created with g_hash_table_new_full() and provided with key_destroy_func and value_destroy_func, when deleting entries in the hash table or destroying the hash table, the library automatically calls these two functions to release the memory, and when destroying the hash table, destroy the entries is not always the same as the order in which the entries were inserted. In the code, we use strdup() to dynamically allocate the key and value of the hash table

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324861532&siteId=291194637