Hash table processing methods, devices, media and equipment
By using key-value separation and balanced binary trees to handle hash collisions in the hash table, and by expanding the capacity in stages, the storage efficiency and performance issues of the hash table are solved, achieving efficient data storage and operation.
Patent Information
- Application Number
- CN202411754170.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-02
- Publication Date
- 2025-11-14
- Estimated Expiration
- 2044-12-02
AI Technical Summary
Existing hash tables have shortcomings in storage and lookup efficiency, especially in terms of performance degradation during hash collisions and expansion.
A key-value separation approach is adopted, using an index array and a data array to store data addresses respectively. A balanced binary tree is used to handle hash collisions, and a step-by-step expansion technique is employed to avoid excessive computation caused by a one-time expansion.
It improves the storage efficiency of hash tables, reduces the lookup complexity when hash collisions occur, optimizing it from O(n) to close to O(logn), and makes the resizing process smoother, avoiding performance degradation.
Smart Images

Figure CN119690969B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data storage technology, and in particular to a hash table processing method, apparatus, medium and device. Background Technology
[0002] With the advent of the information age, the amount of data that needs to be stored is growing exponentially. A hash table, also known as a hash table, is a data storage structure that establishes a one-to-one mapping between the storage location of an element and its key (value). With the development of internet technology, hash tables, as a key-value storage data structure, have been widely used in data storage. Their characteristic is that they trade space for time, calculating the hash value of the key using a hash function to quickly locate the value in the table. For those skilled in the art, how to improve the hash table data structure to achieve efficient data storage and access is a topic that requires continuous research. Summary of the Invention
[0003] In view of this, this application provides a hash table processing method, apparatus, medium and device, the main purpose of which is to use hash tables for efficient data storage.
[0004] According to one aspect of this application, a hash table processing method is provided, the hash table including an index array and a data array, wherein the index array stores the address of data in the data array, and the data array stores the data, the processing method comprising:
[0005] Calculate the hash value of the target data, and calculate the index position based on the hash value;
[0006] Access the index array according to the index position to obtain the address of the target data in the data array corresponding to the index position;
[0007] Based on the address of the target data in the data array, access the data array and perform search, insertion, or deletion operations on the target data.
[0008] One implementation also includes:
[0009] Determine whether the same hash value is calculated for different data; if so, a hash collision is confirmed.
[0010] The data array is organized using a balanced binary tree data structure to handle hash collisions.
[0011] In one implementation, the data array is organized using a balanced binary tree data structure, including:
[0012] If the root node is empty, and the position in the data array is determined to be empty, then the data is directly placed into the corresponding position. If the root node is not empty, then traverse from the root node to find the insertion position of the element to be inserted.
[0013] In one implementation, the step of handling the hash collision includes:
[0014] When inserting data into a binary search tree, the nodes searched during the insertion process are recorded. After insertion, the search proceeds upwards from the inserted node, rotating and adjusting sequentially until the root node is reached.
[0015] In one implementation,
[0016] The hash table includes an old hash table and a new hash table with increased capacity, wherein the capacity of the new hash table is greater than the capacity of the old hash table;
[0017] The method further includes setting an expansion flag to indicate whether an expansion operation is in progress.
[0018] One implementation also includes:
[0019] Read the expansion identifier to determine whether a hash table expansion operation is in progress;
[0020] If so, determine the capacity of the index array and the data array of the new hash table based on the specified expansion size; and, in response to a data operation request, perform the following steps:
[0021] For data insertion operation requests, insert the data into the new hash table;
[0022] For data lookup requests, the old hash table is searched first; if the data is not found there, the new hash table is then searched.
[0023] For data deletion requests, first search the old hash table. If found, delete the data. If not found, then search the new hash table and delete the data.
[0024] One implementation also includes:
[0025] After each hash resizing, it is determined whether all elements in the old hash table have been migrated to the new hash table. If the resizing is complete, the resizing flag is reset.
[0026] According to one aspect of this application, a hash table processing apparatus is provided, the hash table including an index array and a data array, wherein the index array stores the address of data in the data array, and the data array stores the data, the processing apparatus comprising:
[0027] An index position determination unit is used to calculate the hash value of the target data and calculate the index position based on the hash value;
[0028] An index array access unit is used to access the index array according to the index position to obtain the address of the target data corresponding to the index position in the data array;
[0029] The data array access unit is used to access the data array according to the address of the target data in the data array, and to perform search, insertion or deletion operations on the target data.
[0030] According to one aspect of this application, a method is provided.
[0031] According to one aspect of this application, a storage medium is provided that stores a computer program, wherein the computer program is configured to execute the hash table processing method described above when it runs.
[0032] According to one aspect of this application, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the hash table processing method described above.
[0033] By means of the above technical solution, this application provides a hash table processing method, apparatus, medium and device, which stores the address of data in the data array in the index array and stores data in the data array. When performing data operations, the index array is first accessed according to the index position to obtain the address of the target data in the data array corresponding to the index position; then the data array is accessed according to the address of the target data in the data array to perform search, insertion or deletion operations on the target data.
[0034] As can be seen, this application uses index arrays and data arrays respectively. The index array stores the address of the data corresponding to the index in the data array. For example, a 4-byte index array is used to store the position of the data in the data array, instead of storing it in the actual position in memory, which can save storage space.
[0035] Furthermore, in one implementation, compared to the existing hash table which uses linked list storage, this application embodiment uses a balanced binary tree to store data with the same hash value, solving the problem of low search efficiency when hash collisions are severe, and reducing the time complexity from O(n) to close to O(logn).
[0036] In addition, compared with one-time expansion, the embodiments of this application adopt step-by-step expansion to avoid rehashing a large number of elements into a new table in a short period of time. This can avoid the huge amount of computation and long-term blocking caused by one-time expansion, making the entire expansion process smoother.
[0037] In summary, the hash table processing scheme provided in this application can improve the efficiency of data storage.
[0038] The above description is only an overview of the technical solution of this application. In order to better understand the technical means of this application and to implement it in accordance with the contents of the specification, and to make the above and other objects, features and advantages of this application more obvious and understandable, the following are specific embodiments of this application. Attached Figure Description
[0039] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0040] Figure 1 A schematic diagram of a hash table structure in the prior art is shown;
[0041] Figure 2 A flowchart of a hash table processing method provided in an embodiment of this application is shown;
[0042] Figure 3 This illustration shows a schematic diagram of a hash table structure as an example of a hash table processing method provided in this application embodiment;
[0043] Figure 4 A schematic diagram of a hash table processing device provided in an embodiment of this application is shown. Detailed Implementation
[0044] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present application, and not all of them. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present application. It should be noted that, unless otherwise specified, the embodiments and features in the embodiments of the present application can be combined with each other.
[0045] See Figure 1 An existing hash table uses linked lists to store elements with the same hash value, inserting all elements with the same hash value sequentially at the end of the linked list. Searching requires traversing from the head of the linked list until the desired element is found. When the hash table reaches its maximum size, a new hash table needs to be created, and all elements from the old table are sequentially inserted into the new hash table. Each node in the linked list stores the current value and a pointer to the next node.
[0046] The inventors of this application have discovered through research, see [link / reference] Figure 1 The existing hash table has the following problems.
[0047] 1. Existing hash tables use pointers to store the memory address of the next element, which requires 8 bytes in a 64-bit operating system. Balanced binary trees require 16 bytes to store pointers to the left and right nodes. When the number of nodes in the tree is large, the memory usage is relatively high.
[0048] 2. Existing hash tables generally use chaining to resolve hash collisions, placing elements with the same hash value in a linked list. In extreme cases, if a large number of elements have the same hash value, the linked list will become too long. Hash table queries, insertions, and deletions will require traversing the entire linked list, and the time complexity will degenerate from O(1) to O(n), resulting in a significant performance decrease.
[0049] 3. The existing hash table resizing uses a one-time resizing method. When the hash table is full, all elements are rehashed and inserted into the new hash table at once. When the number of elements in the hash table is very large, one-time resizing will cause a lot of computation, and the resizing process will be very time-consuming. At this time, the hash table's query, insertion, and deletion operations may be blocked, resulting in performance degradation.
[0050] Based on the above analysis, this application proposes a hash table processing method to solve one or more of the above problems.
[0051] refer to Figure 2 The above is a flowchart of a hash table processing method provided in an embodiment of this application. The hash table includes an index array and a data array. The index array stores the address of the data in the data array, and the data is stored in the data array. The method includes the following steps S201-S203.
[0052] S201: Calculate the hash value of the target data and calculate the index position based on the hash value;
[0053] S202: Access the index array according to the index position to obtain the address of the target data in the data array corresponding to the index position;
[0054] S203: Access the data array based on the address of the target data in the data array, and perform search, insertion, or deletion operations on the target data.
[0055] In this implementation, the index array stores the address of the data in the data array (rather than the memory address of the data), while the data array stores the data itself. Therefore, by separating the key and value using the index and data arrays, the high memory consumption problem of existing hash tables can be solved. Traditional hash tables use linked lists to store elements with the same hash value. Linked lists require storing pointers to the next element, which, for example, requires 8 bytes on a 64-bit operating system. This embodiment uses a key-value separation approach, storing the key and value separately in the index and data arrays. This eliminates the need for 8 bytes to store pointers; instead, 4 bytes are used to store the data's position in the data array. Storing pointers would require 16 bytes for the left and right pointers in a balanced binary tree, while this implementation only requires 8 bytes, significantly saving memory.
[0056] To address the hash collision problem in existing hash tables, this application proposes using a balanced binary tree data structure combined with a linked list to organize the data array.
[0057] In one implementation, the method further includes: determining whether the same hash value is calculated for different data; if so, determining that a hash collision exists; and organizing the data array based on a balanced binary tree data structure to handle hash collisions.
[0058] Balanced binary trees can use data structures such as AVL trees, red-black trees, skip lists, scapegoat trees, Treaps, and splay trees to organize the data array. Balanced binary trees are used to solve the problem of uncertain heights in binary search trees. If the height difference between the subtrees of a binary search tree is too large, the time complexity of binary search tree operations will increase to O(n). To avoid this situation and prepare for the worst-case scenario, balanced binary trees were developed to minimize the tree's height; its essence is still a binary search tree. In a balanced binary tree, the absolute value of the height difference between the left and right subtrees is less than or equal to 1, and the left and right subtrees are also balanced binary trees. For ease of understanding, each node in the tree is appended with a number representing the height difference between its left and right subtrees. This number is called the node's balance factor (BF), which is calculated as: Balance Factor = Height of the node's left subtree - Height of the node's right subtree. Therefore, the balance factor of all nodes in a balanced binary tree can only be -1, 0, or 1. The advantages of a balanced binary tree lie in its efficient search, insertion, and deletion operations, thanks to its strict balance conditions and self-adjustment mechanism after various operations.
[0059] Therefore, in the `data` array, each piece of data can contain four parts: a hash value, an address pointing to the left subtree, an address pointing to the right subtree, and the data itself (the value). Each part is stored using 4 bytes, for a total of 16 bytes, and they are stored sequentially in the `data` array. Thus, in one implementation, the `data` array is organized as follows: hash value, left subtree address, right subtree address, and value.
[0060] In one implementation, the data array is organized based on a balanced binary tree data structure, including: if the root node is empty, that is, the position in the data array is empty, then the data is directly placed in the corresponding position; if the root node is not empty, then the insertion position of the element to be inserted is found by traversing from the root node.
[0061] In one implementation, using a balanced binary tree to handle hash collisions may include the following steps: when inserting into the binary search tree, record the nodes searched during the insertion process; after insertion, search upwards from the inserted node, performing rotations and adjustments sequentially until the root node is reached.
[0062] As can be seen, the embodiments of this application solve the problem of low insertion and deletion efficiency in traditional chained hash tables by organizing the data array using a balanced binary tree. In existing hash tables, linked lists are used to store elements in the same hash bucket. If multiple keys have the same hash value, the linked list becomes too long, requiring traversal of the entire linked list for insertion and deletion. This causes the complexity of insertion and deletion in the hash table to degenerate from O(1) to O(n), resulting in a significant performance degradation. In the embodiments of this application, by replacing the linked list with a balanced binary tree, the time complexity of insertion and deletion is significantly reduced, with the average time complexity optimized from O(n) to close to O(logn). Even under severe hash collisions, high performance is maintained.
[0063] To address the shortcomings of existing hash table expansion methods that involve a single expansion, this application proposes a step-by-step expansion approach to solve the problems of time consumption and lag associated with single expansion.
[0064] In this embodiment, two hash table structures can be set up. The original hash table is called the old hash table, and the expanded hash table is called the new hash table. Both the old and new hash tables can adopt the aforementioned "index array + data array" structure to efficiently store data using this key-value separation method. The capacity of the expanded new hash table is generally larger than that of the old hash table; for example, the capacity of the new hash table is twice that of the old hash table. Furthermore, to address the problem of one-time expansion of existing hash tables, this embodiment sets an expansion flag. By reading the expansion flag, it is determined whether an expansion operation is currently underway, thus achieving step-by-step expansion. That is, the expansion operation is distributed across insertion, search, and deletion. Each time an insertion, search, or deletion occurs, only a portion of the elements are rehashed and inserted into the new table, avoiding a large amount of computation at any given moment, making the entire expansion process smoother.
[0065] Therefore, in one implementation, the above method further includes: reading the expansion flag to determine whether a hash table expansion operation is in progress; if so, determining the capacity of the index array and the data array of the new hash table based on the specified expansion size; and, in response to a data operation request, performing the following steps:
[0066] 1) For data insertion operation requests, insert the data into the new hash table;
[0067] 2) For data lookup operation requests, first search the old hash table; if not found, then search the new hash table.
[0068] 3) For data deletion requests, first search the old hash table. If found, delete the data. If not found, search the new hash table and delete the data.
[0069] Specifically, after each hash resizing, it is determined whether all elements in the old hash table have been migrated to the new hash table. If the resizing is complete, the resizing flag is reset. For example, a resizing flag value, Value-resizing stamp, is set. When Value-resizing stamp = 1, it indicates that a resizing operation is in progress. When no resizing is performed or the resizing is complete, Value-resizing stamp = 0.
[0070] As can be seen, the embodiments of this application can solve the performance degradation problem of traditional one-time expansion. Traditional one-time expansion requires rehashing all elements and inserting them into the new hash table within a short period of time. When the hash table capacity is large, the expansion process is time-consuming, and a single expansion can cause significant delays. The application may experience slow response or lag during the expansion process. Step-by-step expansion distributes the expansion operation across insertion, search, and deletion. Each time an insertion, search, or deletion is performed, only a portion of the elements are rehashed and inserted into the new table, avoiding a large amount of computation at any one moment, making the entire expansion process smoother.
[0071] See Figure 3 The diagram shows a schematic representation of a hash table structure in an example of a hash table processing method provided in this application.
[0072] In this example, a key-value pair is used for storage. The key stores the address of the data, and the value stores the actual data to be accessed. Two data structures, an index array and a data array, are used to store the index and the corresponding data, respectively. During resizing, both the old and new index arrays are maintained simultaneously to achieve smooth, step-by-step resizing. The data lookup, insertion, and deletion processes are as follows.
[0073] 1. Hash value calculation and index lookup
[0074] When you need to find, insert, or delete a piece of data, first calculate the hash value of the data, and then use the hash value to calculate the index position in the hash table, that is, a certain position in the index array.
[0075] 2. Obtain the data address from the index array
[0076] Based on the index position calculated in step 1, access the index array to retrieve the value stored at that position. The index array stores the position of the data to be accessed in the data array.
[0077] 3. Data storage and retrieval
[0078] Having found the storage address of the data in the `data` array in step 2, we can directly access the corresponding position in the `data` array to read or update the data. If there are multiple elements at that position, the search is performed in the tree according to the value, based on the characteristics of a balanced binary tree. If the value of the data to be queried is greater than the value of the current node, the search is performed in the right subtree; otherwise, it is performed in the left subtree. If both the value and the hash value are equal, the element has been found.
[0079] 4. Data insertion and hash collision handling
[0080] If different keys produce the same hash value, these data with the same hash value are logically organized into a balanced binary tree. If the root node is empty (i.e., the position in the data array is empty), the data is directly inserted into the corresponding position. If the root node is not empty, the insertion position of the element to be inserted is found by traversing from the root node. After insertion, the binary tree is balanced through rotation operations. These are all logical operations; physically, only the left and right subtree fields corresponding to the data need to be modified.
[0081] The rotation operation of a binary tree will be explained below.
[0082] The definition of a balanced binary tree is recursive, satisfying the following two conditions: it must be a binary search tree, and the left and right subtrees of any node must also be balanced binary trees (the difference in height between the left and right subtrees is less than 1). This definition ensures that the depth of a balanced binary tree will not be too large, which is beneficial for searching. Balance factor (BF): For node P, its balance factor = left subtree height - right subtree height, and balance factor = left subtree height - right subtree height. If |balance factor| > 1, then the tree rooted at node P is an unbalanced binary search tree.
[0083] Rotations on a binary tree involve two cases:
[0084] The first is a left rotation, which involves two steps: rotating the root node of the tree to be rotated (denoted as P) to the left (counterclockwise) relative to its right child node (denoted as S), so that the root node P becomes the left child node of the child node S; and making the left subtree of node S the right subtree of node P. The second is a right rotation, which involves two steps: rotating the root node of the tree to be rotated (denoted as P) to the right (clockwise) relative to its left child node (denoted as S), so that the root node P becomes the right child node of the child node S; and making the right subtree of node S the left subtree of node P.
[0085] Therefore, when inserting into a binary search tree, if an imbalance occurs, simply search upwards from the newly inserted node (denoted as P) to find the first node where |BF|>1 (denoted as F), and use that node as the root node for rotation adjustment.
[0086] 5. Deletion operation
[0087] When an element to be deleted is found, it is deleted only logically. Physically, the position of the element is set to empty, and all empty positions in the data array are organized into a linked list. When an element needs to be inserted, it is first inserted into an empty position in the array. If there is no empty position, it is inserted sequentially at the end of the linked list.
[0088] 6. Hash table resizing
[0089] This invention employs a step-by-step expansion method, simultaneously maintaining two index arrays (old and new) and setting an expansion flag to indicate whether the hash table is undergoing step-by-step expansion. When the capacity of the old index array reaches its limit, a new index array is created, with a capacity twice that of the old array. A fixed expansion size is set. Before each data insertion, the expansion flag is read to determine if a hash expansion is in progress. If it is, the specified size is expanded first, and then the data is inserted into the data array, storing the position in the new index array. If it is not undergoing hash expansion, the element position is stored in the old index array. When inserting data into the data array, it is first checked whether there is a free space in the array. If so, the data is placed in the free space in the array first; otherwise, the data is inserted sequentially at the end of the array.
[0090] When searching for data, first determine if the hash array is undergoing expansion. If it is, first search the old index array; if not found, then search the new index array. If the data is not being expanded, directly search the old index array. Find the element's position in the data array within the index array, and then check if the data array exists.
[0091] Before deleting data, it's checked whether the array is undergoing hash resizing. If so, the array is resized by the specified amount. Then, the data is searched for in the old index array. If found, a deletion flag is set. If not found in the old index array, the data is searched for in the new index array. If found, a deletion flag is set. Since the data is stored contiguously in the array, deleting data is logically done, but physically, the corresponding position is set to empty. All free positions in the array are managed using linked lists.
[0092] After each hash resizing, check whether all elements in the old table have been migrated to the new table. If the resizing is complete, reset the hash resizing flag.
[0093] In summary, the hash table processing method provided in this application uses a key-value separation approach for storage, employing an index array and a data array for storage. The index array stores the address of the data corresponding to the index in the data array. For example, 4 bytes are used to store the position of the data in the data array, rather than storing it in the actual memory location, thus saving storage space. Moreover, compared to linked lists, this application uses a balanced binary tree approach to store data with the same hash value, solving the problem of low search efficiency when hash collisions are severe, reducing the time complexity from O(n) to close to O(logn). In addition, compared to one-time expansion, this application uses step-by-step expansion to avoid rehashing a large number of elements into the new table in a short period of time, avoiding the huge amount of computation and long-term blocking caused by one-time expansion, making the entire expansion process smoother.
[0094] Corresponding to the hash table processing method described above, this application also provides a hash table processing apparatus.
[0095] See Figure 4 This illustration shows a schematic diagram of a hash table processing device according to an embodiment of this application. The hash table includes an index array and a data array, wherein the index array stores the address of the data in the data array, and the data array stores the data. The processing device includes:
[0096] The index position determination unit 401 is used to calculate the hash value of the target data and calculate the index position based on the hash value;
[0097] The index array access unit 402 is used to access the index array according to the index position to obtain the address of the target data corresponding to the index position in the data array;
[0098] The data array access unit 403 is used to access the data array according to the address of the target data in the data array, and to perform search, insertion or deletion operations on the target data.
[0099] One implementation also includes:
[0100] The hash collision processing unit 404 is used to determine whether the same hash value is calculated for different data. If so, it determines that a hash collision exists. The data array is organized based on a balanced binary tree data structure, and the hash collision is processed.
[0101] The hash collision processing unit 404 is specifically used for:
[0102] If the root node is empty, that is, the position in the data array is empty, then the data is directly placed in the corresponding position. If the root node is not empty, then traverse from the root node to find the insertion position of the element to be inserted.
[0103] The hash collision processing unit 404 is specifically used for:
[0104] When inserting data into a binary search tree, the nodes searched during the insertion process are recorded. After insertion, the search proceeds upwards from the inserted node, rotating and adjusting sequentially until the root node is reached.
[0105] In one implementation,
[0106] The hash table includes an old hash table and a new hash table with increased capacity, wherein the capacity of the new hash table is greater than the capacity of the old hash table;
[0107] The device also includes an expansion operation control unit 405, which is used to set an expansion identifier to indicate whether an expansion operation is in progress.
[0108] In one implementation, the expansion operation control unit 405 is further configured to:
[0109] Read the expansion identifier to determine whether a hash table expansion operation is in progress;
[0110] If so, determine the capacity of the index array and the data array of the new hash table based on the specified expansion size; and, in response to a data operation request, perform the following steps:
[0111] For data insertion operation requests, insert the data into the new hash table;
[0112] For data lookup requests, the old hash table is searched first; if the data is not found there, the new hash table is then searched.
[0113] For data deletion requests, first search the old hash table. If found, delete the data. If not found, then search the new hash table and delete the data.
[0114] In one implementation, the expansion operation control unit 405 is further configured to:
[0115] After each hash resizing, it is determined whether all elements in the old hash table have been migrated to the new hash table. If the resizing is complete, the resizing flag is reset.
[0116] Embodiments of this application also provide a storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above method embodiments when running.
[0117] Optionally, in this embodiment, the storage medium may be configured to store a computer program for performing the following steps:
[0118] Calculate the hash value of the target data, and calculate the index position based on the hash value;
[0119] Access the index array according to the index position to obtain the address of the target data in the data array corresponding to the index position;
[0120] Based on the address of the target data in the data array, access the data array and perform search, insertion, or deletion operations on the target data.
[0121] Optionally, in this embodiment, the storage medium may include, but is not limited to, various media capable of storing computer programs, such as USB flash drives, read-only memory (ROM), random access memory (RAM), portable hard drives, magnetic disks, or optical disks.
[0122] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0123] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0124] Optionally, in this embodiment, the processor can be configured to perform the following steps via a computer program:
[0125] Calculate the hash value of the target data, and calculate the index position based on the hash value;
[0126] Access the index array according to the index position to obtain the address of the target data in the data array corresponding to the index position;
[0127] Based on the address of the target data in the data array, access the data array and perform search, insertion, or deletion operations on the target data.
[0128] Optionally, specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated here.
[0129] The sequence numbers of the embodiments in this application are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments.
[0130] In the above embodiments of this application, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions of other embodiments.
[0131] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling, direct coupling, or communication connection may be through some interfaces; the indirect coupling or communication connection between units or modules may be electrical or other forms.
[0132] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0133] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0134] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0135] The above description is only a preferred embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.
Claims
1. A hash table processing method, characterized in that, The hash table includes an index array and a data array. The index array stores the address of the data in the data array, and the data is stored in the data array. The index array and data array store the key and value respectively. The processing method includes: Calculate the hash value of the target data, and calculate the index position based on the hash value; Access the index array according to the index position to obtain the address of the target data in the data array corresponding to the index position; Based on the address of the target data in the data array, access the data array and perform search, insertion, or deletion operations on the target data; The method further includes: determining whether the same hash value is calculated for different data; if so, determining that a hash collision exists; organizing the data array based on a balanced binary tree data structure and processing the hash collision; The data array is organized into hash value, left subtree address, right subtree address, and value.
2. The method according to claim 1, characterized in that, The data array is organized using a balanced binary tree data structure, including: If the root node is empty, and the position in the data array is determined to be empty, then the data is directly placed in the corresponding position. If the root node is not empty, then traverse from the root node to find the insertion position of the element to be inserted.
3. The method according to claim 1, characterized in that, The steps for handling the hash collision include: When inserting data into a binary search tree, the nodes searched during the insertion process are recorded. After insertion, the search proceeds upwards from the inserted node, rotating and adjusting sequentially until the root node is reached.
4. The method according to any one of claims 1-3, characterized in that, The hash table includes an old hash table and a new hash table with increased capacity, wherein the capacity of the new hash table is greater than the capacity of the old hash table; The method further includes setting an expansion flag to indicate whether an expansion operation is in progress.
5. The method according to claim 4, characterized in that, Also includes: Read the expansion identifier to determine whether a hash table expansion operation is in progress; If so, determine the capacity of the index array and the capacity of the data array of the new hash table based on the specified expansion size; In response to a data manipulation request, the following steps are performed: For data insertion operation requests, insert the data into the new hash table; For data lookup requests, the old hash table is searched first; if the data is not found there, the new hash table is then searched. For data deletion requests, first search the old hash table. If found, delete the data. If not found, then search the new hash table and delete the data.
6. The method according to claim 5, characterized in that, Also includes: After each hash resizing, it is determined whether all elements in the old hash table have been migrated to the new hash table. If the resizing is complete, the resizing flag is reset.
7. A hash table processing apparatus, characterized in that, The hash table includes an index array and a data array, wherein the index array stores the address of the data in the data array, and the data is stored in the data array. The index array and the data array store the key and value respectively. The processing device includes: An index position determination unit is used to calculate the hash value of the target data and calculate the index position based on the hash value; An index array access unit is used to access the index array according to the index position to obtain the address of the target data corresponding to the index position in the data array; The data array access unit is used to access the data array according to the address of the target data in the data array, and to perform search, insertion or deletion operations on the target data; A hash collision handling unit is used to determine whether the same hash value is calculated for different data. If so, a hash collision is determined. The data array is organized based on a balanced binary tree data structure, and the hash collision is handled. The data array is organized into hash value, left subtree address, right subtree address, and value.
8. A storage medium, characterized in that, The storage medium stores a computer program, wherein the computer program is configured to execute the method described in any one of claims 1 to 6 when it is run.
9. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the method as described in any one of claims 1 to 6.
Citation Information
Patent Citations
Method and apparatus for removing call ticket repeat
CN101442731A
Read and write operation method and apparatus
CN105117471A
Storage capacity expansion method, device and equipment and readable storage medium
CN111159296A