Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ memory table

Tags:

c++

Can anyone recommend a lightweight open source C++ table data structure that has data access similar to a database table? (i.e. a 2D array but with named columns - ideally, each column will hold data of a single type [see below]).

I have had a quick look on Google, but have not found anything very useful.

The way I see it, these are the options available to me:

  1. Write my own from scratch (don't really want to invent the wheel)
  2. rip out the SimpleResult class from mySQL++ and hack around it
  3. use sqlite (don't know how lightwight this will be - since I don't need the querying engine and all the other stuff)
  4. Ask in here to see if anyone is aware of such a library

So here I am, choosing the quickest route (hopefully one that will also prove to be the the most efficient use of my time - since whatever is recommended here is likely to be peer reviewed).

So, can anyone recommend a C++ class/ set of classes that provides a "database table like" interface?

The main requirements are:

  1. Columns have names
  2. Cells can be accessed using row, column indexes
  3. I Can add rows and columns to the table (ideally, I can remove them too)
  4. (Nice to have): columns can have types, so it saves teh cost of converting to/from strings

[EDIT]

To further demonstrate how I want to use the library, please see the pseudo code below to see simple use of such a class (simple, meaning now iteration of rows and columns - which would be really cool). For now just keeping things simple:

typedef MemoryTable::ColType ColumnType;

table = new MemoryTable();

// Set up the structure (this can be modified later using removeColumn() etc
table->addColumn(ColumnType::Integer, 'id');
table->addColumn(ColumnType::String,  'name');
table->addColumn(ColumnType::Boolean, 'gender');
table->addColumn(ColumnType::Double,  'weight');

for (size_t i=0; i<10; i++)
{
    table->addRow();
    numrows = table->getNumRows();
    std::cout << "We now have " << numrows << " rows.\n";

    // Note can access cells using column name or index 
    // Also using generic value getter/setter methods. Can throw exception on type mismatch
    table->setValue(i, 'id', i*i);
    table->setValue(i, 'name', getRandomSimpsonCharacterName());

    //just to show use of a getter method
    table->setValue(i, 'gender', checkGender(table->getValue(i, 'name')));
    table->setValue(i, 3, guessWeight(table->getValue(i, 'name')));
}
like image 450
Stick it to THE MAN Avatar asked Feb 17 '10 19:02

Stick it to THE MAN


People also ask

What is C memory layout?

A C program memory layout in C mainly comprises six components these are heap, stack, code segment, command-line arguments, uninitialized and initialized data segments. Each of these segments has its own read, write permissions.

How is memory divided in C?

Basically, the memory layout of C program contains five segments these are the stack segment, heap segment, BSS (block started by symbol), DS (Data Segment) and text segment. Each segment has own read, write and executable permission.

What are memory tables?

MEMORY tables use a fixed-length row-storage format. Variable-length types such as VARCHAR are stored using a fixed length. MEMORY tables cannot contain BLOB or TEXT columns. MEMORY includes support for AUTO_INCREMENT columns.

How is memory managed in C?

In C, the library function malloc is used to allocate a block of memory on the heap. The program accesses this block of memory via a pointer that malloc returns. When the memory is no longer needed, the pointer is passed to free which deallocates the memory so that it can be used for other purposes.


1 Answers

For creating from scratch (very big scratch) try Boost Multi-index Container. It is not really a database implementation but it could help.

like image 153
Kirill V. Lyadvinsky Avatar answered Oct 27 '22 07:10

Kirill V. Lyadvinsky