aboutsummaryrefslogtreecommitdiff
path: root/enginedata.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'enginedata.cpp')
-rw-r--r--enginedata.cpp57
1 files changed, 49 insertions, 8 deletions
diff --git a/enginedata.cpp b/enginedata.cpp
index f1e9b2c..43a50ce 100644
--- a/enginedata.cpp
+++ b/enginedata.cpp
@@ -1,21 +1,32 @@
#include "enginedata.h"
+#include <iostream>
+#include <cassert>
using namespace std;
-Row::Row(const int nc,const Table *const _t):table(_t){
+Row::Row(const int _nc,const Table *const _t):nc(_nc),table(_t){
items=new RowItem[nc];
}
Row::~Row(void){
if(items)delete[] items;
}
-Row::Row(Row &&other):table(other.table),items(other.items){ //move constr
- other.table=NULL;
- other.items=NULL;
+Row::Row(Row &&other):nc(other.nc),table(other.table),items(other.items){ //move constr
+ other.table=nullptr;
+ other.items=nullptr;
}
-Row::Row(const Row &other):table(other.table){ //copy constr
+Row::Row(const Row &other):nc(other.nc),table(other.table){ //copy constr
items=new RowItem[table->nc];
memcpy(items,other.items,table->nc*sizeof(RowItem));
}
+ostream& operator<<(ostream &os,const Row &r){
+ os<<'[';
+ for(int i=0;i<r.nc;i++){
+ if(i)os<<", ";
+ os<<serialise(r.table->header[i],r.items[i]);
+ }
+ os<<']';
+ return os;
+}
//copies from _hd, so you can freely delete or edit the passed colheader array
@@ -27,12 +38,42 @@ Table::~Table(void){
if(header)delete[] header;
}
Table::Table(Table &&other):name(move(other.name)),nc(other.nc),header(other.header),rows(move(other.rows)){
- other.header=NULL;
+ other.header=nullptr;
}
void Table::insert(Row &&row){
- rows.push_back(move(row));
+ string key=serialise(this->header[0],row.items[0]);
+ cerr<<"Inserting into table '"<<this->name<<"' key="<<key<<" row="<<row<<endl;
+ rows.emplace(key,move(row));
}
void Table::insert(Row &row){
- rows.emplace_back(row);
+ string key=serialise(this->header[0],row.items[0]);
+ cerr<<"Inserting into table '"<<this->name<<"' key="<<key<<" row="<<row<<endl;
+ rows.emplace(key,row);
+}
+
+Maybe<Row> Table::find(const string &key){
+ typedef map<string,Row>::const_iterator cit_t;
+ cit_t end=rows.cend();
+ for(cit_t it=rows.cbegin();it!=end;it++){
+ if(serialise(it->second.table->header[0],it->second.items[0])==key)return Maybe<Row>(it->second);
+ }
+ return Maybe<Row>();
+}
+
+
+string serialise(const ColHeader &header,const RowItem &rowitem){
+ switch(header.type){
+ case RH_INT32:
+ return serialise(rowitem.u.rh_int32);
+ case RH_UINT32:
+ return serialise(rowitem.u.rh_uint32);
+ case RH_BYTES:
+ return serialise(rowitem.u.rh_bytes,header.arg);
+ default:
+ assert(false);
+ }
}
+string serialise(int32_t v){return to_string(v);}
+string serialise(uint32_t v){return to_string(v);}
+string serialise(unsigned char *v,int len){return string((char*)v,len);}