Hash table searching method and device, electronic equipment and storage medium
By combining open addressing and chaining methods with linear probing and auxiliary lookup tables, the contradiction between memory usage and search efficiency in hash tables is resolved, achieving a balance between efficient search and memory utilization.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHENGDU OPPO TELECOMM TECH CORP LTD
- Filing Date
- 2024-12-03
- Publication Date
- 2026-06-05
AI Technical Summary
Existing hash table lookup methods have shortcomings in balancing memory usage and lookup efficiency, especially when the load factor is moderate. The chaining method has high memory usage, while the open addressing method has low lookup efficiency.
The first insertion is performed using open addressing, followed by a second insertion using chaining. Linear probing is used to insert conflicting elements into empty spaces in the hash table, and pointers are set on the linked list to create an auxiliary lookup table to quickly eliminate mismatched hashes.
It improves memory utilization and search efficiency, achieving a balance between performance and memory resource consumption, and shows a more obvious advantage, especially when dealing with large amounts of data.
Smart Images

Figure CN122152811A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, and in particular to a hash table lookup method, apparatus, electronic device, and storage medium. Background Technology
[0002] In computer science, hash tables are widely used in various scenarios, such as database indexes, caching systems, and symbol tables, for efficient element insertion, deletion, and lookup operations. Common hash table lookup methods include chaining and open addressing. Different methods have different advantages and disadvantages in terms of collision handling, memory utilization, and lookup performance. For example, chaining offers good insertion and lookup performance under moderate load factors, but it consumes a large amount of memory. Open addressing, when a collision occurs, searches for other free slots in the hash table to store the conflicting element, without requiring additional linked lists or pointers, resulting in higher memory utilization and improved caching capabilities, but lower lookup efficiency. Therefore, there is an urgent need for those skilled in the art to develop a lookup method that can balance memory consumption and lookup efficiency. Summary of the Invention
[0003] This application discloses a hash table lookup method, apparatus, electronic device, and storage medium, which can improve lookup efficiency and reduce memory usage, thus achieving a better balance between performance and memory resource usage.
[0004] In a first aspect, embodiments of this application disclose a hash table lookup method, including:
[0005] Get the target storage address of the element to be searched;
[0006] The search involves checking if the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or all elements in the linked list corresponding to the target hash address.
[0007] The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method for the first time and the chaining method for the second time by inserting them into the chaining table using the linear probing method. The insertion of a conflicting element requires setting a chain pointer in the storage location corresponding to the hash address of all elements in the corresponding chain to indicate the hash address of the next conflicting element.
[0008] Secondly, embodiments of this application disclose a hash table lookup device, which may include:
[0009] The first acquisition module is used to acquire the target storage address of an element to be searched.
[0010] The first search module is used to search whether the element to be searched exists in all elements under the target hash address that matches the target storage address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or all elements on the linked list corresponding to the target hash address.
[0011] The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method for the first time and the chaining method for the second time by inserting them into the chaining table using the linear probing method. The insertion of a conflicting element requires setting a chain pointer in the storage location corresponding to the hash address of all elements in the corresponding chain to indicate the hash address of the next conflicting element.
[0012] Thirdly, embodiments of this application disclose an electronic device that may include:
[0013] Memory containing executable program code;
[0014] A processor coupled to the memory;
[0015] The processor calls the executable program code stored in the memory to execute a hash table lookup method disclosed in the first aspect of the embodiments of this application.
[0016] Fourthly, embodiments of this application disclose a computer-readable storage medium storing a computer program, wherein the computer program causes a computer to execute a hash table lookup method disclosed in the first aspect of embodiments of this application.
[0017] Fifthly, embodiments of this application disclose a computer program product that, when run on a computer, causes the computer to perform some or all of the steps of any of the methods in the first aspect.
[0018] Sixthly, embodiments of this application disclose an application publishing platform for publishing computer program products, wherein when the computer program product is run on a computer, the computer performs some or all of the steps of any of the methods in the first aspect.
[0019] Compared with the prior art, the embodiments of this application have the following beneficial effects:
[0020] In this embodiment, elements in the target hash table can be inserted using open addressing for the first insertion and chaining for the second insertion. When using chaining, the next conflicting element is inserted into an empty space in the target hash table using linear probing. A linked list pointer is set at the storage location corresponding to the hash address of all elements in the linked list corresponding to the conflicting element, pointing to the hash address of the next conflicting element. By linearly probing the insertion of conflicting elements into empty spaces, memory utilization can be improved. In addition, by setting the linked list through the linked list pointer, when searching for an element, the target hash address matching the target storage address can be found in the target hash table. It is then determined whether the element to be searched exists in the storage location corresponding to the target hash address, or in the linked list corresponding to the target hash address, and whether the element to be searched exists. This improves search efficiency and achieves a better balance between performance and memory resource usage.
[0021] Furthermore, an auxiliary lookup table can be set up. The auxiliary addresses in the auxiliary lookup table are matched one-to-one with the hash addresses in the hash table. The matching results corresponding to the auxiliary addresses in the auxiliary lookup table are set according to the matching situation between the initial storage address of the element with the corresponding hash address in the hash table and that hash address. Thus, when searching for the element to be searched, the auxiliary lookup table can be searched first. When a matching result is found at the auxiliary address that matches the target storage address, the corresponding hash table is searched only if the matching result indicates a match. If the matching result indicates a mismatch, the search for the corresponding hash table is abandoned directly, which can further improve the search efficiency, and the auxiliary lookup table occupies less memory. Attached Figure Description
[0022] To more clearly illustrate the technical solutions in the embodiments of this application, the drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0023] Figure 1 This is a schematic diagram illustrating element insertion in the hash table disclosed in an embodiment of this application;
[0024] Figure 2 The direct-based embodiments provided in this application Figure 1 An application scenario where a lookup fails when the hash table is created;
[0025] Figure 3 This is a schematic diagram illustrating the application of the auxiliary lookup table disclosed in the embodiments of this application;
[0026] Figure 4This is a flowchart illustrating the hash table lookup method disclosed in Embodiment 1 of this application;
[0027] Figure 5 This is a flowchart illustrating the hash table lookup method disclosed in Embodiment 2 of this application;
[0028] Figure 6 This is a flowchart illustrating the method for establishing the hash table and auxiliary lookup table in the hash table lookup method disclosed in the embodiments of this application;
[0029] Figure 7 This is a schematic diagram of the hash table lookup device disclosed in Embodiment 1 of this application;
[0030] Figure 8 This is a schematic diagram of the hash table lookup device disclosed in Embodiment 2 of this application;
[0031] Figure 9 This is a schematic diagram of the hash table lookup device disclosed in Embodiment 3 of this application;
[0032] Figure 10 This is a schematic diagram of the structure of the electronic device disclosed in the embodiments of this application. Detailed Implementation
[0033] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0034] It should be noted that the terms "first," "second," "third," and "fourth," etc., used in the specification and claims of this application are used to distinguish different objects, not to describe a specific order. The terms "comprising" and "having," and any variations thereof, in the embodiments of this application are intended to cover non-exclusive inclusion. For example, a process, method, system, product, or device that includes a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to these processes, methods, products, or devices.
[0035] The electronic device disclosed in this application can be a terminal device such as a personal computer (PC) or a virtual machine, and can perform operations such as insertion, deletion and search of hash tables.
[0036] Hash tables are data structures based on hash functions, used for efficient insertion, deletion, and search operations. In computer science, hash tables are widely used in various scenarios, such as database indexes, caching systems, and symbol tables.
[0037] Therefore, this application discloses a hash table lookup method, apparatus, electronic device, and storage medium, which can improve lookup efficiency and reduce memory usage, achieving a better balance between performance and resource usage, especially when the data volume is large, it has the above advantages.
[0038] Below, we will first introduce the creation of hash tables and auxiliary lookup tables and the insertion of elements involved in the embodiments of this application.
[0039] The first step is to create a hash table: obtain the data to be stored, and determine the number of hash tables based on the size of the data to be stored.
[0040] When the amount of data to be stored is small, a single hash table is sufficient; only one hash table needs to be created. When the amount of data to be stored is large, it is divided into multiple data groups, each containing at least one element to be inserted. Optionally, the data groups can be divided according to the size of a secondary lookup table, which should be large enough to fit on a pre-defined page, ensuring good cache locality and preventing page breaks during searches. For example, the secondary lookup table could be 4KB in size. By grouping the data to be stored, it's equivalent to dividing a large hash table into multiple appropriately sized hash tables, thus solving the performance and management problems caused by a single large table and improving cache locality. It should be noted that because the data groups are divided according to a certain size, only the last data group may contain only one element to be inserted; other data groups contain multiple elements to be inserted.
[0041] The second step involves inserting elements into each hash table. In this embodiment, a combination of open addressing and chaining is used.
[0042] During the first element insertion, the data group is traversed, and the initial storage address of the element to be inserted in the data group is obtained using the open addressing method. Then, the element to be inserted is inserted into the position corresponding to the hash address that matches the initial storage address in a hash table. If an element is detected to have a conflict with an element that has already been inserted into the hash table during traversal, this element is placed into a container as a conflicting element. After one traversal, some or all of the hash address positions in the hash table have been inserted with elements that do not have conflicts. If the container contains conflicting elements, the chaining method is used to traverse the container sequentially. Based on the initial storage address of the conflicting element, a matching hash address is found in the hash table. An empty slot is then detected using linear probing, and the conflicting element is inserted into that slot. A linked list pointer is then set in the storage location corresponding to the hash address that matches the initial storage address, pointing to the hash address where the next conflicting element will be inserted. This process continues until all conflicting elements for a given element have been inserted. A linked list is created using these pointers, and conflicting elements within the linked list are stored in empty slots in the hash table, improving the memory utilization of the hash table. Conflicting elements for other elements are inserted in the same way.
[0043] Optionally, each hash address in the hash table corresponds to a storage location consisting of a first half and a second half. The first half stores the element, i.e., the value of the element, and the second half stores the hash address of the next colliding element in the linked list. For example, if the storage location corresponding to a hash address is 64 bits in size, then the first 32 bits are used to store the element, and the last 32 bits are used to store the hash address of the next colliding element in the linked list.
[0044] For example, Figure 1 This is a schematic diagram illustrating element insertion in the hash table disclosed in an embodiment of this application; Figure 1 In the middle, the top layer is a data group containing elements and their corresponding initial storage addresses. That is, a rectangle contains an element and its initial storage address, which are as follows: a, initial storage address: 1; b, initial storage address: 2; c, initial storage address: 4; d, initial storage address: 2; e, initial storage address: 6; f, initial storage address: 2.
[0045] After the first insertion using open addressing, we obtain Figure 1The intermediate layer's hash table. Class A is inserted into the empty slot at hash address 1 based on its initial storage address: 1; Class B is inserted into the empty slot at hash address 2 based on its initial storage address: 2; Class C is inserted into the empty slot at hash address 4 based on its initial storage address: 4; and Class E is inserted into the empty slot at hash address 6 based on its initial storage address: 6, thus obtaining... Figure 1 The intermediate layer uses a hash table. Furthermore, classes d, f, and b all have the same initial storage address; conflicting elements belonging to class b are first placed into the container. Figure 1 (Containers are not shown in the diagram). It should be noted that elements of class b, class d, and class f have the same initial storage address as class b; however, in the embodiments of this application, the conflicting elements only include classes d and f.
[0046] Combining the chaining method, and using linear probing to insert conflicting elements in the container a second time, we obtain... Figure 1 The lower layer of the diagram illustrates the hash table, namely the hash table established in the embodiments of this application, which is used for subsequent searches. Specifically, class d elements are inserted into the empty slot at hash address 3 via linear probing, and class f elements are inserted into the empty slot at hash address 5 via linear probing. A linked list pointer is set at the storage location at hash address 2, pointing to hash address 3, and a linked list pointer is set at the storage location at hash address 3, pointing to hash address 5. This connects all elements with the same initial storage address to the same hash address via linked lists. However, conflicting elements are stored in empty slots of the hash table instead of using separate hash buckets. This fully utilizes the hash table's memory resources and improves search efficiency, achieving a balance between search efficiency and memory resource usage.
[0047] The third step is to match each hash table and establish an auxiliary lookup table. The hash table established in the second step effectively balances memory resource usage and search efficiency. In this embodiment, establishing an auxiliary lookup table further optimizes search efficiency. When dealing with large amounts of data, multiple hash tables may be established after the first and second steps, requiring the search for the desired element across multiple hash tables. However, collisions in the hash tables are inserted using linear probing. The hash address of a collision element will definitely not match its initial storage address. Therefore, when searching for an element, if the hash address of a collision element is found by matching the storage address of the element being searched, that collision element is definitely not the element being searched for. The search will continue using the pointers in the linked list to find the next collision element until all collision elements in the linked list have been searched before the process ends.
[0048] For example, Figure 2 The direct-based embodiments provided in this application Figure 1 An application scenario where a lookup fails when performing a lookup on the hash table; Figure 2 In Figure 1 The search for element class n is performed on the lowest-level hash table. Element class n is stored at address 3. Matching address 3 leads to hash address 3, but hash address 3 stores element class d, not the element class n. Further searching using the linked list pointer leads to hash address 5, which is also not the element class n. Therefore, in this application scenario, the search starts from hash address 3, traverses the linked list to hash address 5, and only then is the failed search completed.
[0049] Furthermore, if the search result is an empty space in the hash table, it indicates a mismatch and that the element is not the one being searched for. Therefore, in this application, both of these cases are considered to be due to a mismatch between the element's initial storage address and its hash address.
[0050] In this embodiment, an auxiliary lookup table is established for each hash table. The auxiliary addresses of the auxiliary lookup table, the hash addresses of the hash tables, and the storage addresses of elements within the hash addresses in the hash tables are matched one-to-one. The number of auxiliary addresses in the auxiliary lookup table is the same as the number of hash addresses in the hash tables. Each auxiliary address in the auxiliary lookup table stores a matching result at its corresponding storage location. This matching result is set based on the matching status of the initial storage address of the element corresponding to the matched hash address in the hash table. If a match is found, the matching result is set to true; otherwise, it is set to false. This allows for the rapid elimination of mismatched hash tables using the auxiliary lookup table, quickly locating the accurate hash table to complete the element search, further optimizing search efficiency.
[0051] For example, Figure 3 This is a schematic diagram illustrating the application of the auxiliary lookup table disclosed in the embodiments of this application; Figure 3 The hash table below is Figure 1 The hash table corresponding to the lowest level, Figure 3The above is an auxiliary lookup table built for this hash table. The auxiliary addresses in the auxiliary lookup table are matched one-to-one with the hash addresses in the hash table. For example, auxiliary address 1 matches hash address 1, auxiliary address 2 matches hash address 2, and so on. Since the initial storage addresses of elements in classes a, b, c, and e in the hash table all match their corresponding hash addresses, the matching results are set to true. Therefore, the matching results at auxiliary addresses 1, 2, 4, and 6 in the auxiliary lookup table are all set to true. The initial storage addresses of elements in classes d and f do not match their corresponding hash addresses, so the matching results are set to false. Therefore, the matching results at auxiliary addresses 3 and 5 in the auxiliary lookup table are set to false. When searching for an element, the auxiliary lookup table is searched first. When a matching result for a corresponding auxiliary address in the auxiliary lookup table is found to be false, the search in the matching hash table is abandoned, and another auxiliary lookup table is searched until a matching result for a corresponding auxiliary address in the matching auxiliary lookup table is found to be true. Only then is the search in the matching hash table resumed. As can be seen, by first searching the auxiliary lookup table, even if the search fails, the result can be known in just one step. If the hash table is searched directly, according to... Figure 2 It is known that it may take two or more steps to find the result of failure. Therefore, by using an auxiliary lookup table, the search efficiency is further improved.
[0052] Furthermore, the matching result corresponding to each auxiliary address in the auxiliary lookup table only needs to occupy 1 bit, and the entire auxiliary lookup table occupies a small amount of memory, which can ensure that there will be no page crossing in memory when using the fast lookup table.
[0053] Based on the above Figure 1 The hash table and Figure 3 The auxiliary lookup table has been established, and the technical solution of this application will be further described below through specific embodiments.
[0054] like Figure 4 As shown, Figure 4 This is a flowchart illustrating the hash table lookup method disclosed in Embodiment 1 of this application; as shown Figure 4 As shown, the hash table lookup method may include:
[0055] 401. Obtain the target storage address of the element to be searched.
[0056] The execution subject of this application embodiment is an electronic device, as described above, and will not be repeated here.
[0057] Specifically, when it is necessary to look up an element in the hash table, the element to be looked up is retrieved first.
[0058] In a feasible implementation, step 401 may include: obtaining the target storage address of the element to be searched using the open addressing method.
[0059] Furthermore, the target storage address of the element to be searched is obtained through the open addressing method, including: obtaining the total number of key values of the element to be searched and the hash addresses of the target hash table, calculating the key value / total number using the modulo method, obtaining the remainder, and using it as the target storage address of the element to be searched.
[0060] For example, if the key value of the element to be searched is 2 and the total number of hash addresses in the target hash table is 10, then the target storage address obtained is 2, 2 = 2%10.
[0061] 402. Check if the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or the elements stored at the storage location corresponding to the target hash address and all elements in the linked list corresponding to the target hash address.
[0062] The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method in the first insertion and the linear probing method combined with the chaining method to insert conflicting elements. The insertion of a conflicting element requires setting a linked list pointer in the storage location corresponding to the hash address of all elements in the corresponding linked list to indicate the hash address of the next conflicting element.
[0063] Here, the hash address is the index of the hash table array, such as... Figure 1 In this context, the six hash addresses of the hash table are represented by numbers 1 to 6.
[0064] In step 402, based on the target storage address of the element to be searched, a matching target hash address is found in the target hash table, and then it is checked whether the element to be searched exists under all elements of that target hash address.
[0065] In one feasible implementation, the target hash table of this application embodiment is based on Figure 1 The corresponding method is established. All elements under the target hash address can include elements stored at the storage location corresponding to the target hash address, such as... Figure 1 If the hash address is 1, then all elements under hash address 1 belong to class 'a'. All elements under the target hash address can include the elements stored at the storage location corresponding to the target hash address and all elements in the linked list corresponding to the target hash address, such as... Figure 1 If the hash address is 2, then all elements on the linked list corresponding to hash address 2 include those of class b, class d, and class f.
[0066] Furthermore, step 402 may include:
[0067] Check whether the element stored at the storage location corresponding to the target hash address in the target hash table is the element to be searched;
[0068] If not, check if the linked list pointer set at the storage location corresponding to the target hash address points to the next conflicting element. If it does, check if the next conflicting element is the element to be searched. Continue until the element to be searched is found by sequentially using the linked list pointers, or until the last conflicting element is not the element to be searched.
[0069] Furthermore, when the element stored at the storage location corresponding to the target hash address is the element to be searched, or when the element to be searched is found sequentially according to the linked list pointer under the target hash address, it is determined that the element to be searched has been found.
[0070] In the aforementioned feasible implementation, if the element stored at the storage location corresponding to the target hash address is found, and this element is the same as the element to be searched, then the searched element is found, and the process ends. If the element is different from the element to be searched, it is further determined whether the storage location corresponding to the target hash address has a linked list pointer pointing to the next conflicting element. If so, the next conflicting element is obtained based on the hash address pointed to by the linked list pointer. It is then determined whether this conflicting element is the element to be searched. If it is, the process continues at this point. If not, the next conflicting element is obtained based on the next hash address pointed to by the linked list pointer of the conflicting element, and the process continues until a conflicting element is found that is the element to be searched, at which point the process ends. Alternatively, the process ends when all elements in the linked list have been searched and the element to be searched has not been found.
[0071] For example, such as searching Figure 1 In the bottom hash table, if the target hash address is hash address 1, check if class a is the element to be searched. If it is, the search is successful and the process ends. If not, the process also ends because hash address 1 does not exist in the linked list. If the hash address is hash address 2, check if class b stored at hash address 2 is the element to be searched. If it is, the element to be searched has been found and the process ends. If class b is not the element to be searched, use the linked list pointer to find class d at hash address 3. Check if class d is the element to be searched. If it is, the element to be searched has been found and the process ends. If class d is not the element to be searched, continue using the linked list pointer to find class f at hash address 5. Check if class f is the element to be searched. If it is, the element to be searched has been found and the process ends. If not, the process also ends, meaning the element to be searched was not found in the target hash table.
[0072] Alternatively, if the element stored at the storage location corresponding to the target hash address is not the element to be searched, or if the last conflicting element detected by the linked list pointer under the target hash address is still not the element to be searched, and if there is an unchecked next hash table, the next hash table is obtained as the target hash table, and the above steps of checking whether the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table.
[0073] Among them, according to Figure 1 The method and process for creating the hash table shown may involve multiple hash tables. In step 401, a hash table is first obtained as the target hash table. If the element to be searched is not found in the target hash table, the next hash table is obtained as the target hash table, and the search is performed again according to the above search method until the element to be searched is found in a certain hash table, or if the element to be searched is not found after searching all hash tables, a search failure message is displayed.
[0074] As can be seen, through the embodiments of this application, elements in the target hash table can be inserted for the first time using open addressing and for the second time using chaining. When using chaining, the next conflicting element is inserted into an empty space in the target hash table using linear probing. A linked list pointer is set at the storage location corresponding to the hash address of all elements in the linked list corresponding to the conflicting element, pointing to the hash address of the next conflicting element. Thus, by linearly probing the insertion of conflicting elements into empty spaces, memory utilization can be improved. In addition, by setting the linked list through the linked list pointer, when searching for an element, the target hash address matching the target storage address can be found in the target hash table. It is then determined whether the element to be searched exists in the storage location corresponding to the target hash address, or in the linked list corresponding to the target hash address, and whether the element to be searched exists. This improves search efficiency and achieves a better balance between performance and memory resource usage.
[0075] Please see Figure 5 , Figure 5 This is a flowchart illustrating the hash table lookup method disclosed in Embodiment 2 of this application; as shown Figure 5 As shown, the hash table lookup method may include:
[0076] 501. Obtain the target storage address of the element to be searched.
[0077] The execution subject of this application embodiment is an electronic device, as described above, and will not be repeated here.
[0078] 502. Search the target auxiliary lookup table to obtain the target matching result corresponding to the target auxiliary address that matches the target storage address in the target auxiliary lookup table. The target auxiliary address also matches the target hash address. The target matching result is set according to the matching situation between the initial storage address of the element corresponding to the target hash address in the target hash table and the target hash address.
[0079] In this embodiment of the application, an auxiliary lookup table is further established. When searching for an element to be searched, the target auxiliary address in the target auxiliary lookup table is first searched according to the target storage address. Based on the matching result corresponding to the target auxiliary address, it is determined whether it is necessary to further search the corresponding target hash table or whether the target hash table can be directly abandoned.
[0080] For example, the creation of a target auxiliary lookup table is as follows: Figure 3 As shown, a match is indicated when the result is true, and a match is indicated when the result is false.
[0081] Optionally, the auxiliary lookup table can be set on a page of a preset size, such as 4KB. Each auxiliary address in the auxiliary lookup table stores a matching result that occupies 1 bit of memory.
[0082] Where the auxiliary address is the array index of the auxiliary lookup table, for example, such as Figure 3 As shown, the auxiliary addresses are 1 to 6, and the auxiliary addresses 1-6 correspond one-to-one with the hash addresses 1-6. That is, auxiliary address 1 corresponds to hash address 1, and so on.
[0083] 503. When the target matching result indicates a match, check if the element to be searched exists in all elements under the target hash address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or all elements on the linked list corresponding to the target hash address.
[0084] The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method in the first insertion and the linear probing method combined with the chaining method to insert conflicting elements. The insertion of a conflicting element requires setting a chain pointer in the storage location corresponding to the hash address of all elements in the corresponding chain to indicate the hash address of the next conflicting element.
[0085] Furthermore, when the element stored at the storage location corresponding to the target hash address is the element to be searched, or when the element to be searched is found sequentially according to the linked list pointer under the target hash address, it is determined that the element to be searched has been found.
[0086] Alternatively, if the element stored at the storage location corresponding to the target hash address is not the element to be searched, or if the last conflicting element detected by the linked list pointer under the target hash address is still not the element to be searched, if there is an unchecked next auxiliary lookup table, the next auxiliary lookup table is obtained as the target auxiliary lookup table, and the next hash table that matches the next auxiliary lookup table is taken as the target hash table. The step of checking whether the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table.
[0087] In this method, according to Figure 1 and Figure 3 The method and process for creating the hash table and auxiliary lookup table shown may involve multiple hash tables and corresponding auxiliary lookup tables for each hash table. First, the auxiliary lookup table is used to quickly eliminate mismatched hash tables, rapidly locating a possible hash table. The element to be searched is then found within that possible hash table, avoiding issues such as... Figure 2 The failed search scenarios shown further improve search efficiency.
[0088] Through the embodiments of this application, an auxiliary lookup table can be set up. The auxiliary addresses in the auxiliary lookup table are matched one-to-one with the hash addresses in the hash table. The matching results corresponding to the auxiliary addresses in the auxiliary lookup table are set according to the matching situation between the initial storage address of the element with the corresponding hash address in the hash table and the hash address. Therefore, when searching for the element to be searched, the auxiliary lookup table can be searched first. When a matching result is found at the auxiliary address that matches the target storage address, the corresponding hash table is searched only when the matching result indicates a match. If the matching result indicates a mismatch, the search for the corresponding hash table is directly abandoned, which can further improve the search efficiency, and the auxiliary lookup table occupies less memory.
[0089] Please see Figure 6 , Figure 6 This is a flowchart illustrating the method for establishing the hash table and auxiliary lookup table in the hash table lookup method disclosed in the embodiments of this application; as shown... Figure 6 As shown, the methods for establishing the hash table and auxiliary lookup table may include:
[0090] 601. Obtain at least one element to be inserted.
[0091] 602. Establish a hash table and a corresponding auxiliary lookup table, where the hash address of the hash table is matched one-to-one with the auxiliary address of the auxiliary lookup table.
[0092] Optionally, before obtaining at least one element to be inserted, the data to be stored can be obtained and divided into multiple data groups, each data group including at least one element to be inserted, and the size of the data group is determined according to the size of the auxiliary lookup table.
[0093] Furthermore, the above-mentioned establishment of a hash table and a corresponding auxiliary lookup table includes:
[0094] For each data group, create a hash table and a corresponding auxiliary lookup table.
[0095] 603. Using the open addressing method, traverse at least one element to be inserted, and perform the first insertion on the hash table by matching the initial storage address of the element to be inserted with the hash address. During the traversal, elements to be inserted that have conflicts are placed into the container in sequence as conflict elements.
[0096] Optionally, the initial storage address of the element to be inserted is obtained using the open addressing method.
[0097] Furthermore, the initial storage address of the element to be inserted is obtained through the open addressing method, including: obtaining the key value of the element to be inserted and the total number of hash addresses of the hash table, calculating the key value / total number using the modulo method, obtaining the remainder, and using it as the target storage address of the element to be inserted.
[0098] 604. Combining the chaining method, the collision elements in the container are inserted into the empty slots of the hash table using a linear probing method. For all elements in the linked list corresponding to the collision element, a pointer to the linked list is stored at the corresponding hash address to indicate the hash address of the next collision element.
[0099] For example, such as Figure 1 As shown, the conflicting elements detected during the first insertion process are of type d and type f.
[0100] 605. Based on the matching situation between the initial storage address of the element to be inserted and the hash address corresponding to each hash address in the hash table after the element is inserted, set the matching result in the matching auxiliary address in the corresponding auxiliary lookup table.
[0101] Specifically, the hash table after inserting an element is checked. For each hash address, if the initial storage address of the element at that hash address matches the hash address, the match is considered to be a match and is set to true. If the initial storage address of the element at that hash address does not match the hash address or the hash address corresponds to an empty space, the match is considered to be a match and is set to false.
[0102] For example, such as Figure 1 and Figure 3 As shown, and referring to the above explanation.
[0103] In this embodiment, the hash table and auxiliary lookup table can be set to enable the search to first look up the auxiliary lookup table when searching for the element to be searched. When a matching result is found at the auxiliary address that matches the target storage address, the corresponding hash table is searched only when the matching result indicates a match. If the matching result indicates a mismatch, the search for the corresponding hash table is abandoned directly, which can further improve the search efficiency, and the auxiliary lookup table occupies less memory.
[0104] Please see Figure 7 , Figure 7 This is a schematic diagram of the hash table lookup device disclosed in Embodiment 1 of this application; as shown Figure 7 As shown, the hash table lookup device may include:
[0105] The first acquisition module 701 is used to acquire the target storage address of an element to be searched.
[0106] The first search module 702 is used to search whether the element to be searched exists in all elements under the target hash address that matches the target storage address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or all elements on the linked list corresponding to the target hash address.
[0107] The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method for the first time and the chaining method for the second time by inserting them into the chaining table using the linear probing method. The insertion of a conflicting element requires setting a chain pointer in the storage location corresponding to the hash address of all elements in the corresponding chain to indicate the hash address of the next conflicting element.
[0108] In a feasible implementation, the first search module 702 searches the target hash table for all elements under the target hash address that matches the target storage address to determine whether the element to be searched exists. Specifically, the method is as follows:
[0109] Check whether the element stored at the storage location corresponding to the target hash address in the target hash table is the element to be searched;
[0110] If not, check if the linked list pointer set at the storage location corresponding to the target hash address points to the next conflicting element. If it does, check if the next conflicting element is the element to be searched. Continue until the element to be searched is found by sequentially using the linked list pointers, or until the last conflicting element is detected as not being the element to be searched.
[0111] Furthermore, the first search module 702 determines that the element to be searched has been found when the element stored in the storage location corresponding to the target hash address is the element to be searched, or when the element to be searched is found sequentially according to the linked list pointer under the target hash address.
[0112] Please see Figure 8 , Figure 8 This is a schematic diagram of the hash table lookup device disclosed in Embodiment 2 of this application; as shown Figure 8 As shown, the hash table lookup device also includes:
[0113] The second lookup module 801 is used to look up the target auxiliary lookup table and obtain the target matching result corresponding to the target auxiliary address that matches the target storage address in the target auxiliary lookup table. The target auxiliary address also matches the target hash address. The target matching result is set according to the matching situation between the initial storage address of the element corresponding to the target hash address in the target hash table and the target hash address.
[0114] Furthermore, the first search module 702 is used to search the target hash table for all elements under the target hash address that matches the target storage address to determine if the element to be searched exists. Specifically, the method is as follows:
[0115] When the second search module 801 indicates a match in the target matching result, it searches the target hash table to see if the element to be searched exists in all elements under the target hash address.
[0116] Furthermore, the first acquisition module 701 is also used to, when the element stored at the storage location corresponding to the target hash address is not the element to be searched, or when the last conflicting element detected by the linked list pointer under the target hash address is still not the element to be searched, if there is an unchecked next auxiliary lookup table, acquire the next auxiliary lookup table as the target auxiliary lookup table, and take the next hash table that matches the next auxiliary lookup table as the target hash table, triggering the first search module 702 to perform the step of searching whether the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table.
[0117] Please see Figure 9 , Figure 9 This is a schematic diagram of the hash table lookup device disclosed in Embodiment 3 of this application; as shown Figure 9 As shown, the hash table lookup device also includes:
[0118] The table creation module 901 is used to obtain at least one element to be inserted before the first acquisition module 701 obtains the target storage address of the element to be searched; to establish a hash table and an auxiliary lookup table corresponding to the hash table, wherein the hash address of the hash table is matched one-to-one with the auxiliary address of the auxiliary lookup table; using the open addressing method, the at least one element to be inserted is traversed, and the first insertion is performed on the hash table according to the matching of the initial storage address corresponding to the element to be inserted with the hash address, and during the traversal, the elements to be inserted that have conflicts are sequentially placed into a container as conflict elements; combined with the chaining method, the conflict elements in the container are inserted a second time into the empty slots of the hash table using a linear probing method, and the pointers of the linked lists corresponding to the conflict elements are set at the corresponding hash addresses of all elements to indicate the hash address of the next conflict element; according to the matching situation of the initial storage address of the element to be inserted corresponding to each hash address on the hash table after the element is inserted, the matching result is set in the matching auxiliary address of the corresponding auxiliary lookup table.
[0119] Furthermore, the table creation module 901 described above is also used to obtain data to be stored before obtaining at least one element to be inserted, and to divide the data to be stored into multiple data groups, wherein the data group includes the at least one element to be inserted, and the size of the data group is obtained according to the size of the auxiliary lookup table.
[0120] Furthermore, the table creation module 901 is used to create a hash table and an auxiliary lookup table corresponding to the hash table in the following way: for each data group, a hash table and an auxiliary lookup table corresponding to the hash table are created.
[0121] Please see Figure 10 , Figure 10 This is a schematic diagram of the structure of the electronic device disclosed in the embodiments of this application; Figure 10 The electronic device shown may include:
[0122] Memory 1001 storing executable program code;
[0123] Processor 1002 coupled to memory 1001;
[0124] Specifically, the processor 1002 calls the executable program code stored in the memory 1001 and executes it. Figures 4 to 6 Some or all of the steps of any method.
[0125] This application also discloses a computer-readable storage medium storing a computer program, wherein the computer program causes a computer to execute... Figures 4 to 6 A publicly disclosed hash table lookup method.
[0126] This application also discloses a computer program product that, when run on a computer, causes the computer to perform... Figures 4 to 6 Some or all of the steps of any of the disclosed methods.
[0127] This application also discloses an application publishing platform for publishing computer program products, wherein when the computer program product is run on a computer, the computer executes... Figures 4 to 6 Some or all of the steps of any of the disclosed methods.
[0128] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing related hardware. The program can be stored in a computer-readable storage medium, including read-only memory (ROM), random access memory (RAM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), one-time programmable read-only memory (OTPROM), electrically-Erasable Programmable Read-Only Memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, disk storage, magnetic tape storage, or any other computer-readable medium capable of carrying or storing data.
[0129] The above provides a detailed description of a hash table lookup method, apparatus, electronic device, and storage medium disclosed in the embodiments of this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the above embodiments are only for the purpose of helping to understand the method and core ideas of this application. At the same time, for those skilled in the art, there will be changes in the specific implementation methods and application scope based on the ideas of this application. Therefore, the content of this specification should not be construed as a limitation of this application.
Claims
1. A hash table lookup method, characterized in that, include: Get the target storage address of the element to be searched; The search involves checking if the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or all elements in the linked list corresponding to the target hash address. The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method for the first time and the chaining method for the second time by inserting them into the chaining table using the linear probing method. The insertion of a conflicting element requires setting a chain pointer in the storage location corresponding to the hash address of all elements in the corresponding chain to indicate the hash address of the next conflicting element.
2. The method according to claim 1, characterized in that, The process of checking whether the element to be searched exists in the target hash table under the target hash address that matches the target storage address includes: Check whether the element stored at the storage location corresponding to the target hash address in the target hash table is the element to be searched; If not, check if the linked list pointer set at the storage location corresponding to the target hash address points to the next conflicting element. If it does, check if the next conflicting element is the element to be searched. Continue until the element to be searched is found by sequentially using the linked list pointers, or until the last conflicting element is detected as not being the element to be searched.
3. The method according to claim 2, characterized in that, Before checking whether the element to be searched exists in the target hash table under all elements matching the target storage address, the method further includes: Search the target auxiliary lookup table to obtain the target matching result corresponding to the target auxiliary address that matches the target storage address in the target auxiliary lookup table. The target auxiliary address also matches the target hash address. The target matching result is set according to the matching situation between the initial storage address of the element corresponding to the target hash address in the target hash table and the target hash address. The process of checking whether the element to be searched exists in the target hash table under the target hash address that matches the target storage address includes: When the target matching result indicates a match, check if the element to be searched exists in all elements under the target hash address in the target hash table.
4. The method according to claim 3, characterized in that, The method further includes: The element to be searched is determined to be found when the element stored at the storage location corresponding to the target hash address is the element to be searched, or when the element to be searched is found sequentially according to the linked list pointer under the target hash address.
5. The method according to claim 3, characterized in that, The method further includes: If the element stored at the storage location corresponding to the target hash address is not the element to be searched, or if the last conflicting element detected by the linked list pointer under the target hash address is still not the element to be searched, if there is an unchecked next auxiliary lookup table, the next auxiliary lookup table is obtained as the target auxiliary lookup table, and the next hash table that matches the next auxiliary lookup table is taken as the target hash table. Then, the step of checking whether the element to be searched exists in all elements under the target hash address that match the target storage address in the target hash table.
6. The method according to claim 5, characterized in that, Before obtaining the target storage address of the element to be searched, the method further includes: Get at least one element to be inserted; Establish a hash table and a corresponding auxiliary lookup table, wherein the hash address of the hash table is matched one-to-one with the auxiliary address of the auxiliary lookup table; Using the open addressing method, the at least one element to be inserted is traversed. The first insertion is performed on the hash table by matching the initial storage address and hash address of the element to be inserted. During the traversal, elements to be inserted that have conflicts are placed into the container as conflict elements in sequence. Combining the chaining method, the collision elements in the container are inserted into the empty slots of the hash table using a linear probing method, and pointers to the linked lists corresponding to the collision elements are stored at the corresponding hash addresses to indicate the hash address of the next collision element. Based on the matching situation between the initial storage address of the element to be inserted and the hash address corresponding to each hash address in the hash table after the element is inserted, the matching result is set in the matching auxiliary address in the corresponding auxiliary lookup table.
7. The method according to claim 6, characterized in that, Before obtaining at least one element to be inserted, the method further includes: Obtain the data to be stored, divide the data to be stored into multiple data groups, each data group including the at least one element to be inserted, and the size of the data group is determined according to the size of the auxiliary lookup table; The establishment of a hash table and a corresponding auxiliary lookup table includes: For each data group, a hash table and an auxiliary lookup table corresponding to the hash table are created.
8. The method according to claim 7, characterized in that, The size of the auxiliary lookup table is such that it is set on a page of a preset size.
9. The method according to claim 3, characterized in that, The matching result stored at each auxiliary address in the auxiliary lookup table occupies 1 bit of memory.
10. The method according to claim 4, characterized in that, Both the target storage address and the initial storage address of the element to be inserted are obtained according to the open addressing method.
11. A hash table lookup device, characterized in that, include: The first acquisition module is used to acquire the target storage address of an element to be searched. The first search module is used to search whether the element to be searched exists in all elements under the target hash address that matches the target storage address in the target hash table. All elements under the target hash address include the elements stored at the storage location corresponding to the target hash address, or all elements on the linked list corresponding to the target hash address. The elements stored in the target hash table are obtained by inserting them into empty slots in the target hash table using the open addressing method for the first time and the chaining method for the second time by inserting them into the chaining table using the linear probing method. The insertion of a conflicting element requires setting a chain pointer in the storage location corresponding to the hash address of all elements in the corresponding chain to indicate the hash address of the next conflicting element.
12. An electronic device, characterized in that, include: A memory that stores computer programs; A processor coupled to the memory; The processor invokes the computer program stored in the memory to execute the method according to any one of claims 1-10.
13. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1-10.