aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <hallo@tomsmeding.nl>2015-05-11 21:24:57 +0200
committertomsmeding <hallo@tomsmeding.nl>2015-05-11 21:24:57 +0200
commitce356164bac0f739fea97b8bee61c066b9f2ae87 (patch)
treebf73239c76e0288de9378f06c322766889ef1fb9
parentbfd76241386fe54c4394171c861f4b1676b5bf32 (diff)
Add <<debug<< log, clean up some code and let find return a list instead of 1 element
-rw-r--r--Makefile10
-rw-r--r--engine.cpp16
-rw-r--r--enginedata.cpp22
-rw-r--r--enginedata.h9
-rw-r--r--util.cpp13
-rw-r--r--util.h7
6 files changed, 54 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index eb34638..381b573 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ CFLAGS=-Wall -O2
CPPFLAGS=-Wall -O2 -std=c++11
BINARIES=engine
-OBJS=enginedata.o
+OBJS=enginedata.o util.o
DEPFILES=$(OBJS:.o=.d)
@@ -19,12 +19,12 @@ clean:
-include $(OBJS:.o=.d)
-engine: engine.cpp enginedata.o
+engine: engine.cpp enginedata.o util.o
$(CXX) $(CPPFLAGS) -o $@ $^
-#enginedata: enginedata.cpp enginedata.h
-# $(CXX) $(CPPFLAGS) -o %@ enginedata.cpp
+enginedata: enginedata.cpp enginedata.h util.o
+ $(CXX) $(CPPFLAGS) -o %@ enginedata.cpp
-%.o: %.cpp
+%.o: %.cpp %.h
$(CXX) $(CPPFLAGS) $*.cpp -c -o $*.o
$(CXX) -MM $(CPPFLAGS) $*.cpp > $*.d
diff --git a/engine.cpp b/engine.cpp
index bc4165a..67c9524 100644
--- a/engine.cpp
+++ b/engine.cpp
@@ -1,10 +1,11 @@
+#include "enginedata.h"
+#include "Maybe.h"
#include <iostream>
#include <vector>
#include <map>
+#include <list>
#include <cstdlib>
#include <climits>
-#include "enginedata.h"
-#include "Maybe.h"
using namespace std;
@@ -24,10 +25,13 @@ int main(int argc,char **argv){
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);
- Maybe<Row> found=hoitb->find(serialise((int32_t)-1));
+ if(!hoitb->insert(row))cout<<"Failed to insert row!"<<endl; else cout<<"Success."<<endl;
+ if(!hoitb->insert(row))cout<<"Failed to insert row!"<<endl; else cout<<"Success."<<endl;
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;
+ list<Row> found=hoitb->find(serialise((int32_t)-1));
+ cout<<"Found for key -1:"<<endl;
+ for(Row r : found){
+ cout<<r<<endl;
+ }
return 0;
}
diff --git a/enginedata.cpp b/enginedata.cpp
index 43a50ce..ecb4c82 100644
--- a/enginedata.cpp
+++ b/enginedata.cpp
@@ -1,5 +1,7 @@
#include "enginedata.h"
+#include "util.h"
#include <iostream>
+#include <list>
#include <cassert>
using namespace std;
@@ -41,24 +43,26 @@ Table::Table(Table &&other):name(move(other.name)),nc(other.nc),header(other.hea
other.header=nullptr;
}
-void Table::insert(Row &&row){
+bool Table::insert(Row &&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));
+ cerr<<debug<<"Inserting into table '"<<this->name<<"' key="<<key<<" row="<<row<<endl;
+ return rows.emplace(key,move(row)).second;
}
-void Table::insert(Row &row){
+bool Table::insert(Row &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);
+ cerr<<debug<<"Inserting into table '"<<this->name<<"' key="<<key<<" row="<<row<<endl;
+ return rows.emplace(key,row).second;
}
-Maybe<Row> Table::find(const string &key){
+list<Row> Table::find(const string &key){
typedef map<string,Row>::const_iterator cit_t;
cit_t end=rows.cend();
+ list<Row> ret;
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);
+ if(serialise(it->second.table->header[0],it->second.items[0])==key)
+ ret.emplace_back(it->second);
}
- return Maybe<Row>();
+ return ret;
}
diff --git a/enginedata.h b/enginedata.h
index 1b9feb4..e5d488e 100644
--- a/enginedata.h
+++ b/enginedata.h
@@ -1,5 +1,8 @@
+#pragma once
+
#include <string>
#include <map>
+#include <list>
#include "Maybe.h"
using namespace std;
@@ -56,7 +59,7 @@ struct Table{
~Table(void);
Table(Table &&other);
- void insert(Row &&row);
- void insert(Row &row);
- Maybe<Row> find(const string &key);
+ bool insert(Row &&row);
+ bool insert(Row &row);
+ list<Row> find(const string &key);
};
diff --git a/util.cpp b/util.cpp
new file mode 100644
index 0000000..b85f89e
--- /dev/null
+++ b/util.cpp
@@ -0,0 +1,13 @@
+#include "util.h"
+#include <ctime>
+
+using namespace std;
+
+ostream& debug(ostream &os){
+ char buf[64];
+ time_t timeval=time(NULL);
+ struct tm *timeinfo=localtime(&timeval);
+ strftime(buf,64,"%Y-%m-%d %T",timeinfo);
+ os<<"\x1B[36m["<<buf<<"]\x1B[0m ";
+ return os;
+}
diff --git a/util.h b/util.h
new file mode 100644
index 0000000..4c9e140
--- /dev/null
+++ b/util.h
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <iostream>
+
+using namespace std;
+
+ostream& debug(ostream &os);