I am trying to find an efficient structure to store data in c++. Efficiency in both time and storage is important.
I have a set S = {0, 1, 2, 3, ..., N}
, and multiple levels, say L
levels. For each level l ∈ {0, 1, ..., L}
, I need a structure, say M
, to store 2 subsets of S
, which will be the rows and columns of a matrix in the next steps. For example:
S = {0, 1, 2, 3, ..., 15}
L = 2
L1_row = {0, 1, 2, 3, 4, 5}
L1_col = {3, 4, 5, 6, 9}
L2_row = {0, 2, 4, 5, 12, 14}
L2_col = {3, 6, 10, 11, 14, 15}
I have used an unordered_map with integer keys as level and a pair of unordered_sets for row and column, as follow:
unordered_map<int, pair<unordered_set<int>, unordered_set<int>>> M;
However, this is not efficient, for example, {3, 4, 5}
recorded 3 times. As S
is a large set, so M
will contain many repeated numbers.
- In the next step, I will extract row and cols per level from
M
and create a matrix, so, fast access is important. M
may or may not contain all items inS
.M
will fill in at 2 stages, first, rows for all levels, and then columns for all levels.