Method, device, equipment, storage medium and product for searching character strings in memory
By using a combination of binary search algorithm and hash tables in memory to find strings, the problem of low string search efficiency in the existing technology is solved, and more efficient functional execution is achieved.
Patent Information
- Application Number
- CN202210303785.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-24
- Publication Date
- 2025-09-02
- Estimated Expiration
- 2042-03-24
AI Technical Summary
When searching for strings in memory, the existing technology adopts string traversal search method to lead to high time complexity and low efficiency, which affects the efficiency of function execution.
The binary search algorithm and hash table are used to find memory resident strings, and select appropriate algorithms based on string search conditions to reduce the algorithm time complexity and improve search efficiency.
By combining binary search and hash table, the time complexity of string search is reduced, the search efficiency is improved, and the efficiency of function execution is improved.
Smart Images

Figure CN114625933B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to computer technology, and in particular to a method, apparatus, device, storage medium, and product for searching a character string in memory. Background Art
[0002] The Python programming language is widely used due to its advanced data structures and simple, efficient object-oriented programming. Micropython is a complete software implementation of the Python 3 programming language. Written in C and optimized for running on microcontrollers, it provides users with an interactive interpreter (REPL) to immediately execute supported commands.
[0003] When using Micropython to execute supported commands to implement a device function, it is necessary to store in the device memory the mapping between the function corresponding to the function and the underlying C language function that implements the function. The function that implements a function can be represented by a string. To find out whether the mapping relationship for a function exists, you can check whether the string for the function exists.
[0004] Currently, when searching for the string corresponding to a function in memory, a string traversal search is generally used. This results in high string search complexity, which in turn leads to low search efficiency and low efficiency in executing commands to implement the function. Summary of the Invention
[0005] The present application provides a method, apparatus, device, storage medium and product for searching strings in memory, which are used to solve the problem that when a string traversal search method is used to search for the existence of a string, the time complexity of searching the string is high, which leads to low search efficiency and low efficiency of executing commands to implement the function.
[0006] In a first aspect, the present application provides a method for searching a string in memory, comprising:
[0007] Obtaining a target memory-resident string to be searched, wherein the target memory-resident string is consistent with a function name corresponding to a function that implements a target function of the device; and executing a command corresponding to the target function is written in Python programming language;
[0008] Determine the string search conditions that are currently met;
[0009] If it is determined that the currently satisfied string search condition is a linear search condition, a binary search algorithm is used to search whether the target memory resident string exists in the stored resident strings in the memory;
[0010] If it is determined that the currently satisfied string search condition is a combined search condition, then the target memory resident string is searched for in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table.
[0011] In a second aspect, the present application provides a device for searching a character string in a memory, comprising:
[0012] An acquisition module is used to obtain a target memory-resident string to be searched, wherein the target memory-resident string is consistent with the function name of the function corresponding to the target function of the device; and a command for executing the function corresponding to the target function is written in Python programming language;
[0013] A determination module is used to determine the string search conditions that are currently met;
[0014] The search module is used to use a binary search algorithm to search whether the target memory-resident string exists in the resident strings stored in the memory if it is determined that the currently satisfied string search condition is a linear search condition; if it is determined that the currently satisfied string search condition is a combined search condition, search whether the target memory-resident string exists in the resident strings stored in the memory based on a hash table corresponding to the stored resident strings and a data structure corresponding to the hash value index in the hash table.
[0015] In a third aspect, the present application provides an electronic device, comprising: a processor, and a memory communicatively connected to the processor;
[0016] The memory stores computer-executable instructions;
[0017] The processor executes the computer-executable instructions stored in the memory to implement the method for searching a character string in memory described in any one of the above aspects.
[0018] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the string search method described in any one of the above aspects.
[0019] In a fifth aspect, the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the string search method described in any one of the above aspects.
[0020] The in-memory string search method, apparatus, device, storage medium and product provided by the present application obtain a target memory-resident string to be searched, wherein the target memory-resident string is consistent with the function name of the function corresponding to the target function of the device; the command for executing the function corresponding to the target function is written in the Python programming language; the currently satisfied string search condition is determined; if it is determined that the currently satisfied string search condition is a linear search condition, a binary search algorithm is used to search whether the target memory-resident string exists in the stored resident strings in the memory; if it is determined that the currently satisfied string search condition is a combined search condition, the target memory-resident string is searched whether it exists in the stored resident strings in the memory based on the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table. After determining whether to use a binary search algorithm or to search for the target memory-resident string based on the hash table corresponding to the stored resident string and the data structure corresponding to the hash value index in the hash table based on the currently satisfied string search conditions, the use of a binary search algorithm or the use of a hash table corresponding to the stored resident string and the data structure corresponding to the hash value index in the hash table based on the target memory-resident string can reduce the algorithm time complexity and improve the search efficiency. Further, the efficiency of executing commands to achieve target functions can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0022] Figure 1 Schematic diagram of application scenarios provided by embodiments of the present application;
[0023] Figure 2 Flowchart of the method for searching a character string in memory provided in Example 1 of the present application;
[0024] Figure 3 Flowchart of the method for searching a character string in memory provided in Example 2 of this application;
[0025] Figure 4 Flowchart of the method for searching a character string in memory provided in Example 3 of the present application;
[0026] Figure 5 Flowchart of a method for searching a character string in memory provided in Example 4 of the present application;
[0027] Figure 6 A flowchart of a method for searching a character string in memory provided in Example 6 of the present application;
[0028] Figure 7 Flowchart of a method for searching a character string in memory provided in Example 7 of the present application;
[0029] Figure 8 A schematic diagram of the structure of a device for searching a character string in a memory provided in Example 8 of the present application;
[0030] Figure 9 This is a schematic diagram of the structure of the electronic device provided in Example 9 of the present application.
[0031] The above drawings illustrate specific embodiments of the present application, which will be described in more detail below. These drawings and the textual description are not intended to limit the scope of the present application in any way, but rather to illustrate the concepts of the present application to those skilled in the art by reference to specific embodiments. DETAILED DESCRIPTION
[0032] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.
[0033] First, let’s explain the terms involved in this application:
[0034] MicroPython is Python for embedded systems. Python is a scripting language and an object-oriented, interpreted computer programming language. It features concise syntax, ease of use, powerful functionality, and ease of expansion. However, Python cannot perform some low-level operations. MicroPython encapsulates the C language, allowing users to call C using Python syntax. This allows developers to control the underlying hardware by writing Python scripts, providing developers with a convenient embedded hardware operation library and significantly lowering the barrier to entry for embedded development.
[0035] QSTR, short for uniQue STRing, is a method for persisting strings in memory. A string may appear multiple times in a command. When MicroPython processes a command, it only stores one copy of the string body in memory for each occurrence. When the string is used, it searches for the corresponding string in memory. This process is called QSTR persistence.
[0036] In the prior art, the stored resident strings are sorted in storage order to form a linear array. Since the strings in the linear array formed by sorting in storage order are irregular, the target memory resident string can only be searched in the stored resident strings by linear traversal search. The algorithm time complexity of the linear traversal search is O(n), which is high, resulting in low search efficiency. The inventors discovered during their research that by sorting the stored resident strings in a preset arrangement order, a binary search algorithm can be used to search for the target memory resident strings. The algorithm time complexity of the binary search algorithm is greater than O(logN) and less than O(n), so the algorithm time complexity can be reduced. Furthermore, due to the combined search method of the hash table and the red-black tree, the algorithm time complexity is at most O(logN), and the algorithm time complexity decreases exponentially with the increase of the hash table, and the complexity is O(logN) / logK, where N is the number of stored resident strings and K is the size of the hash table. The stored resident strings are stored in a combined manner of the hash table and the red-black tree, and the combined search method of the hash table and the red-black tree is used to search for the target memory resident strings. This can further reduce the algorithm time complexity, improve the search efficiency, and improve the efficiency of executing commands to achieve target functions. Therefore, this application determines the currently satisfied string search conditions. If the linear search condition is met, a binary search algorithm is used to determine whether the target memory-resident string exists in the stored resident strings in memory. If the combined search condition is met, the target memory-resident string is searched for in the stored resident strings in memory using the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table. Both of these can effectively reduce the algorithm's time complexity, improve search efficiency, and thus improve the efficiency of executing commands to achieve the target function.
[0037] The data transmission method provided in this application is intended to solve the above technical problems in the prior art.
[0038] Figure 1 The application scenario diagram provided in the embodiment of this application is as follows: Figure 1As shown, the application scenario of the present application includes: a cloud server cluster 1, an electronic device 2, wherein the electronic device 2 can be an IoT device with an IoT development board, and the cloud IoT platform is in the cloud server cluster 1. The IoT development board in the electronic device 2 can provide a variety of rich hardware operation interfaces, and MicroPython can be applied to the IoT development board to implement the target functions corresponding to the commands written in Python by burning commands written in Python into the IoT development board, so that various function-rich applications can be easily and conveniently built, and IoT applications can be easily updated across platforms. For example, the connect command can be used to enable the electronic device 2 to connect to the cloud IoT platform in the cloud server cluster 1; the register command can also be used to enable the electronic device 2 to register with the cloud IoT platform in the cloud 1. Other intelligent operation commands can also be used to implement various artificial intelligence capabilities in the interaction between the electronic device 2 and the cloud IoT platform.
[0039] Specifically, the user can use the Python programming language to write commands to execute the function corresponding to the target function through the operation interface, such as Figure 1 As shown, if the command is: ABC.connect('abc'), after finding the target memory resident string in the memory using the in-memory string search method provided by the embodiment of the present application, the underlying coding string with which the target memory resident string has a mapping relationship is determined; the target function is executed according to the function code corresponding to the underlying coding string. Figure 1 As shown, the function of connecting to the cloud IoT platform in server cluster 1 is executed. After executing the function, a connection request is sent to the cloud IoT platform in cloud server cluster 1. The cloud IoT platform establishes a connection with the electronic device based on the connection request.
[0040] It is understandable that the in-memory string search method provided in the embodiment of the present application can also be applied in other application scenarios, and the present application does not limit this.
[0041] The following specific embodiments describe in detail the technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0042] Example 1
[0043] Figure 2This is a flow chart of a method for searching for strings in memory provided in the first embodiment of the present application. This embodiment of the present application addresses the problem that when a string traversal search method is used to search for the existence of a string, the time complexity of searching for the string is high, which leads to low search efficiency and low efficiency of executing commands to implement the function. A method for searching for strings in memory is provided. The method in this embodiment is applied to a device for searching for strings in memory, which can be various forms of microcontroller hardware. Such as a single-chip microcomputer, an electronic circuit board, an embedded electronic board, an Internet of Things chip, a development board, etc. Various forms of microcontroller hardware can be located in electronic devices. The electronic device can be a device with microcontroller hardware, such as a laptop computer, a desktop computer, a workbench, a server, a blade server, a cloud server, a mainframe computer, an Internet of Things device, etc.
[0044] like Figure 2 As shown, the specific steps of this method are as follows:
[0045] Step S101: Obtain the target memory-resident character string to be searched.
[0046] The target memory resident string is consistent with the function name of the function corresponding to the target function of the device; the command for executing the function corresponding to the target function is written in Python programming language.
[0047] Specifically, the code to be executed is obtained, the function name of the function corresponding to the target function of the device implemented in the code to be executed is determined, and the function name of the function corresponding to the target function is determined as the target memory resident string to be searched.
[0048] The code to be executed is the command code for executing the function corresponding to the target function.
[0049] In the embodiment of the present application, there is no specific limitation on the method of obtaining the code to be run. For example, a user can use REPL to enable the electronic device to obtain the code to be run, and the electronic device can also obtain the code to be run by reading an imported file.
[0050] For example, the code to be executed is obtained as ABC.connect('abc'), where "ABC" is the module identifier of the target function. "abc" is an input parameter of a function named connect. The function name in the code to be executed is determined to be connect, and connect is determined as the target memory-resident string to be searched.
[0051] Step S102: Determine the currently satisfied string search condition.
[0052] In the embodiment of the present application, the string search conditions include linear search conditions and combined search conditions. Among them, the linear search condition is a condition for searching using a linear search method. The combined search condition is a condition for searching by combining a hash table with the data structure corresponding to the hash value index in the hash table.
[0053] The linear search condition and the combined search condition can be pre-set. The embodiments of the present application do not specifically limit the pre-set linear search condition and the combined search condition. For example, the pre-set linear search condition and the combined search condition can be pre-set based on the number of target memory-resident strings to be searched, or can be pre-set based on the memory size of the electronic device. For example, the string search condition can be pre-set based on the number of target memory-resident strings to be searched. The linear search condition can be: the number of target memory-resident strings is less than 2000, and the combined search condition can be: the number of target memory-resident strings is greater than or equal to 2000.
[0054] The embodiment of the present application does not specifically limit the method for determining the currently satisfied string search condition. For example, if the pre-set linear search condition is that the memory size is less than 256 kilobytes, and the combined search condition is that the memory size is greater than or equal to 256 kilobytes, the currently satisfied string search condition can be determined by determining whether the memory size of the electronic device is less than 256 kilobytes. If it is determined that the memory size of the electronic device is less than 256 kilobytes, the currently satisfied string search condition is determined to be a linear search condition. If it is determined that the memory size of the electronic device is not less than 256 kilobytes, the currently satisfied string search condition is determined to be a combined search condition.
[0055] In the embodiment of the present application, if it is determined that the currently satisfied string search condition is a linear search condition, step S103 is executed; if it is determined that the currently satisfied string search condition is a combined search condition, step S104 is executed.
[0056] Step S103: If it is determined that the currently satisfied string search condition is a linear search condition, a binary search algorithm is used to search whether the target memory-resident string exists in the stored resident strings in the memory.
[0057] In the embodiment of the present application, if it is determined that the currently satisfied string search condition is a linear search condition, the stored resident strings are stored in the memory in a preset order.
[0058] Specifically, when using a binary search algorithm to search whether a target memory-resident string exists in the stored resident strings in the memory, the stored resident strings stored in the memory in a preset order are divided into two search intervals; the target memory-resident string is searched in the two search intervals respectively, and the search interval in which the target memory-resident string exists is determined to be the search interval to be searched. The stored resident strings in the search interval are further divided into two search intervals, and the target memory-resident string is searched in the two search intervals respectively, until only the stored resident strings identical to the target memory-resident string exist in the search interval, or the search interval is empty. If only the stored resident strings identical to the target memory-resident string exist in the search interval, it is determined that the target memory-resident string exists in the stored resident strings in the memory; if the search interval is empty, it is determined that the target memory-resident string does not exist in the stored resident strings in the memory.
[0059] Step S104: If it is determined that the currently satisfied string search condition is a combined search condition, then search whether the target memory resident string exists in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table.
[0060] In an embodiment of the present application, if it is determined that the currently satisfied string search condition is a combined search condition, the stored resident string is stored in the memory according to the data structure corresponding to the hash value index.
[0061] The data structure corresponding to the hash value index can be: a one-dimensional array, a red-black tree, a linked list, a binary tree, etc., which is not specifically limited in the present embodiment.
[0062] Exemplarily, a method for searching for a target memory-resident string based on a hash table corresponding to a stored resident string and a data structure corresponding to a hash value index in the hash table can be: calculating a hash value of the target memory-resident string, and determining a hash value index of the target memory-resident string in the hash table based on the hash value of the target memory-resident string; if the data structure corresponding to the hash value index is a one-dimensional array, a binary search algorithm can be used to search whether the target memory-resident string exists in the stored resident strings in the memory; if the data structure corresponding to the hash value index is a red-black tree, then the red-black tree corresponding to the hash value index is used to search whether the target memory-resident string exists in the stored resident strings in the memory.
[0063] In the embodiment of the present application, the hash value of the target memory-resident string can be calculated according to a preset hash function. The embodiment of the present application does not specifically limit the preset hash function.
[0064] In an embodiment of the present application, if the target memory-resident string is found in the hash table or in the red-black tree, it is determined that the target memory-resident string exists in the stored resident strings in the memory; if the target memory-resident string is not found in the hash table and the target memory-resident string is not found in the red-black tree, it is determined that the target memory-resident string does not exist in the stored resident strings in the memory.
[0065] In an embodiment of the present application, a target memory resident string to be searched is obtained, and the target memory resident string is consistent with the function name of the function corresponding to the target function of the device; a command for executing the function corresponding to the target function is written in Python programming language; a currently satisfied string search condition is determined; if it is determined that the currently satisfied string search condition is a linear search condition, a binary search algorithm is used to search whether the target memory resident string exists in the stored resident strings in the memory; if it is determined that the currently satisfied string search condition is a combined search condition, a hash table corresponding to the stored resident strings and a data structure corresponding to the hash value index in the hash table are used to search whether the target memory resident string exists in the stored resident strings in the memory. After determining whether to use a binary search algorithm or to search for the target memory resident string according to the hash table corresponding to the stored resident strings and a data structure corresponding to the hash value index in the hash table according to the currently satisfied string search condition, the use of a binary search algorithm or the use of a hash table corresponding to the stored resident strings and a data structure corresponding to the hash value index in the hash table to search for the target memory resident string can reduce the algorithm time complexity, improve the search efficiency, and further improve the efficiency of executing the command to achieve the target function.
[0066] Optionally, in an embodiment of the present application, if it is determined that the target memory-resident string exists in the memory, the underlying coding string having a mapping relationship with the target memory-resident string is determined based on the found target memory-resident string; and the target function is executed according to the function code corresponding to the underlying coding string.
[0067] The underlying coding string is a function code that can be directly executed by the electronic device. For example, the underlying coding string can be a function code written in C language.
[0068] Exemplarily, the target memory-resident string is connect, and the corresponding underlying coding string is native_aiot_create_device. The mapping relationship between the stored target memory-resident string and the underlying coding string can be: {MP_OBJ_NEW_QSTR(MP_QSTR_connect), MP_ROM_PTR(&native_aiot_create_device)}. If it is determined that the target memory-resident string connect exists in the memory, the corresponding mapping relationship can be determined, and the string native_aiot_create_device in the mapping relationship is determined as the underlying coding string with which it has a mapping relationship. The target function corresponding to connect can be executed according to native_aiot_create_device.
[0069] In an embodiment of the present application, a mapping relationship between a target memory-resident string and an underlying coding string is stored in the memory. If the target memory-resident string is searched, the underlying coding string having a mapping relationship with the target memory-resident string can be determined based on the mapping relationship between the target memory-resident string and the underlying coding string, so that the electronic device can execute the target function according to the function code corresponding to the underlying coding string.
[0070] Optionally, in an embodiment of the present application, if it is determined that the target memory-resident string does not exist in the stored resident strings in the memory, the mapping relationship between the target memory-resident string and the corresponding underlying coding string is loaded into the memory so that the target memory-resident string exists in the memory, and the underlying coding string with which it has a mapping relationship can be determined, and the target function can be executed according to the function code corresponding to the underlying coding string.
[0071] Example 2
[0072] Figure 3 The flowchart of the method for searching for a string in memory provided in the second embodiment of the present application is based on the above embodiment. This embodiment involves a refinement of the specific implementation method of determining the currently satisfied string search condition in step S102, such as Figure 3 As shown, the specific steps include:
[0073] Step S201: Determine the size of memory resources.
[0074] The memory resource size may be the size of the internal memory of the microcontroller hardware.
[0075] In the embodiment of the present application, the pre-stored linear search condition is that the memory resource size is less than or equal to the preset memory resource threshold; the pre-stored combined search condition is that the memory resource is greater than the preset memory resource threshold.
[0076] Specifically, obtain the linear search condition and the preset memory resource threshold in the combined search condition, determine the memory resource size in the memory resource, compare the memory resource size in the memory resource with the preset memory resource threshold, and judge whether the memory resource size in the memory resource is greater than the preset memory resource threshold. If the memory resource size is greater than the preset memory resource threshold, execute step S203; if the memory resource size is not greater than the preset memory resource threshold, execute step S202.
[0077] Step S202: If it is determined that the memory resource size is less than or equal to the preset memory resource threshold, it is determined that the currently satisfied string search condition is a linear search condition.
[0078] In this embodiment, if it is determined that the memory resource size is less than or equal to the preset memory resource threshold, it means that the memory resources are insufficient and the space occupied by the stored resident strings needs to be saved. The one-dimensional array occupies less space, so the stored resident strings are stored in the memory in the form of a one-dimensional array, and it is determined that the currently satisfied string search condition is a linear search condition.
[0079] In the embodiment of the present application, after determining that the currently satisfied string search condition is a linear search condition, a binary search algorithm may be used to search whether the target memory-resident string exists in the stored resident strings in the memory.
[0080] Step S203: If it is determined that the memory resource size is greater than the preset memory resource threshold, the currently satisfied string search condition is determined to be a combined search condition.
[0081] In this embodiment, if it is determined that the memory resource size is greater than the preset memory resource threshold, it means that the memory resources are sufficient, and there is no need to save the space occupied by the stored resident character strings, which can improve the search efficiency. The search efficiency of the data structure corresponding to the hash table and the hash value index in the hash table is high, so the stored resident character strings are stored in the hash table and the data structure corresponding to the hash value index in the hash table, and the currently satisfied string search condition is determined to be the combined search condition.
[0082] In an embodiment of the present application, after determining that the currently satisfied string search condition is a combined search condition, the target memory resident string can be searched whether it exists in the stored resident strings in the memory based on the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table.
[0083] In an embodiment of the present application, the size of the memory resource is determined; if it is determined that the memory resource size is less than or equal to the preset memory resource threshold, it is determined that the currently satisfied string search condition is a linear search condition; if it is determined that the memory resource size is greater than the preset memory resource threshold, it is determined that the currently satisfied string search condition is a combined search condition. Whether the satisfied string search condition is a linear search condition or a combined search condition is determined based on the size of the memory resource in the memory resource. When the number of stored resident strings is small, a binary search algorithm that occupies less space can be used to search for the target memory resident string. Since the number of stored resident strings is small, the target memory resident string is not searched based on the data structure corresponding to the hash table and the hash value index in the hash table. This will not significantly reduce the search efficiency, but it can reduce the memory occupied by the stored resident strings. This can control the size of the memory occupied by the stored resident strings while reducing the time complexity of the algorithm and improving the search efficiency.
[0084] Example 3
[0085] Figure 4 This is a flowchart of a method for searching a string in memory provided by embodiment 3 of the present application. Based on any of the above embodiments, this embodiment of the present application involves a method for searching a string in memory when the string search condition satisfied is a linear search condition, such as Figure 4 As shown, the specific steps include:
[0086] Step S301: sort the resident character strings stored in the memory according to a preset arrangement order of characters at the same position in the character strings.
[0087] In the embodiment of the present application, the stored resident character strings are sorted according to a preset arrangement order, so that the stored resident character strings can be stored in the memory in a regular manner.
[0088] Among them, the preset arrangement order can be an alphabetical order, which is not specifically limited in the embodiment of the present application.
[0089] Optionally, step S301 may include the following steps:
[0090] Step S3011: sort the resident character strings stored in the memory according to the preset arrangement order of the characters at the current position in the character strings.
[0091] Exemplarily, the preset arrangement order is alphabetical order, and the character at the current position of the stored resident character strings can be determined, and the stored resident character strings can be sorted according to the alphabetical order of the character at the current position of the stored resident character strings. Exemplarily, the stored resident character strings can include ABCD, CDEF, and if the current position is the first letter, then ABCD, when sorted alphabetically according to the first letter, comes before CDEF.
[0092] Step S3012: If it is determined that there are multiple stored resident character strings with the same character at the current position, the multiple stored resident character strings are sorted according to a preset arrangement order of the next position character.
[0093] Specifically, if it is determined that there are multiple stored resident character strings with the same first letter, the multiple stored resident character strings with the same first letter are sorted in the order of the second-position letter in the alphabet; if there are multiple stored resident character strings with the same second-position letter, the multiple stored resident character strings with the same first letter are sorted in the order of the third-position letter in the alphabet, until there are no more stored resident character strings with the same character at the current position.
[0094] Step S302: Use a binary search algorithm to find out whether the target memory resident character string exists in the sorted stored resident character strings.
[0095] Specifically, the stored resident string at the middle position of the sorted stored resident strings is determined, and whether the target memory resident string is equal to the stored resident string at the middle position is determined; if the target memory resident string is the same as the stored resident string at the middle position, the target memory resident string is found; if the target memory resident string is different from the stored resident string at the middle position, it is determined whether the target memory resident string is before the stored resident string at the middle position; if the target memory resident string is before the stored resident string at the middle position, a binary search algorithm is used to find the target memory resident string in the stored resident string before the stored resident string at the middle position. memory-resident string; if the target memory-resident string is not before the stored resident string at the middle position, then a binary search algorithm is used to search for the target memory-resident string in the stored resident string after the stored resident string at the middle position; until the target memory-resident string is found, it is determined that the target memory-resident string exists in the stored resident strings in the memory; or until there is no stored resident string before the stored resident string at the middle position and there is no stored resident string after the stored resident string at the middle position, then the target memory-resident string is not found, and it is determined that the target memory-resident string does not exist in the stored resident strings in the memory.
[0096] In an embodiment of the present application, the resident character strings stored in the memory are sorted according to the preset arrangement order of the characters at the same position in the character string, which can meet the requirements of using a binary search algorithm; using a binary search algorithm to search whether the target memory resident character string exists in the sorted stored resident character strings can improve the efficiency of searching the target memory resident character string.
[0097] Example 4
[0098] Figure 5 The flowchart of the method for searching strings in memory provided by the fourth embodiment of the present application is based on any of the above embodiments. The embodiment of the present application involves a method for constructing a hash table and a red-black tree before searching whether the target memory resident string exists in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table when the string search condition is satisfied. Figure 5 As shown, the specific steps include:
[0099] Step S401: Calculate the hash value of the resident character string stored in the memory.
[0100] Specifically, a hash function is used to calculate the hash value of the resident character string stored in the memory.
[0101] Among them, the hash function used can be Message-Digest Algorithm 5 (English full name: Message-Digest Algorithm 5, abbreviated as: MD5), or Secure Hash Algorithm 1 (English full name: Secure Hash Algorithm 1, abbreviated as: SHA-1), etc., and the embodiments of the present application do not make specific limitations on this.
[0102] Step S402: Calculate the hash value index of the stored resident character string according to the hash value of the stored resident character string to form a hash table.
[0103] The embodiment of the present application does not specifically limit the method of calculating the hash value index based on the hash value. Calculating the hash value index based on the hash value can reduce the number of hash value indexes, thereby reducing the size of the hash table formed by the hash value indexes.
[0104] For example, the size of the hash table can be controlled to 1 / 64 of the total number of stored resident character strings by calculating the hash value index.
[0105] Optionally, the hash value index may be calculated according to the hash value by performing a modulo calculation on the hash value of the stored resident character string and the number of preset hash value indexes to obtain the hash value index of the stored resident character string.
[0106] The number of preset hash value indexes can be pre-set according to the size of the memory. The larger the memory, the more preset hash value indexes there are.
[0107] Specifically, by dividing the preset number of hash value indexes by the stored hash value of the resident character string and performing a modulo calculation, a hash table with the preset number of hash value indexes can be obtained, which can reduce the size of the hash table.
[0108] Step S403: For each hash value index, determine the number of stored resident character strings having the same hash value index.
[0109] Specifically, after calculating the hash value index of the stored resident character strings, the number of stored resident character strings corresponding to each hash value index is determined. The number of stored resident character strings for the same hash value index is different, and the corresponding processing is also different.
[0110] Step S404: Generate corresponding data structures for multiple stored resident character strings with the same hash value index according to the number of stored resident character strings.
[0111] In an embodiment of the present application, when the number of stored resident strings is small, the stored resident strings with the same hash value index can be formed into a linear data structure, such as stored in the form of a one-dimensional array; when the number of stored resident strings is small or large, multiple stored resident strings with the same hash value index can be stored in the form of a red-black tree.
[0112] In an embodiment of the present application, a hash value of a resident string stored in a memory is calculated; a hash value index of the stored resident string is calculated based on the hash value of the stored resident string to form a hash table; for each hash value index, the number of stored resident strings having the same hash value index is determined, which can reduce the number of hash value indexes and thus reduce the size of the hash table formed by the hash value index; a corresponding data structure is generated for multiple stored resident strings having the same hash value index based on the number of stored resident strings; when the currently satisfied string search condition is a combined search condition, the algorithm time complexity can be reduced and the search efficiency can be improved; and when the number of stored resident strings having the same hash value index is small, multiple stored resident strings having the same hash value index can be stored in a manner that occupies less space. Since the number of stored resident strings having the same hash value index is small, not searching for the target memory resident string according to the data structure corresponding to the hash value index in the hash table will not significantly reduce the search efficiency, but can reduce the memory occupied by multiple stored resident strings having the same hash value index.
[0113] Example 5
[0114] Based on the fourth embodiment, the embodiment of the present application relates to an implementation method of generating a corresponding data structure for multiple stored resident strings with the same hash value index according to the number of stored resident strings in step S404, which may specifically include the following steps:
[0115] Step S501: Determine whether the number of stored resident character strings is greater than a preset number threshold.
[0116] Specifically, after determining the number of stored resident strings with the same hash value index, a preset number threshold is obtained, the number of stored resident strings is compared with the preset number threshold, and it is determined whether the number of stored resident strings is greater than the preset number threshold. If the number of stored resident strings is greater than the preset number threshold, step S4042 is executed; if the number of stored resident strings is not greater than the preset number threshold, step S4043 is executed.
[0117] Step S502: If it is determined that the number is greater than the preset threshold, multiple stored resident character strings with the same hash value index are converted into corresponding red-black trees.
[0118] In an embodiment of the present application, multiple stored resident strings with the same hash value index can be converted into a balanced binary tree 2-3 tree, and then the 2-3 tree can be converted into a red-black tree.
[0119] Among them, the converted red-black tree has the following characteristics: 1. Each node can only be red or black; 2. The root node is black; 3. Each leaf node or empty node is black; 4. If a node is red, then its two child nodes are black, that is, two adjacent red nodes cannot appear on a path; 5. All paths from any node to each of its leaves contain the same number of black nodes.
[0120] Step S503: If it is determined that the number is less than or equal to the preset number threshold, multiple stored resident character strings with the same hash value index are sorted according to the preset arrangement order of characters at the same position in the character string to form a one-dimensional array.
[0121] In an embodiment of the present application, the manner in which multiple stored resident strings with the same hash value index are sorted according to the preset arrangement order of characters at the same position in the string is similar to the manner in which step S301 sorts the resident strings stored in the memory according to the preset arrangement order of characters at the same position in the string, and will not be described in detail here.
[0122] Exemplarily, the preset number threshold is 8, the hash value index is 123, and it is determined whether the number of stored resident character strings corresponding to the hash value index 123 is greater than 8; if the number of stored resident character strings with the hash value index 123 is greater than 8, the stored resident character strings with the hash value index 123 are converted into a red-black tree; if the number of stored resident character strings with the hash value index 123 is not greater than 8, the stored resident character strings with the hash value index 123 are sorted according to the preset arrangement order of characters in the same position to form a one-dimensional array.
[0123] In an embodiment of the present application, it is determined whether the number of stored resident strings is greater than a preset number threshold; if it is determined that the number is greater than the preset number threshold, multiple stored resident strings with the same hash value index are converted into corresponding red-black trees; if it is determined that the number is less than or equal to the preset number threshold, multiple stored resident strings with the same hash value index are sorted according to a preset arrangement order of characters at the same position in the string to form a one-dimensional array; when the currently satisfied string search condition is a combined search condition and the number of stored resident strings with the same hash value index is small, the stored resident strings can be formed into a one-dimensional array, which can reduce the memory occupied by the stored resident strings without affecting the search efficiency; and when the currently satisfied string search condition is a combined search condition and the number of stored resident strings with the same hash value index is large, the memory resources occupied by the stored resident strings can be ignored and the stored resident strings can be converted into corresponding red-black trees, which can reduce the time complexity of the algorithm and improve the search efficiency.
[0124] Example 6
[0125] Figure 6 The flowchart of the method for searching strings in memory provided by the sixth embodiment of the present application is based on the fifth embodiment. The embodiment of the present application involves a refinement of the implementation method of searching whether the target memory resident string exists in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table if the currently satisfied string search condition is determined to be a combined search condition in step S103, such as Figure 6 As shown, the specific steps include:
[0126] Step S601: Calculate the hash value of the target memory resident string.
[0127] In the embodiment of the present application, the method of calculating the hash value of the target memory-resident string is similar to the method of calculating the hash value of the resident string stored in the memory in step S401, and will not be repeated here.
[0128] Step S602: searching a hash table for a target hash value index corresponding to the target memory-resident string according to the hash value of the target memory-resident string, and determining the number of stored resident strings corresponding to the target hash value index.
[0129] The target hash value index is a hash value index corresponding to the hash value of the target memory-resident string.
[0130] Specifically, a target hash value index of the target memory resident string is calculated according to the hash value of the target memory resident string; the target hash value index is searched in the hash table; the number of stored resident strings corresponding to the target hash value index is determined, and a preset number threshold is obtained; the preset number threshold is compared with the number of stored resident strings to determine whether the number of stored resident strings is greater than the preset number threshold; if it is determined that the number of stored resident strings is greater than the preset number threshold, step S603 is executed; if it is determined that the number of stored resident strings is not greater than the preset number threshold, step S604 is executed.
[0131] Step S603: If it is determined that the number of stored resident strings is greater than the preset number threshold, a red-black tree indexed by the target hash value is obtained, and a red-black tree algorithm is used to search the target memory-resident string in the red-black tree to determine whether the target memory-resident string exists in the stored resident strings in the memory.
[0132] In an embodiment of the present application, if it is determined that the number of stored resident strings is greater than a preset threshold, the stored resident strings corresponding to the target hash value index are stored in a red-black tree format, and a red-black tree algorithm is used to search for the target memory resident string in the red-black tree. The red-black tree algorithm used can be a pre-order search red-black tree algorithm, an in-order search red-black tree algorithm, a post-order search red-black tree algorithm, etc., which is not specifically limited in the embodiment of the present application.
[0133] Step S604: If it is determined that the number of stored resident strings is less than or equal to the preset number threshold, obtain a one-dimensional array of the target hash value index, and search the one-dimensional array to see whether the target memory resident string exists in the stored resident strings in the memory.
[0134] In an embodiment of the present application, if it is determined that the number of stored resident strings is less than or equal to a preset number threshold, the stored resident strings corresponding to the target hash value index are stored in the form of a one-dimensional array, and thus a search is performed in the one-dimensional array to determine whether the target memory resident string exists in the stored resident strings in the memory.
[0135] The present application does not impose any specific limitation on the method of searching for a target memory-resident string in a one-dimensional array. For example, a traversal search method can be used to search in a one-dimensional array, or a binary search algorithm can be used to search in a one-dimensional array.
[0136] In an embodiment of the present application, when determining that the currently satisfied string search condition is a combined search condition, by calculating the hash value of the target memory-resident string, the target hash value index corresponding to the target memory-resident string is searched in the hash table according to the hash value of the target memory-resident string and the number of stored resident strings corresponding to the target hash value index is determined; according to the number of stored resident strings corresponding to the target hash value index, it is determined whether to search in a one-dimensional array for the target memory-resident string to exist in the stored resident strings in the memory, or to use a red-black tree algorithm to search for the target memory-resident string in the red-black tree; when the number of stored resident strings is small, the target memory-resident string can be searched in the one-dimensional array, which can reduce the memory occupied by the stored resident strings and will not significantly reduce the search efficiency; when the number of stored resident strings is large, the target memory-resident string can be searched in the red-black tree without considering the memory resources occupied by the stored resident strings, which can reduce the time complexity of the algorithm and improve the search efficiency.
[0137] Example 7
[0138] Figure 7 The flowchart of the method for searching for a string in memory provided by the seventh embodiment of the present application is based on any of the above embodiments. The embodiment of the present application involves the steps after determining that the target memory resident string exists in the memory, such as Figure 7 As shown, the specific steps include:
[0139] Step S701: If it is determined that the target memory-resident character string exists in the memory, a total index of the target memory-resident character string is determined according to a preset indexing strategy.
[0140] In an embodiment of the present application, the preset indexing strategy is a strategy for defining the storage location of a target memory-resident string. For example, the preset indexing strategy can define the storage location of the target memory-resident string by its position in a hash table, the storage format of a stored resident string, or its position in a one-dimensional array or a red-black tree.
[0141] Exemplarily, the preset index strategy can be: if the string search condition is a linear search condition, the total index is a four-digit number, indicating the position of the target memory-resident string in the one-dimensional array; if the string search condition is a combined search condition, the total index is a five-digit number, the first three digits indicating the position of the target memory-resident string in the hash table, the fourth digit indicating the storage form of the stored resident string, and the fifth digit indicating the position in the one-dimensional array or in the red-black tree.
[0142] For example, the total index of the target memory-resident string determined according to the preset indexing strategy may be 0005, indicating that the target memory-resident string is at the 5th position in the one-dimensional array. The total index of the target memory-resident string determined according to the preset indexing strategy may be 12315, where 123 indicates that the target memory-resident string is at the 123rd position in the hash table, 1 indicates that the stored resident string is stored in a red-black tree, and 5 indicates that the target memory-resident string is at the 5th position in the red-black tree.
[0143] Step S702: Store the total index of the target memory-resident character strings.
[0144] Specifically, the total index of the target memory-resident character string is determined and stored in the memory.
[0145] Step S703: If it is determined that the memory-resident string to be searched is still the target memory-resident string, then determine, according to the total index, an underlying encoded string that has a mapping relationship with the target memory-resident string.
[0146] Specifically, after determining that the target memory-resident string exists in the memory and storing the total index of the target memory-resident string, the next memory-resident string to be searched is determined. If the next memory-resident string to be searched is the searched target memory-resident string, the next memory-resident string to be searched exists in the memory, and the storage location of the mapping relationship between the target memory-resident string and the corresponding underlying coding string is determined according to the total index, thereby determining the underlying coding string that has a mapping relationship with the target memory-resident string.
[0147] In an embodiment of the present application, by determining the total index of the target memory-resident string and determining the total index of the target memory-resident string according to a preset index strategy, when the memory-resident string to be searched is still the target memory-resident string, the underlying coding string having a mapping relationship with the target memory-resident string can be directly determined according to the total index, which can improve the efficiency of determining the underlying coding string having a mapping relationship with the target memory-resident string, thereby improving the efficiency of executing commands to achieve target functions.
[0148] Example 8
[0149] Figure 8 This is a schematic diagram of the structure of the device for searching strings in memory provided by the eighth embodiment of the present application. The schematic diagram of the structure of the device for searching strings in memory provided by the embodiment of the present application can execute the processing flow provided by the first embodiment of the method for searching strings in memory. Figure 8 As shown, the device 80 for searching a character string in memory includes: an acquisition module 801 , a determination module 802 , and a search module 803 .
[0150] Specifically, the acquisition module 801 is used to obtain the target memory resident string to be searched, where the target memory resident string is consistent with the function name of the function corresponding to the target function of the device; the command for executing the function corresponding to the target function is written in Python programming language.
[0151] Determination module 802, used to determine the string search condition currently satisfied;
[0152] The search module 803 is configured to use a binary search algorithm to search whether the target memory-resident string exists in the stored resident strings in the memory if it is determined that the currently satisfied string search condition is a linear search condition; and to search whether the target memory-resident string exists in the stored resident strings in the memory based on a hash table corresponding to the stored resident strings and a data structure corresponding to a hash value index in the hash table if it is determined that the currently satisfied string search condition is a combined search condition.
[0153] Optionally, the determination module 802 is specifically used to: determine the memory resource size; if it is determined that the memory resource size is less than or equal to the preset memory resource threshold, determine that the currently satisfied string search condition is a linear search condition; if it is determined that the memory resource size is greater than the preset memory resource threshold, determine that the currently satisfied string search condition is a combined search condition.
[0154] Optionally, the memory string search device 80 further includes: a construction module; the construction module is used to sort the resident strings stored in the memory according to a preset arrangement order of characters at the same position in the string; and a search module 803, specifically used to use a binary search algorithm to search whether the target memory resident string exists in the sorted stored resident strings.
[0155] Optionally, the construction module is specifically used to: sort the resident character strings stored in the memory according to the preset arrangement order of the characters at the current position in the string; if it is determined that there are multiple stored resident character strings with the same characters at the current position, then sort the multiple stored resident character strings according to the preset arrangement order of the characters at the next position.
[0156] Optionally, the construction module is further used to: calculate hash values of resident strings stored in the memory; calculate hash value indexes of the stored resident strings based on the hash values of the stored resident strings to form a hash table; determine the number of stored resident strings with the same hash value index for each hash value index; and generate corresponding data structures for multiple stored resident strings with the same hash value index based on the number of stored resident strings.
[0157] Optionally, the construction module is specifically configured to perform a modulo calculation on the hash value of the stored resident character string and the number of preset hash value indexes to calculate and obtain the hash value index of the stored resident character string.
[0158] Optionally, the construction module is specifically used to: determine whether the number of stored resident strings is greater than a preset number threshold; if it is determined that it is greater than the preset number threshold, convert multiple stored resident strings with the same hash value index into corresponding red-black trees; if it is determined that it is less than or equal to the preset number threshold, sort the multiple stored resident strings with the same hash value index according to a preset arrangement order of characters at the same position in the string to form a one-dimensional array.
[0159] Optionally, the search module 803 is specifically used to: calculate the hash value of the target memory-resident string; search the hash table for a target hash value index corresponding to the target memory-resident string according to the hash value of the target memory-resident string and determine the number of stored resident strings corresponding to the target hash value index; if it is determined that the number of stored resident strings is greater than a preset number threshold, obtain a red-black tree of the target hash value index, and use a red-black tree algorithm to search the target memory-resident string in the red-black tree to determine whether the target memory-resident string exists in the stored resident strings in the memory; if it is determined that the number of stored resident strings is less than or equal to the preset number threshold, obtain a one-dimensional array of the target hash value index, and search the one-dimensional array for the target memory-resident string to determine whether the target memory-resident string exists in the stored resident strings in the memory.
[0160] Optionally, the memory string search device 80 also includes: an execution module; the execution module is used to determine the underlying coding string with a mapping relationship based on the found target memory resident string if it is determined that the target memory resident string exists in the memory; and execute the target function according to the function code corresponding to the underlying coding string.
[0161] Optionally, the construction module is further used to: if it is determined that the target memory-resident string exists in the memory, determine the total index of the target memory-resident string according to a preset index strategy; and store the total index of the target memory-resident string; the search module 803 is further used to: if it is determined that the memory-resident string to be searched is still the target memory-resident string, determine the underlying encoded string that has a mapping relationship with the target memory-resident string according to the total index.
[0162] The device provided in the embodiments of the present application can be specifically used to execute any method embodiment of the above-mentioned embodiments 2 to 7, and the specific functions will not be repeated here.
[0163] Example 9
[0164] Figure 9This is a schematic diagram of the structure of the electronic device provided in Example 9 of this application. Figure 9 As shown, the electronic device 90 includes: a processor 901, and a memory 902 communicatively connected to the processor 901.
[0165] The processor 901 and the memory 902 are interconnected; the memory 902 stores computer-executable instructions; and the processor 901 executes the computer-executable instructions stored in the memory 902 to implement the method for searching a character string in memory provided in any of the above embodiments.
[0166] An embodiment of the present application further provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, the method provided by any of the above method embodiments is implemented.
[0167] An embodiment of the present application also provides a computer program product, which includes: computer execution instructions, which are stored in a readable storage medium. At least one processor of an electronic device can read the computer execution instructions from the readable storage medium, and at least one processor executes the computer execution instructions so that the electronic device executes the method provided by any of the above method embodiments.
[0168] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present application that follow the general principles of the present application and include common knowledge or customary techniques in the art not disclosed herein. The description and examples are to be considered as exemplary only, and the true scope and spirit of the present application are indicated by the following claims.
[0169] It should be understood that the present application is not limited to the exact structure described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
Claims
1. A method for searching a string in memory, characterized in that: include: Obtaining a target memory-resident string to be searched, where the target memory-resident string is consistent with a function name corresponding to a function that implements a target function of the device; The commands for executing the functions corresponding to the target functions are written in Python programming language; Determine the size of memory resources; If it is determined that the memory resource size is less than or equal to the preset memory resource threshold, determining that the currently satisfied string search condition is a linear search condition; If it is determined that the memory resource size is greater than the preset memory resource threshold, determining that the currently satisfied string search condition is a combined search condition; Sorting the resident character strings stored in the memory according to a preset arrangement order of characters at the same position in the character strings; If it is determined that the currently satisfied string search condition is a linear search condition, a binary search algorithm is used to search whether the target memory resident string exists in the sorted stored resident strings; the linear search condition is a condition for searching in a linear search manner; If it is determined that the currently satisfied string search condition is a combined search condition, then searching whether the target memory resident string exists in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table; The combined search condition is a condition for searching by combining a hash table with a data structure corresponding to a hash value index in the hash table.
2. The method according to claim 1, characterized in that The step of sorting the resident character strings stored in the memory according to a preset arrangement order of characters at the same position in the character strings includes: Sorting the resident character strings stored in the memory according to the preset arrangement order of the characters at the current position in the character strings; If it is determined that there are multiple stored resident character strings with the same character at the current position, the multiple stored resident character strings are sorted according to a preset arrangement order of the next position character.
3. The method according to claim 1, characterized in that Before searching whether the target memory resident string exists in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table, the method further includes: Calculating a hash value of the resident string stored in the memory; Calculating a hash value index of the stored resident character string according to the hash value of the stored resident character string to form a hash table; For each hash value index, determine the number of stored resident strings having the same hash value index; A corresponding data structure is generated for a plurality of stored resident character strings having the same hash value index according to the number of stored resident character strings.
4. The method according to claim 3, characterized in that The step of calculating the hash value index of the stored resident character string according to the hash value of the stored resident character string includes: A modulo calculation is performed on the hash value of the stored resident character string and the number of preset hash value indexes to calculate the hash value index of the stored resident character string.
5. The method according to claim 4, characterized in that The step of generating a corresponding data structure for a plurality of stored resident character strings having the same hash value index according to the number of stored resident character strings includes: Determine whether the number of stored resident character strings is greater than a preset number threshold; If it is determined that the number is greater than the preset number threshold, multiple stored resident strings with the same hash value index are converted into corresponding red-black trees; If it is determined that the number is less than or equal to the preset number threshold, multiple stored resident character strings with the same hash value index are sorted according to the preset arrangement order of characters at the same position in the character string to form a one-dimensional array.
6. The method according to claim 5, characterized in that The searching, based on a hash table corresponding to the stored resident strings and a data structure corresponding to a hash value index in the hash table, to see whether the target memory resident string exists in the stored resident strings in the memory includes: Calculate the hash value of the target memory resident string; Searching the target memory resident string for a target hash value index corresponding to the target memory resident string in the hash table according to the hash value of the target memory resident string and determining the number of stored resident strings corresponding to the target hash value index; If it is determined that the number of stored resident strings is greater than a preset number threshold, obtaining a red-black tree indexed by the target hash value, and using a red-black tree algorithm to search the target memory-resident string in the red-black tree to determine whether the target memory-resident string exists in the stored resident strings in the memory; If it is determined that the number of stored resident strings is less than or equal to the preset number threshold, a one-dimensional array of target hash value indexes is obtained, and the target memory resident string is searched in the one-dimensional array to see whether it exists in the stored resident strings in the memory.
7. The method according to any one of claims 1 to 6, characterized in that Also includes: If it is determined that the target memory-resident string exists in the memory, then determining the underlying encoding string having a mapping relationship therewith based on the found target memory-resident string; Execute the target function according to the function code corresponding to the underlying encoding string.
8. The method according to any one of claims 1 to 6, characterized in that Also includes: If it is determined that the target memory-resident string exists in the memory, determining the total index of the target memory-resident string according to a preset indexing strategy; Storing the total index of the target memory-resident character string; If it is determined that the memory-resident string to be searched is still the target memory-resident string, an underlying encoded string having a mapping relationship with the target memory-resident string is determined according to the total index.
9. A device for searching a character string in a memory, characterized in that: include: An acquisition module, configured to acquire a target memory-resident string to be searched, wherein the target memory-resident string is consistent with a function name corresponding to a function that implements a target function of the device; The commands for executing the functions corresponding to the target functions are written in Python programming language; a determination module, configured to determine a memory resource size; and if it is determined that the memory resource size is less than or equal to a preset memory resource threshold, determining that the currently satisfied string search condition is a linear search condition; If it is determined that the memory resource size is greater than the preset memory resource threshold, determining that the currently satisfied string search condition is a combined search condition; A construction module is used to sort the resident character strings stored in the memory according to a preset arrangement order of characters at the same position in the character strings; A search module, configured to use a binary search algorithm to search whether the target memory-resident string exists in the sorted stored resident strings if it is determined that the currently satisfied string search condition is a linear search condition; the linear search condition is a condition for searching in a linear search manner; If it is determined that the currently satisfied string search condition is a combined search condition, then searching whether the target memory resident string exists in the stored resident strings in the memory according to the hash table corresponding to the stored resident strings and the data structure corresponding to the hash value index in the hash table; The combined search condition is a condition for searching by combining a hash table with a data structure corresponding to a hash value index in the hash table.
10. An electronic device comprising: a processor, and a memory communicatively connected to the processor; The memory stores computer-executable instructions; The processor executes the computer-executable instructions stored in the memory to implement the method according to any one of claims 1 to 8.
11. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer-executable instructions, which are used to implement the method according to any one of claims 1 to 8 when executed by a processor.
12. A computer program product comprising a computer program, wherein when the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Inquiry method and device for function call, and electronic device
CN109542942A
Download control method and system based on dynamic search strategy and storage medium
CN111049887A
Hardware accelerated query method and system, electronic equipment and storage medium
CN112202677A