views:

137

answers:

5

What's the best way to store the following message into a data structure for easy access?

"A=abc,B=156,F=3,G=1,H=10,G=2,H=20,G=3,H=30,X=23.50,Y=xyz"

The above consists of key/value pairs of the following:

A=abc
B=156
F=3
G=1
H=10
G=2
H=20
G=3
H=30
X=23.50
Y=xyz

The tricky part is the keys F, G and H. F indicates the number of items in a group whose item consists of G and H.

For example if F=3, there are three items in this group: Item 1: G=1, H=10 Item 2: G=2, H=20 Item 3: G=3, H=30

In the above example, each item consists of two key/pair values: G and H. I would like the data structure to be flexible such that it can handle if the item increases its key/pair values. As much as possible, I would like to maintain the order it appears in the string.

UPDATE: I would like to store the key/value pairs as strings even though the value often appears as float or other data type, like a map.

A: 

I work with FIX a lot in Python an Perl, and I tend to use a dictionary or hash. Your keys should be unique within the message. For C++, you could look at std::map or STL extension std::hash_map.

Dr. Watson
"Your keys should be unique within the message." - This is not true. Not only can keys be repeated within repeating groups, the repeating groups themselves can have optional keys.
Marcin
That is a good point, I should have read the OPs post more carefully. Thanks for pointing that out.
Dr. Watson
A: 

If you have a subset of FIX messages you have to support (most exchanges usually use 10-20 types), you can roll your own classes to parse messages into. If you're trying to be more generic, I would suggest creating something like a FIXChunk class. The entirety of the message could be stored in this class, organized into keys and their values, as well as lists of repeating groups. Each of the repeating groups would itself be a FIXChunk.

Marcin
A: 

A simple solution, but you could use a std::multimap<std::string,std::string> to store the data. That allows you to have multiple keys with the same value.

Scott Minster
yeah, but i was hoping to preserve the order as much as possible though...
jasonline
+1  A: 

May not be what you're looking for, but I'd simply recommend using QuickFIX (quickfixengine.org), which is a very high quality C++ FIX library. It has the type "FIX::Message" which does everything you're looking for, I believe.

Kyle C
A: 

In my experience, fix messages are usually stored either in their original form (as a stream of bytes) or as a complex data structure providing a full APIs that can handle their intricacies. After all, a fix message can sometimes represent a tree of data.

The problem with the latter solution is that the transition is expensive in terms of computation cost in high-speed trading systems. If you are building a trading system, you may prefer to lazily calculate the parts of the fix message than you need, which is admittedly easier said than done.

I am not familiar with efficient open-source implementations; companies like the one I work for usually have proprietary implementations.

Uri