As mentioned in the previous article.. NET Hashtable is a traditional algorithm. this document reviews the classic principles of the hash table data structure. next let's take a look at how Hashtable is implemented in Java and PHP. the reason why Java and PHP are combined is that their processing methods are very similar. java. util. hashMap-based. This principle is also applicable to PHP. hashMap is java. util. the lightweight Implementation of Hashtable, and NULL is allowed as a keyword.
As mentioned above, we know that. the modulo operation feature of the NET Hashtable hash function f (K) determines that its capacity must be a certain prime number (if you do not understand it, review article 1 ). while Java HashMap is just opposite. They require that the capacity of the hash table be an even number and it is the N power of 2. that is, Array. length = 2, 4, 8, 16, 32, 64... (converting to binary is exactly 1111... in the form of 110 ). this is because the core of the Java HashMap hash algorithm is the (&) operation, f (K) = HashOf (K) & (Array. length-1). During the operation, the high positions of HashOf (K) are the same as those of 0 and are discarded. The low positions are the same as 1 and are retained, so that 0 <= f (K) <Array. length.
The HashMap conflict processing method is also different. NET Hashtable's open address method, which is "link address method ". when the inserted K2, K3, K4 and so on conflict with the first inserted K1 position, create a linked list at the K1 position and put K2, K3, k4... save it to the linked list, that is, a location can correspond to a "string" of data. when you search for K2, K3, and K4, first find the f (K1) position itself. If the position does not match, the sequential traversal table is displayed. the advantage of such processing is that. the secondary aggregation of the open addressing method in. NET Hashtable (a continuous conflict occurs when the vacancy is detected ).
HashMap also has a Load Factor with a value of 0.75. we can see that the HashMap conflict processing method does not produce secondary aggregation, so the loading factor can be scaled up as appropriate. the initial default capacity is 16. When the actual load data to the capacity ratio exceeds 0.75, the system automatically expands. The new capacity is twice the original capacity (32, 64,128... ). the multiplication 2 operation is directly implemented through the left shift. No.. NET.
The Hashtable construction of PHP is basically the same as that of Java HashMap, but the Load Factor value is 1.
Theoretically ,. NET Hashtable's average search step is about-ln ()/x, while Java HashMap's average search step is about 1 + x/2, here, x is the ratio of the actual number of loaded data to the capacity. It can be seen that the average query length of a hash table is x, and 0 <= x <Load Factor, instead of the function that inserts N data, the search complexity is O (1 ).
From the perspective of actual operations, performance evaluation is complicated. It depends not only on the theoretical search step, but also on the actual Load Factor value, HashOf (K) efficiency, and Resize () efficiency, it can detect the efficiency of array and chain-breaking, and even the small overhead of function pressure parameter passing will have a considerable impact on the total. in C #, a hash table using the link address method is written in a simple way following the Java HashMap. The preliminary test shows that it corresponds.. NET original Hashtable is faster than reading, but the write speed is slow. considering that Hashtable also has thread-safe processing, it is easier to understand the problem because it is slower.
Next we will return to. NET from Java and introduce the new System. Collections. Generic. Dictionary <K, V> type (Dictionary for short) in 2.0 ).
The usage of Dictionary is similar to that of Hashtable, and f (K) uses the same modulo algorithm. However, the other internal structures differ greatly from those of Hashtable and HashMap:
[1] Dictionary does not have the Load Factor variable, or it can be understood as Load Factor = 1. inserting data can fill up the capacity space to the maximum extent.
[2] in general, the elements in a Dictionary are arranged in the order of insertion, which is different from the discretization of Hashtable and HashMap.
The Dictionary has two arrays, namely, "State string" and "data string". The data string accepts and encapsulates the inserted data in order (encapsulates a next pointer, for future convenience), the status string is indexed by f (K) and stores the ing between f (K) and K in the data string. when searching, first access the status string to find f (K), and then find the actual data in the data string based on the ing relationship. when there are multiple f (K) duplicates, the first f (K) in the status string is retained to establish a link between conflicting data in the data string.
We can see from the figure that the data string is faithful to K1, K2, K3... the insertion sequence is arranged until the data string is full. In this process, no conflict occurs. The conflict only occurs in the status string. At the same time, when a conflict occurs, you do not need to detect the gap and move the data itself, you only need to combine conflicting data links.
The default initial size of Dictionary is 3. The expansion method is the same as that of Hashtable. It is a prime number not less than twice the current capacity. in terms of speed, Dictionary reads faster than Hashtable, but it is better to write a hash table using the C # self-written link address method than that of Hashtable, but faster than the hash table using the link address method. considering that the implicit loading Factor of Dictionary is 1 and space can be used to the maximum extent, this is an acceptable way to change space at a speed.
Because Hashtable and Dictionary both exist at the same time, there must be selectivity in use scenarios, and they can be replaced at any time.
[1]Dictionary is recommended in a single-threaded program. It has the advantage of generics, fast reading speed, and sufficient capacity utilization.
[2]Hashtable is recommended in multi-threaded programs. The default Hashtable allows single-threaded writing and multi-threaded reading. For Hashtable, the Synchronized () method can be further called to obtain the type of full thread security. dictionary is NOT thread-safe and must be manually protected using the lock statement, greatly reducing the efficiency.
[3]Dictionary has the ability to sort data by insertion Order (Note: however, when you call Remove () to delete a node, the order is disrupted ), therefore, it is convenient to use a Dictionary to reflect the order.
The three types of Hashtable, HashMap, and Dictionary are discussed in the preceding section. Some immature ideas are added below:
[1]All three hash tables allow any object as keywords. However, in actual use, we generally only use string as the key value, and HashOf (string) for string processing is relatively simple and fast, however, retrieving HashOf (object) from an object is complicated. to further increase the speed, you can consider customizing a hash table that only allows string as the keyword.
[2]Java HashMap, due to the features of f (K) Acquisition and operation, each expansion must be twice as high as the price. however. in theory, as long as the capacity of NET Hashtable and Dictionary is a prime number, the new capacity does not have to be more than twice the old capacity. To further improve the memory utilization, you can consider rewriting the Resize () method, make each expansion a slightly larger prime number. of course, the insertion efficiency will be reduced and you can choose between them.
[3]It is a good idea to specify capacity directly during Hashtable initialization. This reduces the number of Resize () times and reduces the overhead.
From: http://blog.csdn.net/cloudandy/archive/2007/05/11/1604276.aspx