5

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?

1
  • 3
    Do not try to mimic a SQL table in C++, it's not going to work. Think more about attributes in objects and then store the objects in a collection. Commented Nov 25, 2010 at 11:58

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 In retrospect, this is a better solution than my own. Don't try to do something which has already been done or you'll get bad programming karma.
How fast is it? In respect to the functionality I mentioned in the Update section of my original message. Thanks
@novak: compared to what? It's much faster than a file-backed database, but probably slower than a simple, and less powerful, solution. If you need to perform typical database operations, then an in-memory SQLite database isn't a bad suggestion.
3
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.)

7 Comments

This is bad when you need to do something on a whole column. In our project we often have a need of doing operations with columns. e.g. calculate a mean value of a column or interpolate it. And this should happen relatively often.
Well, sice this stuff is in-memory, you could iterate over the vector elements without too much trouble. And if that becomes too slow, use cached results instead of "fresh" calculations for values already requested as long as no change has been made.
Not really. I have a lot of stuff like this, this seems to be critical. Please refer to the Update section in my original message
Saying critical i meant these operations on columns are going to happen many many times (many thousands of times) and on different columns. And I don't think it's a good idea to implement algorithms for certain data column, i'd rather vote for some universal approach. Extracting a column every time looks also odd.
Extracting a column -- you mean COPY the data into a second data structure? Why would you want to do this?
|
2

Make a class with members time, X, Y, valid, number_of_measurements. Put objects in a std::vector or std::list.

Comments

0

What you're describing is a struct to hold the data of a row but an array to represent that row. You need to encapsulate this struct into a class to manage it that way. If you're trying to generalize, I would highly recommend you deal with pointers rather than structs for the simple reason that it's a lot easier to manage deletion and addition of rows when you don't have to worry about what size it might have.

Better still, make an abstract row class which determines the interface (getColumn(int index) for example) which is extended by a class which encapsulates this behavior given a struct. Use std::vector class to handle a list of these abstract rows and encapsulate it into an abstract table with a simple interface.

Abstraction is your friend here.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.