Array data storage method, electronic device and medium
By using a tree structure to store non-default value data in a large array, the problem of low memory utilization of large arrays is solved, achieving efficient memory utilization and data access and modification.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- SHANGHAI UNIVISTA IND SOFTWARE GRP CO LTD
- Filing Date
- 2026-02-05
- Publication Date
- 2026-04-24
AI Technical Summary
In existing technologies, large arrays have low memory utilization because the data in large arrays is not modified most of the time, making it ineffective to allocate memory for unmodified data.
A tree structure is used to store non-default value data in a large array, and a default value is recorded. Memory space is allocated only for non-default values, and the tree structure is used to achieve fast access and modification of data.
It improves memory utilization for storing large array data and enables fast and accurate data access and modification.
Smart Images

Figure CN121635819B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data storage technology, and more particularly to an array data storage method, electronic device, and medium. Background Technology
[0002] In computer programming languages, an array is a container used to store data of the same type. Typically, a program allocates a contiguous block of memory for this container, and users can access the data in the array using indices. In high-level programming languages such as SystemVerilog, in certain special scenarios, it is often necessary to use arrays larger than a preset threshold in size, where a large number of elements are assigned the same initial value; these arrays are called large arrays. Current technology requires allocating sufficiently large memory space for these large arrays to store the corresponding data. However, in real-world scenarios, for most of the program's runtime, and even throughout the entire program's execution, only a small portion of the data in a large array is modified. Allocating memory prematurely for the unmodified data is unnecessary, leading to low memory utilization of large arrays. Therefore, improving the memory utilization of large arrays has become a pressing technical problem. Summary of the Invention
[0003] The purpose of this invention is to provide an array data storage method, electronic device, and medium that improves the memory utilization of large array data storage.
[0004] According to a first aspect of the present invention, an array data storage method is provided, comprising:
[0005] Step S1: Initialize array A and record the corresponding default value D in array A. The total number of elements N in array A is greater than the preset first threshold. The expected total number of non-default value elements M in array A is less than the preset second threshold. The preset second threshold is less than the preset first threshold.
[0006] Step S2: Initialize an empty tree structure T;
[0007] Step S3: Obtain the array index and corresponding value of the non-default value to be added in array A;
[0008] Step S4: Use the array index of the non-default value to be added as the key and the corresponding value as the value, store it in a node to be added corresponding to the tree structure T, insert the node to be added into the tree structure T in index order, and update the tree structure T.
[0009] Step S5: If all elements in array A have been processed, the process ends; otherwise, return to step S3.
[0010] According to a second aspect of the present invention, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being configured to perform the method described in the first aspect of the present invention.
[0011] According to a third aspect of the present invention, a computer-readable storage medium is provided, storing computer-executable instructions for performing the method described in the first aspect of the present invention.
[0012] Compared with existing technologies, this invention has significant advantages and beneficial effects. Through the above technical solution, the array data storage method, electronic device, and medium provided by this invention achieve considerable technological advancement and practicality, and have broad industrial application value. It possesses at least the following beneficial effects:
[0013] This invention uses a tree structure to store large array data. Only non-default values need to be stored in the tree structure, and a default value needs to be recorded. This eliminates the need to allocate a large amount of memory space for large arrays, thus improving the memory utilization of large array data storage. Attached Figure Description
[0014] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0015] Figure 1 A flowchart illustrating an array data storage method provided in an embodiment of the present invention. Detailed Implementation
[0016] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0017] This invention provides an array data storage method, such as... Figure 1 As shown, it includes:
[0018] Step S1: Initialize array A and record the corresponding default value D in array A. The total number of elements N in array A is greater than the preset first threshold. The expected total number of non-default value elements M in array A is less than the preset second threshold. The preset second threshold is less than the preset first threshold.
[0019] It should be noted that an array whose total number of elements N is greater than a preset first threshold and whose expected total number of non-default elements M is less than a preset second threshold can be called a large array. The preset second threshold is less than the preset first threshold; preferably, the preset second threshold < the preset first threshold / 1000. The preset first threshold is set according to specific design requirements. Since the addition, deletion, or modification of array elements may be involved later, step S1 first uses the expected total number of non-default elements M in A for judgment. The size of the preset second threshold is positively correlated with the depth of the tree structure, and the specific preset second threshold can be determined according to the maximum depth that the tree structure can be set to.
[0020] Step S2: Initialize an empty tree structure T.
[0021] In one embodiment, the tree structure is a red-black tree, which is a self-balancing binary search tree that can automatically insert new nodes into the tree structure T according to their index and automatically update the tree structure.
[0022] Step S3: Obtain the array index and corresponding value of the non-default value to be added in array A.
[0023] Step S4: Use the array index of the non-default value to be added as the key and the corresponding value as the value, store it in a node to be added corresponding to the tree structure T, insert the node to be added into the tree structure T in index order, and update the tree structure.
[0024] The storage order in the tree structure can be configured such that smaller array indices correspond to earlier nodes in the tree, and larger array indices correspond to later nodes. Alternatively, larger array indices correspond to earlier nodes in the tree, and smaller array indices correspond to later nodes. The specific configuration should be chosen based on the application requirements.
[0025] Step S5: If all elements in array A have been processed, the process ends; otherwise, return to step S3.
[0026] It should be noted that the above storage method only requires allocating memory space for the non-default values in the large array and the default value of one record, without needing to allocate memory space for all elements, thus effectively improving memory utilization.
[0027] Based on storing data using a tree structure, fast and accurate access to any data in the array can be achieved. As one embodiment, step S5 further includes:
[0028] Step S6: Obtain the index A[i] of the array to be accessed, where the value of i ranges from 1 to M.
[0029] It is understandable that A[i] can be any index of array A. After the tree structure T is built, it is possible to access any array element in array A.
[0030] Step S7: Check if there is a node with key A[i] in the tree structure T. If it exists, return the value corresponding to the node with key A[i]. Otherwise, return the default value D.
[0031] It should be noted that if there is a node with key A[i] in the tree structure T, it means that the value corresponding to A[i] is not the default value, and the value corresponding to the node with key A[i] in the tree structure T needs to be obtained as the return value. If there is no node with key A[i] in the tree structure T, it means that the value corresponding to A[i] is the default value, and the default value D can be returned directly.
[0032] Based on storing data using a tree structure, it is possible to quickly and accurately modify any data in the array. As one embodiment, step S5 is followed by:
[0033] Step C6: Obtain the array index A[j] to be modified and the corresponding value B[j] to be modified, where the value of j ranges from 1 to M.
[0034] Step C7: Check if there is a node with key A[j] in the tree structure T. If it exists, proceed to step C8; otherwise, proceed to step C11.
[0035] It should be noted that if there is no node with key A[j] in the tree structure T, the original value corresponding to A[j] is the default value. In this case, the corresponding modified value B[j] is definitely not the default value. You can directly execute step C11 to add the corresponding tree node in the tree structure T.
[0036] Step C8: Determine whether B[j] is equal to the default value D. If it is equal, proceed to step C9; otherwise, proceed to step C10.
[0037] It should be noted that if a node with key A[j] exists in the tree structure T, the original value corresponding to A[j] is a non-default value. One possibility is to update the value of A[j] to the default value, in which case the corresponding node needs to be deleted from the tree structure T, as implemented in step C9. Another possibility is to update the value of A[j] to another non-default value, in which case the corresponding node in the tree structure T needs to be retained, and the value in the corresponding node only needs to be updated, as implemented in step C10.
[0038] Step C9: Delete the node with key A[j] from the tree structure T, update the tree structure, and end the process.
[0039] Step C10: Update the value of the node with key A[j] in the tree structure T to B[j], and end the process.
[0040] Step C11: Store A[j] as the key and B[j] as the value in a node to be added corresponding to the tree structure T. Insert the node to be added into the tree structure T in the order of its index, update the tree structure T, and end the process.
[0041] It should be noted that the present invention optimizes array data storage by trading time for space. If the depth of the tree structure T is too large, the time overhead will also increase, potentially exceeding the maximum time tolerance limit. In this case, the tree-based storage method is no longer applicable, and it is necessary to switch back to array storage. As one embodiment, the method further includes:
[0042] Step S01: Obtain the number of nodes X in the tree structure T in real time. If X is equal to the preset third threshold, then proceed to step S02: The preset third threshold is greater than or equal to the preset second threshold.
[0043] The value of the preset third threshold is positively correlated with the depth of the tree structure, and can be determined based on the maximum depth that the tree structure can be set to. Preferably, the preset third threshold can be set to be greater than the preset second threshold, thereby providing some room for modification of the array data.
[0044] Step S02: Convert the data of the tree structure T into an array and store it. In the array, the value of the index stored in the tree structure T is set to the value stored in the corresponding node, and the value of the index not stored in the tree structure T is set to the default value D.
[0045] Specifically, the data in the tree structure T is converted into an array form, which can be done by first setting all element values in the ordinary array to the default value D, and then updating the value at the index stored in the tree structure T to the value stored in the corresponding node. The conversion method is fast and accurate.
[0046] Based on storing data using a tree structure, batch copying of array elements can also be implemented. As one embodiment, step S5 is followed by:
[0047] Step D6: Obtain the index range A[l:s] of the array to be copied, where s is the minimum boundary value corresponding to the index range of the array to be accessed, and l is the maximum boundary value corresponding to the index range of the array to be accessed.
[0048] It is understandable that l>s and l≠s.
[0049] Step D7: If ls is greater than the preset first threshold, then proceed to step D8; otherwise, proceed to step D9.
[0050] It should be noted that the expected total number M of non-default value elements in array A is less than the preset second threshold. As a subset of array A, A[l:s] must also satisfy the condition that the expected total number of non-default value elements is less than the preset second threshold. Therefore, it is not necessary to compare it with the preset second threshold. It is only necessary to determine whether ls is greater than the preset first threshold. If it is greater, it means that A[l:s] is also a large array. Therefore, the corresponding copy result can also be stored based on the tree structure.
[0051] Step D8: Construct the tree structure R corresponding to A[l:s] and record the corresponding default value D. Locate the subtree corresponding to A[l:s] from the tree structure T and assign it to the tree structure R. End the process.
[0052] It should be noted that since the tree structure T is stored in index order, the subtree corresponding to A[l:s] can be located directly from the tree structure T.
[0053] Step D9: Construct array U corresponding to A[l:s], set the initial value of all elements in array U to the default value D, locate the subtree corresponding to A[l:s] from the tree structure T, and write the value of each element in the subtree corresponding to A[l:s] into A[l:s] of array U.
[0054] Accurate and fast batch copying can be achieved through steps D6-D9.
[0055] The embodiments of the present invention use a tree structure to store large array data. Only non-default values need to be stored in the tree structure and a default value needs to be recorded. There is no need to allocate a large amount of memory space for large arrays, which improves the memory utilization of large array data storage.
[0056] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains. The terminology used herein in the description of this invention is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. The term "and / or" as used herein includes any and all combinations of one or more of the associated listed items.
[0057] It should be noted that some exemplary embodiments are described as processes or methods depicted as flowcharts. Although the flowcharts describe the steps as sequential processes, many of these steps can be performed in parallel, concurrently, or simultaneously. Furthermore, the order of the steps can be rearranged. A process can be terminated when its operation is complete, but it may also have additional steps not included in the figures. A process can correspond to a method, function, procedure, subroutine, subroutine, etc.
[0058] This invention also provides an electronic device, including: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being configured to perform the method described in this invention.
[0059] This invention also provides a computer-readable storage medium storing computer-executable instructions for performing the methods described in this invention.
[0060] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention in any way. Although the present invention has been disclosed above with reference to preferred embodiments, it is not intended to limit the present invention. Any person skilled in the art can make some modifications or alterations to the above-disclosed technical content to create equivalent embodiments without departing from the scope of the present invention. Any simple modifications, equivalent changes, and alterations made to the above embodiments based on the technical essence of the present invention without departing from the scope of the present invention shall still fall within the scope of the present invention.
Claims
1. A method for storing array data, characterized in that, include: Step S1: Initialize array A and record the corresponding default value D in array A. The total number of elements N in array A is greater than the preset first threshold. The expected total number of non-default value elements M in array A is less than the preset second threshold. The preset second threshold is less than the preset first threshold. Step S2: Initialize an empty tree structure T; Step S3: Obtain the array index and corresponding value of the non-default value to be added in array A; Step S4: Use the array index of the non-default value to be added as the key and the corresponding value as the value, store it in a node to be added corresponding to the tree structure T, insert the node to be added into the tree structure T in index order, and update the tree structure T. Step S5: If all elements in array A have been processed, the process ends; otherwise, return to step S3. Following step S5, the following is also included: Step C6: Obtain the array index A[j] to be modified and the corresponding value B[j] to be modified, where the value of j ranges from 1 to M; Step C7: Check if there is a node with key A[j] in the tree structure T. If it exists, proceed to step C8; otherwise, proceed to step C11. Step C8: Determine whether B[j] is equal to the default value D. If it is equal, proceed to step C9; otherwise, proceed to step C10. Step C9: Delete the node with key A[j] from the tree structure T, update the tree structure, and end the process; Step C10: Update the value of the node with key A[j] in the tree structure T to B[j], and end the process; Step C11: Store A[j] as the key and B[j] as the value in a node to be added corresponding to the tree structure T. Insert the node to be added into the tree structure T in the order of its index, update the tree structure T, and end the process.
2. The method according to claim 1, characterized in that, Following step S5, the following is also included: Step S6: Obtain the array index A[i] to be accessed, where the value of i ranges from 1 to M; Step S7: Check if there is a node with key A[i] in the tree structure T. If it exists, return the value corresponding to the node with key A[i]. Otherwise, return the default value D.
3. The method according to claim 1, characterized in that, The method further includes: Step S01: Obtain the number of nodes X in the tree structure T in real time. If X is equal to the preset third threshold, then execute step S02. The preset third threshold is greater than or equal to the preset second threshold. Step S02: Convert the data of the tree structure T into an array and store it. In the array, the value of the index stored in the tree structure T is set to the value stored in the corresponding node, and the value of the index not stored in the tree structure T is set to the default value D.
4. The method according to claim 1, characterized in that, Following step S5, the following is also included: Step D6: Obtain the index range A[l:s] of the array to be copied, where s is the minimum boundary value corresponding to the index range of the array to be accessed, and l is the maximum boundary value corresponding to the index range of the array to be accessed. Step D7: If l minus s is greater than the preset first threshold, then proceed to step D8; otherwise, proceed to step D9. Step D8: Construct the tree structure R corresponding to A[l:s] and record the corresponding default value D. Locate the subtree corresponding to A[l:s] from the tree structure T and assign it to the tree structure R. End the process. Step D9: Construct array U corresponding to A[l:s], set the initial value of all elements in array U to the default value D, locate the subtree corresponding to A[l:s] from the tree structure T, and write the value of each element in the subtree corresponding to A[l:s] into A[l:s] of array U.
5. The method according to claim 1, characterized in that, The tree structure is a red-black tree.
6. The method according to claim 4, characterized in that, The magnitudes of the preset second threshold and the preset third threshold are positively correlated with the depth of the tree structure.
7. An electronic device, characterized in that, include: At least one processor; And, a memory communicatively connected to the at least one processor; The memory stores instructions that are executed by the at least one processor, the instructions being configured to perform the method of any one of claims 1-6.
8. A computer-readable storage medium, characterized in that, The device stores computer-executable instructions for performing the method of any one of claims 1-6.
Citation Information
Patent Citations
Data insertion method and device based on red-black tree and electronic equipment
CN112559532A