Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to map a specialized string into specified integer

I am doing some financial trading work. I have a set of stock symbols but they have very clear pattern: it's composed of two characters AB, AC AD and current month which is a four digit number: 1503, 1504, 1505. Some examples are:

AB1504
AB1505
AC1504
AC1505
AD1504
AD1505
....

Since these strings are so well designed patterned, I want to map (hash) each of the string into a unique integer so that I can use the integer as the array index for fast accessing, since I have a lot of retrievals inside my system and std::unordered_map or any other hash map are not fast enough. I have tests showing that general hash map are hundred-nanoseconds latency level while array indexing is always under 100 nanos. my ideal case would be, for example, AB1504 maps to integer 1, AB1505 maps to 2...., then I can create an array inside to access the information related to these symbols much faster. I'm trying to figure out some hash algorithms or other methods that can achieve my goal but couldn't find out. Do you guys have any suggestions on this problem?

like image 891
trading4living Avatar asked May 17 '15 16:05

trading4living


People also ask

Can we make a map of string and int?

*You can Create map of both String and Integer, just keep sure that your key is Unique.

How to insert strings in map?

To insert the data in the map insert() function in the map is used. It is used to insert elements with a particular key in the map container. Parameters: It accepts a pair that consists of a key and element which is to be inserted into the map container but it only inserts the unique key.


1 Answers

You can regard the string as a variable-base number representation, and convert that to an integer. For example:

AC1504:
A (range: A-Z)
C (range: A-Z)
15 (range: 0-99)
04 (range: 1-12)

Extract the parts; then a hash function could be

int part1, part2, part3, part4;
...
part1 -= 'A';
part2 -= 'A';
part4 -= 1;
return (((part1 * 26 + part2) * 100 + part3) * 12 + part4;
like image 64
anatolyg Avatar answered Oct 03 '22 01:10

anatolyg