The content of the invention
It is an object of the invention to provide the storage method and querying method of a kind of phone number, for reducing memory space
Pressure, and realize the quick search of phone number to reduce query time.
In order to solve the above technical problems, the present invention provides a kind of storage method of phone number, including:
Phone number is divided into two parts according to number section and a number tail according to preset rules, and is constituted with the number section and number tail
Key-value pair build storage region;
When phone number to be stored is received, the phone number to be stored is parsed according to the preset rules corresponding
Target number section and target tail;
The position of the target number section is found in the storage region, and by target tail storage in the mesh
In the corresponding region of label section.
Preferably, the preset rules are:The number section is first 7 of the phone number to be stored, and described tail is
Latter 4 of the phone number to be stored.
Preferably, the key-value pair constituted with the number section and number tail is built storage region and specifically included:
Using the number section as the first index, and as the key in the key-value pair;
Using described tail as the second index, and as the value in the key-value pair.
Preferably, it is described to be specifically included as the second index using described tail:
Described tail is mapped in the integer array that length is 313 according to the span of number tail, with described whole
Length sequence number in type array is used as the described second index;
Wherein, 32 storage location sequence numbers, institute are included under the length serial number 0-312, and each length sequence number
State length sequence number=described tail/32, the storage location sequence number=described tail %32.
Preferably, the position that the target number section is found in the storage region, and by the target tail
Storage is specifically included in the corresponding region of the target number section:
The position of the target number section is found in the storage region according to the value of the target number section;
Judge in the position of the target number section whether added excessively described target number section;
If it is, by target tail storage in the corresponding region of the target number section;
If it is not, then adding the target number section in the position of the target number section, and the target tail is stored
In the corresponding region of the target number section.
In order to solve the above technical problems, the present invention provides a kind of querying method of phone number, including:
When phone number to be checked is received, the corresponding target number section of the phone number to be checked and target number are parsed
Tail;
In the storage region constructed by the key-value pair constituted with number section and number tail, judge whether and the target number
Section identical number section;Wherein, the number section and described tail are the two parts for being divided into phone number according to preset rules;
If there is with the target number section identical number section, then in the corresponding storage region of the target number section judge
With the presence or absence of with the target tail identical tail;
If there is with the target tail identical tail, it is determined that the storage region include the mobile phone to be checked
Number;
If there is no with the target number section identical number section or if there is no with the target tail identical number
Tail, it is determined that the storage region does not include the phone number to be checked.
Preferably, the preset rules are:The number section is first 7 of the phone number to be stored, and described tail is
Latter 4 of the phone number to be stored.
The storage method of phone number provided by the present invention, due to by key-value pair by the number section of phone number and number tail
Associate, therefore, when multiple phone numbers have identical key, the part data as key can storage be once only
Can, without repeating to store, then again using as the part data storage of the value in key-value pair.In addition, carrying out data
During inquiry, can be inquired about according to key, if without corresponding key, also need not just continue to search for, therefore saved inquiry
Time.As can be seen here, the storage method can overcome in the prior art to store the total data of phone number and be brought
Problem, not only reduce memory space, and reduce query time, improve inquiry velocity.
In addition the querying method of phone number provided by the present invention, is carried out successively with the key-value pair that number section and number tail are constituted
Search, be first to search whether there is target number section in storage region, if it does not exist, then explanation storage region does not include treating
Inquiry mobile phone number;If it is present search whether there is target tail in storage region, if it is present explanation storage
Region includes phone number to be checked, and otherwise, storage region does not include phone number to be checked.As can be seen here, relative to existing
In technology, for each the complete phone number in traveling through storage region, the inquiry velocity of this method is fast, can save and look into
The inquiry time.
Specific embodiment
Below in conjunction with the accompanying drawing in the embodiment of the present invention, the technical scheme in the embodiment of the present invention is carried out clear, complete
Site preparation is described, it is clear that described embodiment is only a part of embodiment of the invention, rather than whole embodiments.Based on this
Embodiment in invention, those of ordinary skill in the art are not under the premise of creative work is made, and what is obtained is every other
Embodiment, belongs to the scope of the present invention.
Core of the invention is to provide the storage method and querying method of a kind of phone number, for reducing memory space
Pressure, and realize the quick search of phone number to reduce query time.
In order that those skilled in the art more fully understand the present invention program, with reference to the accompanying drawings and detailed description
The present invention is described in further detail.
Fig. 1 is a kind of flow chart of the storage method of phone number provided in an embodiment of the present invention.As shown in figure 1, including:
S10:Phone number is divided into two parts according to number section and a number tail according to preset rules, and is constituted with number section and number tail
Key-value pair build storage region.
In specific implementation, the form of phone number is generally 11, and with 1 beginning, the scope of current phone number is
13000000000-18999999999.According to preset rules, phone number is divided into number section and number tail two parts, it is necessary to illustrate
, phone number here is not some specific phone number, is the general designation of whole phone numbers, it is adaptable to any
One phone number.Number section and number tail constitute a complete phone number, therefore, in this step, by number section and number tail composition
Key-value pair, builds a storage region in the way of key-value pair.So in this storage region, will by way of key-value pair
Phone number to be stored is stored.It is understood that a bond energy enough corresponds to multiple values, such as in phone number,
If number section is key, then number tail is exactly the value of this key.Identical key, different values, then it represents that different data, therefore
Identical number section, different number tails, means that different phone numbers.It is understood that the dividing mode of number section and number tail
Have various, i.e., preset rules have various, can in the past 7 and latter 4, the present embodiment is not limited.
S11:When phone number to be stored is received, the corresponding target of phone number to be stored is parsed according to preset rules
Number section and target tail.
It is understood that will be with the preset rules phase in step S10 during parsing phone number to be stored in this step
Together, if such as preset rules are using first 7 of phone number as number section, afterwards if 4 conduct tails, then in this step,
It is also required to first 7 of phone number to be stored as target number section, 4 used as target tail afterwards.It should be noted that above
The number section of middle theory and a number tail are a kind of broadly expression ways, are not to refer in particular to some specific data in number section and number tail.
And be to determine due to the data in phone number to be stored, therefore data in target number section and target tail are also to determine
, with the number section above said and number tail difference.In other words, target number section is the one kind in number section, the target tail number of being also
One kind in tail.
S12:The position of target number section is found in storage region, and target tail is stored corresponding in target number section
In region.
After target number section is determined, the specific data in target number section find target number section in storage region
Position.If arranged according to the size order of number section in storage region, when target number section is 1300000, the then target
The position of number section is just in first position of storage region.
The position of target number section is have found, then by the storage of target tail in the corresponding region of target number section.
In sum, if to store two phone numbers, respectively 15001868406 and 15001868407, then foundation
Above-mentioned steps, the number section of the two phone numbers is identical, i.e., target number section is 1500186, and target tail is respectively
8406 and 8407, then in storage, it is only necessary to 1500186 are stored in the position of target number section 1500186, in the storage of the number section
8406 and 8407 are stored in region.As can be seen here, this method causes that the phone number with identical number section only needs to storage
Number tail, is then associated, by key-value pair without storage number section is repeated several times.
The storage method of the phone number that the present embodiment is provided, including according to preset rules by phone number according to number section and
Number tail is divided into two parts, and builds storage region with the key-value pair that number section and number tail are constituted;When receiving phone number to be stored
When, parse the corresponding target number section of phone number to be stored and target tail according to preset rules;Found in storage region
The position of target number section, and by the storage of target tail in the corresponding region of target number section.Due to by key-value pair by cell-phone number
The number section and a number tail of code are associated, therefore, when multiple phone numbers have identical key, can as the part data of key
Only to store once, without repeating to store, then again using as the part data storage of the value in key-value pair.Separately
Outward, when data query is carried out, can be inquired about according to key, if without corresponding key, also need not just continue to search for, because
This has saved query time.As can be seen here, the storage method can overcome the total data in the prior art to phone number to enter
The brought problem of row storage, not only reduces memory space, and reduces query time, improves inquiry velocity.
On the basis of above-described embodiment, used as preferred embodiment, preset rules are:Number section is cell-phone number to be stored
First 7 of code, number tail is latter 4 of phone number to be stored.
It is understood that present embodiment is the one kind in numerous implementation methods, be not only this in kind of a mode.
On the basis of above-described embodiment, as preferred embodiment, built with the key-value pair that number section and number tail are constituted
Storage region is specifically included:
Using number section as the first index, and as the key in key-value pair;
Using number tail as the second index, and as the value in key-value pair.
It is understood that still do not have strict difference using number tail as key using number section as key, it is contemplated that
The reading habit of user, using number section as the key in key-value pair in the present embodiment, using number tail as the value in key-value pair.This implementation
It is exactly the storage for carrying out number section first using number section as the first index in example, if number section is used as the key in key-value pair,
Then key is exactly the first index.So, in the storage for carrying out phone number, the position of target number section is found by the first index.
Number tail is identical as the principle of the second index, and the present embodiment is repeated no more.
As preferred embodiment, specifically included as the second index using number tail:
Be mapped to for number tail in the integer array that length is 313 by the span of foundation tail, with the length in integer array
Degree sequence number is used as the second index;
Wherein, under length serial number 0-312, and each length sequence number include 32 storage location sequence numbers, length sequence number=
Number tail/32, storage location sequence number=number tail %32.
In the present embodiment, the explanation so that number tail is for latter four of phone number as an example.Four from 0000-9999 afterwards, and totally 10000
It is individual, therefore, in principle, a key will be corresponding to 10000 values.In JAVA, a number tail can be mapped to an integer by us
Group, to facilitate storage.If the second index 32 numbers tails of lower correspondence, then the length for needing integer array is 313 (313*32
=10016) just can the number of satisfaction tail span.Divided with length sequence number, length sequence number is followed successively by 0-312.So
32 data, respectively 0-31 can be stored under each length sequence number.
For 15001868406 and 15001868407, the target tail of the two phone numbers is respectively 8406 Hes
8407, then in storage, it is length sequence number that the second index is calculated first, and length sequence number is calculated by a number tail divided by 32, right
For the two phone numbers, 262 are, storage location sequence number is obtained by number tail divided by 32 remainders, respectively 22 and 23.
Therefore, in the present embodiment, equivalent to again number tail divide into two association storage organizations, with equal length sequence number number
Tail, it is only necessary to store the data after modulo operation.It should be noted that herein, if without specified otherwise, be
Decimal data, storage herein is according to binary storage.
Seen from the above description, number tail of 8 phone numbers takes 1 byte unit (1 integer can store 32 numbers altogether
Tail, 1 integer accounts for 4 bytes in Java language).By calculating, if including whole numbers on blacklist,
13000000000-18999999999, then space all needed for storage is 313*4*600000 byte, i.e. 716MB.Its
In, 313 is the length of array, and 4 is 4 bytes (32 is exactly 4 bytes), 600000 be in 600,000 key-value pairs key (number
Preceding 7 1300000-1899999 of code, i.e., 1,900,000-130 ten thousand=600,000 keys).As can be seen here, this storage method is only needed to
The memory space of 716MB can just store whole phone numbers, it is obvious that not every phone number all adds black name
It is single, therefore, practically necessary memory space can be smaller, is adapted to be placed on storage in internal memory.So in follow-up data query,
The inquiry of data is directly carried out in internal memory, then inquiry velocity is faster.
As can be seen here, in the present embodiment, if being stored with such storage mode, then phase is indexed first
In the case of, it is not necessary to repeat storage number section, in this case, if being indexed with identical second, then only
Need to store the data after modulo operation.Therefore, the pressure of memory space is further reduced.
As preferred embodiment, finding the position of target number section in storage region, and by the storage of target tail
Specifically included in the corresponding region of target number section:
The position of target number section is found in storage region according to the value of target number section;
Judge in the position of target number section whether added label section of looking over so as to check;
If it is, by the storage of target tail in the corresponding region of target number section;
If it is not, then target number section is added in the position of target number section, and by the storage of target tail in target number section pair
In the region answered.
Part the present embodiment with above-described embodiment system is repeated no more, and for the advance storage region for building, is had
The position of possible target number section is empty, i.e., not comprising the phone number for having the number section in storage region, therefore, if target number
The number section is not added in the position of section, then need that the number section is added into the position first, and the storage of target tail exists again then
In corresponding region.
It is understood that if existing for target number section and target tail, then need not store again.
Fig. 2 is a kind of flow chart of the querying method of phone number provided in an embodiment of the present invention.As shown in Fig. 2 including:
S20:When phone number to be checked is received, the corresponding target number section of phone number to be checked and target number are parsed
Tail.
S21:In the storage region constructed by the key-value pair constituted with number section and number tail, judge whether and target number
Section identical number section.Wherein, number section and number tail are the two parts for being divided into phone number according to preset rules.If there is with mesh
Label section identical number section, then into step S22, otherwise into step S24.
S22:Judged whether in the corresponding storage region of target number section and target tail identical tail.If deposited
With target tail identical tail, then into step S23, otherwise into step S24.
S23:Determine that storage region includes phone number to be checked.
S24:Determine that storage region does not include phone number to be checked.
In the present embodiment, it is two parts for being divided into phone number according to preset rules with number section and number tail to mention, with
And implementation method and phone number corresponding to storage region this two parts constructed by the key-value pair constituted with number section and number tail
Storage method part implementation method it is identical, therefore, the present embodiment is repeated no more.
First it is exactly according to pre- if inquired about a phone number after being stored according to above-mentioned storage mode
If rule parsing goes out target number section and target tail.Then judge whether include target number section in storage region, it is possible to understand that
It is, if not including the target number section, the phone number not to be included necessarily, therefore, also just it is not required to be searched again.Can manage
Solution, the speed for the inquiry for inquiring about the phone number more complete than of number section is fast, because a complete hand
The digit of machine number is substantially more.If comprising the target number section, illustrating to be possible to comprising the cell-phone number in storage region
Code, it is necessary to further search for, but the scope now searched be only in the corresponding region of target number section, therefore, inquiry
Scope reduced, also just can accelerate accordingly inquiry speed.
The querying method of the phone number that the present embodiment is provided, is looked into successively with the key-value pair that number section and number tail are constituted
Look for, be first to search whether there is target number section in storage region, if it does not exist, then explanation storage region is not comprising to be checked
Ask phone number;If it is present search whether there is target tail in storage region, if it is present explanation memory block
Domain includes phone number to be checked, and otherwise, storage region does not include phone number to be checked.As can be seen here, relative to existing skill
In art, for each the complete phone number in traveling through storage region, the inquiry velocity of this method is fast, can save inquiry
Time.
Used as preferred embodiment, preset rules are:Number section is first 7 of phone number to be stored, and number tail is to wait to deposit
Latter 4 of storage phone number.
It is understood that present embodiment is the one kind in numerous implementation methods, be not only this in kind of a mode.
The storage method and querying method to a kind of phone number provided by the present invention are described in detail above.Say
Each embodiment is described by the way of progressive in bright book, and what each embodiment was stressed is the difference with other embodiment
Part, between each embodiment identical similar portion mutually referring to.For device disclosed in embodiment, due to its with
Method is corresponding disclosed in embodiment, so description is fairly simple, related part is referring to method part illustration.Should refer to
Go out, for those skilled in the art, under the premise without departing from the principles of the invention, can also be to the present invention
Some improvement and modification are carried out, these are improved and modification is also fallen into the protection domain of the claims in the present invention.