Right now I have a vector std::vector<char> myVector(4) containing any combination of a set of char lets say {@,#,O,*,%,$,!} may be more or less but not many more than that, might not always be 4 members either, but will be constant for any instance one instance.
now I stuck trying to create a data structure that can use an indefinite number of those combination as an index, to another vector.
in pseudo-code I am trying to accomplish:
SomeDataStructure['*']['#']['@']['O'] = someData
(someData is going to be a small class, but that shouldn’t matter)
This is an operation critical piece that needs to run quickly, and will be run very often.
some approached i’ve tried to reason with were:
a 4 dimensional array, but I can access those without numeric indices. Maybe some form of enumeration could solve this. Edit: would maps be a way to do this?
edit:
I resolved this using a map:
std::map<std::vector<char>, someData> myMap;
Since the number of possible characters is limited to 8, you can use an enumeration instead. You’d therefore only need 3-bits to represent each “character”. You can pack several of these 3-bit “characters” into a short integer using bitfields. The resulting packed integer becomes the index into your
vector<SomeData>.The space occupied by this vector would be
space_of_SomeData * 2^(3*number_of_spaces). If, for example,number_of_spacesis 4, this results in4096*space_of_SomeData. This might result in some wasted memory space, but lookups and insertions should be very fast.Here’s some sample code:
If you must absolutely work with
charcharacters, you can easily create a 256-element lookup table that quickly converts ‘char’ characters intoCharSetvalues.As already discussed by others, you can use a
std::map<std::string, SomeData>or even (the possibly faster)std::map<char[4], SomeData, Comparitor>. If the approximate frequency distribution of different character sequences is known, try inserting the most frequent patterns first in the map. Depending on the internal implementation of the map, this may speed up lookups for the most frequent patterns (they are near the top of the underlying binary search tree).