Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table implementation in C++

In our C++ project I have a need in some combined data structure, I think it should be very similar to SQL table. I have a set of arrays (std::vectors) of a same size with different data types. e.g.:

time(int), X(double), Y(double), valid(boolean), number_of_measurements(int)

Let's pretend they are like a table, I need to have an access to them row-wise. For example I need a functionality to insert a row into an arbitrary position of the table which will cause a shift of all rows on one element down, kill row. I might need to sort the table by time, merge it with other tables of a same type.

Is there anything like this in C++ world?

Basically I need an universal solution, which supports any number of columns and rows. And of course the performance makes sense, so I prefer doing it rather in memory than in database.

UPDATE

I see many people are proposing to define a simple row structure and store it in a collection. No this is not going to fly as far as I have a need to frequently operate with columns. E.g. I may need to multiply the whole column or calculate a mean value out of it. I can even interpolate it or apply many different algorithms on a certain column.

And I want to avoid a situation when I extract a column to a vector, apply some algorithms on it and then put it back to a structure. It looks ugly, doesn't it?

I need more or less similar to this:

// Initializing: (three columns: time, X, Y)
table t("time", std::vector<int>(), "X", std::vector<double>(), "Y", std::vector<double>());

// inserting a row
t.insert_row( 1, 20.0f, 20.0f );

// accessing values:
t["time"][10] = 20;

// getting a column:
std::vector<int> time = t["time"];

// sorting
t.sort_by( "time" );

Any thoughts?

like image 859
novak Li Avatar asked Nov 25 '10 11:11

novak Li


People also ask

How do you implement HashTable?

Take an array and use the hash function to hash the 26 possible characters with indices of the array. Then iterate over S and increase the value of the current character of the string with the corresponding index for each character. The complexity of this hashing approach is O(N), where N is the size of the string.

What is HashTable in C?

Advertisements. Hash Table is a data structure which stores data in an associative manner. In hash table, the data is stored in an array format where each data value has its own unique index value. Access of data becomes very fast, if we know the index of the desired data.

Can we use HashMap in C?

Generally you create an array called "buckets" that contain the key and value, with an optional pointer to create a linked list. When you access the hash table with a key, you process the key with a custom hash function which will return an integer.


2 Answers

If you need something SQL-like, you could try SQLite. It's a library that can provide in-memory SQL database with SELECT capabilities, sorting, merging and pretty much all the basic operations you'd expect from a SQL database. It also supports file-backed databases. This should save you the time of implementing your own solution.

like image 57
kichik Avatar answered Sep 21 '22 05:09

kichik


struct Point
{
   int time;
   double X,Y;
   bool valid;
   int number_of_measurements;
};

std::vector<Point> your_table;

Not to forget: std::vector has operations for inserting and deletion. For sorting and merging, include the "algorithm" header from the std library.

EDIT: if you are looking for a in-memory database, especially for C++, I found one named "FastDb":

http://www.garret.ru/fastdb.html

(I have no personal experience with that, so I cannot tell you "how fast" it is.)

like image 21
Doc Brown Avatar answered Sep 22 '22 05:09

Doc Brown