Array data storage method, electronic equipment and medium

By using a tree structure to store non-default values ​​and record default values ​​in large arrays, the problem of low memory utilization of large arrays is solved, achieving efficient memory utilization and fast data operations.

CN121635819AActive Publication Date: 2026-03-10SHANGHAI UNIVISTA IND SOFTWARE GRP CO LTD +1
View PDF 8 Cites 0 Cited by

Patent Information

Application Number
CN202610164153.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-02-05
Publication Date
2026-03-10
Estimated Expiration
2046-02-05

AI Technical Summary

Technical Problem

In existing technologies, large arrays have low memory utilization because the data in large arrays is not modified for most of the program's runtime, resulting in wasted memory space allocated to unmodified data.

Method used

Large array data is stored using a tree structure, storing only non-default values ​​and recording default values. The tree structure is used to automatically update node positions, reducing the memory requirements of large arrays.

Benefits of technology

It improves memory utilization for storing large array data, enables fast and accurate data access and modification, and converts data to array storage when necessary to control time overhead.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121635819A_ABST
    Figure CN121635819A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of data storage, in particular to an array data storage method, electronic equipment and a medium, the method comprises the following steps: S1, initializing an array A and recording a corresponding default value D in the array A, the total number N of elements in the array A being greater than a preset first threshold, and the expected total number M of elements of non-default values in the array A being less than a preset second threshold; step S2, initializing an empty tree structure T; s3, acquiring an array subscript of a non-default value to be newly added in the array A and a corresponding numerical value; s4, storing an array subscript of a non-default value to be newly added into a node to be newly added corresponding to the tree structure T by taking the subscript of the array of the non-default value to be newly added as a key and taking a corresponding numerical value as a value, inserting the node to be newly added into the tree structure T according to a subscript sequence, and updating the tree structure T; and S5, if all the elements in the array A are processed, ending the process, otherwise, returning to the step S3. And the memory utilization rate of large array data storage is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of data storage, in particular to an array data storage method, an electronic device and a medium. BACKGROUND

[0002] In computer programming languages, an array is a container for storing data of the same type. Usually, a program allocates a continuous memory block for the container, and the user can access the data in the array through the subscript. For high-level programming languages such as SystemVerilog, in some special scenarios, an array with a size greater than a preset threshold and a large number of elements being assigned the same initial value is often needed, which is called a large array. In the prior art, a large memory space is allocated for the large array to store the corresponding data. However, in actual scenarios, only a small part of the data in the large array is changed during most of the program running time, or even during the entire program running time. For the part of data that is not changed, it is unnecessary to allocate memory for it too early, resulting in low memory utilization of the large array. Therefore, how to improve the memory utilization of the large array becomes a technical problem to be solved. SUMMARY

[0003] The present application aims to provide an array data storage method, an electronic device and a medium, which improve the memory utilization of large array data storage.

[0004] According to a first aspect of the present application, an array data storage method is provided, comprising: Step S1, initializing an array A and recording the corresponding default value D in the array A, the total number N of elements in the array A being greater than a preset first threshold, and the expected total number M of non-default value elements in the array A being less than a preset second threshold, the preset second threshold being less than the preset first threshold; Step S2, initializing an empty tree structure T; Step S3, obtaining the array subscript of the non-default value to be added in the array A and the corresponding numerical value; Step S4, storing the array subscript of the non-default value to be added as a key and the corresponding numerical value as a value in a to-be-added node corresponding to the tree structure T, inserting the to-be-added node into the tree structure T in subscript order, and updating the tree structure T; Step S5, if all elements in the array A are processed, ending the process, otherwise returning to step S3.

[0005] 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.

[0006] 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.

[0007] 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: 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

[0008] 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.

[0009] Figure 1 A flowchart illustrating an array data storage method provided in an embodiment of the present invention. Detailed Implementation

[0010] 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.

[0011] This invention provides an array data storage method, such as... Figure 1 As shown, it includes: 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.

[0012] 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.

[0013] Step S2: Initialize an empty tree structure T.

[0014] 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.

[0015] Step S3: Obtain the array index and corresponding value of the non-default value to be added in array A.

[0016] 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.

[0017] 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.

[0018] Step S5: If all elements in array A have been processed, the process ends; otherwise, return to step S3.

[0019] 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.

[0020] 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: Step S6: Obtain the index A[i] of the array to be accessed, where the value of i ranges from 1 to M.

[0021] 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.

[0022] 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.

[0023] 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.

[0024] 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: 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.

[0025] 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.

[0026] 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.

[0027] 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.

[0028] 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.

[0029] Step C9: Delete the node with key A[j] from the tree structure T, update the tree structure, and end the process.

[0030] Step C10: Update the value of the node with key A[j] in the tree structure T to B[j], and end the process.

[0031] 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.

[0032] 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: 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.

[0033] 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.

[0034] 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.

[0035] 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.

[0036] 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: 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.

[0037] It is understandable that l>s and l≠s.

[0038] Step D7: If ls is greater than the preset first threshold, then proceed to step D8; otherwise, proceed to step D9. 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.

[0039] 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.

[0040] 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.

[0041] 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.

[0042] Accurate and fast batch copying can be achieved through steps D6-D9.

[0043] 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.

[0044] 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.

[0045] 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.

[0046] 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.

[0047] This invention also provides a computer-readable storage medium storing computer-executable instructions for performing the methods described in this invention.

[0048] 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. An array data storage method, characterized by, The method comprises the following steps: S1, initializing an array A and recording a corresponding default value D in the array A, the total number N of elements in the array A is greater than a preset first threshold value, and the expected total number M of non-default value elements in the array A is less than a preset second threshold value, the preset second threshold value is less than the preset first threshold value; S2, initializing an empty tree structure T; S3, obtaining an array subscript of a non-default value to be added in the array A and a corresponding value; S4, storing the array subscript of the non-default value to be added as a key and the corresponding value as a value in a to-be-added node corresponding to the tree structure T, inserting the to-be-added node into the tree structure T in subscript order, and updating the tree structure T; S5, if all elements in the array A are processed, ending the process, otherwise returning to step S3.

2. The method of claim 1, wherein the step S5 further comprises: S6, obtaining a to-be-accessed array subscript A[i], i ranges from 1 to M; S7, searching the tree structure T for a node with a key of A[i], if the node exists, returning a value corresponding to the node with the key of A[i], otherwise, returning a default value D.

3. The method of claim 1, wherein the step S5 further comprises: C6, obtaining a to-be-modified array subscript A[j] and a corresponding modified value B[j], j ranges from 1 to M; C7, searching the tree structure T for a node with a key of A[j], if the node exists, executing step C8, otherwise, executing step C11; C8, determining whether B[j] is equal to the default value D, if yes, executing step C9, otherwise, executing step C10; C9, deleting the node with the key of A[j] from the tree structure T, updating the tree structure, and ending the process; C10, updating the value of the node with the key of A[j] in the tree structure T to B[j], and ending the process; C11, storing A[j] as a key and B[j] as a value in a to-be-added node corresponding to the tree structure T, inserting the to-be-added node into the tree structure T in subscript order, updating the tree structure T, and ending the process.

4. The method of claim 1, wherein the method further comprises: S01, obtaining a node number X in the tree structure T in real time, if X is equal to a preset third threshold value, executing step S02, the preset third threshold value is greater than or equal to the preset second threshold value; S02, converting the data of the tree structure T into an array form for storage, wherein in the array, the value of a position of a subscript stored in the tree structure T is set as a value stored in a corresponding node, and the value of a position of a subscript not stored in the tree structure T is set as a default value D.

5. The method of claim 1, wherein the step S5 further comprises: D6, obtaining a to-be-copied array subscript range A[l:s], s is a minimum boundary value corresponding to a to-be-accessed array subscript range, and l is a maximum boundary value corresponding to the to-be-accessed array subscript range. ​ ​ ​ ​ Step D7, if the l-s is greater than a preset first threshold, then execute step D8, otherwise, execute step D9; Step D8, construct a tree structure R corresponding to A[l:s], record a corresponding default value D, locate a sub-tree corresponding to A[l:s] from the tree structure T and assign it to the tree structure R, end the flow; Step D9, construct an array U corresponding to A[l:s], set the initial value of all elements in the array U as the default value D, locate the sub-tree corresponding to A[l:s] from the tree structure T, and write the value of each element in the sub-tree corresponding to A[l:s] into A[l:s] of the array U.

6. The method of claim 1, wherein, the tree structure is a red-black tree.

7. The method of claim 4, wherein, the preset second threshold and the preset third threshold are positively correlated with the depth of the tree structure.

8. An electronic device, comprising: comprise: at least one processor; and a memory connected in communication with the at least one processor; wherein the memory stores instructions executed by the at least one processor, the instructions being configured to execute the method of any one of the preceding claims 1-7.

9. A computer-readable storage medium, characterized in that, computer executable instructions for executing the method of any one of the preceding claims 1-7.

Citation Information

Patent Citations

  • Memory allocation method and system

    CN107844372A

  • Method and device for batch inserting key-value pairs into B + tree

    CN109241058A

  • Data insertion method and device based on red-black tree and electronic equipment

    CN112559532A

  • Cold and hot data self-adaptive indexing method and system, storage medium and server

    CN113961513A

  • Design hierarchy tree sub-node information storage method, electronic equipment and medium

    CN117009090A