I am assigned to implement a core database for a game.
It is a 1:N map of int X <--> int Y
. (e.g. pirateship<->its turrets
)
I know for sure that 0<=X<=1000
and 0<=Y<=10000
.
Most indices are used/occupied i.e. dense.
A certain X
can be mapped to many Y
s.
A certain Y
can be mapped to 0-1 X
.
I dream for a datastructure that :-
- allocate memory very infrequently
- query
X->Y
(returnArray<int>
) andY->X
(returnint
) very fast - For iterating, I don't mind iterating
0-10000
of value ofY
.
Question:
- How should I implement it?
- Is there std library that fit my need?
- Does this datastructure have its own name?
My poor solutions
std::unordered_multimap<int,int>
(probably red-black tree) orstd::unordered_map<int, std::vector<int>>
(Edit: Thank Some programmer dude)- it is slow (profiled)
:(
- not cache friendly
- not fully utilize the index
- it is slow (profiled)
std::vector<int, std::vector<int>>
- my current approach- the value (
std::vector<int>
) require a lot of dynamic allocation - potential cache miss and fragmentation
e.g.vector to store X=0 -> Y=?
would be far away fromvector to store X=1 -> Y=?
- the value (
Are there any better approaches?
Sorry if it is a common question,
I couldn't find any that exactly matches this one.