@@ -9,18 +9,17 @@ h1. Chapter 3: Names and Name Table
99
1010h2. `st_table`
1111
12- `st_table` has already been mentioned as a method table and instance table.
13- In this chapter let us look at the detailed mechanism of the `st_table`.
12+ We've already examined `st_table` as a method table and an instance table. In
13+ this chapter let's look at the inner workings of `st_table`.
1414
1515h3. Summary
1616
1717I previously mentioned that the `st_table` is a hash table. What is a hash
18- table? It is a data structure that records one-to-one relations, for
19- example, a variable name and its value, a function name and its body, etc.
18+ table? It is a data structure that records one-to-one relations, for example, a
19+ variable name and its value, or a function name and its body, etc.
2020
2121However, data structures other than hash tables can, of course, record
22- one-to-one relations. For example, a list of following data structure will
23- suffice for this purpose.
22+ one-to-one relations. For example, a linked list will suffice for this purpose.
2423
2524<pre class="emlist">
2625struct entry {
@@ -31,14 +30,13 @@ struct entry {
3130</pre>
3231
3332However, this method is slow. If the list contains a thousand items, in the
34- worst case, it is necessary to traverse links a thousand times. In other
35- words, in proportion to the number of elements, the search time increases.
36- This is bad. Since ancient times, various speed improvement methods have
37- been conceived. The hash table is one of those improved methods. In other
38- words, the point is not that the hash table is necessary but that it can be
39- made faster.
40-
41- Now then, let us examine the `st_table`. But first, this library is not
33+ worst case, it is necessary to traverse a thousand links. In other words, the
34+ search time increases in proportion to the number of elements. This is bad.
35+ Since ancient times, various speed improvement methods have been conceived. The
36+ hash table is one of those improved methods. In other words, the point is not
37+ that the hash table is necessary but that it can be made faster.
38+
39+ Now then, let us examine the `st_table`. As it turns out, this library is not
4240created by Matsumoto, rather:
4341
4442▼ `st.c` credits
@@ -63,29 +61,27 @@ A hash table can be thought as the following: Let us think of an array with
6361
6462!images/ch_name_array.png(Array)!
6563
66- Then let us specify a function f that takes a key and produces an integer `i`
67- from 0 to `n`-1 (0-63). We call this `f` a hash function. `f` when given the same
68- key always produces the `i`. For example, if we make the assumption that the
69- key is limited to positive integers, then if the key is divided by 64 then
70- the remainder will always fall between 0 and 63. This method of calculation
71- can become function `f`.
64+ Then let us specify a function `f` that takes a key and produces an integer `i`
65+ from 0 to `n`-1 (0-63). We call this `f` a hash function. `f` when given the
66+ same key always produces the same `i`. For example, if we assume that the key
67+ is limited to positive integers, we can make the function `f` by dividing the
68+ key by 64. The remainder will always fall between 0 and 63.
7269
7370When recording relationships, given a key, function `f` generates `i`, and
74- place the value into index `i` of the array we have prepared. In other words,
75- the index access into an array is very fast. Therefore the fundamental idea
76- is to change the key into an integer.
71+ places the value into index `i` of the array we have prepared. Index access
72+ into an array is very fast. The key concern is changing a key into an integer.
7773
7874!images/ch_name_aset.png(Array assignment)!
7975
80- However, in the real world it isn't that easy. There is a critical problem
81- with this idea. Because `n` is only 64, if there are more than 64
82- relationships to be recorded, it is certain that i will be the same for two different keys.
83- It is also possible that with fewer than 64, the same thing can occur.
84- For example, given the previous hash function "key % 64", keys 65 and 129
85- will have a hash value of 1. This is called a hash value collision. There
86- are many ways to resolve a collision.
76+ However, in the real world it isn't that easy. There is a critical problem with
77+ this idea. Because `n` is only 64, if there are more than 64 relationships to
78+ be recorded, it is certain that there will be the same index for two different
79+ keys. It is also possible that with fewer than 64, the same thing can occur.
80+ For example, given the previous hash function "key % 64", keys 65 and 129 will
81+ both have a hash value of 1. This is called a hash value collision. There are
82+ many ways to resolve such a collision.
8783
88- For example, if a collision occurs, then insert into the next element.
84+ One solution is to insert into the next element when a collision occurs .
8985This is called open addressing. (Figure 3).
9086
9187!images/ch_name_nexti.png(Open addressing)!
0 commit comments