In my program, a state can be uniquely identified by six integers. Each integer, i
satisfies 0 <= i <= 10
and each state has an associated value. I am currently using a 6-dimensional array to keep track of each states value. I am storing the state values in an array like so state[11][11][11][11][11][11][11]
, where each value has the 6 integers as a key. However, this array would be very sparse, as I only visit a small number of possible states. Is there a better way to represent the key for the value of a state?
If you don't need to have a value in every state you can get away with using a map
Concept code, totally untested.
constexpr int dimension = 6;
using KeyType = std::array<char, dimension>;
int32_t Key(const & KeyType keys) {
int32_t res = 0;
for (auto key : keys) {
res <<= 4;
res += key;
}
return res;
}
void Key2Array(int32_t keyValue, KeyType& keys) {
int idx = dimension-1;
for (auto& key : keys) {
keys[idx--] = keyValue&0x16;
keyValue >>= 4;
}
}
std::map<int32_t, value> states;
states[Key({1,2,3,4,5,6}] = 42;
KeyType key;
Key2Array(0x123456, key);