tags:

views:

100

answers:

3

I am trying to represent a relation (table) in C++ code:

  • The columns of the relation are integers.
  • The number of columns in the relation is fixed at runtime.
  • No duplicates should be allowed (this is the major source of cost).
  • I want to have a map from names to relations.

Any ideas for an efficient implementation, the main issue here is detecting duplicates at insertion time, it can be very costly.

A: 

Make a row object:

class MyRow {
public:
    int getSomeColumn();
    int getAnotherColumn();
    string getSomeColumnName();
    .
    .
    int getHashValue();
};

where the hash value is formed from the contents of the row. Then place these in a hash_map (see TR1 for non MSVS) or hash_set if you overload the ==, <, and size_t() operators.

wheaties
+1  A: 

Make each row of the table a struct Row.

Use a std::set or std::unordered_set to store these structs. Collision (querying) can be detected in (for std::set) O(log n + d) time or (for std::unordered_set) amortized O(d) time where d is the number of columns.

To efficiently map from names to rows, create a boost::bimap<std::string, Row>.

KennyTM
But I do not know the schema of the row at compile time
myahya
@myahya: So use a `std::vector` for the rows.
KennyTM
A: 

KennyTM has a point. You could use SQLite. As described in the link, you can use it to create a temporary in-memory database.

Space_C0wb0y
Unicity a very standard database constraint...
Matthieu M.
He made that comment earlier, I changed my post since then.
Space_C0wb0y