diff options
author | tomsmeding <hallo@tomsmeding.nl> | 2015-05-11 18:39:56 +0200 |
---|---|---|
committer | tomsmeding <hallo@tomsmeding.nl> | 2015-05-11 18:40:08 +0200 |
commit | 82407c1c4c9526e7dc408e936478c8619ded8c66 (patch) | |
tree | b7ab24326c72e58d915ad1afec5adebef51c0689 | |
parent | 6f27f2e53179f476cfb80e634a59bfd3d584db6b (diff) |
Fix up makefile and add functional insert and find implementation
-rw-r--r-- | Makefile | 14 | ||||
-rw-r--r-- | Maybe.h | 21 | ||||
-rw-r--r-- | engine.cpp | 11 | ||||
-rw-r--r-- | enginedata.cpp | 57 | ||||
-rw-r--r-- | enginedata.h | 24 |
5 files changed, 108 insertions, 19 deletions
@@ -4,6 +4,8 @@ CFLAGS=-Wall -O2 CPPFLAGS=-Wall -O2 -std=c++11 BINARIES=engine +OBJS=enginedata.o +DEPFILES=$(OBJS:.o=.d) .PHONY: all clean @@ -11,8 +13,18 @@ BINARIES=engine all: $(BINARIES) clean: - rm $(BINARIES) + rm -f $(BINARIES) $(OBJS) $(DEPFILES) + + +-include $(OBJS:.o=.d) engine: engine.cpp enginedata.o $(CXX) $(CPPFLAGS) -o $@ $^ + +#enginedata: enginedata.cpp enginedata.h +# $(CXX) $(CPPFLAGS) -o %@ enginedata.cpp + +%.o: %.cpp + $(CXX) $(CPPFLAGS) $*.cpp -c -o $*.o + $(CXX) -MM $(CPPFLAGS) $*.cpp > $*.d @@ -0,0 +1,21 @@ +#pragma once + +template <typename T> +class Maybe{ + typedef struct { + T v; + } t_v_wrapper; + + bool is_something; + t_v_wrapper *wr; +public: + Maybe(void):is_something(false),wr(nullptr){} + Maybe(const T &_v):is_something(true),wr(new t_v_wrapper({_v})){} + + operator bool() const{return this->is_something;} + + T value(void){ + if(is_something)return wr->v; + throw "Nothing"; + } +}; @@ -2,7 +2,9 @@ #include <vector> #include <map> #include <cstdlib> +#include <climits> #include "enginedata.h" +#include "Maybe.h" using namespace std; @@ -18,11 +20,14 @@ int main(int argc,char **argv){ tables.emplace("hoi",Table("hoi",3,header)); Table *hoitb=&tables.at("hoi"); Row row(3,hoitb); - row.items[0].u.rh_int32=42; - row.items[1].u.rh_uint32=42; + row.items[0].u.rh_int32=UINT_MAX; + row.items[1].u.rh_uint32=UINT_MAX; row.items[2].u.rh_bytes=new unsigned char[10]; strcpy((char*)row.items[2].u.rh_bytes,"hallo daar"); hoitb->insert(row); - cout<<"hoitb has "<<hoitb->rows.size()<<" row"<<(hoitb->rows.size()==1?"":"s")<<endl; + Maybe<Row> found=hoitb->find(serialise((int32_t)-1)); + cout<<"hoitb has "<<hoitb->rows.size()<<" row"<<(hoitb->rows.size()==1?"":"s")<<'.'<<endl; + if(found)cout<<found.value()<<endl; + else cout<<"No row with key -1 found"<<endl; return 0; } 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);} diff --git a/enginedata.h b/enginedata.h index 29550f6..1b9feb4 100644 --- a/enginedata.h +++ b/enginedata.h @@ -1,5 +1,6 @@ #include <string> -#include <vector> +#include <map> +#include "Maybe.h" using namespace std; @@ -8,11 +9,16 @@ struct Row; struct ColHeader; struct Table; +string serialise(const ColHeader &header,const RowItem &rowitem); +string serialise(int32_t v); +string serialise(uint32_t v); +string serialise(unsigned char *v,int len); + struct RowItem{ union { //watch the pointerness or not-pointerness of the attributes! - int rh_int32; - unsigned int rh_uint32; + int32_t rh_int32; + uint32_t rh_uint32; unsigned char *rh_bytes; } u; }; @@ -28,20 +34,23 @@ struct ColHeader{ }; struct Row{ + const int nc; const Table *table; //pointer to the parent table; don't delete! RowItem *items; - Row(const int nc,const Table *const _t); + Row(const int _nc,const Table *const _t); ~Row(void); - Row(Row &&other); - Row(const Row &other); + Row(Row &&other); //move constr + Row(const Row &other); //copy constr + + friend ostream& operator<<(ostream &os,const Row &r); }; struct Table{ const string name; const int nc; ColHeader *header; - vector<Row> rows; + map<string,Row> rows; //map key is serialised version of the first column Table(const string &_n,const int _nc,const ColHeader *const _hd); ~Table(void); @@ -49,4 +58,5 @@ struct Table{ void insert(Row &&row); void insert(Row &row); + Maybe<Row> find(const string &key); }; |