Go language Map bottom-level implementation

Source: Internet
Author: User
"Map" is the basic data structure in the go language and is often used in everyday use. But how is it implemented at the bottom? The underlying implementation of ' map ' in Golang is a hash table, so the process of implementing ' map ' is actually the process of implementing a hash. In this hash list, there are two main structures, one called ' Hmap ' (' A header for a Go map '), and one called ' Bmap ' (' A bucket for a go map ', usually called ' bucket '). The two structures look something like this: hmap: Diagram, but it is easy to understand the schema of ' map ', all you need to care about is the red field: the buckets array. The structure used for storage in the Golang map is a bucket array. And what is the structure of buckets (i.e. ' bmap ')? Bucket: Compared to "hmap", the structure of the bucket is simple, the red field is still "core", we use the "map" in the key and value is stored here. The "High hash Value" array records the key-related "index" in the current bucket, which is described in more detail later. Another field is a pointer to the expanded bucket, which makes the bucket form a linked list structure. For example: Thus see The relationship between ' hmap ' and ' bucket ' Is this:! [Write a description of the picture here] (https://static.studygolang.com/180826/0743574cda5e2853f82a2c6abce4c2cb.png) and the bucket is a linked list, so the overall structure should be this:! [Write a description of the picture here] (Https://static.stuDygolang.com/180826/73c611ff75e39b50cabc3649bad9cdb7.png) hash table is characterized by a hash function that hashes the key you come to, and gets a unique value, typically a value. Golang's "map" also has such a hash function, will also calculate the unique value, for the use of this value, Golang is also very interesting. Golang the value to be divided by the purpose: high and low. [Write a description of the picture here] (Https://static.studygolang.com/180826/f78e6681a0c9baf8aa677290443b4b7d.png), Blue is high, red is low. The low is then used to find out which bucket of the current key belongs to "Hmap", and the high position is used to find which key in the bucket. As mentioned above, there is an attribute field in the bucket that is the "high hash" array, where the high value of blue is used to declare what "key" is in the current bucket and to facilitate search. It is important to note that the key/value values in our ' map ' are stored in the same array. The order in the array is this:! [Write a description of the picture here] (Https://static.studygolang.com/180826/a662bfc57e0b63211f1f2783129969c6.png) is not a form of key0/value0/key1/value1, The advantage of this is that the space wasted by padding can be eliminated when the length of the key and value is different. Now we can get the entire structure of the Go language ' map ':! [Write a description of the picture here] (https://static.studygolang.com/180826/7d806b2a30f30d85e3ee65fb25929263.png) 346 reads  

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.