I have a [n X n] data matrix. And the first column is 'id' which gets incremented like a primary key (downwards). And it will be read (or write sometimes) from an application. The read normally happens with accessing each row's id. I have thought of the following:
My doubt is will my ideas help when the size 'n' is around 10000? Any other way of efficiently handling a matrix type of data? Will a b-tree work for my requirement?
Your design idea is very good. Database like structures can be stored easily in maps. 10000 values will also be no problem at all.
The type of the map to use depends on the data that you want to store. For a table with a unique key, the fastest solution is the std::unordered_map
. If you want to have sorted primary keys, then you can use a std::map
. This will be a little bit slower, because it needs to sort the keys.
For both maps there is also a multimap solution available, if your key is not unique.
Reading and writing will also be very simple.
Let us look at an example, where we use a unique unsorted key (fastest solution).
Please see:
#include<iostream>
#include<sstream>
#include<vector>
#include<unordered_map>
#include<string>
#include<iterator>
std::stringstream fileStreamSimulation{R"(1 100 101 102 103
2 200 201 202 203 204 205 206 207
3 300 301 302
4 400 401 402 403 404 405
)"};
int main() {
// Define our data container, like a database table with primary index
std::unordered_map<int,std::vector<int>> data{};
// Read all lines from the source file
for (std::string line{}; std::getline(fileStreamSimulation, line); ) {
// Put line into an istringstream for further extraction
std::istringstream iss{line};
// Get the key or primiary index
int key{}; iss >> key;
// Add the rest of the data
data[key] = {std::istream_iterator<int>(iss),{}};
}
// Show debug output
for (const auto [key,vector] : data) {
std::cout << "Key: " << key << "\tData: ";
for (const int i : vector) std::cout << i << ' ';
std::cout << '\n';
}
}